(精华)2020年8月19日 ASP.NET MVC 控制器工厂实现Unity容器注入
发布日期:2021-06-29 15:10:24 浏览次数:2 分类:技术文章

本文共 4108 字,大约阅读时间需要 13 分钟。

DI工厂

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

Unity配置文件

AOP相关

public class LogAfterBehavior : IInterceptionBehavior{
public IEnumerable
GetRequiredInterfaces() {
return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) {
IMethodReturn methodReturn = getNext()(input, getNext); Console.WriteLine("这里是方法执行后。。。"); return methodReturn; } public bool WillExecute {
get {
return true; } }}///
/// 不需要特性/// public class LogBeforeBehavior : IInterceptionBehavior{
public IEnumerable
GetRequiredInterfaces() {
return Type.EmptyTypes; } public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext) {
//Session ///这里是方法执行前 Console.WriteLine("这里是方法执行前"); return getNext().Invoke(input, getNext); } public bool WillExecute {
get {
return true; } }}
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; /// /// 构造函数注入---控制器得是由容器来实例化 /// /// /// /// 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
(2); return View(); }}

转载地址:https://codeboy.blog.csdn.net/article/details/108096060 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(精华)2020年8月19日 C#类库 Excel帮助类
下一篇:(精华)2020年8月19日 ASP.NET MVC 母版页的使用

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月30日 05时36分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章