Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.NET MVC依赖项注入?_Asp.net Mvc_Asp.net Mvc 4_Dependency Injection_Ef Code First_Inversion Of Control - Fatal编程技术网

Asp.net mvc ASP.NET MVC依赖项注入?

Asp.net mvc ASP.NET MVC依赖项注入?,asp.net-mvc,asp.net-mvc-4,dependency-injection,ef-code-first,inversion-of-control,Asp.net Mvc,Asp.net Mvc 4,Dependency Injection,Ef Code First,Inversion Of Control,UnitOfWorkManager public class UnitOfWorkManager : IUnitOfWorkManager { private bool _isDisposed; private readonly BEMContext _context; public UnitOfWorkManager(IBEMContext context) { // http://stackoverflow.com/questions/355

UnitOfWorkManager

public class UnitOfWorkManager : IUnitOfWorkManager
{
    private bool _isDisposed;
    private readonly BEMContext _context;

    public UnitOfWorkManager(IBEMContext context)
    {
        // http://stackoverflow.com/questions/3552000/entity-framework-code-only-error-the-model-backing-the-context-has-changed-sinc
        Database.SetInitializer<BEMContext>(null);

        _context = context as BEMContext;
    }

    /// <summary>
    /// Provides an instance of a unit of work. This wrapping in the manager
    /// class helps keep concerns separated
    /// </summary>
    /// <returns></returns>
    public IUnitOfWork NewUnitOfWork()
    {
        return new UnitOfWork(_context);
    }

    /// <summary>
    /// Make sure there are no open sessions.
    /// In the web app this will be called when the injected UnitOfWork manager
    /// is disposed at the end of a request.
    /// </summary>
    public void Dispose()
    {
        if (!_isDisposed)
        {
            _context.Dispose();
            _isDisposed = true;
        }
    }
}
public class BaseController : Controller
{
    protected readonly IUnitOfWorkManager _unitOfWorkManager;

    public BaseController(IUnitOfWorkManager unitOfWorkManager)
    {
        _unitOfWorkManager = unitOfWorkManager;
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}
public class LocalizationController : BaseController
{
    private readonly ILocalizationService _localizationService;

    public LocalizationController(ILocalizationService localizationService, IUnitOfWorkManager unitOfWorkManager)
        : base(unitOfWorkManager)
    {
        _localizationService = localizationService;
    }

    public ActionResult AddLanguage()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddLanguage(LanguageModel model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (var UnitOfWork = _unitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var language = Mapper.Map<LanguageModel, Language>(model);
                        _localizationService.CreateLanguage(language);
                        UnitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        UnitOfWork.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return View(model);
    }
}
本地化服务

public class LocalizationService : ILocalizationService
{
    private readonly ILanguageRepository _languageRepository;

    public LocalizationService(ILanguageRepository languageRepository)
    {
        _languageRepository = languageRepository;
    }

    public void CreateLanguage(Language language)
    {
        _languageRepository.Insert(language);
    }
}
BaseController

public class UnitOfWorkManager : IUnitOfWorkManager
{
    private bool _isDisposed;
    private readonly BEMContext _context;

    public UnitOfWorkManager(IBEMContext context)
    {
        // http://stackoverflow.com/questions/3552000/entity-framework-code-only-error-the-model-backing-the-context-has-changed-sinc
        Database.SetInitializer<BEMContext>(null);

        _context = context as BEMContext;
    }

    /// <summary>
    /// Provides an instance of a unit of work. This wrapping in the manager
    /// class helps keep concerns separated
    /// </summary>
    /// <returns></returns>
    public IUnitOfWork NewUnitOfWork()
    {
        return new UnitOfWork(_context);
    }

    /// <summary>
    /// Make sure there are no open sessions.
    /// In the web app this will be called when the injected UnitOfWork manager
    /// is disposed at the end of a request.
    /// </summary>
    public void Dispose()
    {
        if (!_isDisposed)
        {
            _context.Dispose();
            _isDisposed = true;
        }
    }
}
public class BaseController : Controller
{
    protected readonly IUnitOfWorkManager _unitOfWorkManager;

    public BaseController(IUnitOfWorkManager unitOfWorkManager)
    {
        _unitOfWorkManager = unitOfWorkManager;
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}
public class LocalizationController : BaseController
{
    private readonly ILocalizationService _localizationService;

    public LocalizationController(ILocalizationService localizationService, IUnitOfWorkManager unitOfWorkManager)
        : base(unitOfWorkManager)
    {
        _localizationService = localizationService;
    }

    public ActionResult AddLanguage()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddLanguage(LanguageModel model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (var UnitOfWork = _unitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var language = Mapper.Map<LanguageModel, Language>(model);
                        _localizationService.CreateLanguage(language);
                        UnitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        UnitOfWork.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return View(model);
    }
}
本地化控制器

public class UnitOfWorkManager : IUnitOfWorkManager
{
    private bool _isDisposed;
    private readonly BEMContext _context;

    public UnitOfWorkManager(IBEMContext context)
    {
        // http://stackoverflow.com/questions/3552000/entity-framework-code-only-error-the-model-backing-the-context-has-changed-sinc
        Database.SetInitializer<BEMContext>(null);

        _context = context as BEMContext;
    }

    /// <summary>
    /// Provides an instance of a unit of work. This wrapping in the manager
    /// class helps keep concerns separated
    /// </summary>
    /// <returns></returns>
    public IUnitOfWork NewUnitOfWork()
    {
        return new UnitOfWork(_context);
    }

    /// <summary>
    /// Make sure there are no open sessions.
    /// In the web app this will be called when the injected UnitOfWork manager
    /// is disposed at the end of a request.
    /// </summary>
    public void Dispose()
    {
        if (!_isDisposed)
        {
            _context.Dispose();
            _isDisposed = true;
        }
    }
}
public class BaseController : Controller
{
    protected readonly IUnitOfWorkManager _unitOfWorkManager;

    public BaseController(IUnitOfWorkManager unitOfWorkManager)
    {
        _unitOfWorkManager = unitOfWorkManager;
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);
    }
}
public class LocalizationController : BaseController
{
    private readonly ILocalizationService _localizationService;

    public LocalizationController(ILocalizationService localizationService, IUnitOfWorkManager unitOfWorkManager)
        : base(unitOfWorkManager)
    {
        _localizationService = localizationService;
    }

    public ActionResult AddLanguage()
    {
        return View();
    }

    [HttpPost]
    public ActionResult AddLanguage(LanguageModel model)
    {
        try
        {
            if (ModelState.IsValid)
            {
                using (var UnitOfWork = _unitOfWorkManager.NewUnitOfWork())
                {
                    try
                    {
                        var language = Mapper.Map<LanguageModel, Language>(model);
                        _localizationService.CreateLanguage(language);
                        UnitOfWork.Commit();
                    }
                    catch (Exception ex)
                    {
                        UnitOfWork.Rollback();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return View(model);
    }
}

Unity正在创建依赖项的新实例,这意味着当IUnitOfWorkManager和ILocalizationService都被实例化时,它们的依赖项将被单独实例化。找到一种方法来共享对上下文的依赖;理想情况下,只有工作单元才应该真正意识到这一点,我认为其他任何东西都应该通过工作单元来改变环境。

我的解决方案是:

/// <summary>
/// Bind the given interface in request scope
/// </summary>
public static class IOCExtensions
{
    public static void BindInRequestScope<T1, T2>(this IUnityContainer container) where T2 : T1
    {
        container.RegisterType<T1, T2>(new HierarchicalLifetimeManager());
    }

    public static void BindInSingletonScope<T1, T2>(this IUnityContainer container) where T2 : T1
    {
        container.RegisterType<T1, T2>(new ContainerControlledLifetimeManager());
    }
}

public static class Bootstrapper
{
    public static IUnityContainer Initialise()
    {
        var container = BuildUnityContainer();

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));

        return container;
    }

    private static IUnityContainer BuildUnityContainer()
    {
        var container = new UnityContainer();

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();    
        RegisterTypes(container);

        return container;
    }

    public static void RegisterTypes(IUnityContainer container)
    {
        container.BindInRequestScope<IBEMContext, BEMContext>();

        container.BindInRequestScope<ILocalizationService, LocalizationService>();

        container.BindInRequestScope<ILanguageRepository, LanguageRepository>();
        container.BindInRequestScope<ILocaleResourceKeyRepository, LocaleResourceKeyRepository>();
        container.BindInRequestScope<ILocaleResourceValueRepository, LocaleResourceValueRepository>();

        container.BindInRequestScope<IUnitOfWorkManager, UnitOfWorkManager>();
    }
}
//
///在请求范围中绑定给定接口
/// 
公共静态类IOCExtensions
{
公共静态void BindInRequestScope(此IUnityContainer容器),其中T2:T1
{
RegisterType(新的层次结构CallifetimeManager());
}
公共静态void BindInSingletonScope(此IUnityContainer容器),其中T2:T1
{
RegisterType(新的ContainerControlledLifetimeManager());
}
}
公共静态类引导程序
{
公共静态IUnityContainer初始化()
{
var container=BuildUnityContainer();
SetResolver(新UnitedDependencyResolver(容器));
返回容器;
}
私有静态IUnityContainer BuildUnityContainer()
{
var container=new UnityContainer();
//在此处向容器注册所有组件
//无需注册控制器
//例如container.RegisterType();
注册类型(容器);
返回容器;
}
公共静态无效注册表类型(IUnityContainer容器)
{
container.BindInRequestScope();
container.BindInRequestScope();
container.BindInRequestScope();
container.BindInRequestScope();
container.BindInRequestScope();
container.BindInRequestScope();
}
}

依赖项注入是如何设置的?您使用的是什么框架?IBMContext实例化是如何配置的?@AlexPaven,我使用Unity,我更新了我的问题,请查看…请参阅书中的第7.2节:编写ASP.NET MVC应用程序。你说得对,但我也知道这一点。我的问题是“我找不到任何方法来共享DI上下文”。我已经在问这个问题了。考虑使用终身管理器。对于这个特定的案例,我建议DbContext应该是每个请求,可能如这里所述:非常感谢。你给我一个主意,我就解决了。