Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bernard Moes 13 posts 134 karma points
    Jul 06, 2017 @ 08:00
    Bernard Moes
    0

    Unit of work

    I used to work with Entity Framework and made use of the Unit of Work pattern to store all changes during a request and commit them in a single transaction. Right now I am writing an Umbraco package which frequently uses the Umbraco code API to store or delete data from the database. For example the RelationService. Umbraco automatically saves changes when I call RelationService.Relate or RelationService.Delete.

    I want to temporarily store these queries and execute them all in a single transaction when I want to commit my changes. Is this possible at all?

    I see the RelationService internally uses Unit of Work and immediately commits changes. Can I somehow wrap another UoW around it and surpress commits made by the RelationService? How can I access the Unit of Work?

    Umbraco RelationService:

     using (var uow = UowProvider.GetUnitOfWork())
            {
                var repository = RepositoryFactory.CreateRelationRepository(uow);
                if (uow.Events.DispatchCancelable(SavingRelation, this, new SaveEventArgs<IRelation>(relation)))
                {
                    uow.Commit();
                    return relation;
                }
    
                repository.AddOrUpdate(relation);
                uow.Commit();
                uow.Events.Dispatch(SavedRelation, this, new SaveEventArgs<IRelation>(relation, false));
                return relation;
            }
    
  • Bernard Moes 13 posts 134 karma points
    Jul 10, 2017 @ 09:30
    Bernard Moes
    1

    Unfortunately still didn't find a solution. It seems like the only way to solve this is by bypassing the Umbraco code API and firing custom SQL queries.

  • Ronald Barendse 39 posts 218 karma points hq c-trib
    Apr 19, 2018 @ 13:05
    Ronald Barendse
    0

    Umbraco has lots of events that fire on save, delete, etc. If you're surpressing the actual database saves in your own UoW, those events could be fired before it's actually persisted!

Please Sign in or register to post replies

Write your reply to:

Draft