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的實現將在下一節介紹,還有其中的工做單元也會在下一節引入。

相關文章
相關標籤/搜索