Sam
Sam

Reputation: 15761

Entity Framework 4 POCO Generation

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

Upvotes: 1

Views: 1379

Answers (1)

BrokenGlass
BrokenGlass

Reputation: 160852

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.

Upvotes: 1

Related Questions