jeudi 30 mars 2017

Giving database context to object Factory

There is a question I always ask myself when I'm using a Factory pattern inside my code (C#, but it applies to any language I suppose).

I have a "Service" that takes care of interacting with my database, do stuff with objects and interacts with my object model.

This Service uses a Factory sometimes to delegate the instanciation of an object. But this factory obviously needs to interact by itself with the database to instanciate my object properly. Is it a good/bad practice to pass the Database context to the Create method for example?

Like this :

var myNewObject = MyFactory.Create(myDatabaseContext);

the other way would be to let the Service always be the only one to talk with the database.

var myNewObject = MyFactory.Create();
var extraProperty = myDatabaseContext.Get(something);
myNewObject.extraProp = extraProperty;

Any advices?

Aucun commentaire:

Enregistrer un commentaire