Home  /  Questions  /  Question



265   99.7
Nov 28, 2010


Patterns In Action 4 - DataObjectFactory

Hi,

In the DataObjectFactory class there is the CreateContext static method that is used to create the context for the ActionEntities.  If I'm using multiple models in my project and want to create those contexts similarly, how would I update the DataObjectFactory to work in this fashion?  What's the best way to go about achieving this?

I could create separate CreateContext methods for each set of entities, but it doesn't seem right (CreateActionContext(), CreateAccountContext(), etc...). 

Any suggestions?  Thanks.

Doug




830   99.9
Dec 03, 2010
Doug:

What do you mean with mulitple models?
Multiple databases, classes, or modules (groups of classes)?

Dan.

 1 comment
 
EF models. The PatternsInAction solution comes with one model - Action.edmx. I'm creating a new solution that contains multiple models (Account.edmx & Security.edmx). The DataObjectFactory.CreateContext static method returns a specific entity type (return new ActionEntities(_connectionString);). I'm trying to come up with a way for the DataObjectFactory to return the correct entity type. public static ObjectContext CreateContext(EntityType entityType) Is this the best way to go about returning different entity collections? Or should I just create separate CreateContext methods for each entity type: public static AccountEntities CreateAccountContext() { return new AccountEntities(_connectionString); } public static SecurityEntities CreateSecurityContext() { return new SecurityEntities(_connectionString); } Doug --- Doug Rohm  Dec 04, 2010

265   99.7
Dec 06, 2010
Hi Dan,
I replied to your question above.  Hopefully that clears up what I'm trying to do.  If not, let me know.

Doug


830   99.9
Dec 12, 2010
Doug:

The DataObjectFactory caches the connectionstring so that the context can be created quickly.

So, the DataObjectFactory represents a database connection, which would suggest that if your EF Models reside in the same database your above suggestions is appropriate (i.e. CreateActionContext, CreateAccountContext, etc).  However, if your EF Models reside in different databases, then you will want to create additional DataObjectFactory classes, one for each database connection.

Hope this helps.