我如何使用 StructureMap 从 UnitOfWork 注入上下文

How i can inject context from UnitOfWork using StructureMap

嗨,我在 MVC5 中有一个 mi 项目,我正在使用 Identity 2.0、commonRepository 和 Structuremap 来注入依赖项,问题是当我在控制器 AccountController 中时,我有一个 Contex,当我的 UnitOfWork 注入它创建的存储库时其他实例。

我如何从我的 UnitOfWork 中注入或替换身份的上下文。

此致

Update

账户控制器

 public class AccountController : Controller
{
    private readonly ApplicationSignInManager SignInManager;
    private readonly ApplicationUserManager UserManager;
    private readonly IAuthenticationManager AuthenticationManager;

   // private readonly IUbicationDao _ubicationDao;
    private readonly ICultureDao _cultureDao;
    private readonly ICurrencyDao _currecieDao;


    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ICultureDao cultureDao, ICurrencyDao currecieDao, IAuthenticationManager authenticationManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
       // _ubicationDao = ubicationDao;
        _cultureDao = cultureDao;
        _currecieDao = currecieDao;
        AuthenticationManager = authenticationManager;
    }}

默认注册表结构图

 public class DefaultRegistry : Registry {
    #region Constructors and Destructors
    public static IList<string> Assemblies
    {
        get
        {
            return new List<string>
            {
                "Interfaz",
                "Persistencia"
            };
        }
    }

    public static IList<Tuple<string, string>> ManuallyWired
    {
        get
        {
            return new List<Tuple<string, string>>()
            {
                Tuple.Create("IUserStore<ApplicationUser>", "UserStore<ApplicationUser>>"),
                Tuple.Create("DbContext", "ApplicationDbContext"),
                Tuple.Create("IAuthenticationManager", "HttpContext.Current.GetOwinContext().Authentication"),
            };
        }
    }

    public DefaultRegistry()
    {
        Scan(
            scan =>
            {

                foreach (var assembly in Assemblies)
                {
                    scan.Assembly(assembly);
                }
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
                scan.With(new ControllerConvention());

            });



        For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
        For<DbContext>().Use<ApplicationDbContext>(new ApplicationDbContext());

        For<IAuthenticationManager>().Use(() => HttpContext.Current.GetOwinContext().Authentication);
        //DAos
        For<ICultureDao>().Use<CultureDao>();
        For<ICurrencyDao>().Use<CurrencyDao>();
        For<IUbicationDao>().Use<UbicationDao>();
        For<IActivatorWrapper>().Use<ActivatorWrapper>();
        For<IUnitOfWorkHelper>().Use<UnitOfWorkHelper>();
    }

    #endregion
}

工作单位

public class UnitOfWorkHelper : IUnitOfWorkHelper
{
    private ApplicationDbContext _sessionContext;
   public event EventHandler<ObjectCreatedEventArgs> ObjectCreated;


    public IApplicationDbContext DBContext
    {
        get
        {
            if (_sessionContext == null)
            {
                _sessionContext = new ApplicationDbContext();
                ((IObjectContextAdapter)_sessionContext).ObjectContext.ObjectMaterialized += (sender, e) => OnObjectCreated(e.Entity);
            }

            return _sessionContext;
        }
    }

    private void OnObjectCreated(object entity)
    {
        if (ObjectCreated != null)
            ObjectCreated(this, new ObjectCreatedEventArgs(entity));
    }

    public void SaveChanges()
    {
        this.DBContext.SaveChanges();
    }

    public void RollBack()
    {
        if (_sessionContext != null)
            _sessionContext.ChangeTracker.Entries()
                .ToList()
                .ForEach(entry => entry.State = EntityState.Unchanged);
    }

    public void Dispose()
    {
        if (_sessionContext != null)
            _sessionContext.Dispose();
    }
}

经过大量的分析和理解,我终于找到了解决方案, 首先,我必须注入相同的上下文以避免注入上下文的新实例。解决方案是:

For<DbContext>().Use(()=>System.Web.HttpContext.Current.GetOwinContext().Get<ApplicationDbContext>());

在我注入并添加 DBContex 的新实例之前。

 For<DbContext>().Use<ApplicationDbContext>(new ApplicationDbContext());