在原有框架基础上进行扩展运用T4模版添加表,新增Spring.Net。
1、T4模版代码,如下:
<#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".cs"#> <# CodeGenerationTools code = new CodeGenerationTools(this); MetadataLoader loader = new MetadataLoader(this); CodeRegion region = new CodeRegion(this, 1); MetadataTools ef = new MetadataTools(this); string inputFile = @"....\\OA.Model\\Model1.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using OA.IDal; namespace OA.IDBSession { public partial interface IDBSession { <# foreach(EntityType entity in ItemCollection.GetItems<EntityType>()){#> I<#=entity.Name#>Dal <#=entity.Name#>Dal { get; set; } <#}#> } }
2、Spring.Net重点说明,主要功能解决层之间的耦合。
1)、引入spring.net
2)、配置web.config
<configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <!--spring.Net配置--> <context> <resource uri="file://~/Config/Service.xml"/> </context> </spring> <connectionStrings>
新建文件夹Config/Service.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object type="OA.BLL.SYS_DEVICE_TABLEService, OA.BLL"
singleton="false" name="SYS_DEVICE_TABLEService" >
</object>
</objects>
完成以上步骤,即可在new对象的时候,进行解耦了。
例如针对 public ISYS_DEVICE_TABLEService DeviceInfoService =new SYS_DEVICE_TABLEService(); 进行解耦
public DeviceInfoController() { IApplicationContext ctx = ContextRegistry.GetContext(); DeviceInfoService = (ISYS_DEVICE_TABLEService)ctx.GetObject("SYS_DEVICE_TABLEService"); } private MLedSystemEntities db = new MLedSystemEntities(); public ISYS_DEVICE_TABLEService DeviceInfoService { get; set; }