I'm trying to learn some best practices while I ramp up on MVC4. I have a solution with three projects:
在学习MVC4的同时,我正在努力学习一些最佳实践。我有三个项目的解决方案:
- Web: MVC stuff
- 网络:MVC的东西
- Core: Data Model
- 核心:数据模型
- Tests: Testing classes
- 测试:测试类
I'm now trying to add:
我现在想补充一点:
4: Services: Business Logic
4:服务:业务逻辑
This will take logic such as "GetFilteredItems()" from my controllers and place them into a Service project, which depends on Core, and upon which Web depends. As I'm beginning to do this, I want to honor single responsibility and create one class per method. My questions:
这将从我的控制器中获取诸如“GetFilteredItems()”之类的逻辑,并将它们放置到一个服务项目中,该项目依赖于核心,Web依赖于哪个项目。当我开始这样做的时候,我希望尊重单个的职责,并为每个方法创建一个类。我的问题:
1) Is this a good approach?
2) Say I need a GetFilteredItems method that takes an ID and returns a List. What's the convention here in terms of naming classes and methods? I can't have a class SampleClass with a method SampleClass() that returns something, right?
这是一个好方法吗?2)假设我需要一个GetFilteredItems方法,它接受一个ID并返回一个列表。这里关于命名类和方法的约定是什么?我不能有一个带有方法SampleClass()的类SampleClass(),它返回一些东西,对吗?
2 个解决方案
#1
1
There is a good discussion on creating a service layer in MVC here.
这里有一个关于在MVC中创建服务层的很好的讨论。
Single responsibility does not require classes with single methods. I have never heard of this approach before. Where is there a discussion on this approach and its benefits?
单一职责不需要具有单一方法的类。我以前从未听说过这种方法。在哪里有关于这种方法及其好处的讨论?
#2
0
Yes this seems like a good approach, although the single responsibility principle doesn't necessarily mean that you need to create a separate class per method.
是的,这似乎是一种很好的方法,尽管单一职责原则并不一定意味着您需要为每个方法创建一个单独的类。
Just make sure that your service classes have a single responsibility.
只需确保您的服务类有一个单一的职责。
For your GetFilteredItems method, you could create a class called ItemService to contain this. This service would also contain any other methods for returning collections of items.
对于GetFilteredItems方法,您可以创建一个名为ItemService的类来包含这个。此服务还将包含返回项目集合的任何其他方法。
You could then put methods pertaining to persisting items in a different service, and so on...
然后,您可以在不同的服务中放置与持久化项目相关的方法,等等……
#1
1
There is a good discussion on creating a service layer in MVC here.
这里有一个关于在MVC中创建服务层的很好的讨论。
Single responsibility does not require classes with single methods. I have never heard of this approach before. Where is there a discussion on this approach and its benefits?
单一职责不需要具有单一方法的类。我以前从未听说过这种方法。在哪里有关于这种方法及其好处的讨论?
#2
0
Yes this seems like a good approach, although the single responsibility principle doesn't necessarily mean that you need to create a separate class per method.
是的,这似乎是一种很好的方法,尽管单一职责原则并不一定意味着您需要为每个方法创建一个单独的类。
Just make sure that your service classes have a single responsibility.
只需确保您的服务类有一个单一的职责。
For your GetFilteredItems method, you could create a class called ItemService to contain this. This service would also contain any other methods for returning collections of items.
对于GetFilteredItems方法,您可以创建一个名为ItemService的类来包含这个。此服务还将包含返回项目集合的任何其他方法。
You could then put methods pertaining to persisting items in a different service, and so on...
然后,您可以在不同的服务中放置与持久化项目相关的方法,等等……