MEF依赖注入简介
依赖注入对于开发人员来说,方便的就是不需要去关注具体的实现类,不需要去New实例对象,直接使用接口层就能让程序自动注入使用,当然,还有其他一些特点,比如web http同一个请求中可以设置同一个对象只实例化一次解决多个类中多次实例化对象浪费资源的问题。不多说,百度能得到更多 多的介绍,这边直接开工搭环境。
1、数据模型Model层创建
数据模型层,首先要创建数据库,再创建Model类。
创建数据库,表,添加一条测试数据
创建数据库

创建数据表

添加测试数据

我们已经知道有几层,所以,先把所有的类库项目全部先建立好,web为MVC的空项目,至于各层代码,分到各层再去处理
项目结构

各层依赖关系

好了,一般情况下,在这个框架里,我们只需要创建相应的Model类与数据库表相对应就行了,比如这个项目,我们只创建两个类:
SysUser.cs
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; }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
SysUserInfo.cs
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; }
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
新建类时,里面会自动加入创建者信息,怎么加的,可参考:
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目录下的所有程序集
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
{
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 + "."));
}
}
}
- 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
- 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
CompositionProvider.cs
using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Hosting;
using System.Composition.Hosting.Core;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.ModelBinding;
using System.Web.Mvc;
namespace InjectExample.Component.Mvc
{
public static class CompositionProvider
{
static CompositionHost _container;
static ExportFactory<CompositionContext> _requestScopeFactory;
static IList<Assembly> _partAssemblies = new List<Assembly>();
public static void SetConfiguration(ContainerConfiguration configuration)
{
if (configuration == null) throw new ArgumentNullException("configuration");
if (IsInitialized) throw new InvalidOperationException("Already initialized.");
_container = configuration.CreateContainer();
var factoryContract = new CompositionContract(
typeof(ExportFactory<CompositionContext>),
null,
new Dictionary<string, object> { { "SharingBoundaryNames", new[] { Boundaries.HttpRequest, Boundaries.DataConsistency, Boundaries.UserIdentity } } }
);
_requestScopeFactory = (ExportFactory<CompositionContext>)_container.GetExport(factoryContract);
ConfigureMvc();
}
static void ConfigureMvc()
{
if (DependencyResolver.Current.GetType().Name != "DefaultDependencyResolver")
{
throw new CompositionFailedException("MVC's DependencyResolver has been changed.");
}
DependencyResolver.SetResolver(new ScopeDependencyResolver());
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().SingleOrDefault());
FilterProviders.Providers.Add(new ScopeFilterAttributeFilterProvider());
System.Web.Mvc.ModelBinderProviders.BinderProviders.Add(new ScopeModelBinderProvider());
}
public static Export<CompositionContext> CreateContext()
{
PostStartDefaultInitialize();
return _requestScopeFactory.CreateExport();
}
internal static CompositionContext Current
{
get
{
var current = CurrentInitialisedScope;
if (current == null)
{
current = _requestScopeFactory.CreateExport();
CurrentInitialisedScope = current;
}
return current.Value;
}
}
internal static Export<CompositionContext> CurrentInitialisedScope
{
get { return (Export<CompositionContext>)HttpContext.Current.Items[typeof(CompositionProvider)]; }
private set { HttpContext.Current.Items[typeof(CompositionProvider)] = value; }
}
public static void AddAssemblies(params Assembly[] assemblies)
{
AddAssemblies((IEnumerable<Assembly>)assemblies);
}
public static void AddAssemblies(IEnumerable<Assembly> assemblies)
{
if (assemblies == null) throw new ArgumentException("assemblies");
foreach (var assembly in assemblies)
AddAssembly(assembly);
}
public static void AddAssembly(Assembly assembly)
{
if (assembly == null) throw new ArgumentNullException("assembly");
_partAssemblies.Add(assembly);
}
internal static void PostStartDefaultInitialize()
{
if (!IsInitialized)
{
SetConfiguration(new MvcContainerConfiguration());
}
}
static bool IsInitialized
{
get { return _requestScopeFactory != null; }
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
CompositionScopeModule.cs
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace InjectExample.Component.Mvc
{
public class CompositionScopeModule : IHttpModule
{
static bool _isInitialized;
public static void Register()
{
if (!_isInitialized)
{
_isInitialized = true;
DynamicModuleUtility.RegisterModule(typeof(CompositionScopeModule));
}
}
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.EndRequest += DisposeCompositionScope;
CompositionProvider.PostStartDefaultInitialize();
}
static void DisposeCompositionScope(object sender, EventArgs e)
{
var scope = CompositionProvider.CurrentInitialisedScope;
if (scope != null)
scope.Dispose();
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 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
- 59
- 60
- 61
- 62
- 63
- 64
ExportModelBinderAttribute.cs
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace InjectExample.Component.Mvc
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field)]
public sealed class ExportModelBinderAttribute : ExportAttribute
{
public ExportModelBinderAttribute(Type modelType)
: base(ScopeModelBinderProvider.GetModelBinderContractName(modelType), typeof(IModelBinder))
{
}
}
}
- 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
- 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
MvcContainerConfiguration.cs
using System;
using System.Collections.Generic;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Mvc;
namespace InjectExample.Component.Mvc
{
public class MvcContainerConfiguration : ContainerConfiguration
{
MvcContainerConfiguration(IEnumerable<Assembly> assemblies, AttributedModelProvider reflectionContext)
{
if (assemblies == null) throw new ArgumentNullException("assemblies");
if (reflectionContext == null) throw new ArgumentNullException("reflectionContext");
this.WithDefaultConventions(reflectionContext);
this.WithAssemblies(assemblies);
}
public MvcContainerConfiguration(IEnumerable<Assembly> assemblies)
: this(assemblies, DefineConventions())
{
}
public MvcContainerConfiguration()
: this(ConfigurationExt.DefaultAssemblies(true))
{
}
internal static Assembly GuessGlobalApplicationAssembly()
{
return HttpContext.Current.ApplicationInstance.GetType().BaseType.Assembly;
}
private static AttributedModelProvider DefineConventions()
{
var rb = new ConventionBuilder();
rb.ForTypesDerivedFrom<IController>().Export();
rb.ForTypesDerivedFrom<IHttpController>().Export();
rb.ForTypesMatching(IsAPart)
.Export()
.ExportInterfaces();
return rb;
}
private static bool IsAPart(Type t)
{
return !t.IsAssignableFrom(typeof(Attribute)) && t.IsInNamespace("Parts");
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
RemoveHttpHeadModule.cs,这个和依赖注入没关系,是附加的功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace InjectExample.Component.Mvc
{
public class RemoveHttpHeadModule : IHttpModule
{
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpContext.Current.Response.Headers.Remove("Server");
}
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreSendRequestHeaders += context_PreSendRequestHeaders;
}
}
}
- 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
- 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
ScopeDependencyResolver.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace InjectExample.Component.Mvc
{
public class ScopeDependencyResolver : IDependencyResolver
{
public object GetService(Type serviceType)
{
object export;
try
{
if (!CompositionProvider.Current.TryGetExport(serviceType, null, out export))
return null;
}
catch
{
return null;
}
return export;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return CompositionProvider.Current.GetExports(serviceType);
}
}
}
- 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
- 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
ScopeFilterAttributeFilterProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Composition;
namespace InjectExample.Component.Mvc
{
class ScopeFilterAttributeFilterProvider : FilterAttributeFilterProvider
{
public ScopeFilterAttributeFilterProvider()
: base(cacheAttributeInstances: false) { }
protected override IEnumerable<FilterAttribute> GetActionAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var attributes = base.GetActionAttributes(controllerContext, actionDescriptor).ToArray();
ComposeAttributes(attributes);
return attributes;
}
protected override IEnumerable<FilterAttribute> GetControllerAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
{
var attributes = base.GetControllerAttributes(controllerContext, actionDescriptor).ToArray();
ComposeAttributes(attributes);
return attributes;
}
void ComposeAttributes(FilterAttribute[] attributes)
{
foreach (var attribute in attributes)
CompositionProvider.Current.SatisfyImports(attribute);
}
}
}
- 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
- 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
ScopeModelBinderProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
namespace InjectExample.Component.Mvc
{
class ScopeModelBinderProvider : IModelBinderProvider
{
const string ModelBinderContractNameSuffix = "++ModelBinder";
public static string GetModelBinderContractName(Type modelType)
{
return modelType.AssemblyQualifiedName + ModelBinderContractNameSuffix;
}
public IModelBinder GetBinder(Type modelType)
{
IModelBinder export;
if (!CompositionProvider.Current.TryGetExport(GetModelBinderContractName(modelType), out export))
return null;
return export;
}
}
}
- 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
- 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
2.3 创建EF自定义实体映射相关底层类
主要是一些基层的单元操作、Repository公共接口和公共实现、DbContext类、数据库视图初始化等等。

DatabaseInitializer.cs
using InjectExample.Component.Mvc;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Mapping;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public static class DatabaseInitializer
{
public static void Initialize()
{
Database.SetInitializer<EfDbContext>(null);
}
public static void PreCreateView()
{
using (var container = CompositionProvider.CreateContext())
{
var context = container.Value.GetExport<EfDbContext>();
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var collection = (StorageMappingItemCollection)objectContext.MetadataWorkspace.GetItemCollection(DataSpace.CSSpace);
collection.GenerateViews(new List<EdmSchemaError>());
}
}
}
}
- 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
- 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
EfDbContext.cs
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
[Shared(Boundaries.DataConsistency)]
[Export(typeof(EfDbContext))]
public class EfDbContext : DbContext
{
public EfDbContext()
: base("InjextExampleEntities")//连接字符串名称
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
Configuration.ValidateOnSaveEnabled = false;
}
[ImportMany]
public IEnumerable<IEntityMapper> EntityMappers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Configuration.ProxyCreationEnabled = false;
Configuration.LazyLoadingEnabled = false;
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
if (EntityMappers == null)
{
return;
}
foreach (var mapper in EntityMappers)
{
mapper.RegistTo(modelBuilder.Configurations);
}
base.OnModelCreating(modelBuilder);
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
EFRepositoryBase.cs
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using EntityFramework.Extensions;
namespace InjectExample.Component
{
public abstract class EfRepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class //EntityBase<TKey>
{
#region 属性
[Import]
public IUnitOfWork UnitOfWork { get; set; }
protected UnitOfWorkContextBase EfContext
{
get
{
if (UnitOfWork is UnitOfWorkContextBase)
{
return UnitOfWork as UnitOfWorkContextBase;
}
return null;
}
}
public virtual IQueryable<TEntity> Entities
{
get { return EfContext.Set<TEntity>().AsNoTracking(); }
}
public virtual IQueryable<TEntity> Table
{
get { return EfContext.Set<TEntity>(); }
}
#endregion
#region 公共方法
public virtual int Insert(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterNew(entity);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Insert(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterNew(entities);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Delete(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterDeleted<TEntity>(entity);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Delete(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterDeleted<TEntity>(entities);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Delete(Expression<Func<TEntity, bool>> predicate, bool isSave = true)
{
PublicHelper.CheckArgument(predicate, "predicate");
List<TEntity> entities = EfContext.Set<TEntity>().Where(predicate).ToList();
return entities.Count > 0 ? Delete(entities, isSave) : 0;
}
public virtual int Update(TEntity entity, bool isSave = true)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterModified<TEntity>(entity);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Update(IEnumerable<TEntity> entities, bool isSave = true)
{
PublicHelper.CheckArgument(entities, "entities");
EfContext.RegisterModified<TEntity>(entities);
return isSave ? EfContext.Commit() : 0;
}
public virtual int Commit(bool validateOnSaveEnabled = true)
{
return EfContext.Commit(validateOnSaveEnabled);
}
public virtual TEntity GetByKey(object key)
{
PublicHelper.CheckArgument(key, "key");
return EfContext.Set<TEntity>().Find(key);
}
public TEntity GetByFiltered(Expression<Func<TEntity, bool>> filter, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "")
{
var query = EfContext.Set<TEntity>().Where(filter);
if (orderBy != null)
{
query = orderBy(query);
}
HandleInclude(ref query, includeProperties);
return query.FirstOrDefault();
}
public virtual int Delete(Expression<Func<TEntity, bool>> predicate)
{
return Entities.Where(predicate).Delete();
}
public virtual int Update(Expression<Func<TEntity, bool>> funWhere, Expression<Func<TEntity, TEntity>> funUpdate)
{
return Entities.Where(funWhere).Update(funUpdate);
}
[Obsolete]
public virtual IList<dynamic> GetListByPage(IQueryable<dynamic> queryable, PagingInfo pageInfo, out int total)
{
queryable = queryable.AsNoTracking();
var q1 = queryable.FutureCount();
var lsitTemp = queryable.Skip(pageInfo.PageIndex).Take(pageInfo.PageSize).Future();
total = q1.Value;
return lsitTemp.ToList();
}
public IQueryable<TEntity> GetListByPage(Expression<Func<TEntity, bool>> filter, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy, PagingInfo pageInfo, string includeProperties = "")
{
pageInfo = pageInfo == null ? new PagingInfo { PageIndex = 1, PageSize = 10 } : pageInfo;
pageInfo.PageSize = pageInfo.PageSize > 0 ? pageInfo.PageSize : 10;
IQueryable<TEntity> query = filter != null ? Entities.Where(filter) : Entities;
HandleInclude(ref query, includeProperties);
pageInfo.RecCount = query.Count();
query = orderBy != null ? orderBy(query) : query;
return pageInfo.NeedPage ? (orderBy != null ? query.Skip((pageInfo.PageIndex - 1) * pageInfo.PageSize).Take(pageInfo.PageSize) : null) : query;
}
public virtual int ExcuteNoQuery(string sql, TransactionalBehavior behavior = TransactionalBehavior.DoNotEnsureTransaction, bool isInsertIdentity = false, string tableName = "", params object[] parameters)
{
if (isInsertIdentity)
{
if (tableName == "") tableName = typeof(TEntity).Name;
if (sql.TrimEnd().EndsWith(";") == false) sql = sql + ";";
sql = "SET IDENTITY_INSERT " + tableName + " ON;" + sql + "SET IDENTITY_INSERT " + tableName + " OFF;";
}
return EfContext.DbContext.Database.ExecuteSqlCommand(behavior, sql, parameters);
}
public virtual List<T> ExcuteQuery<T>(string sql, params object[] parameters)
{
return EfContext.DbContext.Database.SqlQueryWithNoLock<T>(sql, parameters).ToList();
}
#endregion
private void HandleInclude(ref IQueryable<TEntity> query, string includeProperties)
{
if (!string.IsNullOrWhiteSpace(includeProperties))
{
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
}
}
public bool RemoveHoldingEntityInContext(TEntity entity)
{
var objContext = ((IObjectContextAdapter)EfContext.DbContext).ObjectContext;
var objSet = objContext.CreateObjectSet<TEntity>();
var entityKey = objContext.CreateEntityKey(objSet.EntitySet.Name, entity);
Object foundEntity;
var exists = objContext.TryGetObjectByKey(entityKey, out foundEntity);
if (exists)
{
objContext.Detach(foundEntity);
}
return (exists);
}
public void RemoveFromContext(TEntity entity)
{
PublicHelper.CheckArgument(entity, "entity");
EfContext.RegisterRemove(entity);
}
public string GetEntityPropertiesChangedLog(TEntity model)
{
var propertiesChangedLog = string.Empty;
var list = GetEntityPropertiesChanges(model);
foreach (var tuple in list)
{
var displayName = tuple.Item1.Name;
var displayAttribute = tuple.Item1.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false) as System.ComponentModel.DataAnnotations.DisplayAttribute;
if (displayAttribute != null && !string.IsNullOrEmpty(displayAttribute.Name))
{
displayName = displayAttribute.Name;
}
propertiesChangedLog += string.Format("{0}:{1}->{2};", displayName, tuple.Item2, tuple.Item3);
}
return propertiesChangedLog.TrimEnd(';');
}
public List<Tuple<PropertyInfo, string, string>> GetEntityPropertiesChanges(TEntity model)
{
var result = new List<Tuple<PropertyInfo, string, string>>();
#region 修改的字段
var detachFlag = false;
var propertiesChangedLog = string.Empty;
var entry = EfContext.DbContext.Entry(model);
if (entry.State == EntityState.Detached)
{
detachFlag = true;
var keyNames = ((IObjectContextAdapter)EfContext.DbContext).ObjectContext.CreateObjectSet<TEntity>().EntitySet.ElementType.KeyMembers.Select(k => k.Name);
var propertyValues = new List<object>();
foreach (var p in model.GetType().GetProperties().Where(m => keyNames.Contains(m.Name)).ToList())
{
propertyValues.Add(p.GetValue(model));
}
var dbModel = EfContext.Set<TEntity>().Find(propertyValues.ToArray());
entry = EfContext.DbContext.Entry(dbModel);
entry.CurrentValues.SetValues(model);
}
foreach (var propertyName in entry.CurrentValues.PropertyNames.Where(m => entry.Property(m).IsModified).ToList())
{
if (propertyName.StartsWith("Modified"))
continue;
var property = entry.Property(propertyName);
var displayName = propertyName;
var propetyInfo = model.GetType().GetProperty(propertyName);
result.Add(new Tuple<PropertyInfo, string, string>(propetyInfo, (property.OriginalValue == null ? string.Empty : JsonUtility.ToJson(property.OriginalValue)), (property.CurrentValue == null ? string.Empty : JsonUtility.ToJson(property.CurrentValue))));
}
if (detachFlag)
{
entry.State = EntityState.Detached;
}
#endregion
return result;
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
EFUnitOfWorkContext.cs
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
[Shared(Boundaries.DataConsistency)]
[Export(typeof(IUnitOfWork))]
public class EfUnitOfWorkContext : UnitOfWorkContextBase
{
[Import]
public Lazy<EfDbContext> EfDbContext { get; set; }
protected override DbContext Context
{
get
{
return EfDbContext.Value;
}
}
}
}
- 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
- 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
IEntityMapper.cs,公共接口
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public interface IEntityMapper
{
void RegistTo(ConfigurationRegistrar configurations);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
IRepository.cs,公共接口
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public interface IRepository<TEntity>// where TEntity : class
{
#region 属性
IQueryable<TEntity> Entities { get; }
IQueryable<TEntity> Table { get; }
#endregion
#region 公共方法
int Insert(TEntity entity, bool isSave = true);
int Insert(IEnumerable<TEntity> entities, bool isSave = true);
int Delete(TEntity entity, bool isSave = true);
int Delete(IEnumerable<TEntity> entities, bool isSave = true);
int Delete(Expression<Func<TEntity, bool>> predicate, bool isSave = true);
int Update(TEntity entity, bool isSave = true);
int Update(IEnumerable<TEntity> entities, bool isSave = true);
int Commit(bool validateOnSaveEnabled = true);
TEntity GetByKey(object key);
TEntity GetByFiltered(Expression<Func<TEntity, bool>> filter, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = "");
int Delete(Expression<Func<TEntity, bool>> predicate);
int Update(Expression<Func<TEntity, bool>> funWhere, Expression<Func<TEntity, TEntity>> funUpdate);
[Obsolete]
IList<dynamic> GetListByPage(IQueryable<dynamic> source, PagingInfo pageInfo, out int total);
IQueryable<TEntity> GetListByPage(Expression<Func<TEntity, bool>> filter, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy, PagingInfo pageInfo, string includeProperties = "");
int ExcuteNoQuery(string sql, TransactionalBehavior behavior = TransactionalBehavior.DoNotEnsureTransaction, bool isInsertIdentity = false, string tableName = "", params object[] parameters);
List<T> ExcuteQuery<T>(string sql, params object[] parameters);
bool RemoveHoldingEntityInContext(TEntity entity);
void RemoveFromContext(TEntity entity);
string GetEntityPropertiesChangedLog(TEntity model);
List<Tuple<PropertyInfo, string, string>> GetEntityPropertiesChanges(TEntity model);
#endregion
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
IUnitOfWork.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public interface IUnitOfWork
{
#region 属性
bool IsCommitted { get; }
#endregion
#region 方法
int Commit(bool validateOnSaveEnabled = true);
void Rollback();
#endregion
}
}
- 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
- 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
IUnitOfWorkContext.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public interface IUnitOfWorkContext : IUnitOfWork, IDisposable
{
void RegisterNew<TEntity>(TEntity entity) where TEntity : class;
void RegisterNew<TEntity>(IEnumerable<TEntity> entities) where TEntity : class;
void RegisterModified<TEntity>(TEntity entity) where TEntity : class;
void RegisterModified<TEntity>(Expression<Func<TEntity, object>> propertyExpression, TEntity entity)
where TEntity : class;
void RegisterDeleted<TEntity>(TEntity entity) where TEntity : class;
void RegisterDeleted<TEntity>(IEnumerable<TEntity> entities) where TEntity : class;
DataSet SqlQueryForDataSet(CommandType commandType, string sql, SqlParameter[] parameters);
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 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
- 59
- 60
- 61
- 62
- 63
MefInjectionProvider.cs
using System;
using System.Collections.Generic;
using System.Composition;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Composition.Hosting.Core;
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 MefInjectionProvider
{
private static CompositionHost _container;
private static ExportFactory<CompositionContext> _exportFactory;
static MefInjectionProvider()
{
InitInjection();
}
public static Export<CompositionContext> CreateContext()
{
return _exportFactory.CreateExport();
}
public static void Test()
{
}
public static T GetExport<T>()
{
return (T)CreateContext().Value.GetExport(typeof(T));
}
internal static void InitInjection()
{
var conventions = new ConventionBuilder();
var configuration = new ContainerConfiguration()
.WithDefaultConventions(conventions)
.WithAssemblies(Configuration.DefaultAssemblies());
_container = configuration.CreateContainer();
var factoryContract = new CompositionContract(
typeof(ExportFactory<CompositionContext>),
null,
new Dictionary<string, object> { { "SharingBoundaryNames", new[] { Boundaries.HttpRequest, Boundaries.DataConsistency, Boundaries.UserIdentity } } }
);
_exportFactory = (ExportFactory<CompositionContext>)_container.GetExport(factoryContract);
}
public static void Dispose()
{
if (_container != null)
{
_container.Dispose();
}
}
}
public static class Configuration
{
public static IList<Assembly> DefaultAssemblies()
{
var exts = new[] { "exe", "dll" };
var dir = HttpRuntime.BinDirectory != null ? 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());
return configuration;
}
}
public static class Boundaries
{
public const string HttpRequest = "HttpRequest";
public const string DataConsistency = "DataConsistency";
public const string UserIdentity = "UserIdentity";
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
UnitOfWorkContextBase.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Component
{
public abstract class UnitOfWorkContextBase : IUnitOfWorkContext
{
protected abstract DbContext Context { get; }
public bool IsCommitted { get; private set; }
public DbContext DbContext { get { return Context; } }
public int Commit(bool validateOnSaveEnabled = false)
{
validateOnSaveEnabled = false;
if (IsCommitted)
{
return 0;
}
try
{
int result = Context.SaveChanges(validateOnSaveEnabled);
IsCommitted = true;
return result;
}
catch (DbEntityValidationException ex)
{
var entry = ex.EntityValidationErrors.First().Entry;
var err = ex.EntityValidationErrors.First().ValidationErrors.First();
var msg = err.ErrorMessage;
try
{
var displayName = entry.Entity.GetType().GetProperty(err.PropertyName).GetPropertyDisplayName();
msg = string.Format(msg, displayName, entry.CurrentValues.GetValue<object>(err.PropertyName));
}
catch (Exception)
{
}
throw new Exception(msg);
}
catch (DbUpdateException e)
{
if (e.InnerException != null && e.InnerException.InnerException is SqlException)
{
var sqlEx = e.InnerException.InnerException as SqlException;
throw sqlEx;
}
throw;
}
}
public void Rollback()
{
IsCommitted = false;
}
public void Dispose()
{
}
public DbSet<TEntity> Set<TEntity>() where TEntity : class// EntityBase<TKey>
{
return Context.Set<TEntity>();
}
public void RegisterNew<TEntity>(TEntity entity) where TEntity : class// EntityBase<TKey>
{
EntityState state = Context.Entry(entity).State;
if (state == EntityState.Detached)
{
Context.Entry(entity).State = EntityState.Added;
}
IsCommitted = false;
}
public void RegisterNew<TEntity>(IEnumerable<TEntity> entities) where TEntity : class// EntityBase<TKey>
{
try
{
Context.Configuration.AutoDetectChangesEnabled = false;
foreach (TEntity entity in entities)
{
RegisterNew<TEntity>(entity);
}
}
finally
{
Context.Configuration.AutoDetectChangesEnabled = true;
}
}
public void RegisterModified<TEntity>(IEnumerable<TEntity> entities) where TEntity : class// EntityBase<TKey>
{
try
{
Context.Configuration.AutoDetectChangesEnabled = false;
foreach (TEntity entity in entities)
{
EntityState state = Context.Entry(entity).State;
if (state == EntityState.Detached)
{
Context.Entry(entity).State = EntityState.Modified;
}
IsCommitted = false;
}
}
finally
{
Context.Configuration.AutoDetectChangesEnabled = true;
}
}
public void RegisterModified<TEntity>(TEntity entity) where TEntity : class// EntityBase<TKey>
{
Context.Update<TEntity>(entity);
IsCommitted = false;
}
public void RegisterModified<TEntity>(Expression<Func<TEntity, object>> propertyExpression, TEntity entity) where TEntity : class// EntityBase<TKey>
{
}
public void RegisterDeleted<TEntity>(TEntity entity) where TEntity : class// EntityBase<TKey>
{
Context.Entry(entity).State = EntityState.Deleted;
IsCommitted = false;
}
public void RegisterDeleted<TEntity>(IEnumerable<TEntity> entities) where TEntity : class// EntityBase<TKey>
{
try
{
Context.Configuration.AutoDetectChangesEnabled = false;
foreach (TEntity entity in entities)
{
RegisterDeleted<TEntity>(entity);
}
}
finally
{
Context.Configuration.AutoDetectChangesEnabled = true;
}
}
public void RegisterRemove<TEntity>(TEntity entity) where TEntity : class// EntityBase<TKey>
{
Context.Set<TEntity>().Remove(entity);
IsCommitted = false;
}
public DataSet SqlQueryForDataSet(CommandType commandType, string sql, SqlParameter[] parameters)
{
using (var conn = new SqlConnection { ConnectionString = Context.Database.Connection.ConnectionString })
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
var cmd = new SqlCommand { Connection = conn, CommandText = sql, CommandType = commandType, CommandTimeout = conn.ConnectionTimeout };
if (parameters != null && parameters.Length > 0)
{
foreach (var item in parameters)
{
cmd.Parameters.Add(item);
}
}
var adapter = new SqlDataAdapter(cmd);
var ds = new DataSet();
adapter.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}
}
}
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 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
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
至此,Component层也完成了,里面一些其他公共方法 ,我就不一一写出了,到时候我会在文章最后附上源码。
3、Core 数据访问层处理
有了Component层之后,Core层对于开发人员来说就变得十分简单了,但是要创建几个简单的类去满足Component层的需要的映射对象。
SysUserConfiguration.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Configurations
{
[Export(typeof(IEntityMapper))]
public partial class SysUserConfiguration : EntityTypeConfiguration<SysUser>, IEntityMapper
{
public SysUserConfiguration()
{
SysUserConfigurationAppend();
}
partial void SysUserConfigurationAppend();
public void RegistTo(ConfigurationRegistrar configurations)
{
configurations.Add(this);
}
}
}
- 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
- 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
SysUserInfoConfiguration.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Configurations
{
[Export(typeof(IEntityMapper))]
public partial class SysUserInfoConfiguration : EntityTypeConfiguration<SysUserInfo>, IEntityMapper
{
public SysUserInfoConfiguration()
{
SysUserInfoConfigurationAppend();
}
partial void SysUserInfoConfigurationAppend();
public void RegistTo(ConfigurationRegistrar configurations)
{
configurations.Add(this);
}
}
}
- 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
- 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
ISysUserRepository.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Repositories
{
public partial interface ISysUserRepository : IRepository<SysUser>
{ }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
ISysUserInfoRepository.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Repositories
{
public partial interface ISysUserInfoRepository : IRepository<SysUserInfo>
{ }
}
SysUserRepository.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Repositories.Impl
{
[Shared(Boundaries.DataConsistency)]
[Export(typeof(ISysUserRepository))]
internal partial class SysUserRepository : EfRepositoryBase<SysUser>, ISysUserRepository
{ }
}
- 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
- 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
SysUserInfoRepository.cs
using InjectExample.Component;
using InjectExample.Model;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.Repositories.Impl
{
[Shared(Boundaries.DataConsistency)]
[Export(typeof(ISysUserInfoRepository))]
internal partial class SysUserInfoRepository : EfRepositoryBase<SysUserInfo>, ISysUserInfoRepository
{ }
}
- 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
- 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
下面是真正的数据操作Service类,随便怎么写,这里演示两表连接查询方法
ISysUserService.cs
using InjectExample.Model;
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.SysUsers
{
public interface ISysUserService
{
SysUserVModel GetById(long id);
}
}
SysUserService.cs,Service方法实现,引用Repository时,使用Import属性自动注入
using InjectExample.Core.Repositories;
using InjectExample.Model;
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.Core.SysUsers.Impl
{
[Export(typeof(ISysUserService))]
public class SysUserService : CoreServiceBase, ISysUserService
{
[Import]
public ISysUserRepository SysUserRepository { get; set; }
[Import]
public ISysUserInfoRepository SysUserInfoRepository { get; set; }
public SysUserVModel GetById(long id)
{
var user = (from u in SysUserRepository.Entities
join ui in SysUserInfoRepository.Entities
on u.ID equals ui.SysUserId
select new SysUserVModel
{
ID = u.ID,
Age = u.Age,
Name = u.Name,
Remark = u.Remark,
LoginCount = ui.LoginCount,
LastLoginTime = ui.LastLoginTime
}).FirstOrDefault();
return user;
}
}
}
- 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
- 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
这里,Core层也完成了。
4、BLL逻辑处理层实现
BLL实现上,这里就是简单引用一下Service层的方法。

ISysUserContract.cs
using InjectExample.Model;
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.BLL.SysUsers
{
public interface ISysUserContract
{
SysUserVModel GetById(long id);
}
}
SysUserContract.cs,引用 Service层时,也是使用 Import 属性自动注入
using InjectExample.Component;
using InjectExample.Core.SysUsers;
using InjectExample.Model;
using InjectExample.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InjectExample.BLL.SysUsers.Impl
{
[Export(typeof(ISysUserContract))]
public class SysUserContract : ISysUserContract
{
[Import]
public ISysUserService SysUserService { get; set; }
public SysUserVModel GetById(long id)
{
if (id <= 0) throw new BusinessException("ID不正确!");
return SysUserService.GetById(id);
}
}
}
- 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
- 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
BLL代码完成。
5、web表示层处理
因为依赖注入框架,内部用的是反射机制,所以在web运行后,首先要做的就是初始化加载dll,还有一些其他的内容。
在Global.asax 的 Application_Start 方法中,调用方法初始化数据
protected void Application_Start()
{
//禁用MVC响应标头
MvcHandler.DisableMvcResponseHeader = true
AreaRegistration.RegisterAllAreas()
RouteConfig.RegisterRoutes(RouteTable.Routes)
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
//MefInjectionProvider.Test()
//Db初始化
DatabaseInitializer.Initialize()
DatabaseInitializer.PreCreateView()
}
还有,依赖注入的初始化,继承于 IHttpModule ,所以在webconfig文件中加入注册代码,也能实现自动初始化,web.config加内容:

<modules>
<add name="CompositionScopeModule" type="InjectExample.Component.Mvc.CompositionScopeModule" preCondition="managedHandler" />
<add name="RemoveHttpHeadModule" type="InjectExample.Component.Mvc.RemoveHttpHeadModule" />
</modules>
在App_Start下加FilterConfig.cs文件,内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace InjectExample.Web.App_Start
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}
web.config加连接字符串:
<connectionStrings>
<add name="InjextExampleEntities" connectionString="Data Source=192.168.9.222;Initial Catalog=Example;Persist Security Info=True;User ID=dev;Password=123456;MultipleActiveResultSets=True;App=EntityFramework;" providerName="System.Data.SqlClient" />
</connectionStrings>
然后,就可以运行了,再添加测试数据显示控制器和View页面:

HomeController.cs
using InjectExample.BLL.SysUsers;
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace InjectExample.Web.Controllers
{
public class HomeController : Controller
{
[Import]
public ISysUserContract SysUserContract { get; set; }
public ActionResult Index()
{
ViewBag.SysUser = SysUserContract.GetById(1);
return View();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
Index.cshtml
@{
Layout = null;
var model = ViewBag.SysUser as InjectExample.Model.ViewModel.SysUserVModel;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>这是测试首页</title>
</head>
<body>
<div>
这是测试首页<br />
用户名称 @(model.Name)<br />
用户年龄 @(model.Age)<br />
登陆次数 @(model.LoginCount)<br />
最后登陆 @(model.LastLoginTime)<br />
</div>
</body>
</html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
这次可以显示测试数据了,附运行结果图:

框架搭建完毕,此框架的使用,也是很方便,一般Model层、Core层的Configurations和Repositories中的代码,都可用CodeSmith等模版生成器自动生成,开发人员只需要实现具体的数据访问和逻辑处理层就可以在web中调用了,非常方便。
这个框架搭建起来,比上一单的简单三层框架要复杂了很多,不过只要理解了,只是耗时多一点而已。
源码免费下地地址:
http://download.csdn.net/detail/pukuimin1226/9616604
版权声明:
作者:真爱无限
出处:http://blog.csdn.net/pukuimin1226/
本文为博主原创文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接.