前言
前面一章已经讲解了MutualService.Data.DAL.DALData,MutualService.Data.DAL.IDAL目录里面的内容,本章会继续讲后面的目录内容,让我们继续看下去吧
ORM
DAL目录
1、 UnitOfWorkDbContextFactory:主要是起到连接数据库之用
public class UnitOfWorkDbContextFactory : IUnitOfWorkFactory
{
public IUnitOfWork Create(string connectionString)
{
return UnitOfWorkFactory.Instance.Create(connectionString);
}
public IUnitOfWork Create()
{
return UnitOfWorkFactory.Instance.Create();
}
/// 使用默认配置创建工作单元的实例。
/// </summary>
/// <param name="Name">数据库连接串名称。</param>
/// <returns>工作单元的实例。</returns>
public IUnitOfWork CreateForConnectionStrings(string Name)
{
return UnitOfWorkFactory.Instance.CreateForConnectionStrings(Name);
}
}
2、 DalFactory:业务工厂接口
public class DalFactory : IDalFactory
{
public Common.Interfaces.IUnitOfWorkFactory CreateOfWorkFactory()
{
return new UnitOfWorkDbContextFactory();
}
public IDALPermissionsMembersRole CreateDalPermissionsMembersRole(IUnitOfWork iUnitOfWork)
{
return new DALPermissionsMembersRole(iUnitOfWork);
}
}
3、DAL.tt:实现IDAL.tt的相关类,DaoBase.tt
<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
output extension=".cs" encoding="utf-8" #>
<#
string Namespace="Data"; // TODO:DAL接口的命名空间,根据项目的需要可在T4模板里进行修改。
string dalInterfaceNamespace="MutualService."+Namespace+".DAL.IDAL"; // TODO:DAL接口的命名空间,根据项目的需要可在T4模板里进行修改。
#>
<#
CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);
string inputFile = @"..\DALData\"+Namespace+"Model.edmx";
EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();
EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);
// Write out support code to primary template output file
WriteHeader(fileManager);
BeginNamespace(namespaceName, code);
WriteCustomObservableCollection();
EndNamespace(namespaceName);
EntityContainer container = ItemCollection.GetItems<EntityContainer>().FirstOrDefault();
4、DaoBase.tt:实现数据库的增删改查等相关ADO.NET的操作
#region 查询过滤预处理
/// <summary>
/// 执行任何查询操作之前的预处理器。
/// </summary>
private readonly QueryableDataFilter<<#=entityNamespace+"."+code.Escape(entity)#>> defaultFilter;
/// <summary>
/// 创建一个带过滤器的查询对象。
/// </summary>
/// <param name="filter">查询过滤器。</param>
/// <returns>可查询对象。</returns>
private IQueryable<<#=entityNamespace+"."+code.Escape(entity)#>> CreateQueryable(QueryableDataFilter<<#=entityNamespace+"."+code.Escape(entity)#>> filter = null) {
IQueryable<<#=entityNamespace+"."+code.Escape(entity)#>> source = Db.<#= entity.Name #>;
var newFilter = filter;
if (filter == null) {
newFilter = DefaultDataHandle;
}
if (newFilter != null) {
source = newFilter(source);
}
return source;
}
DALFactory目录
1、DALCreater.cs:业务层只要通过此进行创建对应的实例,从而实现数据库增删改查操作
public class DALCreater
{
/*
private const string _configPath = "/Config/APPSystem/DALPlugInConfig.xml";
private const string _iDalAssembly = "LocalService.Data.DAL.IDAL";
private const string _defaultDalAssembly = "LocalService.Data.DAL.DAL";
private Common.Utils.Plug.PlugInHelper _plugInHelper;*/
private static object _lockInstance = new object();
private static DALCreater _instance = null;
public static DALCreater Instance()
{
//return new DALCreater();
if (_instance == null)
{
lock (_lockInstance)
{
if (_instance == null)
{
_instance = new DALCreater();
}
}
}
return _instance;
}
private DALCreater()
{
//_plugInHelper = new Common.Utils.Plug.PlugInHelper(Common.Utils.WebForm.ConfigHelper.ConfigAddress + _configPath, _iDalAssembly, _defaultDalAssembly);
}
private static readonly object LockObject = new object();
private IDAL.IDalFactory _dalFactory;
public IDAL.IDalFactory IdalFactory
{
get
{
//return new DalFactory();
if (_dalFactory == null)
{
lock (LockObject)
{
if (_dalFactory == null)
{
_dalFactory = new DalFactory();// _plugInHelper.GetPlugInClass<IDAL.IDalFactory>("IDalFactory", "DalFactory");
}
}
}
return _dalFactory;
}
}
}
Model目录
1、Model.tt:生成对应数据库表的实体映射类
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
<#=String.Join(String.Empty, extraUsings.Select(u => "using " + u + ";" + Environment.NewLine).ToArray())#>
<#+
fileManager.EndBlock();
}
void BeginNamespace(string namespaceName, CodeGenerationTools code)
{
CodeRegion region = new CodeRegion(this);
if (!String.IsNullOrEmpty(namespaceName))
{
#>
namespace <#=code.EscapeNamespace(namespaceName)#>
{
<#+
PushIndent(CodeRegion.GetIndent(1));
}
}
void EndNamespace(string namespaceName)
{
if (!String.IsNullOrEmpty(namespaceName))
{
PopIndent();
#>
}
<#+
}
}
string GetColumnDesc(EFSQLToCode SQLToCode,string tableName, string ColumnName)
{
return @"/// <summary>
/// "+SQLToCode.GetColumnDesc(tableName,ColumnName)
+@"
/// </summary>";
}
string GetTableTitle(EFSQLToCode SQLToCode,string tableName)
{
return @"/// <summary>
/// "+SQLToCode.GetTableTitle(tableName)
+@"
/// </summary>";
}
调用顺序
1、直接通过工作单元调用:属于全局调用
DALCreater->UnitOfWorkDbContextFactory->UnitOfWorkFactory->model.content.tt->.edmx
2、某个类调用:也需要创建工作单元之后传入,因为ORM最小操作单位就是工作单元
DALCreater->DalFactory(IDalFactory)->DAL.tt(IDAL.tt)->DaoBase.tt(IDALBase.cs,Model.tt)->model.content.tt->.edmx
结语
ORM结构就讲解到这里了,有什么不懂的话,可以直接联系我,下面是整个ORM的代码,你们可以参考:
ORM链接