2011-02-18 242 views
1

好吧...所以我用EF4創建了我的模型。大!實體框架4 POCO代

然後我關閉了代碼生成並下載了這個擴展: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呢?我現在甚至需要使用它嗎?

依賴注入?任何人都在意解釋。

多謝,
山姆

回答

1

還有斯科特 - 阿倫一大篇關於Testing Entity Framework 4 - 創建POCO類是一個良好的開端,但如果你想測試你的業務層由EF分開,你將有介紹一個工作單位,協調跨多個存儲庫的保存狀態並允許DI。

+0

@Sam:依賴注入 – BrokenGlass 2011-02-18 23:01:35