views:

377

answers:

2

Hello,

I have been wondering if the Factory Pattern and the Repository Pattern is keened to go hand in hand in a Domain Driven Design project?

The reason i ask is the way i'm doing this is like so:

GUI -> ClassFactory -> ClassProduct (is in the Domain Model) -> ClassProductRepository -> Datasource

The GUI calls the ClassFactory to separate the GUI from business logic. The ClassProduct calls the ClassProductRepository to separate the business logic from the datasource.

Is this a wrong approach of using these Design Patterns with Domain Driven Design? If so, please state your opinion on this subject.

+1  A: 

I would suggest sticking to MVC as a generic approach to separate your business logic from your view and your controller. This has been well documented and well studied approach, though its the best that we have currently.

Though, it seems like you're trying to use the pattern just for the reason of using the pattern. This is good for learning,but in most application systems I've seen the patterns you have described either don't exist and a simpler approach works or that its a nightmare to maintain.

Please see my previous answer to a question similar like this, that has a video that can help you in your design in the future.
Linky

Chad
I'm not using it just to use it. I want my system to be low coupled, so i get an easy maintainable system. I get that MVC would help me to this goal, but i also want the model layer to be low coupled. So to use the example in topic we could just swap GUI with whatever class i wanna use from the model layer. Would this approach just take my wish of low coupling to the extreme?
Poku
Inside the 'Model' you can use the state pattern to implement your own model that can be replaced on the fly. Though for the most part your way ahead of the game if you can just can logically separate your model from your view.
Chad
+1  A: 

Your on the right track. As Chad pointed out, you'll want to use a GUI interface separation pattern as an additional layer between your domain and the UI. MVC, MVP, Presentation Model, etc are established and well documented patterns for UI separation. Martin Fowler's excellent PoEAA covers many of them

As for your main question. Yes. Factories and Repositories work very well together. In fact, Evans suggests in DDD that in some cases you can delegate responsibility for object creation from your repository to your factory classes when you're reconstructing objects from the data store.

client <=> repository -> factory
               |
               v
            database
  1. The client requests an object from the repository.
  2. The repository queries the database.
  3. The repository sends raw data to the factory.
  4. The factory returns object.

An over simplification but you get the idea. One point that isn't touched on by Evans (but which Fowler covers) is dependency injection. As your domain complexity continues to grow you may want to consider moving to an IoC container for managing object life cycles.

codeelegance