Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Class - Save Objects

Tags:

c#

factory

I have a factory class that populates objects with data. I want to implementing saving from the object, but do not want to populate the object with db stuff - is it stupid to have my Factory that creates the class also save the data?

ie: in my .Save() method on the object i would call Factory.Save(myObject);

like image 514
schmoopy Avatar asked Oct 10 '08 01:10

schmoopy


2 Answers

The factory class is a creational pattern that helps with creating new objects.

There are various patterns which deal with persisting objects, one of which is data mapper http://martinfowler.com/eaaCatalog/dataMapper.html

This is often used in conjection with Repository http://martinfowler.com/eaaCatalog/repository.html

You can use these patterns to abstract the database away from your domain/business objects, and access them from within your application to query and save objects.

So the data mapper/repository is responsible for both aspects of persistence (populating from database, and saving back to database).

like image 130
Keith Patton Avatar answered Nov 14 '22 23:11

Keith Patton


If you are concerned about database stuff in classes - have you considered using O/R mapper?

This would keep the database stuff entirely out of your code and your domain objects clean.

Maybe take a look at NHibernate or Active Record.

like image 37
Fionn Avatar answered Nov 15 '22 00:11

Fionn