• 通过T4模板实现代码自动生成


    1:准备.tt模板

     
    using BBFJ.OA.IBLL;
    using BBFJ.OA.IDAL;
    using BBFJ.OA.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BBFJ.OA.BLL
    {
        
        public partial class ActionInfoService :BaseService<ActionInfo>,IActionInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.ActionInfoDal;
            }
        }   
        
        public partial class DepartmentService :BaseService<Department>,IDepartmentService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.DepartmentDal;
            }
        }   
        
        public partial class R_UserInfo_ActionInfoService :BaseService<R_UserInfo_ActionInfo>,IR_UserInfo_ActionInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.R_UserInfo_ActionInfoDal;
            }
        }   
        
        public partial class RoleInfoService :BaseService<RoleInfo>,IRoleInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.RoleInfoDal;
            }
        }   
        
        public partial class UserInfoService :BaseService<UserInfo>,IUserInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.UserInfoDal;
            }
        }   
        
    }
    IManager.tt

    注意修改.tt模板中的

    string inputFile = @"..\BBFJ.OA.Model\Model.edmx";

    using BBFJ.OA.IBLL;
    using BBFJ.OA.IDAL;
    using BBFJ.OA.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace BBFJ.OA.BLL

    3: 修改原类的属性为部分类 partial

    public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService

    并注释掉重复内容

    using BBFJ.OA.IBLL;
    using BBFJ.OA.IDAL;
    using BBFJ.OA.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BBFJ.OA.BLL
    {
       public partial  class UserInfoService : BaseService<UserInfo>,IUserInfoService
        {
            //public override void SetCurrentDal()
            //{
            //   //子类拿到父类的CurrentDal属性
            //    //子类拿到父类的DBSession属性
            //    CurrentDal = this.CurrentDBSession.UserInfoDal;
            //}
            #region 02批量删除
            public bool DeleteEntities(List<int> list)
            {
                //拿到父类的DBSession属性,拿到具体的数据操作类,调用查询方法
                var userInfoList = this.CurrentDBSession.UserInfoDal.LoadEntities(u=>list.Contains(u.ID));
                foreach (var userInfo in userInfoList)
                {
                    this.CurrentDBSession.UserInfoDal.DeleteEntity(userInfo);
                }
                return this.CurrentDBSession.SaveChanges();
            }
            #endregion
    
            #region 03完成用户信息的搜索
            public IQueryable<UserInfo> LoadSearchEntities(Model.Search.UserInfoSearch userInfoSearch,short delFlag)
            {
                var temp = this.CurrentDBSession.UserInfoDal.LoadEntities(c=>c.DelFlag == delFlag );
                //根据用户名搜索
                if (!String.IsNullOrEmpty(userInfoSearch.UserName))
                {
                    temp = temp.Where<UserInfo>(u=>u.UName.Contains(userInfoSearch.UserName));
                }
                if (!String.IsNullOrEmpty(userInfoSearch.UserRemark))
                {
                    temp = temp.Where<UserInfo>(u => u.Remark.Contains(userInfoSearch.UserRemark));
                }
                userInfoSearch.TotalCount = temp.Count();
                return temp.OrderBy<UserInfo, int>(u => u.ID).Skip<UserInfo>((userInfoSearch.PageIndex - 1) * userInfoSearch.PageSize).Take<UserInfo>(userInfoSearch.PageSize);
            }
            #endregion
            
        }
    }
    UserInfoService

    4: 常见的错误:

    修改方法 public partial class UserInfoService : BaseService<UserInfo>,IUserInfoService 类前加"partial" 关键字

     

    注释到重复方法 

     最终生成代码

     
    using BBFJ.OA.IBLL;
    using BBFJ.OA.IDAL;
    using BBFJ.OA.Model;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BBFJ.OA.BLL
    {
        
        public partial class ActionInfoService :BaseService<ActionInfo>,IActionInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.ActionInfoDal;
            }
        }   
        
        public partial class DepartmentService :BaseService<Department>,IDepartmentService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.DepartmentDal;
            }
        }   
        
        public partial class R_UserInfo_ActionInfoService :BaseService<R_UserInfo_ActionInfo>,IR_UserInfo_ActionInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.R_UserInfo_ActionInfoDal;
            }
        }   
        
        public partial class RoleInfoService :BaseService<RoleInfo>,IRoleInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.RoleInfoDal;
            }
        }   
        
        public partial class UserInfoService :BaseService<UserInfo>,IUserInfoService
        {
        
    
             public override void SetCurrentDal()
            {
                CurrentDal = this.CurrentDBSession.UserInfoDal;
            }
        }   
        
    }
    自动生成代码
  • 相关阅读:
    深度学习入门零基础笔记(一)一些相关链接
    华为云计算笔记(摘要略读,零基础)(九)(虚拟化特性介绍华为虚拟化产品特性)
    华为云计算笔记(摘要略读,零基础)(八)(虚拟化特性介绍-虚拟化特性)
    华为云计算笔记(摘要略读,零基础)(七)(虚拟化特性介绍集群特性介绍)
    华为云计算笔记(摘要略读,零基础)(六)(云计算存储基础介绍)
    华为云计算笔记(摘要略读,零基础)(五)(云计算网络基础介绍)
    华为云计算笔记(摘要略读,零基础)(四)(实验 FusionCompute安装)
    华为云计算笔记(摘要略读,零基础)(三)(KVM介绍、FusionCompute架构)
    华为云计算笔记(摘要略读,零基础)(一)(云计算介绍)
    基于kolla部署openstack
  • 原文地址:https://www.cnblogs.com/YK2012/p/6644625.html
Copyright © 2020-2023  润新知