MVC5+EF+IOC
1 Unity.Config
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration"/>
</configSections>
<unity>
<sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Unity.Interception.Configuration"/>
<containers>
<container name="ruanmouContainer">
<extension type="Interception"/>
<register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Ruanmou.EF.Model.JDDbContext, Ruanmou.EF.Model"/>
<register type="Ruanmou.Bussiness.Interface.IUserCompanyService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.UserCompanyService, Ruanmou.Bussiness.Service">
<interceptor type="InterfaceInterceptor"/>
</register>
<register type="Ruanmou.Bussiness.Interface.IUserService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.UserService, Ruanmou.Bussiness.Service">
<interceptor type="InterfaceInterceptor"/>
<interceptionBehavior type="Ruanmou.Project.AOP.LogBeforeBehavior, Ruanmou.MVC5"/>
<interceptionBehavior type="Ruanmou.Project.AOP.LogAfterBehavior, Ruanmou.MVC5"/>
</register>
<register type="Ruanmou.Bussiness.Interface.ICompanyService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CompanyService, Ruanmou.Bussiness.Service">
<interceptor type="InterfaceInterceptor"/>
</register>
</container>
<container name="ruanmouContainerGeneric">
<register type="System.Data.Entity.DbContext, EntityFramework" mapTo="Ruanmou.EF.Model.JDDbContext, Ruanmou.EF.Model"/>
<register type="Ruanmou.Bussiness.Interface.IBaseService`1,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.BaseService`1, Ruanmou.Bussiness.Service"/>
<register type="Ruanmou.Bussiness.Interface.ICategoryService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CategoryService, Ruanmou.Bussiness.Service"/>
<register type="Ruanmou.Bussiness.Interface.ICommodityService,Ruanmou.Bussiness.Interface" mapTo="Ruanmou.Bussiness.Service.CommodityService, Ruanmou.Bussiness.Service"/>
</container>
</containers>
</unity>
</configuration>
2 DIFactory
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using Unity;
namespace Ruanmou.MVC5.Utility
{
public class DIFactory
{
public static IUnityContainer GetContainer()
{
IUnityContainer 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;
}
}
}
3 ElevenControllerFactory
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Unity;
namespace Ruanmou.MVC5.Utility
{
public class ElevenControllerFactory : DefaultControllerFactory
{
private Logger logger = new Logger(typeof(ElevenControllerFactory));
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);
}
}
}