Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design Patterns: Factory and Repository

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.

like image 419
Poku Avatar asked Oct 29 '09 15:10

Poku


People also ask

What is difference between factory pattern and repository pattern?

The Factory pattern is for creating objects, while the Repository pattern describes a general method of encapsulating CRUD operations against a data source.

What is repository in design pattern?

The Repository pattern. Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

What is the repository pattern the factory pattern Why are patterns important?

In modern MVC and Web-API applications, a repository and / or factory pattern are often used to provide a separation of concern as well as aid the developer in code reuse. The repository pattern allows business logic to stay in the domain layer and out of the data layer.

What is difference between factory and repository pattern in Magento 2?

Factories are service classes that instantiate non-injective classes, that is, models that represent the database entity. They create an abstraction layer between ObjectManager and the business code. The repository object is responsible for reading and writing information about your object to the object store.


2 Answers

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.

like image 66
Kenneth Cochran Avatar answered Sep 28 '22 03:09

Kenneth Cochran


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

like image 23
Chad Avatar answered Sep 28 '22 03:09

Chad