存储库模式和/或/ vs业务逻辑层 [英] Repository pattern and/or/vs business logic layer
问题描述
我正在尝试使用Repository Pattern。我有一个存储库对象将数据加载到POCO。我还创建了一个业务逻辑层,它增加了一些功能,但基本上包装了POCO。所以最终我有一个BLL,它加载了使用存储库的DAO。
我对这个解决方案不太满意。我有三层,但我觉得BLL没有提供足够的功能来保留它。另一方面,我不想将我的逻辑放在存储库层和数据访问层?
所以我的问题是我应该在哪里应用逻辑?你使用哪个解决方案(DAO + repo或DAO + BLL + rep或任何其他)?
在设计域时,有两种基本的方法来考虑业务规则。
1。)域实体是基本的POCO / DTO 秒。你把它们交给域名服务。这些服务可能与另一个类一样简单,或者他们真的可以是另一个服务器上的实际服务器。
var user = repository .Find(x => x.UserName == userName);
if(userLogonService.IsValidUser(user,password)){
userLogonService.UpdateUserAsLoggedOn(user);
}
repository.SaveChanges();
2。)域实体包含自己的操作逻辑。这更接近许多MVC模式。因为你问,这是我喜欢的模型。
var user = repository.Find (x => x.UserName == userName);
if(user.CheckPassword(password)){
user.LogOnNow();
}
repository.SaveChanges();
两者都是完全有效的模式。 #1具有独立的业务运营层,但遭受贫血域模型。 #2可以导致大域实体,如果您的域名开始变得复杂,或者模型可以做很多事情。
编辑#1:回应John Kraft
Oven.Bake(myPizza)与myPizza.Bake()
我大部分同意。你有单独的烤箱服务,还是有几十个可用的烤箱存放在烘箱里,烤箱只是另一个域实体?在#2中,烤箱是域的一部分。我倾向于做域名建模的方式,大多数名词都是域实体,除非你确定是完全一个。
但是比萨饼烘烤时会发生什么。
界面ICanBeBaked {
int BakeMinutes {get; }
int BakeTemp {get; }
void Bake();
}
class Pizza:ICanBeBaked {
int BakeMinutes {get {return 15; }}
int BakeTemp {get {return 425; }}
void Bake(){
//熔化奶酪!
this.isBaked = true;
}
}
class Oven {
void Bake(ICanBeBaked thingToBake){
//设置temp,保留这个烤箱的持续时间等
thingToBake.Bake();
}
}
I have a problem I want to know your opinion.
I am trying to use Repository Pattern. I have a repository object which load data to a POCO. I have also created a Business logic layer which adds a little bit of functionality but basically wraps the POCO. So in the end I have a BLL which loads DAO with usage of repository.
I am not very happy with this solution. I have a three layers but I feel that BLL is not providing enought functionality to keep it there. On the other hand I do not want to put my logic in the repository layer nor data access layer?
So my question is where should I put logic for application? Which solution do you use(DAO + repo or DAO + BLL + rep or any other)?
There are two basic ways to think about business rules when designing your domain.
1.) The domain entities are basic POCO/DTOs. And you hand them off to domain services. These services could be as simple as another class, or they really could be actual services sitting on another server.
var user = repository.Find(x => x.UserName == userName);
if (userLogonService.IsValidUser(user, password)) {
userLogonService.UpdateUserAsLoggedOn(user);
}
repository.SaveChanges();
2.) The domain entities contain their own operation logic. This is closer to what many MVC patterns will follow. And since you asked, this is the model that I prefer.
var user = repository.Find(x => x.UserName == userName);
if (user.CheckPassword(password)) {
user.LogOnNow();
}
repository.SaveChanges();
Both are completely valid patterns. #1 has a discrete business operation tier, but suffers from an Anemic Domain Model. #2 can lead to big domain entities if you domain starts to become complicated, or if a model can do a lot of things.
EDIT #1: Response to John Kraft
Oven.Bake(myPizza) vs. myPizza.Bake()
I mostly agree. Do you have a single Oven service, or do you have dozens of available ovens stored in an oven repository where oven is just another domain entity? In #2, the oven is part of the domain. The way I tend to do domain modeling, most nouns are domain entities, unless you are 100% sure that there is exactly one of the thing.
But something does happen to pizza when it is baked.
interface ICanBeBaked {
int BakeMinutes { get; }
int BakeTemp { get; }
void Bake();
}
class Pizza : ICanBeBaked {
int BakeMinutes { get { return 15; } }
int BakeTemp { get { return 425; } }
void Bake() {
// melt cheese!
this.isBaked = true;
}
}
class Oven {
void Bake(ICanBeBaked thingToBake) {
// set the temp, reserve this oven for the duration, etc.
thingToBake.Bake();
}
}
这篇关于存储库模式和/或/ vs业务逻辑层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!