単体テストを容易にするために、古い Active-Record ベースの API を新しい構造に移植しています。データ アクセスにはリポジトリ パターンを使用し、依存性注入には StructureMap を使用しています。
私は今、すべてをどのように構築するかについて少し苦労しています。例として「製品」クラスを使用すると、以前はモデル、リポジトリ、およびファクトリ パーツがすべて製品オブジェクトに組み込まれていました。
Dim p As New Product
If(p.Load(1))
p.Name = "New Name"
p.Save()
End If
オブジェクトのコレクションを取得するために、基本的なファクトリとして機能するオブジェクトに Shared(static) メソッドがありました
Dim arrProds = Product.GetProducts()
いくつかの例を見た後、新しい構造には、IProductRepository、IProductService (ファクトリ)、および Product モデル クラスがあります。だから、このようなもの
Dim prodSvc = ObjectFactory.GetInstance(Of IProductService)
Dim prod = prodSvc.GetProduct(1) //Prod is an Instance of 'Product'
prod.Name = "New Name"
prodSvc.Save(prod)
ただし、Product クラスには関連データをロードする機能があります。たとえば、
Dim arrRelatedProds = prod.RelatedProducts
Product クラスの内部は次のようになります
Class Product
Function RelatedProducts() As IList(Of Product)
// prodSvc is an instance of IProductService which is passed into the
// Product class in the constructor (or via Dependency Injection)
Return Me.prodSvc.GetRelatedProducts(Me.ProductID)
End Function
End Class
テストが難しく、「モデル」クラス (製品) が IProductService クラスを直接呼び出すという事実が気に入らないため、これは好きではありません。
これらすべてを構造化するためのより良い方法について誰か提案がありますか?
乾杯
ジェームズ
編集 おそらくこれを尋ねるのは間違った時期です! この質問に答えられるようにするために追加できる説明はありますか??