From 4eee5cfd5f10a34dffbbb997ed8bc89ffe9b7c2e Mon Sep 17 00:00:00 2001 From: "Morten@Thinkpad-X220" Date: Tue, 9 Oct 2012 08:45:15 -0200 Subject: [PATCH] Adds template repository and an abstract file repository U4-967 --- .../Repositories/FileRepository.cs | 71 +++++++++++++++++++ .../Repositories/ITemplateRepository.cs | 8 +++ .../Repositories/ScriptRepository.cs | 53 +++----------- .../Repositories/TemplateRepository.cs | 60 ++++++++++++++++ src/Umbraco.Core/Umbraco.Core.csproj | 3 + 5 files changed, 150 insertions(+), 45 deletions(-) create mode 100644 src/Umbraco.Core/Persistence/Repositories/FileRepository.cs create mode 100644 src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs create mode 100644 src/Umbraco.Core/Persistence/Repositories/TemplateRepository.cs diff --git a/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs new file mode 100644 index 0000000000..a0981caa58 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/FileRepository.cs @@ -0,0 +1,71 @@ +using System.Collections.Generic; +using System.IO; +using System.Text; +using Umbraco.Core.IO; +using Umbraco.Core.Models; +using Umbraco.Core.Persistence.UnitOfWork; + +namespace Umbraco.Core.Persistence.Repositories +{ + internal abstract class FileRepository : IRepository + where TEntity : IFile + { + private IUnitOfWork _work; + private readonly IFileSystem _fileSystem; + + protected FileRepository(IUnitOfWork work, IFileSystem fileSystem) + { + _work = work; + _fileSystem = fileSystem; + } + + /// + /// Returns the Unit of Work added to the repository + /// + protected IUnitOfWork UnitOfWork + { + get { return _work; } + } + + protected IFileSystem FileSystem + { + get { return _fileSystem; } + } + + #region Implementation of IRepository + + public virtual void AddOrUpdate(TEntity entity) + { + var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); + FileSystem.AddFile(entity.Name, stream, true); + } + + public void Delete(TEntity entity) + { + if (_fileSystem.FileExists(entity.Name)) + { + _fileSystem.DeleteFile(entity.Name); + } + else if(_fileSystem.FileExists(entity.Path)) + { + _fileSystem.DeleteFile(entity.Path); + } + } + + public abstract TEntity Get(TId id); + + public abstract IEnumerable GetAll(params TId[] ids); + + public bool Exists(TId id) + { + return _fileSystem.FileExists(id.ToString()); + } + + public void SetUnitOfWork(IUnitOfWork work) + { + _work = work; + } + + #endregion + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs new file mode 100644 index 0000000000..ab3a1def12 --- /dev/null +++ b/src/Umbraco.Core/Persistence/Repositories/ITemplateRepository.cs @@ -0,0 +1,8 @@ +using Umbraco.Core.Models; + +namespace Umbraco.Core.Persistence.Repositories +{ + public interface ITemplateRepository : IRepository + { + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs b/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs index b4a23d9d0f..99b39a6128 100644 --- a/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/ScriptRepository.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Text; using Umbraco.Core.IO; @@ -12,61 +11,35 @@ namespace Umbraco.Core.Persistence.Repositories /// /// Represents the Script Repository /// - internal class ScriptRepository : IScriptRepository + internal class ScriptRepository : FileRepository, IScriptRepository { - private IUnitOfWork _work; - private readonly IFileSystem _fileSystem; - public ScriptRepository(IUnitOfWork work) + : base(work, FileSystemProviderManager.Current.GetFileSystemProvider("script")) { - _work = work; - _fileSystem = FileSystemProviderManager.Current.GetFileSystemProvider("script"); - } - - /// - /// Returns the Unit of Work added to the repository - /// - protected IUnitOfWork UnitOfWork - { - get { return _work; } } #region Implementation of IRepository - public void AddOrUpdate(Script entity) + public override Script Get(string id) { - var stream = new MemoryStream(Encoding.UTF8.GetBytes(entity.Content)); - _fileSystem.AddFile(entity.Name, stream, true); - } - - public void Delete(Script entity) - { - if (_fileSystem.FileExists(entity.Name)) - { - _fileSystem.DeleteFile(entity.Name); - } - } - - public Script Get(string id) - { - if(!_fileSystem.FileExists(id)) + if(!FileSystem.FileExists(id)) { throw new Exception(string.Format("The file {0} was not found", id)); } - var stream = _fileSystem.OpenFile(id); + var stream = FileSystem.OpenFile(id); byte[] bytes = new byte[stream.Length]; stream.Position = 0; stream.Read(bytes, 0, (int)stream.Length); var content = Encoding.UTF8.GetString(bytes); - var path = _fileSystem.GetRelativePath(id); + var path = FileSystem.GetRelativePath(id); var script = new Script(path) {Content = content}; return script; } - public IEnumerable