【问题标题】:Entity Framework 4 POCO Generation实体框架 4 POCO 生成
【发布时间】:2011-06-30 03:52:29
【问题描述】:

好的...所以我使用 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

任何建议都会对我很有帮助。

服务层适合哪里?

AutoMapper 怎么样?我现在还需要使用它吗?

依赖注入?谁愿意解释一下。

非常感谢,
山姆

【问题讨论】:

    标签: vb.net entity-framework-4 entity poco


    【解决方案1】:

    Scott Allen 写了一篇关于Testing Entity Framework 4 的精彩文章 - 创建 POCO 类是很好的第一步,但是如果您想测试与 EF 分开的业务层,您将不得不引入一个协调保存状态的工作单元跨多个存储库并允许 DI。

    【讨论】:

    • @Sam - 这是依赖注入
    • 依赖注入有什么好的读物吗?究竟是什么?为什么要使用它?
    • 依赖注入使您能够在启动或执行期间为接口提供实现。它使您能够创建松散耦合的组件。
    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 1970-01-01
    • 2011-06-04
    • 2011-08-24
    • 2011-08-21
    • 2011-11-16
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多