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

时间:2015-03-04 18:37:10

标签: c# asp.net-mvc-5 asp.net-identity structuremap unit-of-work

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

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

问候

  

更新

的AccountController

 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;
    }}

DefaultRegistry StructureMap

 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
}

的UnitOfWork

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();
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

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

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