ASP.NET 5:依赖注入

1.背景

如果某个具体的(或类)对象被客户程序所依赖,通常把它们抽象成抽象类或接口。简单说,客户程序摆脱所依赖的具体类型,称之为面向接口编程。

那么问题来了?如何选择客户程序所需要的实现类?在使用创建型模式下创建对象是不难解决这个问题。

但如果设计的不是具体业务逻辑,而是公共类库或框架程序,对外只提供抽象而已,该如何把外部使用的类型传递给它们?

我们可以采用“依赖注入”的方式,将加工好的抽象类型实体“注入”到客户程序中。

注:依赖注入DI(Dependency Injection),它和控制反转IOC(Inversion of Control)是同一个意思,不同术语叫法。

2.场景

客户程序获取年份,我们先设计一个接口:

using System;

namespace Blog.Consoles
{
public interface ITimeProvider
{
DateTime CurrentDate { get; }
}
}

并对这个接口实现(可以多种实现哦,这也是设计接口的原因):

using System;

namespace Blog.Consoles
{
public class TimeProvider : ITimeProvider
{
public DateTime CurrentDate
{
get { return DateTime.Now; }
}
}
}

那么我们在客户程序使用:

using System;

namespace Blog.Consoles
{
class Program
{
static void Main(string[] args)
{
ITimeProvider tp = new TimeProvider();
Console.WriteLine(tp.CurrentDate.Year); Console.ReadKey();
}
}
}

这样实现的依赖关系是:

显然这样客户程序还需要知道具体类型TimeProvider,我们增加一个对象:

using System;
using System.Collections.Generic; namespace Blog.Consoles
{
public class Assembler
{
//保存抽象类型和实体类型
static Dictionary<Type, Type> d = new Dictionary<Type, Type>(); static Assembler()
{
//注册抽象类型需要使用的实体类型
d.Add(typeof(ITimeProvider), typeof(TimeProvider));
} public object Create(Type type)
{
if ((type == null) || !d.ContainsKey(type))
{
throw new NullReferenceException();
} return Activator.CreateInstance(d[type]);
} public T Create<T>()
{
return (T)Create(typeof(T));
}
}
}

此时再改造依赖关系:

这样客户程序只依赖Assembler和ITimeProvider,并不知道TimeProvider存在。

接下来如何写注入代码?下面分几种方式。

3.构造注入

构造注入使用构造方法,通过Assembler或其它机制把抽象类型作为参数传递。其实现代码:

using System;

namespace Blog.Consoles
{
class Program
{
public ITimeProvider _tp;
public Program(ITimeProvider tp)
{
_tp = tp;
} public int GetYear()
{
return _tp.CurrentDate.Year;
} static void Main(string[] args)
{
ITimeProvider time = new Assembler().Create<ITimeProvider>();
Program p = new Program(time);
Console.WriteLine(p.GetYear()); Console.ReadKey();
}
}
}

4.设置注入

通过属性方法赋值实现的,相对于构造方法一次性注入的方式,设置注入可以在需要时有更改的机会。实现代码:

using System;

namespace Blog.Consoles
{
class Program
{
public ITimeProvider _tp { get; set; } public int GetYear()
{
return _tp.CurrentDate.Year;
} static void Main(string[] args)
{
ITimeProvider time = new Assembler().Create<ITimeProvider>(); Program p = new Program();
p._tp = time; Console.WriteLine(p.GetYear()); Console.ReadKey();
}
}
}

还可以简写:

using System;

namespace Blog.Consoles
{
class Program
{
public ITimeProvider _tp { get; set; } public int GetYear()
{
return _tp.CurrentDate.Year;
} static void Main(string[] args)
{
var p = new Program() { _tp = new Assembler().Create<ITimeProvider>() }; Console.WriteLine(p.GetYear()); Console.ReadKey();
}
}
}

5.MEF注入

导出部件:

下面不使用Assembler对象,实现代码:

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection; namespace Blog.Consoles
{
class Program
{
private static CompositionContainer container; private void Compose()
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
container = new CompositionContainer(catalog);
//将部件和宿主程序添加到组合容器
container.ComposeParts(this, new TimeProvider());
} //导入部件
[Import]
public ITimeProvider _tp { get; set; } public int GetYear()
{
return _tp.CurrentDate.Year;
} static void Main(string[] args)
{
var p = new Program();
p.Compose(); Console.WriteLine(p.GetYear()); Console.ReadKey();
}
}
}

 6.ASP.NET 5注入

修改Startup.cs:

测试:

7.小结

关于依赖注入的方式,还有接口注入,自定义特性等(它们之间区别和选择,自行体会或查资料),不常用就不说了。

感受到ASP.NET 5自带DI爽就行啦!(ASP.NET 5真的海皮啦!)

后面的帖子,进入项目实战了,最近比较忙,请园友保持耐心。

 
分类: DotNet 2015

ASP.NET 5:依赖注入的更多相关文章

  1. # ASP.NET Core依赖注入解读&使用Autofac替代实现

    标签: 依赖注入 Autofac ASPNETCore ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Aut ...

  2. 实现BUG自动检测 - ASP.NET Core依赖注入

    我个人比较懒,能自动做的事绝不手动做,最近在用ASP.NET Core写一个项目,过程中会积累一些方便的工具类或框架,分享出来欢迎大家点评. 如果以后有时间的话,我打算写一个系列的[实现BUG自动检测 ...

  3. [译]ASP.NET Core依赖注入深入讨论

    原文链接:ASP.NET Core Dependency Injection Deep Dive - Joonas W's blog 这篇文章我们来深入探讨ASP.NET Core.MVC Core中 ...

  4. asp.net core 依赖注入几种常见情况

    先读一篇注入入门 全面理解 ASP.NET Core 依赖注入, 学习一下基本使用 然后学习一招, 不使用接口规范, 直接写功能类, 一般情况下可以用来做单例. 参考https://www.cnblo ...

  5. ASP.NET Core依赖注入——依赖注入最佳实践

    在这篇文章中,我们将深入研究.NET Core和ASP.NET Core MVC中的依赖注入,将介绍几乎所有可能的选项,依赖注入是ASP.Net Core的核心,我将分享在ASP.Net Core应用 ...

  6. 自动化CodeReview - ASP.NET Core依赖注入

    自动化CodeReview系列目录 自动化CodeReview - ASP.NET Core依赖注入 自动化CodeReview - ASP.NET Core请求参数验证 我个人比较懒,能自动做的事绝 ...

  7. ASP.NET Core 依赖注入最佳实践——提示与技巧

    在这篇文章,我将分享一些在ASP.NET Core程序中使用依赖注入的个人经验和建议.这些原则背后的动机如下: 高效地设计服务和它们的依赖. 预防多线程问题. 预防内存泄漏. 预防潜在的BUG. 这篇 ...

  8. ASP.NET Core依赖注入最佳实践,提示&技巧

    分享翻译一篇Abp框架作者(Halil İbrahim Kalkan)关于ASP.NET Core依赖注入的博文. 在本文中,我将分享我在ASP.NET Core应用程序中使用依赖注入的经验和建议. ...

  9. ASP.NET Core依赖注入解读&使用Autofac替代实现【转载】

    ASP.NET Core依赖注入解读&使用Autofac替代实现 1. 前言 2. ASP.NET Core 中的DI方式 3. Autofac实现和自定义实现扩展方法 3.1 安装Autof ...

  10. ASP.NET Core 依赖注入基本用法

    ASP.NET Core 依赖注入 ASP.NET Core从框架层对依赖注入提供支持.也就是说,如果你不了解依赖注入,将很难适应 ASP.NET Core的开发模式.本文将介绍依赖注入的基本概念,并 ...

随机推荐

  1. 【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)

    Cocos Code IDE 是一个基于 Eclipse 的跨平台 IDE ,专门为 cocos2d lua & js 开发者准备,通过此工具.你能够方便的创建游戏project.编写而且调试 ...

  2. SDUT 2933-人活着系列Streetlights(最小生成树Kruskal+和理查德设置来实现)

    人活着系列之Streetlights Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 人活着假设是为了家庭,亲情----能够说 ...

  3. mbed列--基于飞思卡尔FRDM KL25Z鼠标设计的高速实现

    ========================================================== 原创文章转载请注明:blog.csdn.net/guo8113 ========= ...

  4. ArcMap合并之路 -- 该段路合并成一个完整的路

    #1: 用 Arctoolbox\Data Management Tools\Generalization\dissolve 工具 #2: Options:dissolve field 项选" ...

  5. Cocos2d-x 3.x plist+png 做动画

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  6. Solr/SolrCloud -error

    状态 2014-08-20 10:46:22,356 INFO [coreZkRegister-1-thread-1] [org.apache.solr.cloud.ShardLeaderElecti ...

  7. mysql5.6设置主从报错1236,Increase max_allowed_packet on master,原因却是Binlog偏移量不对

    在试Mysql5.6,搭了个主从: CHANGE MASTER TO MASTER_HOST='1.2.3.4', master_user='slave', master_password='xxxq ...

  8. 微软系统工具套件SysinternalsSuite各个工具功能说明

    下载地址:http://download.sysinternals.com/files/SysinternalsSuite.zip 工具名    工具说明 Accesschk      Windows ...

  9. Android+NDK+CDT+eclipse+OPenGL ES编制和native调试

    周围环境: NDK版本号r8,eclipse和Android运用adt-bundle-windows-x86打包版本是更方便, 一.NDK汇集 1.源代码 NDK的examples文件夹中有Hello ...

  10. 使用SQLServer Audit来监控触发器的启用、禁用情况

    原文:使用SQLServer Audit来监控触发器的启用.禁用情况 使用情景: 有时候会发现在触发器中的业务逻辑没有执行,可能是因为触发器的逻辑错误所引起的.但是有时候却是因为一些触发器被禁用了. ...