首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实体框架4 POCO生成

实体框架4 POCO生成
EN

Stack Overflow用户
提问于 2011-02-19 06:46:00
回答 1查看 1.4K关注 0票数 1

好的..。因此,我使用EF4创建了我的模型。太棒了!

然后我关闭了代码生成并下载了这个扩展:http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313 (POCO实体生成器)。太棒了!

运行它,它会生成我所有的类。这就是我要做的全部吗?它似乎起作用了,我的存储库访问对象并持久化到数据库中。

请看一下下面的代码,让我知道我是否在正确的轨道上。

**示例代码**

控制器:

代码语言:javascript
复制
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

型号:

代码语言:javascript
复制
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

存储库和接口:

代码语言:javascript
复制
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呢?我现在还需要用它吗?

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

非常感谢,

相同的

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-02-19 06:53:45

Scott Allen有一篇关于Testing Entity Framework 4的很好的文章--创建POCO类是一个很好的第一步,但是如果你想测试你的业务层与EF分离,你必须引入一个工作单元来协调跨多个存储库的保存状态,并允许DI。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5047361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档