使用codesmith生成业务层代码之表

第一在主模板中注册新的业务层模板

<%@ Register Name="BizClassTemplate" Template="beingnet.entity.cst" MergeProperties="False" ExcludeProperties="" %>

第二向业务层的模板中复制粘贴业务层代码

        并替换表的名

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class <% = CurrentTable.Name%>
    {
        private DALFactory.DALFactory dataFactory = DataProvider.DefaultProvider;

        #region 增
        public int 添加(Entity.<% = CurrentTable.Name%> obj)
        {
            //通过工厂获得当前提供程序
            //用反射,通过web.config获得当前提供程序的名,再实例化


            return dataFactory.DAL<% = CurrentTable.Name%>Provider.Insert(obj);
        }
      //  public int 添加(string 机构名称, string 简称, int 排序)
       // {
       //     return this.添加(Guid.NewGuid(), 机构名称, 简称, 排序);
       // }
        public int 添加(Guid 机构ID, string 机构名称, string 简称, int 排序)
        {

            return this.添加(new OA.Entity.<% = CurrentTable.Name%>()
            {
                机构ID = 机构ID,
                简称 = 简称,
                名称 = 机构名称,
                排序 = 排序

            });
        }

        #endregion

        #region 改
        public int 修改(Entity.<% = CurrentTable.Name%> obj)
        {
          
            return dataFactory.DAL<% = CurrentTable.Name%>Provider.Update(obj);

        }
        public int 修改(Guid 机构ID, string 机构名称, string 简称, int 排序)
      {
            return this.修改(new OA.Entity.机构()
            {
                机构ID = 机构ID,
                名称 = 机构名称,
               简称 = 简称,
                排序 = 排序
            });

        }
        #endregion

        #region 删
        public int 删除(Entity.<% = CurrentTable.Name%> obj)
        {
                       return dataFactory.DAL<% = CurrentTable.Name%>Provider.Delete(obj);
        }
        public int 删除(Guid 机构ID)
        {
            return this.删除(new OA.Entity.<% = CurrentTable.Name%>()
            {
                机构ID = 机构ID
            });

        }
        #endregion

        #region 查
        public Entity.<% = CurrentTable.Name%> 取<% = CurrentTable.Name%>ById(Guid 机构ID)
        {
  
            return dataFactory.DAL<% = CurrentTable.Name%>Provider.LoadById(机构ID);
        }
        public List<Entity.<% = CurrentTable.Name%>> 取所有<% = CurrentTable.Name%>()
        {
            return dataFactory.DAL<% = CurrentTable.Name%>Provider.LoadAll();

        }

       // public List<Entity.部门> 取属于机构的部门(Guid 机构ID)
        //{
        //    return dataFactory.DAL机构Provider.Load部门By机构(机构ID);
        //}
    }
        #endregion
 
   第三在主模板中的<script  runat="template"> </script>

          中添加方法

 private void SaveEntityClasses()
    {
          CodeTemplate entitytemplate = new EntityClassTemplate();
            foreach(TableSchema table in this.ChooseSourceDatabase.Tables)
            {
                  entitytemplate.SetProperty("CurrentTable",table);
                entitytemplate.RenderToFile(this.templateOutputDirectory + "\\OA.Entity\\"  + table.Name+".cs",true);
             
                Debug.WriteLine(table.Name);
            }
          
    }

第四引用该方法

  this.GenerateBizClasses();

注:出现的错误,生成的代码位置错误

   纠正方法,在属性栏中重新指定新的位置

 

该过程仅仅替换表名

posted @ 2011-09-06 00:07  园博客  阅读(238)  评论(0)    收藏  举报