在我的项目中,业务逻辑全部在应用服务中,域服务只是一些实体,谁能告诉我或给我一个例子来展示如何在域驱动设计中将业务逻辑添加到域服务?很感谢!
UPDATE
我写了一个简单的解释,这个解决方案是一个投票系统,解决主要部分是:
Vote.Application.Service.VoteService.cs:
- namespace Vote.Application.Service
- {
- public class VoteService
- {
- private IVoteRepository _voteRepository;
- private IArticleRepository _articleRepository;
- public VoteService(IVoteRepository voteRepository,IArticleRepository articleRepository)
- {
- _voteRepository = voteRepository;
- _articleRepository = articleRepository;
- }
- public bool AddVote(int articleId,string ip)
- {
- var article = _articleRepository.Single(articleId);
- if (article == null)
- {
- throw new Exception("this article not exist!");
- }
- else
- {
- article.VoteCount++;
- }
- if (IsRepeat(ip,articleId))
- return false;
- if (IsOvertakeTodayVoteCountLimit(ip))
- return false;
- _voteRepository.Add(new VoteRecord()
- {
- ArticleID = articleId,IP = ip,VoteTime = DateTime.Now
- });
- try
- {
- _voteRepository.UnitOfWork.Commit();
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private bool IsRepeat(string ip,int articleId)
- {
- //An IP per article up to cast 1 votes
- //todo
- return false;
- }
- private bool IsOvertakeTodayVoteCountLimit(string ip)
- {
- //An IP per day up to cast 10 votes
- //todo
- return false;
- }
- }
- }
Vote.Domain.Contract.IVoteRepository.cs:
- namespace Vote.Domain.Contract
- {
- public interface IVoteRepository
- : IRepository<VoteRecord>
- {
- void Add(VoteRecord model);
- }
- }
Vote.Domain.Contract.IArticleRepository.cs:
- namespace Vote.Domain.Contract
- {
- public interface IArticleRepository
- : IRepository<Article>
- {
- void Add(VoteRecord model);
- Article Single(int articleId);
- }
- }
Vote.Domain.Entities.VoteRecord:
- namespace Vote.Domain.Entities
- {
- public class VoteRecord
- {
- public int ID { get; set; }
- public DateTime VoteTime { get; set; }
- public int ArticleID { get; set; }
- public string IP { get; set; }
- }
- }
Vote.Domain.Entities.Article:
- namespace Vote.Domain.Entities
- {
- public class Article
- {
- public int ID { get; set; }
- public string Title { get; set; }
- public string Content { get; set; }
- public int VoteCount { get; set; }
- }
- }
我想把application.service中的Business Login移到Domain.service(当前不是这个项目),谁能帮帮我?怎么做才合理?很感谢!
解决方法
DDD正在关注如何设计领域模型以满足需求,数据库中的模式并不重要.
如果您的域实体只是属性包,那么您似乎违反了Anemic Model anti-pattern.业务逻辑应该在域实体中.因此,在您的情况下,为了避免业务逻辑泄漏到应用程序服务.例如,您可以拥有一个名为Client的新模型,以便在必要时存储Ip或其他属性.
为了更容易理解,客户端是否超过了白天的限制,此方法应该在Client类中.与IsRepeated方法类似.
所以,你的域对象应该是:
- public class Client
- {
- public string Ip { get; set; }
- // More properties if needed
- public List<Vote> Votes { get; set; }
- public bool ExceedLimitInDay()
- {
- }
- }
- public class Vote
- {
- public int Id { get; set; }
- public DateTime VoteTime { get; set; }
- public Article Article { get; set; }
- public Client { get; set; }
- }
- public class Article
- {
- public int Id { get; set; }
- public string Title { get; set; }
- public string Content { get; set; }
- public List<Vote> Votes { get; set; }
- public bool IsRepeated(string ip)
- {
- return Votes.Select(v => v.Client.Ip == ip).Any();
- }
- }
注意:如果您不需要创建新的表Client,只需将其映射到Vote表.至于VoteCount属性,它是不必要的,因为您可以根据投票列表进行计数