From fba2ce8a7af76be5ab2b2fd342e230b1026dfcd2 Mon Sep 17 00:00:00 2001 From: sitereactor Date: Tue, 6 Nov 2012 13:07:18 -0100 Subject: [PATCH] Refactoring the FileRepository to implement IUnitOfWorkRepository, so the files use the same transaction approach as database entities. --- src/Umbraco.Core/Models/IFile.cs | 2 +- .../Repositories/FileRepository.cs | 61 ++++++++++-- .../Repositories/TemplateRepository.cs | 95 +++++++++++-------- 3 files changed, 110 insertions(+), 48 deletions(-) diff --git a/src/Umbraco.Core/Models/IFile.cs b/src/Umbraco.Core/Models/IFile.cs index 25415328e8..64706e4bef 100644 --- a/src/Umbraco.Core/Models/IFile.cs +++ b/src/Umbraco.Core/Models/IFile.cs @@ -6,7 +6,7 @@ namespace Umbraco.Core.Models /// Defines a File /// /// Used for Scripts, Stylesheets and Templates - public interface IFile : IValueObject + public interface IFile : IEntity { /// /// Gets the Name of the File including extension diff --git a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs index 6d207b5784..b1a28a74cd 100644 --- a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs @@ -3,11 +3,12 @@ using System.IO; using System.Text; using Umbraco.Core.IO; using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Persistence.Repositories { - internal abstract class FileRepository : IRepository + internal abstract class FileRepository : IUnitOfWorkRepository, IRepository where TEntity : IFile { private IUnitOfWork _work; @@ -36,19 +37,14 @@ namespace Umbraco.Core.Persistence.Repositories public virtual void AddOrUpdate(TEntity entity) { - var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); - FileSystem.AddFile(entity.Name, stream, true); + _work.RegisterAdded(entity, this); } public virtual void Delete(TEntity entity) { - if (_fileSystem.FileExists(entity.Name)) + if (_work != null) { - _fileSystem.DeleteFile(entity.Name); - } - else if(_fileSystem.FileExists(entity.Path)) - { - _fileSystem.DeleteFile(entity.Path); + _work.RegisterRemoved(entity, this); } } @@ -67,5 +63,52 @@ namespace Umbraco.Core.Persistence.Repositories } #endregion + + #region Implementation of IUnitOfWorkRepository + + public void PersistNewItem(IEntity entity) + { + PersistNewItem((TEntity)entity); + } + + public void PersistUpdatedItem(IEntity entity) + { + PersistUpdatedItem((TEntity)entity); + } + + public void PersistDeletedItem(IEntity entity) + { + PersistDeletedItem((TEntity)entity); + } + + #endregion + + #region Abstract IUnitOfWorkRepository Methods + + protected virtual void PersistNewItem(TEntity entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + FileSystem.AddFile(entity.Name, stream, true); + } + + protected virtual void PersistUpdatedItem(TEntity entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + FileSystem.AddFile(entity.Name, stream, true); + } + + protected virtual void PersistDeletedItem(TEntity entity) + { + if (_fileSystem.FileExists(entity.Name)) + { + _fileSystem.DeleteFile(entity.Name); + } + else if (_fileSystem.FileExists(entity.Path)) + { + _fileSystem.DeleteFile(entity.Path); + } + } + + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs index a987153600..d420be0596 100644 --- a/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs @@ -25,44 +25,6 @@ namespace Umbraco.Core.Persistence.Repositories #region Overrides of FileRepository - public override void AddOrUpdate(Template entity) - { - var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); - if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) - { - _viewsFileSystem.AddFile(entity.Name, stream, true); - } - else - { - FileSystem.AddFile(entity.Name, stream, true); - } - - entity.ResetDirtyProperties(); - } - - public override void Delete(Template entity) - { - //Check for file under the Masterpages filesystem - if (FileSystem.FileExists(entity.Name)) - { - FileSystem.DeleteFile(entity.Name); - } - else if (FileSystem.FileExists(entity.Path)) - { - FileSystem.DeleteFile(entity.Path); - } - - //Check for file under the Views/Mvc filesystem - if (_viewsFileSystem.FileExists(entity.Name)) - { - _viewsFileSystem.DeleteFile(entity.Name); - } - else if (_viewsFileSystem.FileExists(entity.Path)) - { - _viewsFileSystem.DeleteFile(entity.Path); - } - } - public override Template Get(string id) { string masterpageName = string.Concat(id, ".master"); @@ -144,5 +106,62 @@ namespace Umbraco.Core.Persistence.Repositories } #endregion + + #region Abstract IUnitOfWorkRepository Methods + + protected override void PersistNewItem(Template entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) + { + _viewsFileSystem.AddFile(entity.Name, stream, true); + } + else + { + FileSystem.AddFile(entity.Name, stream, true); + } + + entity.ResetDirtyProperties(); + } + + protected override void PersistUpdatedItem(Template entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + if (UmbracoSettings.DefaultRenderingEngine == RenderingEngine.Mvc) + { + _viewsFileSystem.AddFile(entity.Name, stream, true); + } + else + { + FileSystem.AddFile(entity.Name, stream, true); + } + + entity.ResetDirtyProperties(); + } + + protected override void PersistDeletedItem(Template entity) + { + //Check for file under the Masterpages filesystem + if (FileSystem.FileExists(entity.Name)) + { + FileSystem.DeleteFile(entity.Name); + } + else if (FileSystem.FileExists(entity.Path)) + { + FileSystem.DeleteFile(entity.Path); + } + + //Check for file under the Views/Mvc filesystem + if (_viewsFileSystem.FileExists(entity.Name)) + { + _viewsFileSystem.DeleteFile(entity.Name); + } + else if (_viewsFileSystem.FileExists(entity.Path)) + { + _viewsFileSystem.DeleteFile(entity.Path); + } + } + + #endregion } } \ No newline at end of file