有关接口和 DI 的问题吗?
我在 MVC 应用程序中使用 Service/Repository/EF/POCO 模式,并且有几个关于接口的问题。
1)我应该为每个服务创建一个接口吗? 2)我应该为每个存储库创建一个接口吗?
或者,我应该每层都有一个通用接口(IService(Of T)、IRepository(Of T))。
我不明白的是,在控制器中如何说,它在其构造函数中采用 IService(Of Category) 接口,如何实现具体类中的方法?
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private _Service As IService(Of Category)
Public Sub New(ByVal Service As IService(Of Category))
_Service = Service
End Sub
Function Index() As ActionResult
Return View()
End Function
End Class
_Service 没有我的具体 CategoryService 类的方法?
有道理吗?
I am using the Service/Repository/EF/POCO pattern in a MVC app, and had a couple questions about the Interfaces.
1) Should I make an Interface per Service?
2) Should I make an Interface per Repository?
Or, should I have a generic interface per layer (IService(Of T), IRepository(Of T)).
What I dont understand is how in the controller say, it takes a IService(Of Category) interface in it's constructor, how do I implements the methods in the concrete class?
Public Class HomeController
Inherits System.Web.Mvc.Controller
Private _Service As IService(Of Category)
Public Sub New(ByVal Service As IService(Of Category))
_Service = Service
End Sub
Function Index() As ActionResult
Return View()
End Function
End Class
The _Service does not have the methods of my concrete CategoryService class?
Make any sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用具体的接口进行服务。如果您的服务可以通过通用接口来描述,那么您很可能根本不需要它们。通用接口通常用于存储库,因为存储库通常提供相同的核心方法。
Use concrete interface for service. If your services can be described by generic interface you most probably don't need them at all. Generic interface is often used for repositories because repositories usually offer same core methods.
对于我自己来说,我使用强类型的通用会话对象,这个类位于我的域项目中,包含我所有的域类。您应该看看这篇文章: http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx 使用代码优先方法。
希望有帮助!
这是我的 Session 类的代码:
For myself, I use a generic session object that is strongly type, this class is in my domain project witch contains all my domain classes. You should take a look at this post : http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx using Code-First approach.
Hope it helps!
Here my code for my Session class :