From e42865ef9d31dc1234df9b2c024972c834d247c5 Mon Sep 17 00:00:00 2001 From: "Morten@Thinkpad-X220" Date: Mon, 8 Oct 2012 10:16:59 -0200 Subject: [PATCH] Adds Unit of Work for working with files U4-975 --- .../Persistence/UnitOfWork/FileUnitOfWork.cs | 145 ++++++++++++++++++ .../UnitOfWork/FileUnitOfWorkProvider.cs | 17 ++ .../UnitOfWork/PetaPocoUnitOfWorkProvider.cs | 3 + src/Umbraco.Core/Umbraco.Core.csproj | 3 + 4 files changed, 168 insertions(+) create mode 100644 src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs create mode 100644 src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWorkProvider.cs diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs new file mode 100644 index 0000000000..03c8e3da4e --- /dev/null +++ b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWork.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Transactions; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Persistence.UnitOfWork +{ + /// + /// Represents the Unit of Work implementation for working with files + /// + internal class FileUnitOfWork : IUnitOfWork + { + private Guid _key; + private List _operations; + + public FileUnitOfWork() + { + _key = Guid.NewGuid(); + _operations = new List(); + } + + #region Implementation of IUnitOfWork + + /// + /// Registers an instance to be added through this + /// + /// The + /// The participating in the transaction + public void RegisterAdded(IEntity entity, IUnitOfWorkRepository repository) + { + _operations.Add( + new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Insert + }); + } + + /// + /// Registers an instance to be changed through this + /// + /// The + /// The participating in the transaction + public void RegisterChanged(IEntity entity, IUnitOfWorkRepository repository) + { + _operations.Add( + new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Update + }); + } + + /// + /// Registers an instance to be removed through this + /// + /// The + /// The participating in the transaction + public void RegisterRemoved(IEntity entity, IUnitOfWorkRepository repository) + { + _operations.Add( + new Operation + { + Entity = entity, + ProcessDate = DateTime.Now, + Repository = repository, + Type = TransactionType.Delete + }); + } + + public void Commit() + { + using(var scope = new TransactionScope()) + { + foreach (var operation in _operations.OrderBy(o => o.ProcessDate)) + { + switch (operation.Type) + { + case TransactionType.Insert: + operation.Repository.PersistNewItem(operation.Entity); + break; + case TransactionType.Delete: + operation.Repository.PersistDeletedItem(operation.Entity); + break; + case TransactionType.Update: + operation.Repository.PersistUpdatedItem(operation.Entity); + break; + } + } + // Commit the transaction + scope.Complete(); + } + + // Clear everything + _operations.Clear(); + _key = Guid.NewGuid(); + } + + public object Key + { + get { return _key; } + } + + #endregion + + #region Operation + + /// + /// Provides a snapshot of an entity and the repository reference it belongs to. + /// + private sealed class Operation + { + /// + /// Gets or sets the entity. + /// + /// The entity. + public IEntity Entity { get; set; } + + /// + /// Gets or sets the process date. + /// + /// The process date. + public DateTime ProcessDate { get; set; } + + /// + /// Gets or sets the repository. + /// + /// The repository. + public IUnitOfWorkRepository Repository { get; set; } + + /// + /// Gets or sets the type of operation. + /// + /// The type of operation. + public TransactionType Type { get; set; } + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWorkProvider.cs b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWorkProvider.cs new file mode 100644 index 0000000000..44010c60ce --- /dev/null +++ b/src/Umbraco.Core/Persistence/UnitOfWork/FileUnitOfWorkProvider.cs @@ -0,0 +1,17 @@ +namespace Umbraco.Core.Persistence.UnitOfWork +{ + /// + /// Represents a Unit of Work Provider for creating a + /// + internal class FileUnitOfWorkProvider : IUnitOfWorkProvider + { + #region Implementation of IUnitOfWorkProvider + + public IUnitOfWork GetUnitOfWork() + { + return new FileUnitOfWork(); + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs index 5fcdc0ad07..647e020b1a 100644 --- a/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs +++ b/src/Umbraco.Core/Persistence/UnitOfWork/PetaPocoUnitOfWorkProvider.cs @@ -1,5 +1,8 @@ namespace Umbraco.Core.Persistence.UnitOfWork { + /// + /// Represents a Unit of Work Provider for creating a + /// internal class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider { #region Implementation of IUnitOfWorkProvider diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 9529a5b973..0187123a98 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -44,6 +44,7 @@ + @@ -116,6 +117,8 @@ + +