【发布时间】:2012-08-16 05:21:47
【问题描述】:
在我的项目中,业务逻辑都在应用程序服务中,域服务只是一些实体,谁能告诉我或给我一个例子来展示如何在域驱动设计中将业务逻辑添加到域服务中?非常感谢!
更新
我写了一个简单的解决方案,这个解决方案是一个投票系统,解决方案的主要部分是:
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(目前不是这个项目),谁能帮帮我?怎么做才合理?非常感谢!
【问题讨论】:
-
你能提供一些你的领域对象的例子吗?
-
@casablanca 我更新了我的问题
-
IP是什么意思?文章和投票记录之间有什么关系吗?请发布类文章
-
@CuongLe 感谢评论,我已经更新了我的问题。
-
我仍然很想将域服务用于您的应用程序服务所做的很多事情。根据我对 DDD 应用程序服务的了解(根据我在书中看到的内容!)倾向于仅使用请求/响应 dto 对象来路由请求。
标签: c# architecture domain-driven-design