EntityFramework4を使用してASP.NETMVC3アプリケーションを作成しています。リポジトリ/サービスパターンを使用しており、フィードバックを探していました。
私は現在次のものを持っています:
MVCアプリケーション(GTG.dll)
- GTG
- GTG.Controllers
- GTG.ViewModels
ビジネスPOCO(GTG.Business.dll)
- これには、すべてのビジネスオブジェクト(顧客、注文、請求書など)が含まれます。
EFモデル/リポジトリ(GTG.Data.dll)
- GTG.Business(GTG.Context.tt)エンティティPOCOジェネレーターテンプレートを使用しました。
- GTG.Data.Repositories
サービスレイヤー(GTG.Data.Services.dll)
- GTG.Data.Services-集約ルートごとに1つずつ、すべてのサービスオブジェクトが含まれます。
以下は、小さなサンプルコードです。
コントローラ
Namespace Controllers
Public Class HomeController
Inherits System.Web.Mvc.Controller
Function Index() As ActionResult
Return View(New Models.HomeViewModel)
End Function
End Class
End Namespace
モデル
Namespace Models
Public Class HomeViewModel
Private _Service As CustomerService
Public Property Customers As List(Of Customer)
Public Sub New()
_Service = New CustomerService
_Customers = _Service.GetCustomersByBusinessName("Striano")
End Sub
End Class
End Namespace
サービス
Public Class CustomerService
Private _Repository As ICustomerRepository
Public Sub New()
_Repository = New CustomerRepository
End Sub
Function GetCustomerByID(ByVal ID As Integer) As Customer
Return _Repository.GetByID(ID)
End Function
Function GetCustomersByBusinessName(ByVal Name As String) As List(Of Customer)
Return _Repository.Query(Function(x) x.CompanyName.StartsWith(Name)).ToList
End Function
End Class
リポジトリ
Namespace Data.Repositories
Public Class CustomerRepository
Implements ICustomerRepository
Public Sub Add(ByVal Entity As Business.Customer) Implements IRepository(Of Business.Customer).Add
End Sub
Public Sub Delete(ByVal Entity As Business.Customer) Implements IRepository(Of Business.Customer).Delete
End Sub
Public Function GetByID(ByVal ID As Integer) As Business.Customer Implements IRepository(Of Business.Customer).GetByID
Using db As New GTGContainer
Return db.Customers.FirstOrDefault(Function(x) x.ID = ID)
End Using
End Function
Public Function Query(ByVal Predicate As System.Linq.Expressions.Expression(Of System.Func(Of Business.Customer, Boolean))) As System.Linq.IQueryable(Of Business.Customer) Implements IRepository(Of Business.Customer).Query
Using db As New GTGContainer
Return db.Customers.Where(Predicate)
End Using
End Function
Public Sub Save(ByVal Entity As Business.Customer) Implements IRepository(Of Business.Customer).Save
End Sub
End Class
End Namespace