有关接口和DI的问题? [英] Question about Interfaces and DI?
问题描述
我正在MVC应用程序中使用Service/Repository/EF/POCO模式,并且对接口有一些疑问.
I am using the Service/Repository/EF/POCO pattern in a MVC app, and had a couple questions about the Interfaces.
1)我应该为每个服务建立一个接口吗?2)我应该为每个存储库创建一个接口吗?
1) Should I make an Interface per Service? 2) Should I make an Interface per Repository?
或者,我应该在每层上使用一个通用接口(IService(T),IRepository(T)).
Or, should I have a generic interface per layer (IService(Of T), IRepository(Of T)).
我不明白的是控制器中的说法,它在构造函数中带有IService(Of Category)接口,如何在具体类中实现方法?
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
_Service没有我的具体CategoryService类的方法?
The _Service does not have the methods of my concrete CategoryService class?
有道理吗?
推荐答案
使用具体的接口进行服务.如果您的服务可以通过通用接口来描述,那么您很可能根本不需要它们.通用接口通常用于存储库,因为存储库通常提供相同的核心方法.
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.
这篇关于有关接口和DI的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!