加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#-----------IOC依赖注入

(2022-07-04 13:53:26)
分类: C#
https://blog.csdn.net/weixin_30265171/article/details/95780443   【ICO实例】

四、ASP.NET MVC使用Grace的Demo

Grace支持ASP.NET MVC4和MVC5,这里以MVC5为例,此部分Demo在附件的“WebCoreApplicationUseGrace”工程中。

1. 先在项目中添加Grace.MVC5包,依赖项有Grace包。也可以使用这里的源码:https://github.com/ipjohnson/Grace.MVC

2. 新建一个类,用于配置注册接口及其实现


 1 public class DependencyInjectionScope
 2 {
 3     public static DependencyInjectionContainer GetContainer()
 4     {
 5         //容器
 6         var container = new DependencyInjectionContainer();
 7         container.Configure(m =>
 8         {
 9             //这里演示如何简单注册同一个接口对应其实现
10             m.Export().As();
11             m.Export().As();
12 
13             //这里演示如何使用键值注册同一个接口的多个实现
14             m.Export().AsKeyed("A");
15             //这里同时演示使用带参数构造器来键值注册
16             m.Export().AsKeyed("B").WithCtorParam<<span style="box-sizing: border-box; outline: 0px; margin: 0px; padding: 0px; overflow-wrap: break-word; color: rgb(0, 0, 255);">string>(() => { return "kkkkk"; });
17 
18             //这里演示依赖倒置而使用构造器带键值注入
19             m.Export().As().WithCtorParam().LocateWithKey("B");
20         });
21 
22         return container;
23     }
24 }

3. 在Global.asax.cs中MvcApplication类的Application_Start方法指定MVC的控制器实例工厂为Grace的DisposalScopeControllerActivator,至此配置完毕。


 1 public class MvcApplication : System.Web.HttpApplication
 2 {
 3     protected void Application_Start()
 4     {
 5         AreaRegistration.RegisterAllAreas();
 6         FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
 7         RouteConfig.RegisterRoutes(RouteTable.Routes);
 8         BundleConfig.RegisterBundles(BundleTable.Bundles);
 9 
10         //获取注册容器
11         var graceIocContainer = DependencyInjectionScope.GetContainer();
12         //MVC指定Grace的工厂为控制器实例工厂
13         ControllerBuilder.Current.SetControllerFactory(new DisposalScopeControllerActivator(graceIocContainer));
14     }
15 }

4. 在控制器中使用方法如下。一般的可以使用构造器参数方式赋值实例,有特殊注入的(如下面带键值的注入)可以先实例IExportLocatorScope出来,再用其Locate来获得实例。


 1 public class HomeController : Controller
 2 {
 3     //IExportLocatorScope可以获取从InjectionScope和LifeTimeScope注入的类型实例
 4     IExportLocatorScope locator;
 5 
 6     IAccountRepository accountRepo;
 7     IAccountService accountSvc;
 8     IUserRepository userRepo;
 9     IUserService userSvc;
10 
11     public HomeController(IExportLocatorScope locator, IAccountRepository accountRepo, IAccountService accountSvc, IUserService userSvc)
12     {
13         this.locator = locator;
14         //获取简单注册实例
15         this.accountRepo = accountRepo;
16         this.accountSvc = accountSvc;
17         //获取指定键值的实例
18         this.userRepo = this.locator.Locate(withKey: "A");
19         this.userSvc = userSvc;
20 
21     }
22 
23     public ActionResult Index()
24     {
25         return Json(new
26         {
27             accountRepoGet = accountRepo.Get(),
28             accountSvcGet = accountSvc.Get(),
29             userRepoGet = userRepo.Get(),
30             userSvcGet = userSvc.Get(),
31         }, JsonRequestBehavior.AllowGet);
32     }
35 }

以上代码输出如下:


{
    "accountRepoGet": "[Account]简单注册调用[Repo]",
    "accountSvcGet": "[Account]简单注册调用[Repo][Service]",
    "userRepoGet": "[User]键值注册调用A[Repo]",
    "userSvcGet": "[User]键值注册调用B,Ctor param1:kkkkk[Repo][Service]"
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有