小议传统分层与新式分层,抑或与DDD分层

    技术2022-07-02  187

    引言

    本文提到的分层只是软件架构上的分层。文中的传统分层指的是传统的三层结构:UI(界面表现层),BLL(业务逻辑层),DAL(数据访问层)。文中提出的观点也都是个人的一点认识,与任何组织没有关系,如有异议,还请各位踊跃拍砖。

    当然了,出现的这些问题,也可能只是我个人的问题,不代表每个人都存在。无则加勉,有则改正吧。如果是个人的问题,那就当是个人总结吧,大家看看就算了。

    这里说到的传统分层,也有可能是我对于分层的错误理解造成的,但是我看见的不只是我的项目是这么的结构,很多的项目也都是这样的结构。里面的代码, 都和我理解的一样,至少可以说明,还是有一部分人犯了和我一样的错误。

     

    传统分层

     

    传统分层最大的问题就在于割裂了上层与下层之间的联系,把他们之间的关系变成了简单的接口调用,变成了完全的接口形式主义。同时,忽略了下层是为上层提供服务的,不是单独存在的,下层提供的服务应该受到上层的规约。当然,也不是说不可以提供更多的服务,但是至少应该提供上层需要的,然后再考虑提供一些“边角料”。

    我们先看一个传统分层的解决方案结构。

    相互之间的引用关系是:UI引用BLL、Entity、Common;BLL引用IDAL、Entity、Common;IDAL引用Entity;DAL引用IDAL、Entity、Common。

    显示行号 复制代码 UI  Code private void button1_Click(object sender, EventArgs e) { _02_BLL.OrderBll orderBll = Common.ServiceLocator.LoadService<_02_BLL.OrderBll>(); orderBll.SubmitOrder(new _05_Entity.OrderEntity() { OrderSeqNo = "123", OrderAmount = 1000 }); } .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} <script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i 显示行号 复制代码 BLL Code public class OrderBll { IOrderDal _orderDal; public OrderBll() { _orderDal = Common.ServiceLocator.LoadService<_03_IDAL.IOrderDal>(); } public bool SubmitOrder(OrderEntity order) { return _orderDal.Add(order); } } .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} <script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

    显示行号 复制代码 IDAL Code public interface IOrderDal { bool Add(OrderEntity order); } .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} <script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

    显示行号 复制代码 DAL Code public class OrderDal:_03_IDAL.IOrderDal { public bool Add(_05_Entity.OrderEntity order) { IDbConnection conn = new SqlConnection(); conn.Open(); IDbCommand comm = conn.CreateCommand(); IDbDataParameter param = comm.CreateParameter(); param.Direction = ParameterDirection.Input; param.DbType = DbType.String; param.ParameterName = "OrderSeqNo"; if (comm.ExecuteNonQuery() > 0) return true; else return false; } } .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} <script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i 显示行号 复制代码 Unity Container Locator using System.Configuration; using System.Globalization; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using Microsoft.Practices.Unity.InterceptionExtension; using System.Web; using System; namespace Common { public static class ServiceLocator { // private static readonly InterfaceInterceptor injector = new InterfaceInterceptor (); private static readonly TransparentProxyInterceptor injector = new TransparentProxyInterceptor(); public static IUnityContainer Container { get; private set; } static ServiceLocator() { Container = new UnityContainer(); UnityConfigurationSection unitySection = ConfigurationManager.GetSection("unity") as UnityConfigurationSection; if (unitySection == null) { throw new ConfigurationErrorsException(string.Format(CultureInfo.CurrentCulture, "missing unity configuration section")); } Container.AddNewExtension<Interception>(); unitySection.Configure(Container, "Container"); } /// /// 没?有D为a映3射?指?定¨别e名? /// name属?性?没?有D赋3值μ /// /// /// public static T LoadService () { Container.Configure<Interception>().SetDefaultInterceptorFor (injector); return Container.Resolve (); } /// /// 为a映3射?指?定¨别e名? /// name属?性?赋3值μ /// /// /// /// public static T LoadService ( string serviceName) { Container.Configure<Interception>().SetDefaultInterceptorFor (injector); return Container.Resolve (serviceName); } } } .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important} <script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i  

    .src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px} .src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px} .src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden} .toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;} .src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif} .src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important} .number_show{ padding-left:52px !important; list-style:decimal outside !important} .number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd} .number_hide{ padding-left:0px !important; list-style-type:none !important} .number_hide li{ list-style-type:none !important; border-left:0px} ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd} ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important} .linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word} ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

    上层在调用下层的时候,不是调用自己需要的,而是从下层提供的服务中筛选一些可以用的,凑合用一下。如果发现没有自己可以用的,就在IDAL文件中添加一个,然后在DAL文件中实现以下,这下好了,BLL可以用了。这样也会造成在开发DAL层的时候,没有充分考虑BLL的需要,自己盲目实现一些,然后BLL用的时候,才发现很多还要重新写。造成大量的浪费,代码、人力、时间、精力都浪费一部分了。也会影响到开发的进度。

     

    新式分层,抑或叫做DDD分层

     

    说是新式分层,其实也是我随便叫的。就是这段时间看了博客园的一些DDD文章,还有就是codeplex上的两个项目NLayerApp和SmartCA的源码。看了这些之后,有的一点理解。不管是神似还是形似吧,反正觉得比以前传统分层有一点道理,就分享出来了,也挣点人气,骗点点击量,哈哈。

    先来一张图吧。

    DDD中的分层主要是四层:Presentation(表现层),Application(应用层),Domain(领域层),Infrastructure(基础层)。

    Presentation相当于以前的UI层。Application是一个任务的调度层,没有实际的业务逻辑,也不推荐放入业务逻辑,实际中可以用Wcf来代替,或者使用普通的类库就可以。其实就是根据UI的业务调用,然后映射到具体的领域对象身上去。也可以通过Application来暴露粗粒度的业务处理,因为领域层的业务处理是细粒度的。没有了以前的DAL,把它的部分放入了Infrastructure层。

    以前的IDAL被放入了Domain,在图上,是Domain.Core项目,这个项目会被Infrastructure引用。因为Domain.Core中的IDAL相当于是领域对象对于持久化提出的要求,也就是说必须要实现这些功能,这些都是领域对象需要的。具体的如何实现,具体如何持久化,是持久化到关系数据库还是文件系统,还是内存数据库,都由具体的实现来定义。也就是图中的Infrastructure.Data.Core和Infrastructure.Data.MainModule项目。这两个项目中的实现,实现的都是领域对象持久化所需要的,不会实现一大丢不必要的方法。不知道这算不算是改进呢?

     

    结论

    传统分层,或者说是我理解的传统分层,存在的问题就是割裂层之间的联系,形式化了层之间的联系。没有表达出上层对于下层的需要,下层是为上层服务的,这些联系。DDD的分层可以解决这个问题,会提高开发的效率,少走一些弯路。

    不知道大家是如何认识的呢????????期待大家的回复,拍砖。

    Technorati 标签: DDD, NLayer, 分层

    最新回复(0)