服务层是否充当 DAL 的外观?
我正在阅读有关服务层和存储库的内容。现在我想知道服务层是否必须包装 dal。我经常使用存储库和 MVP 模式。演示者现在掌握了业务逻辑。但我越想越觉得,将业务逻辑放在演示器或数据访问层中并不是一个逻辑位置。所以这就是服务层的切入点。
但是演示者现在是否与服务层对话?演示者是否“允许”访问存储库?或者一切都应该通过服务层进行?在后一种情况下,服务层只是一个中间人:
MyFooService:
public List<Foo> GetAllFoo()
{
var listFoo = new FooRepository().GetAll().TiList();
return listFoo;
}
Presenter:
public List<Foo> GetAllFoo()
{
var listFoo = new MyFooService().GetAllFoo();
return listFoo;
}
这是好方法吗?或者是否“允许”演示者直接调用存储库?
I am reading up about service layers and repositories. Now I am wondering if a service layer must wrap the dal. I am working a lot with repositories and the MVP pattern. The presenters now holds the business logic. But the more I think about it, it is not a logic place to put the business logic in the presenter nor the data access layer. So this is the point the service layer comes in.
But does the presenter now talks to the service layer? And is it 'allowed' that the presenter can access the repositories? Or should everything go via the service layer? In the latter case, the service layer is just a middleman:
MyFooService:
public List<Foo> GetAllFoo()
{
var listFoo = new FooRepository().GetAll().TiList();
return listFoo;
}
Presenter:
public List<Foo> GetAllFoo()
{
var listFoo = new MyFooService().GetAllFoo();
return listFoo;
}
Is the good way to go? Or is it 'allowed' that the presenter directly calls the repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时,您不需要过度设计事物或在不需要时强行采用模式。
DAL 本身就是一种访问数据的特殊服务。
通常,您的服务层将执行与您的数据不直接相关的操作。想想诸如 PaymentService、AnalyticsService 之类的东西,它们可以分成可重用的组件。
假设您需要在所有社交媒体上分享帖子,您可以将其放入一项服务中,该服务负责登录正确的社交媒体并发帖:
现在,从您的控制器/演示者中,您可以调用此服务。
只是为了让您清楚业务逻辑是什么:
它是您需要做的一些事情,并且您可以在不接触界面的情况下完成它,这需要被封装。更多现实世界的示例是
SecurityService.ResetPassword()
当您从控制器调用此方法时:
您可以在 Web 应用程序或 Windows 应用程序中使用添加的业务逻辑,并通过某个接口获取用户的输入。 如何做到这一点就是控制器逻辑。
Sometimes you do not need to over engineer things or force into patterns when you don't need them.
The DAL, is itself sorta a special service to access data.
Typically your service layer will do stuff not related directly to your data. Think things like PaymentService, AnalyticsService etc things like that, that can be separated into a re-usable component.
Lets say you needed to share a post on to all social media, you could put this into a service that does the job of signing into the right social media and posting:
Now from your controller/presenter you can call this service.
Just so you're clear on what business logic is:
Its some stuff you need to do, and you can do it without touching an interface, this needs to be encapsulated. More real world examples are
SecurityService.ResetPassword()
When you call this from the controller:
You could use the business logic of adding in a web app, or in a windows app, and get the inputs from the user through some interface. How you do that is controller logic.
我想说,如果您正在进行“认真的”中型到大规模开发,最好不要将业务逻辑放入表示层中。如何隔离它是另一个问题。
另一方面,如果您使用 Entity Framework 或 NHibernate 之类的东西,我只会在确实需要抽象数据访问时创建存储库,例如在测试时使用模拟。
I would say that if you are doing "serious" middle to big scale development it is better not to put business logic into your presentation layer. How you isolate it is another question.
On the other side if you use something like Entity Framework or NHibernate I would only create repositories if it is really necessary to abstract data access, for using mocks when testing, for example.