关于自定义代码生成器

原创 2012年03月30日 09:25:21

在2010年以前一直都喜欢用动软的代码生成器来生成三层结构,借此缩短开发时间,但是在做完ERP项目之后发现,有时代码本身就存在着相同或者类似的地方,只要我们用心去找还是可以找到很多的,在遇到这个问题的时候要么抽象成泛型接口,要么就用制定为项目而生的代码生成器来用,具体就是看项目本身和业务本身的需求了。最近也是在从事一款HR产品的编程,在整个产品的过程中因为周期短,项目量重,所以我把整个项目的服务层和业务层都使用代码生成器,生成特定的增删改查,不过遗憾的是这些只能满足单表操作。下面就是我的一段代码:

1. 业务实现类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using SD.LLBLGen.Pro.ORMSupportClasses;

using Company.{0}.BusinessLogic.CommonTools.Utils;
using Company.{0}.BusinessLogic.CommonTools.QueryBuilder;
using Company.{0}.BusinessLogic.I{1};
using Company.{0}.BusinessEntity.{1};
using Company.DataAccess.{0}.I{1}Save;
using Company.DataAccess.{0}.I{1}Linq;
using Company.DataAccess.{0}.I{1}Read;
using Company.DataModel.{0}.{1}.EntityClasses;
using Company.DataModel.{0}.{1}.FactoryClasses;
using Company.DataModel.{0}.{1}.HelperClasses;
using Company.DataModel.{0}.{1}.DBAdapter;
using Company.DataModel.{0}.{1}.Linq;
using Company.Framework.Factory;
using Company.Framework.ILinq;

namespace Company.{0}.BusinessLogic.{1}
{

    public class {Y} : I{Y}
    {
        #region 自生成代码

        public virtual bool Add({Y}Model model)
        {
            bool result = false;
            I{X1}_{Y}Save {X2}{Y}Save = ObjectFactory.CreateObject<I{X1}_{Y}Save>();
            {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
            EntityUtil.CloneData<{Y}Model, {X1}{Y}Entity>(model, {X2}{Y}Entity);
            if ({X2}{Y}Save.Save({X2}{Y}Entity, true))
            {
                result = true;
            }
            return result;
        }

        public virtual bool Add(List<{Y}Model> listModels)
        {
            bool result = true;
            using (TransactionScope scope = new TransactionScope())
            {
                I{X1}_{Y}Save {X2}{Y}Save = ObjectFactory.CreateObject<I{X1}_{Y}Save>();
                {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
                foreach ({Y}Model model in listModels)
                {
                    EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}Entity, model);
                    if (!{X2}{Y}Save.Save({X2}{Y}Entity, true))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    scope.Complete();
                }
            }
            return result;
        }

        public virtual {Y}Model Read(int {Z})
        {
            I{X1}_{Y}Read {X2}{Y}Read = ObjectFactory.CreateObject<I{X1}_{Y}Read>();
            {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity({Z});
            {X2}{Y}Read.Read({X2}{Y}Entity);
            {Y}Model {X2}{Y}Model = new {Y}Model();
            EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}Entity, {X2}{Y}Model);
            return {X2}{Y}Model;
        }

        public virtual List<{Y}Model> ReadList(List<int> list{Z})
        {
            I{X1}_{Y}Read {X2}{Y}Read = ObjectFactory.CreateObject<I{X1}_{Y}Read>();
            List<{Y}Model> {X2}{Y}ModelList = new List<{Y}Model>();
            foreach (int {Z} in list{Z})
            {
                {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity({Z});
                {X2}{Y}Read.Read({X2}{Y}Entity);
                {Y}Model {X2}{Y}Model = new {Y}Model();
                 EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}Entity, {X2}{Y}Model);
                {X2}{Y}ModelList.Add({X2}{Y}Model);
            }
            return {X2}{Y}ModelList;
        }

        public virtual List<{Y}Model> ReadList({Y}Model model)
        {
            List<{Y}Model> {X2}{Y}ModelList = new List<{Y}Model>();

            DataAccessAdapter adpter = new DataAccessAdapter();
            LinqMetaData context = new LinqMetaData(adpter);
            //var sql = QueryBuilder.Create<{X1}{Y}Entity>()


            /*var sql = QueryBuilder.Create<{X1}{Y}Entity>()
                .Like(c => c.{T}, model.{T});//构造搜索条件

            List<{X1}{Y}Entity> {X2}{Y}EntityList = context.{X1}{Y}.Where(sql.Expression)
                .OrderBy(c => c.{Z}).ToList<{X1}{Y}Entity>(); //执行查询
            EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}EntityList, {X2}{Y}ModelList);//将结果集 转成 业务层实体列表
			*/
            return {X2}{Y}ModelList;
        }

        public virtual bool Save({Y}Model model)
        {
            bool result = false;

            {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
            DataAccessAdapter adapter = new DataAccessAdapter();
            {X2}{Y}Entity.{Z} = model.{Z};
            adapter.FetchEntity({X2}{Y}Entity);

            EntityUtil.CloneData<{Y}Model, {X1}{Y}Entity>(model, {X2}{Y}Entity);
            if (adapter.SaveEntity({X2}{Y}Entity, true))
            {
                result = true;
            }
            return result;
        }

        public virtual bool SaveList(List<{Y}Model> listModel)
        {
            bool result = true;
            using (TransactionScope scope = new TransactionScope())
            {

                {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
                DataAccessAdapter adapter = new DataAccessAdapter();
                foreach ({Y}Model model in listModel)
                {                 
                    {X2}{Y}Entity.{Z} = model.{Z};
                    adapter.FetchEntity({X2}{Y}Entity);

                    EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}Entity, model);
                    if (!adapter.SaveEntity({X2}{Y}Entity, true))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    scope.Complete();
                }
            }
            return result;
        }

        public virtual bool Delete(int {Z})
        {
            bool result = false;
            {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
            DataAccessAdapter adapter = new DataAccessAdapter();
            {X2}{Y}Entity.{Z} = {Z};
            adapter.FetchEntity({X2}{Y}Entity);
            {X2}{Y}Entity.RecordStatus = -1;
  
            if (adapter.SaveEntity({X2}{Y}Entity, true))
            {
                result = true;
            }
            return result;
        }

        public virtual bool Delete({Y}Model model)
        {
            bool result = false;
            {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
            DataAccessAdapter adapter = new DataAccessAdapter();
            {X2}{Y}Entity.{Z} = model.{Z};
            model.RecordStatus = -1;
            adapter.FetchEntity({X2}{Y}Entity);
            EntityUtil.CloneData<{X1}{Y}Entity, {Y}Model>({X2}{Y}Entity, model);

            if (adapter.SaveEntity({X2}{Y}Entity, true))
            {
                result = true;
            }
            return result;
        }

        public virtual bool DeleteList(string list{Z}s)
        {
            bool result = true;
            using (TransactionScope scope = new TransactionScope())
            {
                I{X1}_{Y}Save {X2}{Y}Save = ObjectFactory.CreateObject<I{X1}_{Y}Save>();
                string[] PA = list{Z}s.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                DataAccessAdapter adapter = new DataAccessAdapter();
                foreach (string {Z} in PA)
                {
                    {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
                    {X2}{Y}Entity.{Z} = int.Parse( {Z});
                    adapter.FetchEntity({X2}{Y}Entity);
                    {X2}{Y}Entity.RecordStatus = -1;

                    if (!adapter.SaveEntity({X2}{Y}Entity, true))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    scope.Complete();
                }
            }
            return result;
        }

        public virtual bool DeleteList(List<int> list{Z}s)
        {
            bool result = true;
            using (TransactionScope scope = new TransactionScope())
            {
                I{X1}_{Y}Save {X2}{Y}Save = ObjectFactory.CreateObject<I{X1}_{Y}Save>();
                
                DataAccessAdapter adapter = new DataAccessAdapter();
                foreach (int {Z} in list{Z}s)
                {
                    {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
                    {X2}{Y}Entity.{Z} = {Z};
                    adapter.FetchEntity({X2}{Y}Entity);
                    {X2}{Y}Entity.RecordStatus = -1;

                    if (!adapter.SaveEntity({X2}{Y}Entity, true))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    scope.Complete();
                }
            }
            return result;
        }

        public virtual bool DeleteList(List<{Y}Model> listModels)
        {
            bool result = true;
            using (TransactionScope scope = new TransactionScope())
            {
                I{X1}_{Y}Save {X2}{Y}Save = ObjectFactory.CreateObject<I{X1}_{Y}Save>();
                {X1}{Y}Entity {X2}{Y}Entity = new {X1}{Y}Entity();
                DataAccessAdapter adapter = new DataAccessAdapter();
                foreach ({Y}Model model in listModels)
                {

                    {X2}{Y}Entity.{Z} = model.{Z};
                    adapter.FetchEntity({X2}{Y}Entity);
                    {X2}{Y}Entity.RecordStatus = -1;

                    if (!adapter.SaveEntity({X2}{Y}Entity, true))
                    {
                        result = false;
                        break;
                    }
                }
                if (result)
                {
                    scope.Complete();
                }
            }
            return result;
        }
        #endregion
    }
}
2.实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
{5}

namespace Company.{0}.{1}.{2}
{
	{6}
    public class {3}{4}
    {
		{X}
    }
}


总结:其实这个代码生产器的实现并不难,难点在于要找出所有业务模块类似的东西,然后用替换的方法去整体修改。


相关文章推荐

rapid-generator4.06 JAVA代码生成器配置,模板自定义

一: 需求      读取数据库表结构,通过对字段类型、名称等分析得到需要的各种变量,根据模板生成相应的pojo类、hibernate的xml配置文件、dao和service的接  口和类。 二: 配...

关于自定义代码生成器(续)---SQL2008查询数据库字段主键,外键,唯一键等相关属性

2. 查询字段名,表名,架构名,序号,是否主键,是否外键,是否唯一键,是否为空,是否自动增长,默认值,字段类型,大小,长度,小数位数,相关属性select a.*,isnull(b.rtable,'0...
  • nlx0201
  • nlx0201
  • 2013年01月24日 14:05
  • 1506

JEECG V3.0 版本(jbpm5 工作流自定义+WEB UI快速开发库+代码生成器) spring mvc +hibernate

简要说明 JEECG V3.0版本推翻了原有SSH2架构,采用SpringMVC+Hibernate+Spring jdbc基础架构, 采用面向声明的开发模式,基于泛型方式编写极少代码即可实现...

mybatis自定义代码生成器(Generator)——自动生成model&dao代码

mybatis自定义代码生成器(Generator)——自动生成model&dao代码

关于使用MyBatis-Generator时自定义注解生成器的一些问题

使用MyBatis-Generator和通用Mapper整合时自定义注解生成器注意的问题
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于自定义代码生成器
举报原因:
原因补充:

(最多只允许输入30个字)