question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why the UnitofWork is present in the Repository

See original GitHub issue

I see the unit of work is a logical container that works along with multiple repositories. Even I see the article uses same https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

But according to your implementation it looks below

namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;

public interface IRepository<T> where T : IAggregateRoot
{
    IUnitOfWork UnitOfWork { get; }
}

Then we call like

await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);
await _buyerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

I see this looks different than other way where we put all the repositories in the UnitOfWork like below

public interface IUnitOfWork
{
    IOrderRepository OrderRepository { get; }
    IBuyerRepository OrderRepository { get; }
    Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken));
}

then make a single call to the UnitOfWork like below which hides the different repositories it is dealing with await _unitOfWork.SaveEntitiesAsync();

This way also helps us to inject just a UnitOfWork in the constructor instead of N repositories.

Could you please explain the reason of design the UnitOfWork in Repository and the benifits of this approach compared to keeping the repositories inside UnitOfWork. Thanks in advance!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nhdinhcommented, Mar 18, 2022

In the view of microservice, there is only one aggregate in each service. And that aggregate should maintain the state of all entities in the service. Repository is responsible for getting Aggregate out of persistence and return to Service layer. In this way, uow will be the helper for repository to maintain its session working with persistence database. One service, one repository and one aggregate in a single service. Therefore, I see no reason why to put many repositories in a single unitofwork.

1reaction
MithrilMancommented, May 27, 2022

@erjain I don’t agree with the given explainations and I don’t think this should be closed.

A service doesn’t need to have only one aggregate, maybe @nhdinh wanted to say “aggregate root” but again a service can have multiple. Even in this repository, the Order service has 2 aggregate roots: Order and Buyer.

Aggregate root repositories are sharing the same unit of work. Given the fact that entity framework is used, if you alter two different aggregate roots in an operation (command in CQRS terminology) directly or by a side effect and you call aggregate_A_Repository.UnitOfWork.SaveEntitiesAsync, under the hood even the modifications done on the aggregate_B_ are persisted.

Ending line of my comment is: actually current repository/unitofwork is quite confusing, also the Update method doesn’t have sense because even without calling it, your entity will still be persisted - with all changes done - after the command ends, because of how the TransactionBehavior works

I’d like to have feedback on this about the persons who made this repository (and MS documentation/ebook) because I’ve a feeling like there is a lack of cohesion between the good intention (a reference for microservices + ddd + … ) with current implementation, are tye still hanging around?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should a UnitOfWork contain every Repository?
It means that the unit of work should contain all repositories depending to each ... In .Net Core, I've found a nicer way...
Read more >
Confusion over using of UnitOfWork in Repository Pattern
The unit of work class coordinates the work of multiple repositories by creating a single database context class shared by all of them....
Read more >
Repository and Unit of Work Pattern
The Repository pattern and Unit of Work pattern are used together most of the time. Therefore, I will combine them in this post...
Read more >
C# — UnitOfWork And Repository Pattern
The UnitOfWork and repository patterns are intended to act like a abstraction layer between business logic and data access layer.
Read more >
Unit Of Work in Repository Pattern
The unit of work in C# manages in-memory database CRUD operations on entities as one transaction. In simple words, we can say that,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found