关闭

关于自定义代码生成器

916人阅读 评论(2) 收藏 举报

在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}
    }
}


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


2
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:126543次
    • 积分:2299
    • 等级:
    • 排名:第16242名
    • 原创:68篇
    • 转载:2篇
    • 译文:3篇
    • 评论:129条
    博客专栏
    文章分类
    最新评论