当前位置: 首页 > news >正文

DDD领域模型企业级系统(二)

用户层:

1.请求应用层获取用户显示的信息

2.发送命令给应用层要求执行某个命令

应用层:

对用户界面提供各种应用功能(包括信息获取与命令执行),应用层不包含业务逻辑,业务层是由应用层调用领域层(领域对象或领域服务)来完成的,应用层是很薄的一层

领域层:

包含领域对象和领域服务,完成系统所需的业务处理,是系统的核心。业务逻辑与仓储接口都在领域层

基础机构层:

包含其他层所需要使用的所有基础服务与技术,比如仓储的实现(与数据打交道)、短消息发送、Json字符串处理

工作单元:

保证聚合间的一致性

通常在领域服务或应用服务中使用工作单元

仓储实现不用考虑数据库事务

 

一般通过应用层访问仓储,而且是使用领域层定义的仓储接口,具体仓储的实现调用可以通过IOC的机制在应用层通过服务定位器模式找到。

一般不要在领域层访问仓储,如果领域层中的领域对象或领域服务的业务逻辑处理确实需要访问仓储,建议不通过服务定位器的方式在领域层进行服务解析,

而是应该在领域对象或领域服务的构造函数中传入仓储接口,具体是哪个仓储实现,仍然在服务层通过应用模式找到,这样保证服务层只关注业务,而不关注其他方面

一些界面需要获取的查询信息,不应该通过领域对象直接返回给应用服务层,应该在应用 层实现DTO

 

代码:

 

   /// <summary>
    /// 仓储的接口   维护这个接口里面的状态
    /// </summary>
    /// <typeparam name="TAggreateRoot"></typeparam>
   public interface IRepository<TAggreateRoot> where TAggreateRoot:class, IAggreateRoot
    {
        void Create(TAggreateRoot aggreateroot);
        TAggreateRoot GetByID(Guid id);
        List<TAggreateRoot> GetByCondition(Expression<Func<TAggreateRoot, bool>> condition);
        void Update(TAggreateRoot aggreateroot);
        void Remove(TAggreateRoot aggreateroot);
        void RemovdByID(Guid id);
    }

 

/// <summary>
    /// 仓储上下文的接口  全部放到上下文中  通过工作单元 一起提交
    /// IDisposable  定时释放资源
    /// </summary>
    public interface IRepositoryContext:IUnitOfWork,IDisposable
    {
        //创建对象的集合
        void RegisterCreate<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot : class, IAggreateRoot;

        //修改
        void RegisterUpdate<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot : class, IAggreateRoot;

        //删除
        void RegisterRemove<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot : class, IAggreateRoot;
        Guid ContextID { get;  }

    }

 

public interface IUnitOfWork
    {
        void Commit();
        void RollBack();
        //是否提交
        bool Committed { get; set; }
    }

 仓储上下文:

 public abstract class RepositoryContext : IRepositoryContext, IDisposable
    {
        //保证线程本地化
        private readonly ThreadLocal<Dictionary<Guid, object>> localcreatedics = new ThreadLocal<Dictionary<Guid, object>>();

        private readonly ThreadLocal<Dictionary<Guid, object>> localupdatedics = new ThreadLocal<Dictionary<Guid, object>>();

        private readonly ThreadLocal<Dictionary<Guid, object>> localremovedics = new ThreadLocal<Dictionary<Guid, object>>();

        //是否已经提交
        private readonly ThreadLocal<bool> localcommitted = new ThreadLocal<bool>(()=>true);

       public virtual void RegisterCreate<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot :
            class, IAggreateRoot
        {
            if (aggreateroot.Id.Equals(Guid.Empty))
            {
                throw new AggregateException("聚合根ID不能为空");
            }
            if (localcreatedics.Value.ContainsKey(aggreateroot.Id))
            {
                throw new InvalidOperationException("新创建的领域对象已经存在在集合中");
            }
            //向集合总提交  聚合根的id  和聚合根
            localcreatedics.Value.Add(aggreateroot.Id, aggreateroot);
            //标注为未提交
            localcommitted.Value = false;
        }

      public virtual void RegisterUpdate<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot :
            class, IAggreateRoot
        {
            if (aggreateroot.Id.Equals(Guid.Empty))
            {
                throw new AggregateException("聚合根ID不能为空");
            }
            if (localupdatedics.Value.ContainsKey(aggreateroot.Id))
            {
                throw new InvalidOperationException("更新的领域对象已经存在在集合中");
            }
            if (localremovedics.Value.ContainsKey(aggreateroot.Id))
            {
                throw new InvalidOperationException("领域对象正在被更新,不能删除");
            }
            localremovedics.Value.Add(aggreateroot.Id, aggreateroot);
            localcommitted.Value = false;
        }

       public virtual  void RegisterRemove<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot :
            class, IAggreateRoot
        {
            if (aggreateroot.Id.Equals(Guid.Empty))
                throw new ArgumentException("聚合根ID不能为空");
            if (localremovedics.Value.ContainsKey(aggreateroot.Id))
                throw new InvalidOperationException("删除的领域对象已经存在在集合中");
            if (localupdatedics.Value.ContainsKey(aggreateroot.Id))
                throw new InvalidOperationException("领域对象正在被更新,不能删除");
            localremovedics.Value.Add(aggreateroot.Id, aggreateroot);
            localcommitted.Value = false;
        }

        public Guid ContextID
        {
            get
            {
                return Guid.NewGuid();
            }
        }

        public abstract void Commit();
      

        /// <summary>
        /// 提交的属性
        /// </summary>
        public bool Committed
        {
            get { return localcommitted.Value; }
            set { localcommitted.Value = value; }
        }

        /// <summary>
        /// 释放资源
        /// </summary>
        public virtual void Dispose()
        {
            localcreatedics.Dispose();
            localupdatedics.Dispose();
            localremovedics.Dispose();
            localcommitted.Dispose();
        }

        //在仓储中具体实现
        public  abstract void RollBack();  
    }

 具体的实现 :

/// <summary>
    /// 仓储类的实现  
    /// </summary>
    public class EFRepository<TAggreateRoot> : EFRepositoryContext, IRepository<TAggreateRoot>
        where TAggreateRoot :class,IAggreateRoot
    {
        public void Create(TAggreateRoot aggreateroot)
        {
            base.RegisterCreate(aggreateroot);
        }

        public List<TAggreateRoot> GetByCondition(Expression<Func<TAggreateRoot, bool>> condition)
        {
            throw new NotImplementedException();
        }

        public TAggreateRoot GetByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public void RemovdByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public void Remove(TAggreateRoot aggreateroot)
        {
            base.RegisterRemove(aggreateroot);
        }

        public void Update(TAggreateRoot aggreateroot)
        {
            base.RegisterUpdate(aggreateroot);
        }
    }

仓储上线文的实现:

/// <summary>
    /// 仓储上下文的实现
    /// </summary>
    public class EFRepositoryContext : RepositoryContext
    {
        public override void RegisterCreate<TAggreateRoot>(TAggreateRoot aggreateroot)
        {
            base.RegisterCreate<TAggreateRoot>(aggreateroot);
            Committed = false;
        }
        public override void RegisterRemove<TAggreateRoot>(TAggreateRoot aggreateroot)
        {
            base.RegisterRemove<TAggreateRoot>(aggreateroot);
            Committed = false;
        }
        public override void RegisterUpdate<TAggreateRoot>(TAggreateRoot aggreateroot)
        {
            base.RegisterUpdate<TAggreateRoot>(aggreateroot);
            Committed = false;
        }
        public override void Commit()
        {
            Committed = true;
        }

        public override void RollBack()
        {
            Committed = true;
        }

        public override void Dispose()
        {
            base.Dispose();
        }
    }

 

判断ID:

 public interface  IEntity
    {
        Guid Id { get; }
    }

 

public  interface IAggreateRoot:IEntity
    {
    }

 

 public abstract class AggreateRoot:Entity
    {
    }

 

 public abstract class Entity : IEntity
    {
        public Guid Id
        {
            get
            {
                var id = Guid.NewGuid();
                return id;
            }
        }

        /// <summary>
        /// 比较两个对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }
            //判断两个实列是否相等
            if (ReferenceEquals(this, obj))
            {
                return true;
            }
            //判断ID是否相等
            return this.Id == (obj as IEntity).Id;
        }

        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
    }

 

转载于:https://www.cnblogs.com/sunliyuan/p/6512230.html

相关文章:

  • 为嵌入式梦想而考研
  • LAMP集群项目二 初始化系统
  • jQuery——选择器
  • 第一次毕业设计任务书
  • JS转换HTML转义符,防止javascript注入攻击,亲测可用
  • SQL SERVER的排名函数
  • live555 解析
  • ORA-07445错误修正
  • ajax3
  • 英文名
  • 在 Android 中进程的级别有哪些?
  • UNION ALL导致的ORA-07445错误
  • 计算机网络: IP地址,子网掩码,默认网关,DNS服务器详解
  • 纠结+纠结
  • HTTP 返回状态代码详细解释
  • $translatePartialLoader加载失败及解决方式
  • [deviceone开发]-do_Webview的基本示例
  • “Material Design”设计规范在 ComponentOne For WinForm 的全新尝试!
  • 3.7、@ResponseBody 和 @RestController
  • DataBase in Android
  • github指令
  • js写一个简单的选项卡
  • Phpstorm怎样批量删除空行?
  • SpiderData 2019年2月13日 DApp数据排行榜
  • SpiderData 2019年2月16日 DApp数据排行榜
  • uva 10370 Above Average
  • 聊聊flink的BlobWriter
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 设计模式(12)迭代器模式(讲解+应用)
  • 什么软件可以提取视频中的音频制作成手机铃声
  • 微信小程序实战练习(仿五洲到家微信版)
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • MPAndroidChart 教程:Y轴 YAxis
  • 正则表达式-基础知识Review
  • # Apache SeaTunnel 究竟是什么?
  • #14vue3生成表单并跳转到外部地址的方式
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #常见电池型号介绍 常见电池尺寸是多少【详解】
  • (13)Latex:基于ΤΕΧ的自动排版系统——写论文必备
  • (14)Hive调优——合并小文件
  • (C语言)编写程序将一个4×4的数组进行顺时针旋转90度后输出。
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (十)T检验-第一部分
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (太强大了) - Linux 性能监控、测试、优化工具
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (转)C#调用WebService 基础
  • (转)fock函数详解
  • (转)jQuery 基础
  • (转)大道至简,职场上做人做事做管理
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .“空心村”成因分析及解决对策122344