c# – 如何在域驱动设计中将业务逻辑添加到域服务?

前端之家收集整理的这篇文章主要介绍了c# – 如何在域驱动设计中将业务逻辑添加到域服务?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的项目中,业务逻辑全部在应用服务中,域服务只是一些实体,谁能告诉我或给我一个例子来展示如何在域驱动设计中将业务逻辑添加到域服务?很感谢!

UPDATE

我写了一个简单的解释,这个解决方案是一个投票系统,解决主要部分是:

Vote.Application.Service.VoteService.cs:

  1. namespace Vote.Application.Service
  2. {
  3. public class VoteService
  4. {
  5. private IVoteRepository _voteRepository;
  6. private IArticleRepository _articleRepository;
  7.  
  8. public VoteService(IVoteRepository voteRepository,IArticleRepository articleRepository)
  9. {
  10. _voteRepository = voteRepository;
  11. _articleRepository = articleRepository;
  12. }
  13.  
  14. public bool AddVote(int articleId,string ip)
  15. {
  16. var article = _articleRepository.Single(articleId);
  17. if (article == null)
  18. {
  19. throw new Exception("this article not exist!");
  20. }
  21. else
  22. {
  23. article.VoteCount++;
  24. }
  25.  
  26. if (IsRepeat(ip,articleId))
  27. return false;
  28.  
  29. if (IsOvertakeTodayVoteCountLimit(ip))
  30. return false;
  31.  
  32. _voteRepository.Add(new VoteRecord()
  33. {
  34. ArticleID = articleId,IP = ip,VoteTime = DateTime.Now
  35. });
  36.  
  37. try
  38. {
  39. _voteRepository.UnitOfWork.Commit();
  40. return true;
  41. }
  42. catch (Exception ex)
  43. {
  44. throw ex;
  45. }
  46. }
  47.  
  48. private bool IsRepeat(string ip,int articleId)
  49. {
  50. //An IP per article up to cast 1 votes
  51. //todo
  52. return false;
  53. }
  54.  
  55. private bool IsOvertakeTodayVoteCountLimit(string ip)
  56. {
  57. //An IP per day up to cast 10 votes
  58. //todo
  59. return false;
  60. }
  61. }
  62. }

Vote.Domain.Contract.IVoteRepository.cs:

  1. namespace Vote.Domain.Contract
  2. {
  3. public interface IVoteRepository
  4. : IRepository<VoteRecord>
  5. {
  6. void Add(VoteRecord model);
  7. }
  8. }

Vote.Domain.Contract.IArticleRepository.cs:

  1. namespace Vote.Domain.Contract
  2. {
  3. public interface IArticleRepository
  4. : IRepository<Article>
  5. {
  6. void Add(VoteRecord model);
  7.  
  8. Article Single(int articleId);
  9. }
  10. }

Vote.Domain.Entities.VoteRecord:

  1. namespace Vote.Domain.Entities
  2. {
  3. public class VoteRecord
  4. {
  5. public int ID { get; set; }
  6.  
  7. public DateTime VoteTime { get; set; }
  8.  
  9. public int ArticleID { get; set; }
  10.  
  11. public string IP { get; set; }
  12. }
  13. }

Vote.Domain.Entities.Article:

  1. namespace Vote.Domain.Entities
  2. {
  3. public class Article
  4. {
  5. public int ID { get; set; }
  6.  
  7. public string Title { get; set; }
  8.  
  9. public string Content { get; set; }
  10.  
  11. public int VoteCount { get; set; }
  12. }
  13. }

我想把application.service中的Business Login移到Domain.service(当前不是这个项目),谁能帮帮我?怎么做才合理?很感谢!

解决方法

DDD正在关注如何设计领域模型以满足需求,数据库中的模式并不重要.

如果您的域实体只是属性包,那么您似乎违反了Anemic Model anti-pattern.业务逻辑应该在域实体中.因此,在您的情况下,为了避免业务逻辑泄漏到应用程序服务.例如,您可以拥有一个名为Client的新模型,以便在必要时存储Ip或其他属性.

为了更容易理解,客户端是否超过了白天的限制,此方法应该在Client类中.与IsRepeated方法类似.

所以,你的域对象应该是:

  1. public class Client
  2. {
  3. public string Ip { get; set; }
  4. // More properties if needed
  5.  
  6. public List<Vote> Votes { get; set; }
  7.  
  8. public bool ExceedLimitInDay()
  9. {
  10. }
  11. }
  12.  
  13. public class Vote
  14. {
  15. public int Id { get; set; }
  16. public DateTime VoteTime { get; set; }
  17. public Article Article { get; set; }
  18. public Client { get; set; }
  19. }
  20.  
  21. public class Article
  22. {
  23. public int Id { get; set; }
  24. public string Title { get; set; }
  25. public string Content { get; set; }
  26.  
  27. public List<Vote> Votes { get; set; }
  28.  
  29. public bool IsRepeated(string ip)
  30. {
  31. return Votes.Select(v => v.Client.Ip == ip).Any();
  32. }
  33. }

注意:如果您不需要创建新的表Client,只需将其映射到Vote表.至于VoteCount属性,它是不必要的,因为您可以根据投票列表进行计数

猜你在找的C#相关文章