Refactoring the FileRepository to implement IUnitOfWorkRepository, so the files use the same transaction approach as database entities.

This commit is contained in:
sitereactor
2012-11-06 13:07:18 -01:00
parent ff51d81540
commit fba2ce8a7a
3 changed files with 110 additions and 48 deletions

View File

@@ -6,7 +6,7 @@ namespace Umbraco.Core.Models
/// Defines a File
/// </summary>
/// <remarks>Used for Scripts, Stylesheets and Templates</remarks>
public interface IFile : IValueObject
public interface IFile : IEntity
{
/// <summary>
/// Gets the Name of the File including extension

View File

@@ -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<TId, TEntity> : IRepository<TId, TEntity>
internal abstract class FileRepository<TId, TEntity> : IUnitOfWorkRepository, IRepository<TId, TEntity>
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
}
}

View File

@@ -25,44 +25,6 @@ namespace Umbraco.Core.Persistence.Repositories
#region Overrides of FileRepository<string,Template>
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
}
}