Servrvice层的代码:
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <object name="UserInfoService" type="SunOA.BLL.UserInfoService, SunOA.BLL" singleton="false" > <property name="DbSession" ref="DbSession" /> <property name="CurrentDal" ref="UserInfoDal" /> <!--<constructor-arg index="0" ref="DbSession" />--> </object> <object name="OrderInfoService" type="SunOA.BLL.OrderInfoService, SunOA.BLL" singleton="false" > <property name="DbSession" ref="DbSession" /> <property name="CurrentDal" ref="OrderInfoDal" /> <!--构造函数注入--> <!--<constructor-arg index="0" ref="DbSession" />--> </object> </objects>
Dal层代码:
<?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <!--<object name="DbSessionFactory" type="SunOA.DALFactory.DbSessionFactory,SunOA.DALFactory " singleton="true" > </object>--> <!--spring.net 通过工厂的实例方法来创建对象的配置demo--> <!--<object name="DbSession" type="SunOA.DALFactory.DbSession, SunOA.DALFactory" singleton="false" factory-method="GetCurrentDbSession" factory-object="DbSessionFactory" > </object>--> <!--spring.net 通过工厂的一个静态方法来创建对象的 配置demo。 type就直接配置到工厂类型就可以了。--> <object name="DbSession" type="SunOA.DALFactory.DbSessionFactory, SunOA.DALFactory" singleton="false" factory-method="GetCurrentDbSession" > </object> <object name="OrderInfoDal" type="SunOA.EFDAL.OrderInfoDal, SunOA.EFDAL" singleton="false" > </object> <object name="UserInfoDal" type="SunOA.EFDAL.UserInfoDal, SunOA.EFDAL" singleton="false" > </object> </objects>
获取数据库的所有表:
exec sp_tables
拿到表中的列:
exec sp_columns users
查看数据库名称:
exec sp_databases
T4模板的使用:
SunOA.IDAL下:IDals
<#@ 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 = @"..\SunOA.Model\DataModel.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 SunOA.Model; namespace SunOA.IDAL { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public partial interface I<#=entity.Name#>Dal : IBaseDal<<#=entity.Name#>> { } <#}#> }
IDbSession.tt
<#@ 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 = @"..\SunOA.Model\DataModel.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> namespace SunOA.IDAL { public partial interface IDbSession { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> I<#=entity.Name#>Dal <#=entity.Name#>Dal { get;} <#}#> } }
SunOA.EFDAL.Dals
<#@ 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 = @"..\SunOA.Model\DataModel.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using SunOA.IDAL; using SunOA.Model; namespace SunOA.EFDAL { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public partial class <#=entity.Name#>Dal:BaseDal<<#=entity.Name#>>,I<#=entity.Name#>Dal { } <#}#> }
DbSession.tt
<#@ 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 = @"..\SunOA.Model\DataModel.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #> using SunOA.EFDAL; using SunOA.IDAL; namespace SunOA.DALFactory { public partial class DbSession :IDbSession { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public I<#=entity.Name#>Dal <#=entity.Name#>Dal { get { return StaticDalFactory.Get<#=entity.Name#>Dal(); } } <#}#> } }
StaticDalFactory
<#@ 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 = @"..\SunOA.Model\DataModel.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.Reflection; using System.Text; using System.Threading.Tasks; using System.Web; using SunOA.EFDAL; using SunOA.IDAL; using SunOA.NHDAL; namespace SunOA.DALFactory { public partial class StaticDalFactory { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public static I<#=entity.Name#>Dal Get<#=entity.Name#>Dal() { return Assembly.Load(assemblyName).CreateInstance(assemblyName + ".<#=entity.Name#>Dal") as I<#=entity.Name#>Dal; } <#}#> } }
IBLL.IServices
<#@ 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 = @"..\SunOA.Model\DataModel.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 SunOA.Model; namespace SunOA.IBLL { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public partial interface I<#=entity.Name#>Service:IBaseService<<#=entity.Name#>> { } <#}#> }
IBLL.ServerXmlSpring
<#@ template language="C#" debug="false" hostspecific="true"#> <#@ include file="EF.Utility.CS.ttinclude"#><#@ output extension=".xml"#><# CodeGenerationTools code = new CodeGenerationTools(this); MetadataLoader loader = new MetadataLoader(this); CodeRegion region = new CodeRegion(this, 1); MetadataTools ef = new MetadataTools(this); string inputFile = @"..\SunOA.Model\DataModel.edmx"; EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile); string namespaceName = code.VsNamespaceSuggestion(); EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this); #><?xml version="1.0" encoding="utf-8" ?> <objects xmlns="http://www.springframework.net"> <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> <object name="<#=entity.Name#>Service" type="SunOA.BLL.<#=entity.Name#>Service, SunOA.BLL" singleton="false" > </object> <#}#> </objects>
BLL.Services
<#@ 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 = @"..\SunOA.Model\DataModel.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 SunOA.DALFactory; using SunOA.EFDAL; using SunOA.IBLL; using SunOA.IDAL; using SunOA.Model; using SunOA.NHDAL; namespace SunOA.BLL { <# foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name)) { #> public partial class <#=entity.Name#>Service:BaseService<<#=entity.Name#>>,I<#=entity.Name#>Service //crud { public override void SetCurrentDal() { CurrentDal = DbSession.<#=entity.Name#>Dal; } } <#}#> }
添加类的时候,生成转换所有的T4模板。