MEF依赖注入简介
依赖注入对于开发人员来说,方便的就是不需要去关注具体的实现类,不需要去New实例对象,直接使用接口层就能让程序自动注入使用,当然,还有其他一些特点,比如web http同一个请求中可以设置同一个对象只实例化一次解决多个类中多次实例化对象浪费资源的问题。不多说,百度能得到更多 多的介绍,这边直接开工搭环境。
1、数据模型Model层创建
数据模型层,首先要创建数据库,再创建Model类。
创建数据库,表,添加一条测试数据
创建数据库
创建数据表
添加测试数据
??我们已经知道有几层,所以,先把所有的类库项目全部先建立好,web为MVC的空项目,至于各层代码,分到各层再去处理
项目结构??
各层依赖关系
好了,一般情况下,在这个框架里,我们只需要创建相应的Model类与数据库表相对应就行了,比如这个项目,我们只创建两个类:
SysUser.cs
/* ==============================================================================
* 功能描述:SysUserInfo
* 创 建 者:蒲奎民
* 创建日期:2016-08-29 15:58:13
* CLR Version :4.0.30319.42000
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Model
{
public partial class SysUser
{
public long ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Remark { get; set; }
}
}SysUserInfo.cs
/* ==============================================================================
* 功能描述:SysUserInfo
* 创 建 者:蒲奎民
* 创建日期:2016-08-29 15:58:13
* CLR Version :4.0.30319.42000
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Model
{
public partial class SysUserInfo
{
public long ID { get; set; }
public long SysUserId { get; set; }
public int LoginCount { get; set; }
public DateTime LastLoginTime { get; set; }
}
}??新建类时,里面会自动加入创建者信息,怎么加的,可参考:
VS2013修改模版、创建类模版文件参考: ??
http://blog.csdn.net/pukuimin1226/article/details/51685279
??至此,Model类就创建完成。??
2、底层公共方法层Component处理
??这层可以说是在这个框架搭建中最复杂的,但是,创建完之后,不需要怎么改动的地方。
好,为了能满足框架代码需要引用的Nuget包,我这里列出一下。??
2.1 添加Nuget包和 .Net程序集引用
<package id="EntityFramework" version="6.1.3" targetFramework="net45" /> <package id="EntityFramework.Extended" version="6.1.0.168" targetFramework="net45" /> <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net45" /> <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" /> <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net45" /> <package id="Microsoft.Composition" version="1.0.30" targetFramework="net45" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" /> <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
??在nuget管理器中添加完成就行。
所有项目都勾上,免得麻烦。
还有一些,要添加系统的程序集,比如 System.Web,System.Configuration等等。 ??
2.2 创建mvc依赖注入相关类:
??
先添加一个扩展类ConfigurationExt.cs,用来加载bin目录下的所有程序集??
/* ==============================================================================
* 功能描述:Configuration
* 创 建 者:蒲奎民
* 创建日期:2016-08-29 10:47:49
* CLR Version :4.0.30319.42000
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Composition.Hosting;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace InjectExample.Component
{
public static class ConfigurationExt
{
/// <summary>
/// 或取应用程序 Bin 路径
/// </summary>
/// <param name="inWeb">是否 web 应用</param>
/// <returns></returns>
public static IList<Assembly> DefaultAssemblies(bool inWeb)
{
var exts = new[] { "exe", "dll" };
var dir = inWeb ? HttpRuntime.BinDirectory : AppDomain.CurrentDomain.BaseDirectory;
var ns = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;//获取本类的命名空间
var nsStrs = ns.Split(new char[] { '.' });//拆分要取命名空间前缀
var files = Directory.EnumerateFiles(dir, nsStrs[0] + "*", SearchOption.TopDirectoryOnly)
.Where(file => exts.Any(x => file.EndsWith(x, StringComparison.OrdinalIgnoreCase)))
.ToList();
return files.Select(Assembly.LoadFrom).ToList();
}
public static ContainerConfiguration WithDefaultAssemblies(this ContainerConfiguration configuration)
{
configuration.WithAssemblies(DefaultAssemblies(true));
return configuration;
}
public static bool IsInNamespace(this Type type, string namespaceFragment)
{
return type.Namespace != null && (type.Namespace.EndsWith("." + namespaceFragment) || type.Namespace.Contains("." + namespaceFragment + &quo