Hi,
I am in the process of refactoring our BI layers to make our code more "loosely coupled" and I was interested to hear what you guys thought might be interesting improvements to make?
At present, our API is something like the following:-
// Fetch a collection of objects
ProductCollection prods = Product.GetProducts();
// Load an individual object, make change and save it back
Product p = new Product();
if(p.Load(productID))
{
p.Name = "New Name";
p.Save();
}
As you can see, our methods for fetching collections of objects/loading individual objects and saving changes are all built into the "Model" class. Each of our Model classes inherits from an ObjectBase base class which includes DB access functions and change tracking so when someone changes a value through a property the object is automatically marked dirty and notifications are fired to whatever object (UI) has subscribed to those events.
What I would like to do is use the "Repository pattern" so that we can abstract the Database implementation away from the Model. However, much of the code I have been looking at seems to suggest that the "Model" class should not contain any intelligence and should just be a container for data. Instead, the logic should be applied through the use of services. Does this then mean to accomplish the above I would need to do something like
List<Product> prods = ProductService.GetProducts();
Product p = ProductService.GetSingleProduct(productID);
p.Name = "New Name";
ProductService.SaveProduct(p);
This seems a more complex way of doing it and makes it harder to encapsulate functionality within business objects.
Can someone explain why this is a better way of doing it or maybe have I misunderstood the concepts?
Thanks
James