Did*_*xis 2 c# oop orm repository repository-pattern
我注意到,我的存储库是否将对象或基元作为参数,或者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...?
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要来自多个对象的信息呢?当然,从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...
}
Run Code Online (Sandbox Code Playgroud)
通常情况下,您会在短时间内看到(如果您使用多个存储库)这是一个模式本身,并且可以在某种Base类中重构(用于数据访问).但是为了做到这一点,你将无法为Add/Update等提供扩展参数(也不应该 - 只考虑具有10+属性的对象),而只考虑具有对象本身的模式.
所以使用选项1;)