ZCoder
ZCoder

Reputation: 2339

I am confused between Unit of work pattern and Factory Pattern

factory Pattern Source https://en.wikipedia.org/wiki/Factory_method_pattern

In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.

My question is that unit of work pattern, and factory pattern is the same pattern with a different name I am not sure

Upvotes: 0

Views: 1749

Answers (3)

Amit Joshi
Amit Joshi

Reputation: 16389

No, they are not the same.

You already have good understanding of Factory pattern as stated in your question and you also linked a resource explaining it. I will not repeat that part here.

Factory pattern is Creational pattern where UnitOfWork is Behavioral pattern.

Unit Of Work (UoW) is injected in Repositories. UoW may create the Repositories based on your design; but creation (of repositories) is not an objective of UoW. Based on design, UoW in some cases may play additional role of Factory.

As stated here:

A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

As you can see in above quote, primary responsibility of UoW is to keep track of changes done in business transaction and flush (or discard) those changes at the end of scope.

This question discusses about UoW in details; may help you.

Upvotes: 1

Ray Tayek
Ray Tayek

Reputation: 10003

Probably not the same. Factory Method is a design pattern. Unit of Work is an Object-Relational Behavioral Pattern.

Upvotes: 1

Johnny
Johnny

Reputation: 9509

My question is that unit of work pattern, and factory pattern is the same pattern with a different name I am not sure

Why do you think they are the same patterns?


I have to disappoint you they are not the same patterns. While you already found and explained what factory pattern is the unit of work is something else.

By Martin Fowler:

Maintains a list of objects affected by a business transaction and coordinates the writing out of changes. (“Patterns of Enterprise Application Architecture” by Martin Fowler)

Usually, it is going along with repository pattern and it is used to:

  • Maintains lists of business objects in-memory which have been changed (inserted, updated, or deleted) during a transaction.
  • Once the transaction is completed, all these updates are sent as one big unit of work to be persisted physically in a database in one go.

You could find a nice explanation of repository pattern together with unit of work pattern here.

Upvotes: 3

Related Questions