Enterprise Solution 生成实体数据访问接口与实现类型 Code Smith 6.5 模板文件下载
数据库表定义为SalesOrder,用LLBL Gen Pro生成的实体定义是SalesOrderEntity,再用Code Smith生成的数据读写接口是ISalesOrderManager,最后是生成接口实现类型SalesOrderManager。
命名规范如下:
SalesOrder => SalesOrderEntity => ISalesOrderManager => SalesOrderManager
这个过程高度机械化,接口与实现类型生成完成,被框架调用即可完成数据读写。接口与实现类是万能的接口,可满足任意条件的查询与修改需求。
Interface.cst 接口文件:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="True" Description="Template description here." %> <%--<%@ Property Name="ProjectName" Type="System.String" Default="" Optional="False" Category="Project" Description="" %> --%> <%@ Property Name="AssemblyFile" Type="System.String" Default="" Optional="False" Category="Project" Description="" Editor="System.Windows.Forms.Design.FileNameEditor"%> <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Project" Description="" %> <%@ Assembly Name="System.Data" %> <%@ Import Namespace="System.Data" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Assembly Name="SD.LLBLGen.Pro.ORMSupportClasses.NET20" %> <%@ Import Namespace="SD.LLBLGen.Pro.ORMSupportClasses" %> <%@ Import Namespace="Foundation" %> <%@ Assembly Name="Foundation.Common" %> using System.Collections.Generic; using System.Data; using System.Text; using SD.LLBLGen.Pro.ORMSupportClasses; using <%=GetBusinessLogicProjectName%>; using <%=GetBusinessLogicProjectName%>.FactoryClasses; using <%=GetBusinessLogicProjectName%>.EntityClasses; using <%=GetBusinessLogicProjectName%>.HelperClasses; using <%=GetBusinessLogicProjectName%>.InterfaceClasses; using <%=GetBusinessLogicProjectName%>.DatabaseSpecific; <%-- using <%=ProjectName%>.Managers; --%> <script runat="template"> public string GetEntityName { get { string entityName; entityName=EntityClassHelper.GetEntityName(SourceTable.Name,AssemblyFile); entityName=EntityClassHelper.TrimEntityName(entityName); return entityName; } } public string GetFullEntityName { get { string entityName; entityName=EntityClassHelper.GetEntityName(SourceTable.Name,AssemblyFile); return entityName; } } public string JudgeCommon(int i, int length) { if(i==length) return ""; else return ","; } public string TrimEntityLowercaseName { get { string entityName; entityName=EntityClassHelper.GetEntityName(SourceTable.Name,AssemblyFile); entityName=EntityClassHelper.TrimEntityName(entityName); entityName=EntityClassHelper.TrimEntityLowercaseName(entityName); return entityName; } } public string GetBusinessLogicProjectName { get { string prjojectName=EntityClassHelper.PrefixProjectName(AssemblyFile); return prjojectName; } } </script> <%-- DataType get AnsiString SystemType get System.String --%> namespace <%=GetBusinessLogicProjectName%>.InterfaceClasses { public interface I<%=GetEntityName%>Manager { <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,IPrefetchPath2 prefetchPath); <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,IPrefetchPath2 prefetchPath,ExcludeIncludeFieldsList fieldList); EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket); EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket,ISortExpression sortExpression); EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket,ISortExpression sortExpression, IPrefetchPath2 prefetchPath); EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket, ISortExpression sortExpression, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList fieldList); <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=TrimEntityLowercaseName%>); <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=TrimEntityLowercaseName%> ,EntityCollection entitiesToDelete); <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=TrimEntityLowercaseName%>, EntityCollection entitiesToDelete, string seriesCode); void Delete<%=GetEntityName%>(<%=GetFullEntityName%> <%=TrimEntityLowercaseName%>); bool Is<%=GetEntityName%>Exist(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); bool Is<%=GetEntityName%>Exist(IRelationPredicateBucket filterBucket); int Get<%=GetEntityName%>Count(IRelationPredicateBucket filterBucket); <%=GetFullEntityName%> Clone<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); void Post<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); void Post<%=GetEntityName%>(<%=GetFullEntityName%> <%=TrimEntityLowercaseName%>); } }
Manager.cst 接口实现文件:
<%@ CodeTemplate Language="C#" TargetLanguage="C#" Src="" Inherits="" Debug="True" Description="Template description here." %> <%--<%@ Property Name="ProjectName" Type="System.String" Default="" Optional="False" Category="Project" Description="????" %> --%> <%@ Property Name="AssemblyFile" Type="System.String" Default="" Optional="False" Category="Project" Description="" Editor="System.Windows.Forms.Design.FileNameEditor" %> <%@ Property Name="SourceTable" Type="SchemaExplorer.TableSchema" Category="Project" Description="" %> <%@ Assembly Name="System.Data" %> <%@ Import Namespace="System.Data" %> <%@ Assembly Name="SchemaExplorer" %> <%@ Import Namespace="SchemaExplorer" %> <%@ Assembly Name="SD.LLBLGen.Pro.ORMSupportClasses.NET20" %> <%@ Import Namespace="SD.LLBLGen.Pro.ORMSupportClasses" %> <%@ Import Namespace="Foundation" %> <%@ Assembly Name="Foundation.Common" %> using System; using System.Collections.Generic; using System.Data; using System.Text; using SD.LLBLGen.Pro.ORMSupportClasses; using <%=GetBusinessLogicProjectName%>; using <%=GetBusinessLogicProjectName%>.FactoryClasses; using <%=GetBusinessLogicProjectName%>.EntityClasses; using <%=GetBusinessLogicProjectName%>.HelperClasses; using <%=GetBusinessLogicProjectName%>.InterfaceClasses; using <%=GetBusinessLogicProjectName%>.DatabaseSpecific; using <%=GetBusinessLogicProjectName%>.Managers; using Foundation.Common; <%--using EntityCollection = <%=GetBusinessLogicProjectName%>.HelperClasses.EntityCollection; --%> <script runat="template"> public string GetEntityName { get { string entityName; //Debugger.Break(); entityName=EntityClassHelper.GetEntityName(SourceTable.Name,AssemblyFile); entityName=EntityClassHelper.TrimEntityName(entityName); return entityName; } } public string GetFullEntityName { get { string entityName; //Debugger.Break(); entityName=EntityClassHelper.GetEntityName(SourceTable.Name,AssemblyFile); return entityName; } } public string JudgeCommon(int i, int length) { if(i==length) return ""; else return ","; } public string GetBusinessLogicProjectName { get { string prjojectName=EntityClassHelper.PrefixProjectName(AssemblyFile); return prjojectName; } } </script> namespace <%=GetBusinessLogicProjectName%>.Managers { [RemoteService("<%=GetEntityName%>Manager")] public class <%=GetEntityName%>Manager : Foundation.Common.ManagerBase, I<%=GetEntityName%>Manager { public <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>) { return Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,null); } public <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,IPrefetchPath2 prefetchPath) { return Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,prefetchPath,null); } public <%=GetFullEntityName%> Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>,IPrefetchPath2 prefetchPath,ExcludeIncludeFieldsList fieldList) { <%=GetFullEntityName%> _<%=GetEntityName%>=new <%=GetFullEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); using (DataAccessAdapterBase adapter=GetCompanyDataAccessAdapter()) { bool found=adapter.FetchEntity(_<%=GetEntityName%>, prefetchPath, null, fieldList); if (!found) throw new Foundation.Common.RecordNotFoundException("Invalid <%=GetEntityName%>"); } return _<%=GetEntityName%>; } public EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket) { return Get<%=GetEntityName%>Collection(filterBucket,null); } public EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket,ISortExpression sortExpression) { return Get<%=GetEntityName%>Collection(filterBucket,sortExpression,null); } public EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket,ISortExpression sortExpression, IPrefetchPath2 prefetchPath) { return Get<%=GetEntityName%>Collection(filterBucket,sortExpression,prefetchPath,null); } public EntityCollection Get<%=GetEntityName%>Collection(IRelationPredicateBucket filterBucket, ISortExpression sortExpression, IPrefetchPath2 prefetchPath, ExcludeIncludeFieldsList fieldList) { EntityCollection <%=GetEntityName%>Collection =new EntityCollection(new <%=GetEntityName%>EntityFactory()); using (DataAccessAdapterBase adapter =GetCompanyDataAccessAdapter()) { adapter.FetchEntityCollection(<%=GetEntityName%>Collection, filterBucket, 0,sortExpression, prefetchPath, fieldList); } return <%=GetEntityName%>Collection ; } public <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=GetEntityName%>) { return Save<%=GetEntityName%>(<%=GetEntityName%>,null); } public <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=GetEntityName%> ,EntityCollection entitiesToDelete) { return Save<%=GetEntityName%>(<%=GetEntityName%>,entitiesToDelete,string.Empty); } public <%=GetFullEntityName%> Save<%=GetEntityName%>(<%=GetFullEntityName%> <%=GetEntityName%>, EntityCollection entitiesToDelete, string seriesCode) { using (DataAccessAdapterBase adapter =GetCompanyDataAccessAdapter()) { try { adapter.StartTransaction(IsolationLevel.ReadCommitted, "Save<%=GetEntityName%>"); adapter.SaveEntity(<%=GetEntityName%>, true, false); adapter.Commit(); } catch { adapter.Rollback(); throw; } } return <%=GetEntityName%>; } public void Delete<%=GetEntityName%>(<%=GetFullEntityName%> <%=GetEntityName%>) { using (DataAccessAdapterBase adapter =GetCompanyDataAccessAdapter()) { if (!adapter.IsEntityExist<<%=GetFullEntityName%>>(<%=GetEntityName%>)) return; try { adapter.StartTransaction(IsolationLevel.ReadCommitted, "Delete<%=GetEntityName%>"); adapter.DeleteEntity(<%=GetEntityName%>); adapter.Commit(); } catch { adapter.Rollback(); throw; } } } public bool Is<%=GetEntityName%>Exist(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>) { RelationPredicateBucket filterBucket = new RelationPredicateBucket(); <% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %> filterBucket.PredicateExpression.Add(<%=GetEntityName%>Fields.<%=EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%>==<%=EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%>); <% } %> return Is<%=GetEntityName%>Exist(filterBucket); } public bool Is<%=GetEntityName%>Exist(IRelationPredicateBucket filterBucket) { return (Get<%=GetEntityName%>Count(filterBucket) > 0); } public int Get<%=GetEntityName%>Count(IRelationPredicateBucket filterBucket) { using (DataAccessAdapterBase adapter =GetCompanyDataAccessAdapter()) { return adapter.GetDbCount<<%=GetFullEntityName%>>(filterBucket); } } public <%=GetFullEntityName%> Clone<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>) { <%=GetFullEntityName%> source = this.Get<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>); <%=GetFullEntityName%> _<%=GetEntityName%> = (<%=GetFullEntityName%>)CloneEntity(source); <%-- //try to remove all primary key //currency.Ccy = string.Empty; //currency.CreatedBy = Shared.CurrentUser.UserId; //currency.CreatedDate = DateTime.Now; //currency.RevisedBy = Shared.CurrentUser.UserId; //currency.RevisedDate = DateTime.Now; --%> return _<%=GetEntityName%>; } public void Post<%=GetEntityName%>(<% for (int i = 0; i < SourceTable.PrimaryKey.MemberColumns.Count; i++) { %><%=SourceTable.PrimaryKey.MemberColumns[i].SystemType %> <%= EntityClassHelper.TrimFieldName(SourceTable.PrimaryKey.MemberColumns[i].Name)%><%=JudgeCommon(i,SourceTable.PrimaryKey.MemberColumns.Count-1)%><% } %>) { return; } public void Post<%=GetEntityName%>(<%=GetFullEntityName%> <%=GetEntityName%>) { return; } } }