tags:

views:

248

answers:

3

Hello, im creating a Factory class that will contruct and return an object. I normally would do all of the data stuff at the Data Access Layer, but i dont think i could reach my objective and still do so. What i want to do is use a SQLDataReader to quickly read the data information and populate the object to be returned from the factory. Is this a stupid idea? Is there a better approach? I would prefer not to just return a DataSet from the DAL if possible, or is it a question of performance vs. maintainability?

Thanks in advance

A: 

If you're sure you're going to be using all of the data you load from the SQLDataReader, then yes, you could do it at construction time in the factory. However if the data set has many fields, which only a minority of will be used, then demand-loading the data at the time the accessors are called would be a better use of resources.

That said, I suggest you load it in the factory when you have all the 'pieces' at hand, and if it's not exactly right, you'll know what needs fixing. Always start with the simplest thing that could possibly work.

caskey
Thank you very much for your comment, that is what i will try.
schmoopy
+1  A: 

In most cases this is a good idea as this way provides two major benefits:

  1. This way you can seperate the data access and business logic, which means if you change database design the upper layer algorithms do not need to be changed.

  2. From OO stand point, you are converting some pure data into objects and may also added behavior to the objects, which makes the code easier to maintain and reuseable.

Yang Lu
A: 

I would say it depends on your expected use of the Factory. If this is a factory on your Data Access Layer, which will be used to populate business objects with data from the database, then yes this is the place to do it. (The IRepository pattern is something like this...sort of).

If your factory is not intended to live near your data access code, I would keep them separate. Remember the Single Responsibility Principle: Objects should have only one reason to change. If the factory is just populating objects with data, then that is an appropriate use, however if it is populating objects with data in addition to doing other things, then it is best not to add the data stuff.

There are trade-offs whichever way you go, so generally I like to like to keep objects as simple as possible for as long as possible.

ckramer