63

Combining Unit of Work and Repository Pattern is something used fairly widely nowadays. As Martin Fowler says a purpose of using UoW is to form a Business Transaction while being ignorant of how repositories actually work (being persistent ignorant). I've reviewed many implementations; and ignoring specific details (concrete/abstract class, interface,...) they are more or less similar to what follows:

public class RepositoryBase<T>
{
    private UoW _uow;
    public RepositoryBase(UoW uow) // injecting UoW instance via constructor
    {
       _uow = uow;
    }
    public void Add(T entity)
    {
       // Add logic here
    }
    // +other CRUD methods
}

public class UoW
{
    // Holding one repository per domain entity

    public RepositoryBase<Order> OrderRep { get; set; }
    public RepositoryBase<Customer> CustomerRep { get; set; }
    // +other repositories

    public void Commit()
    {
       // Psedudo code: 
       For all the contained repositories do:
           store repository changes.
    }
}

Now my problem:

UoW exposes public method Commit to store the changes. Also, because each repository has a shared instance of UoW, each Repository can access method Commit on UoW. Calling it by one repository makes all other repositories store their changes too; hence the result the whole concept of transaction collapses:

class Repository<T> : RepositoryBase<T>
{
    private UoW _uow;
    public void SomeMethod()
    {
        // some processing or data manipulations here
        _uow.Commit(); // makes other repositories also save their changes
    }
}

I think this must be not allowed. Considering the purpose of the UoW (business transaction), the method Commit should be exposed only to the one who started a Business Transaction for example Business Layer. What surprised me is that I couldn't find any article addressing this issue. In all of them Commit can be called by any repo being injected.

PS: I know I can tell my developers not to call Commit in a Repository but a trusted Architecture is more reliable than trusted developers!

Alireza
  • 10,237
  • 6
  • 43
  • 59
  • 1
    If you're comfortable with the concept of eventual consistency, you can use domain events to implement the 'transaction'. It's more domain driven, elegant and clean but you need to involve a service bus and make your handlers idempotent – MikeSW Oct 24 '13 at 07:54

8 Answers8

31

I do agree with your concerns. I prefer to have an ambient unit of work, where the outermost function opening a unit of work is the one that decides whether to commit or abort. Functions called can open a unit of work scope which automatically enlists in the ambient UoW if there is one, or creates a new one if there is none.

The implementation of the UnitOfWorkScope that I used is heavily inspired by how TransactionScope works. Using an ambient/scoped approach also removes the need for dependency injection.

A method that performs a query looks like this:

public static Entities.Car GetCar(int id)
{
    using (var uow = new UnitOfWorkScope<CarsContext>(UnitOfWorkScopePurpose.Reading))
    {
        return uow.DbContext.Cars.Single(c => c.CarId == id);
    }
}

A method that writes looks like this:

using (var uow = new UnitOfWorkScope<CarsContext>(UnitOfWorkScopePurpose.Writing))
{
    Car c = SharedQueries.GetCar(carId);
    c.Color = "White";
    uow.SaveChanges();
}

Note that the uow.SaveChanges() call will only do an actual save to the database if this is the root (otermost) scope. Otherwise it is interpreted as an "okay vote" that the root scope will be allowed to save the changes.

The entire implementation of the UnitOfWorkScope is available at: http://coding.abel.nu/2012/10/make-the-dbcontext-ambient-with-unitofworkscope/

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • 3
    I read it and I really relieved to see someone has similar concern. But I wonder why not using `TransactionScope`? It is more convenient and makes the architecture more flexible to be expanded and changed later. Moreover, you blocked the call to `SaveChanges` in run-time for a `ReadOnly UoW`. That's okay but I have bad feeling about it. Honestly, I think what repositories consume as a `UoW` shouldn't expose `SaveChanges` to them. – Alireza Oct 28 '13 at 19:13
  • A key feature of the UoW is to ensure that all entities loaded within a business transaction are loaded by the same UoW/DBContext. TransactionScope is not enough for that. Regarding SaveChanges: Another design could be two classes; one for reading and one for writing, that both used the same ambient DBContext. – Anders Abel Oct 29 '13 at 08:17
  • What happens if a developer creates a nested `UnitOfWorkScope` with inner and outer both having `UnitOfWorkScopePurpose.Writing`? If the outer `UnitOfWork` is aborted does the inner still get saved? – Colin Nov 01 '13 at 13:48
  • It is only the outermost UnitOfWorkScope that will actually save changes and it will only be allowed if all child scopes have "voted yes" by calling `SaveChanges()`. If any child scope failed to call `SaveChanges()`, e.g. because of an exception, nothing will be saved. – Anders Abel Nov 01 '13 at 14:42
  • @AndersAbel, What about using UnitOfWork starting Transaction in Action filter - OnActionExecuting and UnitOfWork committing in OnActionExecuted? – Maulik Modi Apr 16 '21 at 15:11
  • @AndersAbel thanks for sharing, I used this to implement my TransactionScope that works directly with IDbConnection without EF. I have 1 question though, if a `[ThreadStatic]` field (connection in my case) is not set to null after being disposed, when a new thread is started (new request in ASP .NET web api application) it still has the previous value , is this expected behaviour? Because the `ThreadStatic` docs say *"If you do not specify an initial value, you can rely on the field being initialized to its default value if it is a value type, or to null if it is a reference type."* – guxxo Aug 20 '21 at 10:56
13

Make your repositories members of your UoW. Don't let your repositories 'see' your UoW. Let UoW handle the transaction.

Chalky
  • 1,624
  • 18
  • 20
  • 1
    My answer is poor. Please ignore. So, how to handle nested business transactions? I've been pondering this for a while. This is a thought, not an answer, as it's not tested: Should the controller perform the commit, and then don't make the commit available to the repositories/service objects? – Chalky Nov 04 '14 at 22:28
  • 1
    You've been re-thinking too much IMO. There is no "standard" (or perfect) implementation of UoW because the implementation depends, to some extent, on the ORM being used. But I think you have captured the intent of the pattern very well in your answer. – David Kirkland Dec 08 '15 at 09:58
  • True. I think my answer is OK, in a non-DDD situation, where you've got navigation properties, and you have transactions spanning multiple repositories. Something not focussed on much: Fowler's repository definition is 'business objects'. – Chalky Dec 11 '16 at 21:40
4

Don't pass in the UnitOfWork, pass in an interface that has the methods you need. You can still implement that interface in the original concrete UnitOfWork implementation if you want:

public interface IDbContext
{
   void Add<T>(T entity);
}

public interface IUnitOfWork
{
   void Commit();
}

public class UnitOfWork : IDbContext, IUnitOfWork
{
   public void Add<T>(T entity);
   public void Commit();
}

public class RepositoryBase<T>
{
    private IDbContext _c;

    public RepositoryBase(IDbContext c) 
    {
       _c = c;
    }

    public void Add(T entity)
    {
       _c.Add(entity)
    }
}

EDIT

After posting this I had a rethink. Exposing the Add method in the UnitOfWork implementation means it is a combination of the two patterns.

I use Entity Framework in my own code and the DbContext used there is described as "a combination of the Unit-Of-Work and Repository pattern".

I think it is better to split the two, and that means I need two wrappers around DbContext one for the Unit Of Work bit and one for the Repository bit. And I do the repository wrapping in RepositoryBase.

The key difference is that I do not pass the UnitOfWork to the Repositories, I pass the DbContext. That does mean that the BaseRepository has access to a SaveChanges on the DbContext. And since the intention is that custom repositories should inherit BaseRepository, they get access to a DbContext too. It is therefore possible that a developer could add code in a custom repository that uses that DbContext. So I guess my "wrapper" is a bit leaky...

So is it worth creating another wrapper for the DbContext that can be passed to the repository constructors to close that off? Not sure that it is...

Examples of passing the DbContext:

Implementing the Repository and Unit of Work

Repository and Unit of Work in Entity Framework

John Papa's original source code

Community
  • 1
  • 1
Colin
  • 22,328
  • 17
  • 103
  • 197
  • May be the only solution is this. What is your experience? What do you do in your projects? Have you ever noticed this problem or do you event consider this an issue. – Alireza Oct 29 '13 at 13:46
  • I've tended to follow the usual pattern but I do think you have a valid point. – Colin Oct 29 '13 at 14:05
  • The bad thing about passing `DBContext` or `ObjectContext` is that you cannot access other `Repositories` within any repository. Suppose one `Repository` has it's special and own way of storing the related entity. Simply adding that entity to the DBContext means falsely bypassing the related Repository and logic. – Alireza Nov 01 '13 at 13:54
  • 1
    @Alireza I prefer that my repositories cannot access one another. They do nothing more than CRUD and any special logic goes into my service classes – Colin Nov 01 '13 at 13:58
  • Hmmm, One thing I strive to achieve is to keep any `IQueriable` object within DAL (or DAL implementation) and not exposing it to Service layer. This way I can take advantage of the innate power of `IQueriable` (If DAL implementation is based on EF) and meanwhile make the user layer(s) completely ignorant of how DAL works and what methods it supports and what it doesn't support. Not only because of this, but also in general I think Repositories can talk to each other – Alireza Nov 01 '13 at 14:05
  • @Alireza you can't access other repositories, but you can access all the `DbSets` on `DbContext`....oh bother – Colin Nov 01 '13 at 14:22
  • Exactly, and I think this is bad :) – Alireza Nov 01 '13 at 14:32
  • So...you will have two "Unit-Of-Work" implementations. The difference being that one has a `SaveChanges` and one doesn't? – Colin Nov 01 '13 at 14:55
  • UoW is a wrapper around DBContext. If you consider DBContext as just another UoW then yes we have two UoW, – Alireza Nov 01 '13 at 15:43
  • @Alireza I was unclear. You need two wrappers around DbContext. One is a UOW that exposes SaveChanges and Repositories to the Service layer. The other exposes generic CRUD methods and Repositories to the Repositories. So they will be similar. – Colin Nov 01 '13 at 17:10
  • Repositories themselves expose whatever they support such as CRUD so I don't think it's necessary to tailor UoW to expose the methods again(however, I admit all of these varies from on architecture to the other depending on implementer idea). On the other hand, wrapping a shared instance of DBContext by two classes is the only descent solution I've found so far to solve the problem. The only difference is that one of the wrappers dose not provide SaveChanges/Commit. – Alireza Nov 01 '13 at 18:56
3

Realize it has been a while since this was asked, and people may have died of old age, transferred to management etc. but here goes.

Taking inspiration from databases, transaction controllers and the two phase commit protocol, the following changes to the patterns should work for you.

  1. Implement the unit of work interface described in Fowler's P of EAA book, but inject the repository into each UoW method.
  2. Inject the unit of work into each repository operation.
  3. Each repository operation calls the appropriate UoW operation and injects itself.
  4. Implement the two phase commit methods CanCommit(), Commit() and Rollback() in the repositories.
  5. If required, commit on the UoW can run Commit on each repository or it can commit to the data store itself. It can also implement a 2 phase commit if that is what you want.

Having done this, you can support a number of different configurations depending on how you implement the repositories and the UoW. e.g. from simple data store without transactions, single RDBMs, multiple heterogeneous data stores etc. The data stores and their interactions can be either in the repositories or in the UoW, as the situation requires.

interface IEntity
{
    int Id {get;set;}
}

interface IUnitOfWork()
{
    void RegisterNew(IRepsitory repository, IEntity entity);
    void RegisterDirty(IRepository respository, IEntity entity);
    //etc.
    bool Commit();
    bool Rollback();
}

interface IRepository<T>() : where T : IEntity;
{
    void Add(IEntity entity, IUnitOfWork uow);
    //etc.
    bool CanCommit(IUnitOfWork uow);
    void Commit(IUnitOfWork uow);
    void Rollback(IUnitOfWork uow);
}

User code is always the same regardless of the DB implementations and looks like this:

// ...
var uow = new MyUnitOfWork();

repo1.Add(entity1, uow);
repo2.Add(entity2, uow);
uow.Commit();

Back to the original post. Because we are method injecting the UoW into each repo operation the UoW does not need to be stored by each repository, meaning Commit() on the Repository can be stubbed out, with Commit on the UoW doing the actual DB commit.

Patrick Farry
  • 101
  • 1
  • 5
  • You provide sensible suggestions to the question, however, keep in mind that the *Question/Answer* format adopted by *Stackoverflow* is not that of a *discussion usergroup/forum*, so your answer could actually be improved by removing some unnecessary comments. Still, +1. – Patrick Trentin Oct 25 '17 at 21:10
2

In .NET, data access components typically automatically enlist to ambient transactions. Hence, saving changes intra-transactionally becomes separated from comitting the transaction to persist the changes.

Put differently - if you create a transaction scope you can let the developers save as much as they want. Not until the transaction is committed the observable state of the database(s) will be updated (well, what is observable depends on the transaction isolation level).

This shows how to create a transaction scope in c#:

using (TransactionScope scope = new TransactionScope())
{
    // Your logic here. Save inside the transaction as much as you want.

    scope.Complete(); // <-- This will complete the transaction and make the changes permanent.
}
lightbricko
  • 2,649
  • 15
  • 21
  • It may be a workaround. The `TransactionScope` should wrap the whole UoW class(not just the Commit method). However, instead of looking for a workaround I'm looking for why folks didn't notice this issue or may be I am wrong. Thank you very much anyway – Alireza Oct 23 '13 at 18:04
  • 1
    Encapsulating data manipulations in a transaction like this is common practice and this is how I do it myself. I have never regarded this as a 'workaround' but rather as one simple (and important) part of the code. To me, calling this a workaround is similar to saying 'the data type int is just a workaround used because strings don't work well with multiplication'. – lightbricko Oct 23 '13 at 18:13
  • Where do you create the TransactionScope? In Commit method? – Alireza Oct 23 '13 at 18:16
  • No, I don't create the transaction scope in the commit method. It depends on the application architecture. Currently I develop an application that is using the [Command Query Responsibility Segregation (CQRS) pattern](http://en.wikipedia.org/wiki/CQRS#Command_Query_Responsibility_Segregation). In this application I create a transaction scope already when a command is received on the server. – lightbricko Oct 23 '13 at 18:23
  • I agree that your way works when back-end storage is a transaction-supporting data source such as MSSqlServer. But what if one repository works on a source like ordinary file which does not support transaction? Moreover, what if on UoW.Commit an specific checking should be done that if RepoA has dirty data and RepoB has too, then RepoC should store a report somewhere? Clearly, if RepoA has saved itself sooner, on a later call to Commit it will be assumed that RepoA has nothing changed and no report will be generated. – Alireza Oct 23 '13 at 18:36
  • Regarding the reports you generate: Since I don't know how your system works, I can't answer that. Regarding resources that do not support transactions: You are correct that I assumed that the resources supported transactions. After all, the title of this question has "... Bussiness Transaction Concept" in it. There are different ways to work with non-transactional resources. One way with the file system example you had is to add transaction support for it, such as using http://transactionalfilemgr.codeplex.com Note that I have not used it though. – lightbricko Oct 23 '13 at 18:39
  • It was only a hypothetical example to demonstrate the failure of the proposed solution. I just want to say if one Repository sooner calls Commit whether in a transaction scope or not, it may cause some undesirable result – Alireza Oct 23 '13 at 18:43
2

I too have been recently researching this design pattern and by utilizing the Unit Of Work and Generic Repository Pattern I was able to extract the Unit of Work "Save Changes" for the Repository implementation. My code is as follows:

public class GenericRepository<T> where T : class
{
  private MyDatabase _Context;
  private DbSet<T> dbset;

  public GenericRepository(MyDatabase context)
  {
    _Context = context;
    dbSet = context.Set<T>();
  }

  public T Get(int id)
  {
    return dbSet.Find(id);
  }

  public IEnumerable<T> GetAll()
  {
    return dbSet<T>.ToList();
  }

  public IEnumerable<T> Where(Expression<Func<T>, bool>> predicate)
  {
    return dbSet.Where(predicate);
  }
  ...
  ...
}

Essentially all we are doing is passing in the data context and utilizing the entity framework's dbSet methods for basic Get, GetAll, Add, AddRange, Remove, RemoveRange, and Where.

Now we will create a generic interface to expose these methods.

public interface <IGenericRepository<T> where T : class
{
  T Get(int id);
  IEnumerable<T> GetAll();
  IEnumerabel<T> Where(Expression<Func<T, bool>> predicate);
  ...
  ...
}

Now we would want to create an interface for each entity in entity Framework and inherit from IGenericRepository so that the interface will expect to have the method signatures implemented within the inherited repositories.

Example:

public interface ITable1 : IGenericRepository<table1>
{
}

You will follow this same pattern with all of your entities. You will also add any function signatures in these interfaces that are specific to the entities. This would result in the repositories needing to implement the GenericRepository methods and any custom methods defined in the interfaces.

For the Repositories we will implement them like this.

public class Table1Repository : GenericRepository<table1>, ITable1
{
  private MyDatabase _context;

  public Table1Repository(MyDatabase context) : base(context)
  {
    _context = context;
  }
} 

In the example repository above I am creating the table1 repository and inheriting the GenericRepository with a type of "table1" then I inherit from the ITable1 interface. This will automatically implement the generic dbSet methods for me, thus allowing me to only focus on my custom repository methods if any. As I pass the dbContext to the constructor I must also pass the dbContext to the base Generic Repository as well.

Now from here I will go and create the Unit of Work repository and Interface.

public interface IUnitOfWork
{
  ITable1 table1 {get;}
  ...
  ...
  list all other repository interfaces here.

  void SaveChanges();
} 

public class UnitOfWork : IUnitOfWork
{
  private readonly MyDatabase _context;
  public ITable1 Table1 {get; private set;}

  public UnitOfWork(MyDatabase context)
  {
    _context = context; 

    // Initialize all of your repositories here
    Table1 = new Table1Repository(_context);
    ...
    ...
  }

  public void SaveChanges()
  {
    _context.SaveChanges();
  }
}

I handle my transaction scope on a custom controller that all other controllers in my system inherit from. This controller inherits from the default MVC controller.

public class DefaultController : Controller
{
  protected IUnitOfWork UoW;

  protected override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    UoW = new UnitOfWork(new MyDatabase());
  }

  protected override void OnActionExecuted(ActionExecutedContext filterContext) 
  {
    UoW.SaveChanges();
  }
}

By implementing your code this way. Every time a request is made to the server at the beginning of an action a new UnitOfWork will be created and will automatically create all the repositories and make them accessible to the UoW variable in your controller or classes. This will also remove your SaveChanges() from your repositories and place it within the UnitOfWork repository. And last this pattern is able to utilize only a single dbContext throughout the system via dependency injection.

If you are concerned about parent/child updates with a singular context you could utilize stored procedures for your update, insert, and delete functions and utilize entity framework for your access methods.

logan gilley
  • 191
  • 2
  • 5
1

In a very simple application

In some applications, the domain model and the database entities are identical, and there is no need to do any data mapping between them. Let's call them "domain entities". In such applications, the DbContext can act both as a repository and a unit of work simultaneously. Instead of doing some complicated patterns, we can simply use the context:

public class CustomerController : Controller
{
    private readonly CustomerContext context; // injected

    [HttpPost]
    public IActionResult Update(CustomerUpdateDetails viewmodel)
    {
        // [Repository] acting like an in-memory domain object collection
        var person = context.Person.Find(viewmodel.Id);

        // [UnitOfWork] keeps track of everything you do during a business transaction
        person.Name = viewmodel.NewName;
        person.AnotherComplexOperationWithBusinessRequirements();

        // [UnitOfWork] figures out everything that needs to be done to alter the database
        context.SaveChanges();
    }
}

Complex queries on larger apps

If your application gets more complex, you'll start writing some large Linq queries in order to access your data. In that situation, you'll probably need to introduce a new layer that handle these queries, in order to prevent yourself from copy pasting them across your controllers. In that situation, you'll end up having two different layers, the unit of work pattern implemented by the DbContext, and the repository pattern that will simply provide some Linq results executing over the former. Your controller is expected to call the repository to get the entities, change their state and then call the DbContext to persist the changes to the database, but proxying the DbContext.SaveChanges() through the repository object is an acceptable approximation:

public class PersonRepository
{
    private readonly PersonDbContext context;
    public Person GetClosestTo(GeoCoordinate location) {} // redacted
}
public class PersonController
{
    private readonly PersonRepository repository;
    private readonly PersonDbContext context; // requires to Equals repository.context

    public IActionResult Action()
    {
        var person = repository.GetClosestTo(new GeoCoordinate());
        person.DoSomething();
        context.SaveChanges();
        // repository.SaveChanges(); would save the injection of the DbContext
    }
}

DDD applications

It gets more interesting when domain models and entities are two different group of classes. This will happen when you will start implementing DDD, as this requires you to define some aggregates, which are clusters of domain objects that can be treated as a single unit. The structure of aggregates does not always perfectly map to your relational database schema, as it can provides multiple level of abstractions depending on the use case you're dealing with.

For instance, an aggregate may allow a user to manage multiple addresses, but in another business context you'll might want to flatten the model and limit the modeling of the person's address to the latest value only:

public class PersonEntity
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public bool IsValid { get; set; }
    public ICollection<AddressEntity> Addresses { get; set; }
}

public class AddressEntity
{
    [Key]
    public int Id { get; set; }
    public string Value { get; set; }
    public DateTime Since { get; set; }
    public PersonEntity Person { get; set; }
}

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string CurrentAddressValue { get; private set; }
}

Implementing the unit of work pattern

First let's get back to the definition:

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.

The DbContext keeps tracks of every modification that happens to entities and will persist them to the database once you call the SaveChanges() method. Like in the simpler example, unit of work is exactly what the DbContext does, and using it as a unit of work is actually how Microsoft suggest you'd structure a .NET application using DDD.

Implementing the repository pattern

Once again, let's get back to the definition:

A repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.

The DbContext, cannot act as a repository. Although it behaves as an in-memory collection of entities, it does not act as an in-memory collection of domain objects. In that situation, we must implement another class for the repository, that will act as our in-memory collection of domain models, and will map data from entities to domain models. However, you will find a lot of implementations that are simply a projection of the DbSet in the domain model and provide IList-like methods that simply maps entities back and reproduce the operations on the DbSet<T>.

Although this implementation might be valid in multiple situations, it overemphasizes over the collection part, and not enough on the mediator part of the definition.

A repository is a mediator between the domain layer and the infrastructure layer, which means its interface is defined in the domain layer. Methods described in the interface are defined in the domain layer, and they all must have a meaning in the business context of the program. Ubiquitous language being a central concept of DDD, these methods must provide a meaningful name, and perhaps "adding a person" is not the right business way to name this operation.

Also, all persistence-related concepts are strictly limited to the implementation of the repository. The implementation defines how a given business operation translates in the infrastructure layer, as a series of entities manipulation that will eventually be persisted to the database through an atomic database transaction. Also note that the Add operation on a domain model does not necessarily implies an INSERT statement in the database and a Remove will sometimes end up in an UPDATE or even multiple INSERT statements !

Actually, here is a pretty valid implementation of a repository pattern:

public class Person
{
    public void EnsureEnrollable(IPersonRepository repository)
    {
        if(!repository.IsEnrollable(this))
        {
            throw new BusinessException<PersonError>(PersonError.CannotEnroll);
        }
    }
}
public class PersonRepository : IPersonRepository
{
    private readonly PersonDbContext context;

    public IEnumerable<Person> GetAll()
    {
        return context.Persons.AsNoTracking()
            .Where(person => person.Active)
            .ProjectTo<Person>().ToList();
    }

    public Person Enroll(Person person)
    {
        person.EnsureEnrollable(this);
        context.Persons.Find(person.Id).Active = true;
        context.SaveChanges(); // UPDATE statement
        return person;
    }

    public bool IsEnrollable(Person person)
    {
        return context.Persons.Any(entity => entity.Id == person.Id && !entity.Active);
    }
}

Business transaction

You're saying a purpose of using unit of work is to form a Business Transaction, which is wrong. The purpose of the unit of work class is to keeps track of everything you do during a business transaction that can affect the database, to alter the database as a result of your work in an atomic operation. The repositories do share the unit of work instances, but bear in mind that dependency injection usually uses a scoped lifetime manager when injecting dbcontext. This means that instances are only shared within the same http request context, and different requests will not share changes tracking. Using a singleton lifetime manager will share instances among different http request which will provoke havoc in your application.

Calling the unit of work save changes method from a repository is actually how you are expected to implementation a DDD application. The repository is the class that knows about the actual implementation of the persistence layer, and that will orchestrate all database operations to commit/rollback at the end of transaction. Saving changes from another repository when calling save changes is also the expected behavior of the unit of work pattern. The unit of work accumulates all changes made by all repositories until someone calls a commit or a rollback. If a repository makes changes to the context that are not expected to be persisted in the database, then the problem is not the unit of work persisting these changes, but the repository doing these changes.

However, if your application does one atomic save changes that persists change operations from multiple repositories, it probably violates one of the DDD design principles. A repository is a one-to-one mapping with an aggregate, and an aggregate is a cluster of domain objects that can be treated as a single unit. If you are using multiple repositories, then you are trying to modify multiple units of data in a single transaction.

Either your aggregate is designed too small, and you need to make a larger one that holds all data for your single transaction, with a repository that will handle all that data in a single transaction ; either you're trying to make a complex transaction that spans over a wide part of your model, and you will need to implement this transaction with eventual consistency.

ArwynFr
  • 1,383
  • 7
  • 13
0

Yes, this question is a concern to me, and here's how I handle it.

First of all, in my understanding Domain Model should not know about Unit of Work. Domain Model consists of interfaces (or abstract classes) that don't imply the existence of the transactional storage. In fact, it does not know about the existence of any storage at all. Hence the term Domain Model.

Unit of Work is present in the Domain Model Implementation layer. I guess this is my term, and by that I mean a layer that implements Domain Model interfaces by incorporating Data Access Layer. Usually, I use ORM as DAL and therefore it comes with built-in UoW in it (Entity Framework SaveChanges or SubmitChanges method to commit the pending changes). However, that one belongs to DAL and does not need any inventor's magic.

On the other hand, you are referring to the UoW that you need to have in Domain Model Implementation layer because you need to abstract away the part of "committing changes to DAL". For that, I would go with Anders Abel's solution (recursive scropes), because that addresses two things you need to solve in one shot:

  • You need to support saving of aggregates as one transaction, if the aggregate is an initiator of the scope.
  • You need to support saving of aggregates as part of the parent transaction, if the aggregate is not the initiator of the scope, but is part of it.
Tengiz
  • 8,011
  • 30
  • 39