在我的项目中,业务逻辑都在应用程序服务中,域服务只是一些实体,谁能告诉我或给我一个例子来展示如何在域驱动设计中将业务逻辑添加到域服务中?很感谢!
更新
我写了一个简单的解决方案,这个解决方案是一个投票系统,解决方案的主要部分是:
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);
}
}
投票.域.实体.投票记录:
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; }
}
}
投票.域.实体.文章:
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中的业务登录移动到Domain.service(当前不是这个项目),谁能帮助我?怎么做才合理?很感谢!