本篇文章必备知识:
1、编程语言基础
2、对面向对象编程的深刻理解
3、概念性词:高内聚低耦合,解耦,接口编程
网上有很多介绍关于IOC的文章,比较官方的定义晦涩难懂,就不在这里赘述了。
本文用的例子就是对新闻进行增删改查(偷懒一下,这些方法并没有真正实现,只是为了演示)
先上代码:
注册:
在PreApplicationStartMethod(在Application_Start方法执行之前)执行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59using System; using System.Web; using Test; using Ninject.Web.Common; using Microsoft.Web.Infrastructure.DynamicModuleHelper; using Ninject; using Test.Presentations; [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectInitialize), "Initialize")] [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectInitialize), "Dispose")] namespace Test { public class NinjectInitialize { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); public static void Initialize() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } public static void Dispose() { bootstrapper.ShutDown(); } /// <summary> /// Creates the kernel that will manage your application. /// </summary> /// <returns>The created kernel.</returns> private static IKernel CreateKernel() { var kernel = new StandardKernel(); try { kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); RegisterServices(kernel); return kernel; } catch { kernel.Dispose(); throw; } } /// <summary> /// Load your modules or register your services here! /// </summary> /// <param name="kernel">The kernel.</param> private static void RegisterServices(IKernel kernel) { kernel.Bind<INewsPresentation>().To<NewsPresentation>(); } } }
逻辑层:
1
2
3
4
5
6
7
8
9
10
11/// <summary> /// 逻辑层接口 /// </summary> public interface INewsPresentation { string Create(NewsModel model); string Delete(NewsModel model); string Update(NewsModel model); IList<NewsModel> Select(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25<pre name="code" class="csharp"> /// <summary> /// 逻辑层实现INewsPresentation /// </summary> public class NewsPresentation : INewsPresentation { public string Create(Models.NewsModel model) { return "调用了Create"; } public string Delete(Models.NewsModel model) { return "调用了Delete"; } public string Update(Models.NewsModel model) { return "调用了Update"; } public IList<Models.NewsModel> Select() { throw new NotImplementedException(); } }
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15/// <summary> /// 新闻管理-控制器 /// </summary> public class HomeController : Controller { [Inject] public INewsPresentation newsPresentation { get; set; } // // GET: /Home/ public ActionResult Index() { return this.Content(newsPresentation.Delete(new Models.NewsModel())); } }
1、首先看控制器,我们通常要使用逻辑层的对象是直接
NewsPresentation n = new NewsPresentation(); 搞定,这样的话逻辑层就和此类耦合起来,不易维护。现在我们使用接口,有一个newsPresentation自动属性即为逻辑层的对象,而我们在控制器中并未设置其属性值直接在Index中调用其公共方法,?
这个接口的实现对象是如何赋值的呢这就是第一步注册起的作用了,Ninject的工作就是把逻辑层的实现类和接口进行绑定,对调用逻辑层处理类的地方使用[Inject]属性标记,在注册时对所有引用类进行对象注入(即:依赖注入),一般是由操作类(本文即为HomeController)控制要引用的对象(即:依赖项)的初始化工作,现在这都交由Ninject来完成,依赖项反转了操作类的控制(这个即为:控制反转)
基本原理:
注册时,Ninject调用Bind方法绑定接口和实现类,放入容器中待用,在创建控制器实例时检查是否有标记为[Ninject]的成员,如果有就到容器中查找对应的接口实现类,然后创建一个该实例赋值给接口成员,这就是依赖注入的简单原理
所有的IOC框架实现机制大体一致(Ninject、Autofac,etc.)
最后
以上就是含糊帽子最近收集整理的关于使用IOC轻量级框架Ninject开发的全部内容,更多相关使用IOC轻量级框架Ninject开发内容请搜索靠谱客的其他文章。
发表评论 取消回复