DDD领域驱动之干货(二)

   基于仓储的实现

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

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

二、上一节中咱们已经实现了model,如今咱们就来实现仓储。app

首先声明一个借口,这里我定义为IRepository,而这个接口我要用作泛型,因此接口以下:ide

   仓储的实现

三、如今给这个接口写上接口的方法:测试

 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

接口的方法写完了。ui

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

好了,如今接口方法咱们写完了,如今去写实现类。3d

我这里只作了一个测试因此简单的贴下代码。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

差点忘记了个人项目架构图。以下图。blog

测试以下:

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

相关文章
相关标签/搜索