首页 > 代码库 > (写给像我一样刚离开校园进入公司的小菜鸟)在领域架构下,如何实现简单的展示页面以及增删改查(第一步)
(写给像我一样刚离开校园进入公司的小菜鸟)在领域架构下,如何实现简单的展示页面以及增删改查(第一步)
第一次到公司,现在进行的项目已经开始了有一段时间了,底层架构早已搭建好,经过一段时间熟悉,现在为自己记录一下
大致结构如此图所示,我们需要完成的任务只需要涉及三个类库Domain,Model,Web
流程如下:
1.Model:
1.1 Entities文件=>>新建实体模型
2.Domain:
2.0 Domain类库下有个模型工厂文件件ModelCreateFactory在里面添加实体模型与数据模型转换的TSETZAaaaModelFactory.cs
代码如下
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using MARS.Model.Entities; 7 using MARS.Model.OracleModel; 8 namespace MARS.Domain.ModelCreateFactory 9 { 10 public static class TSETZAaaaModelFactory 11 { 12 /// <summary> 13 /// 将实体模型转换成数据模型 14 /// </summary> 15 /// <param name="m">数据模型</param> 16 /// <returns></returns> 17 public static TSETZAaaaInfo ToEntityInfo(TSETZAaaa m) 18 { 19 if (m == null) 20 { 21 return null; 22 } 23 else 24 { 25 return new TSETZAaaaInfo() 26 { 27 Id = m.ID, 28 DaId = m.DAID, 29 Zah = m.ZAH, 30 Xm = m.XM, 31 Gzd = m.GZD, 32 Csrq = m.CSRQ, 33 Xb = m.XB, 34 Mz = m.MZ, 35 Sfzh = m.SFZH, 36 Fjdz = m.FJDZ, 37 Xzz = m.XZZ, 38 Mqxm = m.MQXM, 39 Fqxm = m.FQXM, 40 Mqsfz = m.MQSFZ, 41 Mqsfzh = m.MQSFZH, 42 Mqmz = m.MQMZ, 43 Fqmz = m.FQMZ, 44 Jwh = m.JWH, 45 Zrr = m.ZRR, 46 Lxdh = m.LXDH, 47 Szxx = m.SZXX, 48 Jdr = m.JDR, 49 Djrq = m.DJRQ, 50 Djdw = m.DJDW, 51 Dazt = m.DAZT 52 53 }; 54 } 55 } 56 /// <summary> 57 /// 多条数据的转换(将实体模型转换成数据模型) 58 /// </summary> 59 /// <param name="mList"></param> 60 /// <returns></returns> 61 public static List<TSETZAaaaInfo> ToEntityInfoList(List<TSETZAaaa> mList) 62 { 63 if (mList.Count<1) 64 { 65 return null; 66 } 67 else 68 { 69 var mm = from m in mList 70 select new TSETZAaaaInfo 71 { 72 Id = m.ID, 73 DaId = m.DAID, 74 Zah = m.ZAH, 75 Xm = m.XM, 76 Gzd = m.GZD, 77 Csrq = m.CSRQ, 78 Xb = m.XB, 79 Mz = m.MZ, 80 Sfzh = m.SFZH, 81 Fjdz = m.FJDZ, 82 Xzz = m.XZZ, 83 Mqxm = m.MQXM, 84 Fqxm = m.FQXM, 85 Mqsfz = m.MQSFZ, 86 Mqsfzh = m.MQSFZH, 87 Mqmz = m.MQMZ, 88 Fqmz = m.FQMZ, 89 Jwh = m.JWH, 90 Zrr = m.ZRR, 91 Lxdh = m.LXDH, 92 Szxx = m.SZXX, 93 Jdr = m.JDR, 94 Djrq = m.DJRQ, 95 Djdw = m.DJDW, 96 Dazt = m.DAZT 97 98 }; 99 return mm.ToList();100 }101 102 }103 /// <summary>104 /// 将数据模型转换成实体模型105 /// </summary>106 /// <param name="m"></param>107 /// <returns></returns>108 public static TSETZAaaa ToEntityModel(TSETZAaaaInfo m)109 {110 if (m==null)111 {112 return null;113 }114 else115 {116 return new TSETZAaaa()117 {118 ID = m.Id,119 DAID = m.DaId,120 ZAH = m.Zah,121 XM = m.Xm,122 GZD = m.Gzd,123 CSRQ = m.Csrq,124 XB = m.Xb,125 MZ = m.Mz,126 SFZH = m.Sfzh,127 FJDZ = m.Fjdz,128 XZZ = m.Xzz,129 MQXM = m.Mqxm,130 FQXM = m.Fqxm,131 MQSFZH = m.Mqsfzh,132 MQSFZ = m.Mqsfz,133 MQMZ = m.Mqmz,134 FQMZ = m.Fqmz,135 JWH = m.Jwh,136 ZRR = m.Zrr,137 LXDH = m.Lxdh,138 SZXX = m.Szxx,139 JDR = m.Jdr,140 DJRQ = m.Djrq,141 DJDW = m.Djdw,142 DAZT = m.Dazt143 144 };145 }146 }147 /// <summary>148 /// 多条转换(将数据模型转换成实体模型)149 /// </summary>150 /// <param name="mList"></param>151 /// <returns></returns>152 public static List<TSETZAaaa> ToEntityInfoList(List<TSETZAaaaInfo> mList)153 {154 if (mList.Count<1)155 {156 return null;157 }158 else159 {160 var mm = from m in mList161 select new TSETZAaaa162 {163 ID = m.Id,164 DAID = m.DaId,165 ZAH = m.Zah,166 XM = m.Xm,167 GZD = m.Gzd,168 CSRQ = m.Csrq,169 XB = m.Xb,170 MZ = m.Mz,171 SFZH = m.Sfzh,172 FJDZ = m.Fjdz,173 XZZ = m.Xzz,174 MQXM = m.Mqxm,175 FQXM = m.Fqxm,176 MQSFZH = m.Mqsfzh,177 MQSFZ = m.Mqsfz,178 MQMZ = m.Mqmz,179 FQMZ = m.Fqmz,180 JWH = m.Jwh,181 ZRR = m.Zrr,182 LXDH = m.Lxdh,183 SZXX = m.Szxx,184 JDR = m.Jdr,185 DJRQ = m.Djrq,186 DJDW = m.Djdw,187 DAZT = m.Dazt188 189 };190 return mm.ToList();191 }192 193 }194 195 196 197 198 }199 }
2.1 Interface文件=>>新建接口(IService文件夹 && IRepository文件夹)
2.2 Interface文件/IRepository文件/IDbSession.cs=>>注册IRepository文件下所新建的IRepository接口
2.3 Implement文件/Repository文件/ClassReposito文件新建(Repository数据访问类)
2.4 Implement文件/RepositoryFactory.cs简单工厂实现低耦合,数据库访问层的统一入口
2.5 Implement文件/Repository文件/DbSession.cs=>>实现接口,注册返回方法
2.6 Implement文件夹/Service文件新建(Service业务逻辑类)
3.Web:
3.1 在Global.cs里面配置业务逻辑层的关系(具体实现的目的大概是我所描述的,但是菜鸟不知道详细的意思,求解答)
3.2 新建控制器=>> 在控制器添加依赖注入
这里开始展示代码
1.1 首先在Model类库Entities文件下根据OracleModel数据模型里面的“TSETZAaaa.cs”表创建一个“TSETZAaaaInfo.cs”
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Runtime.Serialization; 6 /*------------------------------------------------------- 7 // Copyright (C) 2014 8 // 9 // 文件名:TSETZAaaaInfo.cs 10 // 功能描述:dcy 领域层数据库实体 11 // 12 // 13 // 创建标识: -- 2014-01-13 10:55:30 14 // 15 // 添加标识: 16 // 17 // 添加标识: 18 // 19 //------------------------------------------------------*/ 20 21 namespace MARS.Model.Entities 22 { 23 public class TSETZAaaaInfo 24 { 25 /// <summary> 26 /// ID 27 /// </summary> 28 [DataMember] 29 public string Id 30 { 31 get; 32 set; 33 } 34 [DataMember] 35 public string DaId 36 { 37 get; 38 set; 39 } 40 [DataMember] 41 public string Zah 42 { 43 get; 44 set; 45 } 46 [DataMember] 47 public string Xm 48 { 49 get; 50 set; 51 } 52 [DataMember] 53 public string Gzd 54 { 55 get; 56 set; 57 } 58 [DataMember] 59 public DateTime? Csrq 60 { 61 get; 62 set; 63 } 64 [DataMember] 65 public string Xb 66 { 67 get; 68 set; 69 } 70 [DataMember] 71 public string Mz 72 { 73 get; 74 set; 75 } 76 [DataMember] 77 public string Sfzh 78 { 79 get; 80 set; 81 } 82 [DataMember] 83 public string Fjdz 84 { 85 get; 86 set; 87 } 88 [DataMember] 89 public string Xzz 90 { 91 get; 92 set; 93 } 94 [DataMember] 95 public string Mqxm 96 { 97 get; 98 set; 99 }100 [DataMember]101 public string Fqxm 102 { 103 get; 104 set; 105 }106 [DataMember]107 public string Mqsfzh 108 { 109 get;110 set; 111 }112 [DataMember]113 public string Mqsfz 114 {115 get; 116 set; 117 }118 [DataMember]119 public string Mqmz 120 { 121 get; 122 set;123 }124 [DataMember]125 public string Fqmz126 {127 get; 128 set; 129 }130 [DataMember]131 public string Jwh 132 { 133 get;134 set; 135 }136 [DataMember]137 public string Zrr 138 { 139 get;140 set;141 }142 [DataMember]143 public string Lxdh 144 { 145 get; 146 set;147 }148 [DataMember]149 public string Szxx 150 { 151 get;152 set; 153 }154 [DataMember]155 public string Jdr 156 { 157 get;158 set; 159 }160 [DataMember]161 public DateTime? Djrq 162 { 163 get; 164 set; 165 }166 [DataMember]167 public string Djdw 168 { 169 get; 170 set;171 }172 [DataMember]173 public string Dazt 174 { 175 get;176 set; 177 }178 }179 }
2.1 Interface文件下有两个文件夹分别放IRepository以及IService的接口
先来IRepository文件下的接口命名为ITSETZAaaaRepository.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using MARS.Model.OracleModel; 6 7 /*------------------------------------------------------- 8 // Copyright (C) 2014// 9 // 文件名:ITSETZAaaaRepository.cs10 // 功能描述: 11 //12 //13 // 创建标识: dcy -- 2015-01-13 11:22:1114 //15 // 添加标识:16 //17 // 添加标识:18 //19 //------------------------------------------------------*/20 namespace MARS.Domain.Interface21 {22 public partial interface ITSETZAaaaRepository:IBaseRepository<TSETZAaaa>23 {24 }25 }
在接着写IService文件下的接口命名为ITSETZAaaaService.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using MARS.Model.OracleModel; 7 using System.Threading.Tasks; 8 using MARS.Model.Entities; 9 10 /*-------------------------------------------------------11 // Copyright (C) 2014 12 //13 // 文件名:ITSETZAaaaService.cs14 // 功能描述: 15 //16 //17 // 创建标识: -- 2015-01-13 10:49:5018 //19 // 添加标识:20 //21 // 添加标识:22 //`23 //------------------------------------------------------*/24 namespace MARS.Domain.Interface25 {26 public interface ITSETZAaaaService:IBaseService<TSETZAaaa>27 {28 /// <summary>29 /// 新增30 /// </summary>31 /// <param name="m"></param>32 /// <returns></returns>33 TSETZAaaaInfo Add(TSETZAaaaInfo a);34 /// <summary>35 /// 根据Id删除36 /// </summary>37 /// <param name="id"></param>38 /// <returns></returns>39 bool Delete(string id);40 /// <summary>41 /// 修改42 /// </summary>43 /// <param name="a"></param>44 /// <returns></returns>45 bool Updata(TSETZAaaaInfo a);46 /// <summary>47 /// 根据Id查询实体48 /// </summary>49 /// <param name="id"></param>50 /// <returns></returns>51 TSETZAaaaInfo GetById(string id);52 /// <summary>53 /// 获取所有数据54 /// </summary>55 /// <param name="pagesize"></param>56 /// <param name="pageindex"></param>57 /// <param name="total"></param>58 /// <returns></returns>59 List<TSETZAaaaInfo> LoadData();60 }61 }
2.2 注册 Interface文件下的IRepository文件下的IDbSession.cs
加入这个接口方法
1 ITSETZAaaaRepository TSETZAaaaRepository { get; }
2.3 Implement文件下先在Repository文件下的ClassReposito文件新建(Repository数据访问类)命名为TSETZAaaaRepository.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using MARS.Model.OracleModel; 7 using MARS.Model.Entities; 8 using MARS.Domain.Interface; 9 namespace MARS.Domain.Implement10 {11 public partial class TSETZAaaaRepository:BaseRepository<TSETZAaaa>,ITSETZAaaaRepository12 {13 }14 }
2.4 Implement文件下的RepositoryFactory.cs添加一个方法
1 public static ITSETZAaaaRepository TSETZAaaaRepository2 {3 get4 {5 return new TSETZAaaaRepository();6 }7 8 }
2.5 实现注册接口 Implement文件下的Repository文件下的DbSession.cs实现前面2.2IDbSession的接口方法
1 public ITSETZAaaaRepository TSETZAaaaRepository2 {3 get 4 {5 return new TSETZAaaaRepository(); 6 7 }8 }
2.6 Implement文件夹下的Service文件新建(Service业务逻辑类)命名为TSETZAaaaService.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using MARS.Domain.Interface; 7 using MARS.Model; 8 using MARS.Model.Entities; 9 using MARS.Model.OracleModel; 10 using MARS.Model.Enum; 11 using MARS.Domain.ModelCreateFactory; 12 /*------------------------------------------------------- 13 // Copyright (C) 2014 14 // 15 // 文件名:TSETZAaaaServiec.cs 16 // 功能描述: 17 // 18 // 19 // 创建标识: -- 2015-01-13 11:35:11 20 // 21 // 添加标识: 22 // 23 // 添加标识: 24 // 25 //------------------------------------------------------*/ 26 namespace MARS.Domain.Implement 27 { 28 public class TSETZAaaaServiec:BaseService<TSETZAaaa>,ITSETZAaaaService 29 { 30 //---------------------------------------- 31 public override void SetCurrentRepository() 32 { 33 CurrentRepository = _dbSession.TSETZAaaaRepository; 34 } 35 36 37 //---------------------------------------- 38 /// <summary> 39 /// 新增 40 /// </summary> 41 /// <param name="a"></param> 42 /// <returns></returns> 43 public TSETZAaaaInfo Add(TSETZAaaaInfo a) 44 { 45 var b =CurrentRepository.AddEntities(TSETZAaaaModelFactory.ToEntityModel(a)); 46 if (b != null && _dbSession.SaveChanges() > 0) 47 { 48 _dbSession.SaveChanges(); 49 return (TSETZAaaaModelFactory.ToEntityInfo(b)); 50 51 } 52 return null; 53 54 } 55 /// <summary> 56 /// 删除 57 /// </summary> 58 /// <param name="id"></param> 59 /// <returns></returns> 60 public bool Delete(string id) 61 { 62 TSETZAaaa m=new TSETZAaaa(); 63 m.ID=id; 64 var d = _dbSession.TSETZAaaaRepository.Delete(c => c.ID == m.ID,m); 65 if (d) 66 { 67 _dbSession.SaveChanges(); 68 return true; 69 } 70 else 71 { 72 return false; 73 } 74 } 75 76 /// <summary> 77 /// 修改 78 /// </summary> 79 /// <param name="a"></param> 80 /// <returns></returns> 81 public bool Updata(TSETZAaaaInfo a) 82 { 83 var u = _dbSession.TSETZAaaaRepository.Update(c => c.ID == a.Id, TSETZAaaaModelFactory.ToEntityModel(a)); 84 if (u!=null) 85 { 86 _dbSession.SaveChanges(); 87 return true; 88 } 89 else 90 { 91 return false; 92 } 93 } 94 /// <summary> 95 /// 根据id获取数据模型 96 /// </summary> 97 /// <param name="id"></param> 98 /// <returns></returns> 99 public TSETZAaaaInfo GetById(string id)100 {101 return TSETZAaaaModelFactory.ToEntityInfo(_dbSession.TSETZAaaaRepository.LoadEntities(c => c.ID == id).FirstOrDefault());102 }103 104 /// <summary>105 /// 加载所有数据106 /// </summary>107 /// <returns></returns>108 public List<TSETZAaaaInfo> LoadData()109 {110 var data =http://www.mamicode.com/ TSETZAaaaModelFactory.ToEntityInfoList(_dbSession.TSETZAaaaRepository.GetAll().ToList());111 if (data!=null)112 {113 return data;114 }115 else116 {117 return null;118 }119 }120 }121 }
3.1 Global.cs建立容器
1 #region 依赖注入 2 var builder = new ContainerBuilder(); 3 SetupResolveRules(builder); 4 builder.RegisterControllers(Assembly.GetExecutingAssembly()); 5 var container = builder.Build(); 6 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); 7 #endregion 8 9 private void SetupResolveRules(ContainerBuilder builder)10 {11 12 builder.RegisterType<TSETZAaaaServiec>().As<ITSETZAaaaService>();13 14 }
3.2 新建控制器添加依赖注入 代码如下:
1 #region 依赖注入2 private readonly ITSETZAaaaService _ITSETZAaaaService;3 4 public TSETZAaaaController(ITSETZAaaaService ITSETZAaaaService)5 {6 _ITSETZAaaaService = ITSETZAaaaService;7 }8 9 #endregion
好了 第一部分就这样结束,后面我还会上传配合EasyUi完成的页面展示以及增删改查 文章问题很多啊,希望大家发现能留言便于我及时更正 谢谢
(写给像我一样刚离开校园进入公司的小菜鸟)在领域架构下,如何实现简单的展示页面以及增删改查(第一步)