我将基于Active-Record的旧API移植到新的结构中,使单元测试变得更加简单等。我们将数据库模式用于数据访问,使用StructureMap进行依赖注入。模型,工厂和知识库
我现在正在努力一点,如何构造一切。以我们的'产品'类为例,我们以前曾将我们的模型,存储库和工厂部件都内置到产品对象中,例如用于加载,编辑和保存我们可能喜欢的产品(简化): -
Dim p As New Product
If(p.Load(1))
p.Name = "New Name"
p.Save()
End If
而获得对象的集合,我们有哪些充当出厂基本
Dim arrProds = Product.GetProducts()
找几个例子后,对象上的共享(静态)方法,在我们的新结构,我们现在有一个IProductRepository ,IProductService(这是工厂)和Product model类。所以,像这样
Dim prodSvc = ObjectFactory.GetInstance(Of IProductService)
Dim prod = prodSvc.GetProduct(1) //Prod is an Instance of 'Product'
prod.Name = "New Name"
prodSvc.Save(prod)
但是,产品类有加载相关数据的能力,例如
Dim arrRelatedProds = prod.RelatedProducts
这里面的产品类看起来像这样
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
我不喜欢这个,因为它很难测试,因为我不喜欢我的'Model'类(Product)直接调用IProductService类。
有没有人有更好的方法来构建所有这些建议?
干杯
詹姆斯
编辑 大概一年的时间不对问这个!有没有任何澄清,我可以补充,使这个问题可以回答?