From 94c82c7cf76a8aa37bdfe4247e5d86addd27b908 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 3 May 2016 10:51:06 +0200 Subject: [PATCH] U4-6147 - cleanup units of work --- .../Persistence/UnitOfWork/FileUnitOfWork.cs | 9 +++------ .../Persistence/UnitOfWork/IUnitOfWork.cs | 7 +++++-- .../Persistence/UnitOfWork/NPocoUnitOfWork.cs | 5 ++--- .../Persistence/UnitOfWork/UnitOfWorkBase.cs | 11 +++++++++-- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs index d7562e716f..7a0fe55fda 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs @@ -8,17 +8,14 @@ /// mechanism. internal class FileUnitOfWork : UnitOfWorkBase { - private readonly RepositoryFactory _factory; - /// /// Initializes a new instance of the class with a a repository factory. /// /// A repository factory. /// This should be used by the FileUnitOfWorkProvider exclusively. public FileUnitOfWork(RepositoryFactory factory) - { - _factory = factory; - } + : base(factory) + { } /// /// Creates a repository. @@ -28,7 +25,7 @@ /// The created repository for the unit of work. public override TRepository CreateRepository(string name = null) { - return _factory.CreateRepository(this, name); + return Factory.CreateRepository(this, name); } } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/IUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/IUnitOfWork.cs index a3ec517546..0f8d775020 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/IUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/IUnitOfWork.cs @@ -33,7 +33,7 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// /// Begins the unit of work. /// - /// When a unit of work begins, a local transaction scope is created at database level. + /// When a unit of work begins, a local transaction scope is created at database level. /// This is useful eg when reading entities before creating, updating or deleting, and the read /// needs to be part of the transaction. Flushing or completing the unit of work automatically /// begins the transaction (so no need to call Begin if not necessary). @@ -58,8 +58,11 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// before it is disposed, all queued operations are cleared and the scope is rolled back (and also /// higher level transactions if any). /// Whether this actually commits or rolls back the transaction depends on whether the transaction scope - /// is part of a higher level transactions. The database transaction is committed or rolled back only + /// is part of a higher level transactions. The database transaction is committed or rolled back only /// when the upper level scope is disposed. + /// If any operation is added to the unit of work after it has been completed, then its completion + /// status is resetted. So in a way it could be possible to always complete and never flush, but flush + /// is preferred when appropriate to indicate that you understand what you are doing. /// void Complete(); diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/NPocoUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/NPocoUnitOfWork.cs index 8e68f9ade3..08550c2b76 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/NPocoUnitOfWork.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/NPocoUnitOfWork.cs @@ -9,7 +9,6 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// internal class NPocoUnitOfWork : UnitOfWorkBase, IDatabaseUnitOfWork { - private readonly RepositoryFactory _factory; private ITransaction _transaction; /// @@ -19,9 +18,9 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// A repository factory. /// This should be used by the NPocoUnitOfWorkProvider exclusively. internal NPocoUnitOfWork(UmbracoDatabase database, RepositoryFactory factory) + : base(factory) { Database = database; - _factory = factory; } /// @@ -37,7 +36,7 @@ namespace Umbraco.Core.Persistence.UnitOfWork /// The created repository for the unit of work. public override TRepository CreateRepository(string name = null) { - return _factory.CreateRepository(this, name); + return Factory.CreateRepository(this, name); } /// diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/UnitOfWorkBase.cs b/src/Umbraco.Core/Persistence/UnitOfWork/UnitOfWorkBase.cs index 16fd84aa73..98fd1fa6d8 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/UnitOfWorkBase.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/UnitOfWorkBase.cs @@ -9,8 +9,15 @@ namespace Umbraco.Core.Persistence.UnitOfWork { private readonly Queue _operations = new Queue(); - // fixme eventually kill this - public virtual T CreateRepository(string name = null) where T:IRepository { throw new NotImplementedException(); } + protected UnitOfWorkBase(RepositoryFactory factory) + { + Factory = factory; + } + + protected RepositoryFactory Factory { get; } + + public abstract TRepository CreateRepository(string name = null) + where TRepository : IRepository; /// /// Registers an entity to be added as part of this unit of work.