第三方容器unity工厂

public class DIFactory
{
    private static IUnityContainer _Container = null;
    private readonly static object DIFactoryLock = new object();
    public static IUnityContainer GetContainer()
    {
        if (_Container == null)
        {
            lock (DIFactoryLock)
            {
                if (_Container == null)
                {
                    //container.RegisterType
                    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                    fileMap.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory + "CfgFiles\\Unity.Config");
                    Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                    UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection(UnityConfigurationSection.SectionName);
                    _Container = new UnityContainer();
                    section.Configure(_Container, "ruanmouContainer");
                }
            }
        }
        return _Container;
    }
}

替换mvc控制器工厂

public class XTControllerFactory : DefaultControllerFactory
{
    private Logger logger = new Logger(typeof(XTControllerFactory));

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        this.logger.Warn($"{controllerType.Name}被构造...");

        IUnityContainer container = DIFactory.GetContainer();
        //return base.GetControllerInstance(requestContext, controllerType);
        return (IController)container.Resolve(controllerType);
    }
}

全局注册实现注入

protected void Application_Start()
{

    AreaRegistration.RegisterAllAreas();//注册区域
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);//注册全局的Filter
    RouteConfig.RegisterRoutes(RouteTable.Routes);//注册路由
    BundleConfig.RegisterBundles(BundleTable.Bundles);//合并压缩 ,打包工具 Combres

    ControllerBuilder.Current.SetControllerFactory(new XTControllerFactory());

    this.logger.Info("网站启动了。。。");
}

使用

public class ThirdController : Controller
{
    private IUserService _iUserService = null;
    private ICompanyService _iCompanyService = null;
    private IUserCompanyService _iUserCompanyService = null;
    /// <summary>
    /// 构造函数注入---控制器得是由容器来实例化
    /// </summary>
    /// <param name="userService"></param>
    /// <param name="companyService"></param>
    /// <param name="userCompanyService"></param>
    public ThirdController(IUserService userService, ICompanyService companyService, IUserCompanyService userCompanyService)
    {
        this._iUserService = userService;
        this._iCompanyService = companyService;
        this._iUserCompanyService = userCompanyService;

        
    }


    // GET: Third
    public ActionResult Index()
    {
        //JDDbContext context = new JDDbContext();
        //IUserService service = new UserService(context);
        IUserService service = this._iUserService;
        User user = service.Find<User>(2);
        return View();
    }
}

具体的unity配置请参照https://blog.csdn.net/weixin_41181778/article/details/103439805

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐