实体框架 4 POCO 生成
好的...所以我已经使用 EF4 创建了我的模型。伟大的!
然后我关闭了代码生成并下载了此扩展: http://visualstudiogallery .msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313 (POCO 实体生成器)。惊人的!
运行它,它会生成我的所有课程。这就是我所要做的一切吗?它似乎有效,我的存储库获取对象并持久保存到数据库。
请看一下下面的代码,让我知道我是否走在正确的轨道上。
** 示例代码 **
控制器:
Namespace Controllers
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return View(New Models.HomeModel)
End Function
End Class
End Namespace
模型:
Namespace Models
Public Class HomeModel
Private _Repository As Titan.Business.Repositories.ICustomerRepository
Private _SalesRepRepo As Titan.Business.Repositories.ISalesRepresentativeRepository
Public Property Customers As IEnumerable(Of Titan.Business.Customer)
Public Property SalesReps As IEnumerable(Of Titan.Business.SalesRepresentative)
Public Sub New()
_Repository = New Titan.Business.Repositories.CustomerRepository
_SalesRepRepo = New Titan.Business.Repositories.SalesRepresentativeRepository
_Customers = _Repository.Query(Function(x) x.LastName.StartsWith("Str"))
_SalesReps = _SalesRepRepo.Query(Function(x) x.LastName.StartsWith("Str"))
End Sub
End Class
End Namespace
存储库和接口:
Namespace Repositories
Public Interface IRepository(Of T)
Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of Func(Of T, Boolean))) As IEnumerable(Of T)
Function GetByID(ByVal ID As Integer) As T
Sub Add(ByVal Entity As T)
Sub Delete(ByVal Entity As T)
Sub Save(ByVal Entity As T)
End Interface
Public Interface ICustomerRepository
Inherits IRepository(Of Customer)
End Interface
Public Interface ISalesRepresentativeRepository
Inherits IRepository(Of SalesRepresentative)
End Interface
End Namespace
Namespace Repositories
Public Class SalesRepresentativeRepository
Implements ISalesRepresentativeRepository
Public Sub Add(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Add
End Sub
Public Sub Delete(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Delete
End Sub
Public Function GetByID(ByVal ID As Integer) As SalesRepresentative Implements IRepository(Of SalesRepresentative).GetByID
End Function
Public Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of System.Func(Of SalesRepresentative, Boolean))) As System.Collections.Generic.IEnumerable(Of SalesRepresentative) Implements IRepository(Of SalesRepresentative).Query
Using db As New GTGContainer
Return db.SalesRepresentatives.Where(Predicate).ToList
End Using
End Function
Public Sub Save(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Save
End Sub
End Class
End Namespace
任何建议对我都会很有帮助。
服务层适合在哪里?
自动映射器怎么样?我现在还需要使用它吗?
依赖注入?有哪位好心人解释一下。
非常感谢,
山姆
Ok... So I have created my model using EF4. Great!
I then turned off the code generation and downloaded this extension: http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313
(POCO Entity Generator). Awesome!
Ran it, and it generates all of my classes. Is that all I have to do? It seems to work, my repositories get to the objects and persist to the DB.
Please have a look at the following code and let me know if I am on the right track.
** Sample Code **
Controller:
Namespace Controllers
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return View(New Models.HomeModel)
End Function
End Class
End Namespace
Model:
Namespace Models
Public Class HomeModel
Private _Repository As Titan.Business.Repositories.ICustomerRepository
Private _SalesRepRepo As Titan.Business.Repositories.ISalesRepresentativeRepository
Public Property Customers As IEnumerable(Of Titan.Business.Customer)
Public Property SalesReps As IEnumerable(Of Titan.Business.SalesRepresentative)
Public Sub New()
_Repository = New Titan.Business.Repositories.CustomerRepository
_SalesRepRepo = New Titan.Business.Repositories.SalesRepresentativeRepository
_Customers = _Repository.Query(Function(x) x.LastName.StartsWith("Str"))
_SalesReps = _SalesRepRepo.Query(Function(x) x.LastName.StartsWith("Str"))
End Sub
End Class
End Namespace
Repository and Interfaces:
Namespace Repositories
Public Interface IRepository(Of T)
Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of Func(Of T, Boolean))) As IEnumerable(Of T)
Function GetByID(ByVal ID As Integer) As T
Sub Add(ByVal Entity As T)
Sub Delete(ByVal Entity As T)
Sub Save(ByVal Entity As T)
End Interface
Public Interface ICustomerRepository
Inherits IRepository(Of Customer)
End Interface
Public Interface ISalesRepresentativeRepository
Inherits IRepository(Of SalesRepresentative)
End Interface
End Namespace
Namespace Repositories
Public Class SalesRepresentativeRepository
Implements ISalesRepresentativeRepository
Public Sub Add(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Add
End Sub
Public Sub Delete(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Delete
End Sub
Public Function GetByID(ByVal ID As Integer) As SalesRepresentative Implements IRepository(Of SalesRepresentative).GetByID
End Function
Public Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of System.Func(Of SalesRepresentative, Boolean))) As System.Collections.Generic.IEnumerable(Of SalesRepresentative) Implements IRepository(Of SalesRepresentative).Query
Using db As New GTGContainer
Return db.SalesRepresentatives.Where(Predicate).ToList
End Using
End Function
Public Sub Save(ByVal Entity As SalesRepresentative) Implements IRepository(Of SalesRepresentative).Save
End Sub
End Class
End Namespace
Any suggestions would be so helpful to me.
Where does the service layer fit in?
What about the AutoMapper? Do I even need to use that now?
Dependency Injection? Anyone care to explain.
Thanks a bunch,
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Scott Allen 写了一篇关于测试实体框架 4 的精彩文章 - 创建POCO 类是一个很好的第一步,但如果您想测试与 EF 分开的业务层,您将必须引入一个工作单元来协调跨多个存储库的保存状态并允许 DI。
There's a great article by Scott Allen about Testing Entity Framework 4 - creating the POCO classes is a good first step, but if you want to test you business layer separate from EF you will have to introduce a Unit of Work that coordinates saving state across multiple repositories and allows DI.