How should I architect my Model and Data Access layer objects in my website?

Posted by Robin Winslow on Programmers See other posts from Programmers or by Robin Winslow
Published on 2012-10-17T20:33:02Z Indexed on 2012/10/17 23:19 UTC
Read the original article Hit count: 375

Filed under:
|
|
|

I've been tasked with designing Data layer for a website at work, and I am very interested in architecture of code for the best flexibility, maintainability and readability.

I am generally acutely aware of the value in completely separating out my actual Models from the Data Access layer, so that the Models are completely naive when it comes to Data Access. And in this case it's particularly useful to do this as the Models may be built from the Database or may be built from a Soap web service.

So it seems to me to make sense to have Factories in my data access layer which create Model objects. So here's what I have so far (in my made-up pseudocode):

class DataAccess.ProductsFromXml extends DataAccess.ProductFactory {}
class DataAccess.ProductsFromDatabase extends DataAccess.ProductFactory {}

These then get used in the controller in a fashion similar to the following:

var xmlProductCreator = DataAccess.ProductsFromXml(xmlDataProvider);
var databaseProductCreator = DataAccess.ProductsFromXml(xmlDataProvider);

// Returns array of Product model objects
var XmlProducts = databaseProductCreator.Products();
// Returns array of Product model objects
var DbProducts = xmlProductCreator.Products();

So my question is, is this a good structure for my Data Access layer? Is it a good idea to use a Factory for building my Model objects from the data? Do you think I've misunderstood something? And are there any general patterns I should read up on for how to write my data access objects to create my Model objects?

© Programmers or respective owner

Related posts about database

Related posts about mvc