DDD:实体如何处理外部依赖

场景

修改用户名时,要验证用户名的唯一性。

实现1

1 public class User
2 {
3       public void ChangeUsername(string newUsername)
4       { 
5               //使用服务定位器获取IUsernameUniqueService ,执行验证。
6       }
7 }

实现2

 1 public class User
 2 {
 3         public void ChangeUsername(string newUsername)
 4         { 
 5             EventBus.Send(new  UsernameChangedEvent(newUsername));
 6         }
 7 }
 8  
 9 public class UsernameChangedEventHandler
10 {
11         public UsernameChangedEventHandler(IUsernameUniqueService usernameUniqueService){}
12  
13         public void Process(UsernameChangedEvent){} 
14 }

实现3

 1 public class ChangeUsernameCommandHandler
 2 {
 3         public ChangeUsernameCommandHandler(IUsernameUniqueService usernameUniqueService){}
 4 
 5         public void Process(ChageUsernameCommand){}
 6 }
 7 
 8 public class User
 9 {
10         public void ChangeUsername(IUsernameUniqueService usernameUniqueService, newUsername){}
11 }

实现4

 1 public class UsernameChangeService
 2 {
 3       public UsernameChangeService(IUsernameUniqueService usernameUniqueService){}
 4 
 5       public void ChangeUsername(Guid userId, string newUsername){ }
 6 }
 7 
 8 public class User
 9 {
10       Internal void ChangeUsername(string newUsername){ }
11 }

实现5

 1 public class UsingUser
 2 {
 3       public UsingUser(User user, IUsernameUniqueService usernameUniqueService){}
 4        
 5       public void ChangeUsername(string newUsername){ }
 6 }
 7 
 8 public class User
 9 {
10       Internal void ChangeUsername(string newUsername){  }
11 }

备注

到底用哪一个,先不做评判。

posted on 2013-03-26 09:28  幸福框架  阅读(1109)  评论(0编辑  收藏  举报

导航

我要啦免费统计