tags:

views:

3670

answers:

4

Up until now I've been using Active records in all my c# database driven applications. But now my application requires my persistence code being split from my business objects. I have read a lot of posts regarding Martin Fowler's data mapping pattern, but my knowledge of this pattern is still very limited.

Let's use the following example:

If I have 2 tables - Customer and CustomerParameters. The CustomerParameters table contains default Customer values for creating a new Customer.

I will then have to create a CustomersMapper class to handle all of the Customer persistence. My Customer and CustomersList class will then collaborate with this mapper class in order to persist customer data.

I have the following questions:

  1. How would I transfer raw data TO & FROM my Customer class to the mapper without breaking certain business rules? DTO's?

  2. Is it acceptable to have a SaveAll and LoadAll method in my Mapper class for updating and loading multiple customers' data? If so, in case of SaveAll, how will the mapper know when to update or insert data?

  3. Will the Customer mapper class be responsible for retrieving the default values from the CustomerParameters table as well, or will it be better to create a CustomerParameters mapper?

A O/R mapper tool is not really here. The database I'm using is Transactional and requires that I write my own Mapper Pattern.

Any ideas and comments will be greatly appreciated.

A: 

I would suggest that you take a look at an O/R-mapper tool before you try to implement the Data Mapper pattern yourself. It will save you a lot of time. A popular choice of O/R-mapper is NHibernate.

Petter Wigle
A O/R mapper tool is not really a option for me. The database im using is Transactional and requires that I write my own Mapper Pattern.
MegaByte
+3  A: 

Shaun I would answer your questions this way:

ad 1) Mapper is responsible for creating Customer object. Your Mapper object will have something like RetrieveById method (for example). It will accept an ID and somehow (that't he responsibility of the Mapper object) construct the valid Customer object. The same is true the other way. When you call Mapper.Update method with a valid Customer object, the Mapper object is responsible for making sure that all the relevant data are persisted (wherever appropriate - db, memory, file, etc.)

ad 2) As I noted above retrieve/persist are methods on Mapper object. It is its responsibility to provide such a functionality. Therefore LoadAll, SaveAll (probably passing an array of value objects) are valid Mapper methods.

ad 3) I would say yes. But you can separate various aspects of Mapper objects into separate classes (if you want to/need to): default values, rule validation, etc.

I hope it helps. I really suggest/recommend you to read Martin Fowler's book Patterns of Enterprise Application Architecture.

David Pokluda
A: 

You could check out iBATIS.NET as an alternative to NHibernate. It's also an O/R tool, but I've found it to be a little easier to use than NHibernate.

http://ibatis.apache.org/dotnetdownloads.cgi

Cory Larson
A: 

Try looking in the direction of xml. The surrounding technologies make reading/writing files and doing transformations simpler. You can access it as a dom, an event model, text to be parsed, orm (some commercial tools automate the task), etc. Modern browsers support it natively, as well as through javascript. PHP also has multiple parsers. A search on the Adobe website reveals a torrent of ways to use xml transformations. It's the X in AJAX. My web based consulting services are coming online as soon as possible, at which time my library and Glossary will be available! Check back with me soon as I'm solving these same problems, as my project is to expose helpful resources(books, articles, design documents, etc.) to paying web design clients depending on what resources are needed to assist the paying clients, delivered dynamically. Customization occurs as we learn about the client! Other designers and developers are a different class of users, with an inheirited item object! Traditionally tables are used, however my system uses a recursive linked list structure. I know that the same type of support exists in C# for xmlhttprequest style asynchronous requests.

Sinthia V