我注意到,我的存储库是否将对象或基元作为参数,或者CREATE方法是否只返回int(来自数据库的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...
}