zoukankan      html  css  js  c++  java
  • DDD领域驱动之干货(二)

       基于仓储的实现

    1、前言:本着第一节写的有些糊涂,主要是自己喜欢实干,不太喜欢用文字表述,就这样吧。下面切入正题。

    博客园里面有很多的大佬,我这里就不一一解释概览,有兴趣的朋友可以去看大佬们写的概览。好了不多说了。我们先来看看仓储的实现。

    2、上一节中我们已经实现了model,现在我们就来实现仓储。

    首先声明一个借口,这里我定义为IRepository,而这个接口我要用做泛型,所以接口如下:

       仓储的实现

    3、现在给这个接口写上接口的方法:

     1 using Mio.Domain.BaseModel;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Linq.Expressions;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace Mio.Domain.Repositories
    10 {
    11     /// <summary>
    12     /// 在DDD中仓储只能操作聚合根
    13     /// </summary>
    14     /// <typeparam name="TEntity"></typeparam>
    15     public interface IRepository<TEntity> where TEntity : AggregateRoot
    16     {
    17         IEnumerable<TEntity> LoadListAll(Expression<Func<TEntity, bool>> predicate);
    18         IQueryable<TEntity> LoadAll(Expression<Func<TEntity, bool>> predicate);
    19         
    20         IEnumerable<TEntity> LoadListForSql(string sql);
    21         IQueryable<TEntity> LoadForSql(string sql);
    22 
    23         /// <summary>
    24         /// 根据聚合根的ID值,从仓储中读取聚合根。
    25         /// </summary>
    26         /// <param name="key">聚合根的ID值。</param>
    27         /// <returns>聚合根实例。</returns>
    28         TEntity GetKey(Guid key);
    29         /// <summary>
    30         /// 将指定的聚合根添加到仓储中。
    31         /// </summary>
    32         /// <param name="aggregateRoot">需要添加到仓储的聚合根实例。</param>
    33         void Add(TEntity aggregateRoot);
    34         /// <summary>
    35         /// 将指定的聚合根从仓储中移除。
    36         /// </summary>
    37         /// <param name="aggregateRoot">需要从仓储中移除的聚合根。</param>
    38         void Remove(TEntity aggregateRoot);
    39         /// <summary>
    40         /// 更新指定的聚合根。
    41         /// </summary>
    42         /// <param name="aggregateRoot">需要更新的聚合根。</param>
    43         void Update(TEntity aggregateRoot);
    44     }
    45 }
    View Code

    接口的方法写完了。

    上节说了每一个领域模型都是特有的,所以在这里每一个领域模型都有一个固定的仓储实现。

    好了,现在接口方法我们写完了,现在去写实现类。

    我这里只做了一个测试所以简单的贴下代码。

     1 using Mio.Domain.Model;
     2 using Mio.Domain.Repositories;
     3 using Mio.Repository.EFRepository;
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 
    10 namespace Mio.Repository.ModelRepository
    11 {
    12     /// <summary>
    13     /// 用户角色
    14     /// </summary>
    15     public class UserRoleRepositoryImpl : RepositoryImpl<UserRole>, IUserRoleRepository
    16     {
    17         public Role GetRoleForUser(User user)
    18         {
    19             var context = lazy.Context as MioContext;
    20             if (context != null)
    21             {
    22                string sql = "SELECT  *   FROM  Role as a  INNER JOIN UserRole as b ON a.Id = b.RoleId INNER JOIN [User] as c ON c.Id = b.Id WHERE c.Id = '"+user.Id+"'";
    23                return context.Set<Role>().SqlQuery(sql).FirstOrDefault();
    24              
    25                 //var query = from role in context.Role
    26                 //            from userRole in context.Userrole
    27                 //            from usr in context.User
    28                 //            where role.Id == userRole.RoleId &&
    29                 //                usr.Id == userRole.Id &&
    30                 //                usr.Id == user.Id
    31                 //            select role;       
    32                 //return query.FirstOrDefault();
    33             }
    34             throw new InvalidOperationException("The provided repository context is invalid.");
    35         }
    36     }
    37 }
    View Code

    差点忘记了我的项目架构图。如下图。

    测试如下:

    其中引用了automapper。automapper的实现将在下一节介绍,还有其中的工作单元也会在下一节引入。

  • 相关阅读:
    为什么ip层收到的报文可能已经设置了路由
    由socket fd泄漏想到的一些问题
    Html.DropDownLis绑定数据库
    CSS 属性备注
    获取IP
    读取TXT并筛选数据写入新建TXT
    C#打印图片
    C#生成二维码
    NPOI操作Excel
    Bootstrap
  • 原文地址:https://www.cnblogs.com/edna-lzh/p/6888930.html
Copyright © 2011-2022 走看看