【发布时间】:2011-10-10 20:09:15
【问题描述】:
我注意到,对于我的存储库是否将对象或原语作为参数,或者 CREATE 方法是否只返回一个 int(来自 DB 的 ID)或完整的对象,真的没有押韵或理由。
所以我的问题是,存储库应该传递并返回对象还是原语?你能就这件事给出什么建议?您能分享任何一种方法的陷阱或经验吗?
例子:
public class ProductRepository : IProductRepository
{
// Pass in the whole object to the repo method...?
public int Add(Product product)
{
// return just the productId...?
}
// Pass in the individual primitive values...?
public Product Add(string productName, decimal productPrice, string description)
{
// return the whole Product object...?
}
}
如果需要来自多个对象的信息怎么办?当然,从 OOP 的角度来看,最好在这里传递对象,不是吗? (我在这里很厚脸皮......)
public int Add(int merchantId, Product product)
{
// database call needs merchant info...
}
public int Add(Merchant merchant, Product product)
{
var merchantId = merchant.ID;
// database call needs merchant info...
}
【问题讨论】:
标签: c# oop orm repository repository-pattern