I've noticed, there's really no rhyme or reason to whether my repositories take objects or primitives as parameters, or whether CREATE methods return just an int (ID from the DB) or the full-fledged object.
So my question is, should repositories pass around and return objects, or primitives? What advice can you give on this matter? Can you share any pitfalls or experiences with either approach?
Example:
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...?
}
}
What about if info is needed from multiple objects? Surely from an OOP standpoint it's better to pass around objects here, no? (I'm being cheeky here...)
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...
}
normaly you will see after a short time (if you use multiple Repositories) that this is a pattern itself and can be refactored in some kind of Base-class (for the data-access). But in order to do this you won't be able to give the expanded parameters for Add/Update, etc. (and neither should you - just think of an object with 10+ properties) but just the pattern with the object itself.
So use option 1 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With