Fixing an issue with the MediaService.

Making the two UOW Providers public, so they can be used for newing up services.
Adding a few constructor options to the PP UOW Provider to avoid config lookup.
This commit is contained in:
Morten Christensen
2012-12-12 09:00:00 -01:00
parent 41403c13a9
commit e71a5fd5eb
8 changed files with 67 additions and 34 deletions

View File

@@ -263,7 +263,7 @@ namespace Umbraco.Core.Persistence.Repositories
Database.Insert(dto); Database.Insert(dto);
//Create the PropertyData for this version - cmsPropertyData //Create the PropertyData for this version - cmsPropertyData
var propertyFactory = new PropertyFactory(((Content)entity).ContentType, entity.Version, entity.Id); var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id);
var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties);
//Add Properties //Add Properties
foreach (var propertyDataDto in propertyDataDtos) foreach (var propertyDataDto in propertyDataDtos)

View File

@@ -176,12 +176,21 @@ namespace Umbraco.Core.Persistence.Repositories
Database.Insert(dto); Database.Insert(dto);
//Create the PropertyData for this version - cmsPropertyData //Create the PropertyData for this version - cmsPropertyData
var propertyFactory = new PropertyFactory(((Models.Media)entity).ContentType, entity.Version, entity.Id); var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id);
var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties);
var keyDictionary = new Dictionary<int, int>();
//Add Properties //Add Properties
foreach (var propertyDataDto in propertyDataDtos) foreach (var propertyDataDto in propertyDataDtos)
{ {
Database.Insert(propertyDataDto); var primaryKey = Convert.ToInt32(Database.Insert(propertyDataDto));
keyDictionary.Add(propertyDataDto.PropertyTypeId, primaryKey);
}
//Update Properties with its newly set Id
foreach (var property in entity.Properties)
{
property.Id = keyDictionary[property.PropertyTypeId];
} }
((ICanBeDirty)entity).ResetDirtyProperties(); ((ICanBeDirty)entity).ResetDirtyProperties();
@@ -215,7 +224,7 @@ namespace Umbraco.Core.Persistence.Repositories
Database.Insert(dto); Database.Insert(dto);
//Create the PropertyData for this version - cmsPropertyData //Create the PropertyData for this version - cmsPropertyData
var propertyFactory = new PropertyFactory(((Models.Media)entity).ContentType, entity.Version, entity.Id); var propertyFactory = new PropertyFactory(entity.ContentType, entity.Version, entity.Id);
var propertyDataDtos = propertyFactory.BuildDto(entity.Properties); var propertyDataDtos = propertyFactory.BuildDto(entity.Properties);
//Add Properties //Add Properties
foreach (var propertyDataDto in propertyDataDtos) foreach (var propertyDataDto in propertyDataDtos)

View File

@@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// Represents a Unit of Work Provider for creating a <see cref="FileUnitOfWork"/> /// Represents a Unit of Work Provider for creating a <see cref="FileUnitOfWork"/>
/// </summary> /// </summary>
internal class FileUnitOfWorkProvider : IUnitOfWorkProvider public class FileUnitOfWorkProvider : IUnitOfWorkProvider
{ {
#region Implementation of IUnitOfWorkProvider #region Implementation of IUnitOfWorkProvider

View File

@@ -1,19 +1,47 @@
using System; using Umbraco.Core.Configuration;
using System.Threading;
using System.Web;
using Umbraco.Core.Configuration;
namespace Umbraco.Core.Persistence.UnitOfWork namespace Umbraco.Core.Persistence.UnitOfWork
{ {
/// <summary> /// <summary>
/// Represents a Unit of Work Provider for creating a <see cref="PetaPocoUnitOfWork"/> /// Represents a Unit of Work Provider for creating a <see cref="PetaPocoUnitOfWork"/>
/// </summary> /// </summary>
internal class PetaPocoUnitOfWorkProvider : IDatabaseUnitOfWorkProvider public class PetaPocoUnitOfWorkProvider : IDatabaseUnitOfWorkProvider
{ {
private readonly string _connectionString;
private readonly string _providerName;
/// <summary>
/// Parameterless constructor
/// </summary>
public PetaPocoUnitOfWorkProvider()
{
_connectionString = GlobalSettings.UmbracoConnectionName;
}
/// <summary>
/// Constructor to explicitly set the connectionstring to use
/// </summary>
/// <param name="connectionString">Connection String to use</param>
public PetaPocoUnitOfWorkProvider(string connectionString)
{
_connectionString = connectionString;
}
/// <summary>
/// Constructor to explicitly set the connectionstring and provider name to use,
/// which will avoid the lookup of any additional config settings.
/// </summary>
/// <param name="connectionString">Connection String to use</param>
/// <param name="providerName">Database Provider</param>
public PetaPocoUnitOfWorkProvider(string connectionString, string providerName)
{
_connectionString = connectionString;
_providerName = providerName;
}
#region Implementation of IUnitOfWorkProvider #region Implementation of IUnitOfWorkProvider
/// <summary> /// <summary>
/// Creates a Unit of work with a new UmbracoDatabase instance for the work item/transaction. /// Creates a Unit of work with a new UmbracoDatabase instance for the work item/transaction.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
@@ -24,9 +52,10 @@ namespace Umbraco.Core.Persistence.UnitOfWork
/// </remarks> /// </remarks>
public IDatabaseUnitOfWork GetUnitOfWork() public IDatabaseUnitOfWork GetUnitOfWork()
{ {
return new PetaPocoUnitOfWork( var database = string.IsNullOrEmpty(_providerName)
new UmbracoDatabase( ? new UmbracoDatabase(_connectionString)
GlobalSettings.UmbracoConnectionName)); : new UmbracoDatabase(_connectionString, _providerName);
return new PetaPocoUnitOfWork(database);
} }
#endregion #endregion
@@ -37,7 +66,7 @@ namespace Umbraco.Core.Persistence.UnitOfWork
/// <returns></returns> /// <returns></returns>
internal static IDatabaseUnitOfWork CreateUnitOfWork() internal static IDatabaseUnitOfWork CreateUnitOfWork()
{ {
var provider = new PetaPocoUnitOfWorkProvider(); var provider = new PetaPocoUnitOfWorkProvider(GlobalSettings.UmbracoConnectionName);
return provider.GetUnitOfWork(); return provider.GetUnitOfWork();
} }
} }

View File

@@ -8,7 +8,6 @@ using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase; using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Persistence; using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Publishing; using Umbraco.Core.Publishing;
@@ -24,7 +23,10 @@ namespace Umbraco.Core.Services
private readonly IUserService _userService; private readonly IUserService _userService;
private HttpContextBase _httpContext; private HttpContextBase _httpContext;
public ContentService(IDatabaseUnitOfWorkProvider provider, IPublishingStrategy publishingStrategy) public ContentService(IDatabaseUnitOfWorkProvider provider) : this(provider, new PublishingStrategy())
{}
internal ContentService(IDatabaseUnitOfWorkProvider provider, IPublishingStrategy publishingStrategy)
{ {
_uowProvider = provider; _uowProvider = provider;
_publishingStrategy = publishingStrategy; _publishingStrategy = publishingStrategy;
@@ -37,8 +39,6 @@ namespace Umbraco.Core.Services
_uowProvider = provider; _uowProvider = provider;
} }
//TODO Add GetLatestUnpublishedVersions(int id){}
/// <summary> /// <summary>
/// Creates an <see cref="IContent"/> object using the alias of the <see cref="IContentType"/> /// Creates an <see cref="IContent"/> object using the alias of the <see cref="IContentType"/>
/// that this Content is based on. /// that this Content is based on.

View File

@@ -16,7 +16,7 @@ namespace Umbraco.Core.Services
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param> /// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param> /// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns> /// <returns><see cref="IMedia"/></returns>
IMedia CreateContent(int parentId, string mediaTypeAlias, int userId = -1); IMedia CreateMedia(int parentId, string mediaTypeAlias, int userId = -1);
/// <summary> /// <summary>
/// Gets an <see cref="IMedia"/> object by Id /// Gets an <see cref="IMedia"/> object by Id

View File

@@ -6,7 +6,6 @@ using Umbraco.Core.Auditing;
using Umbraco.Core.Models; using Umbraco.Core.Models;
using Umbraco.Core.Persistence; using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.Repositories;
using Umbraco.Core.Persistence.UnitOfWork; using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services namespace Umbraco.Core.Services
@@ -44,7 +43,7 @@ namespace Umbraco.Core.Services
/// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param> /// <param name="mediaTypeAlias">Alias of the <see cref="IMediaType"/></param>
/// <param name="userId">Optional id of the user creating the media item</param> /// <param name="userId">Optional id of the user creating the media item</param>
/// <returns><see cref="IMedia"/></returns> /// <returns><see cref="IMedia"/></returns>
public IMedia CreateContent(int parentId, string mediaTypeAlias, int userId = -1) public IMedia CreateMedia(int parentId, string mediaTypeAlias, int userId = -1)
{ {
var uow = _uowProvider.GetUnitOfWork(); var uow = _uowProvider.GetUnitOfWork();
using (var repository = RepositoryResolver.Current.Factory.CreateMediaTypeRepository(uow)) using (var repository = RepositoryResolver.Current.Factory.CreateMediaTypeRepository(uow))
@@ -55,12 +54,12 @@ namespace Umbraco.Core.Services
if (!mediaTypes.Any()) if (!mediaTypes.Any())
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", mediaTypeAlias)); throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", mediaTypeAlias));
var contentType = mediaTypes.First(); var mediaType = mediaTypes.First();
if (contentType == null) if (mediaType == null)
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", mediaTypeAlias)); throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", mediaTypeAlias));
var media = new Models.Media(parentId, contentType); var media = new Models.Media(parentId, mediaType);
var e = new NewEventArgs { Alias = mediaTypeAlias, ParentId = parentId }; var e = new NewEventArgs { Alias = mediaTypeAlias, ParentId = parentId };
@@ -307,7 +306,6 @@ namespace Umbraco.Core.Services
/// <param name="userId">Id of the User deleting the Media</param> /// <param name="userId">Id of the User deleting the Media</param>
public void Delete(IMedia media, int userId = -1) public void Delete(IMedia media, int userId = -1)
{ {
var uow = _uowProvider.GetUnitOfWork(); var uow = _uowProvider.GetUnitOfWork();
using (var repository = RepositoryResolver.Current.Factory.CreateMediaRepository(uow)) using (var repository = RepositoryResolver.Current.Factory.CreateMediaRepository(uow))
{ {
@@ -346,7 +344,6 @@ namespace Umbraco.Core.Services
{ {
SetUser(media, userId); SetUser(media, userId);
repository.AddOrUpdate(media); repository.AddOrUpdate(media);
repository.AddOrUpdate(media);
uow.Commit(); uow.Commit();
if (Saved != null) if (Saved != null)

View File

@@ -65,7 +65,7 @@ namespace Umbraco.Tests.Services
/// </summary> /// </summary>
private volatile Exception _error = null; private volatile Exception _error = null;
private const int MaxThreadCount = 1; private const int MaxThreadCount = 20;
[Test] [Test]
public void Ensure_All_Threads_Execute_Successfully_Content_Service() public void Ensure_All_Threads_Execute_Successfully_Content_Service()
@@ -145,20 +145,18 @@ namespace Umbraco.Tests.Services
{ {
try try
{ {
var folderMediaType = ServiceContext.ContentTypeService.GetMediaType(1031);
Debug.WriteLine("Created content on thread: " + Thread.CurrentThread.ManagedThreadId); Debug.WriteLine("Created content on thread: " + Thread.CurrentThread.ManagedThreadId);
//create 2 content items //create 2 content items
var folder1 = MockedMedia.CreateMediaFolder(folderMediaType, -1); var folder1 = mediaService.CreateMedia(-1, "Folder", 0);
folder1.Name = "test" + Guid.NewGuid(); folder1.Name = "test" + Guid.NewGuid();
Debug.WriteLine("Saving folder1 on thread: " + Thread.CurrentThread.ManagedThreadId); Debug.WriteLine("Saving folder1 on thread: " + Thread.CurrentThread.ManagedThreadId);
mediaService.Save(folder1, 0); mediaService.Save(folder1, 0);
Thread.Sleep(100); //quick pause for maximum overlap! Thread.Sleep(100); //quick pause for maximum overlap!
var folder2 = MockedMedia.CreateMediaFolder(folderMediaType, -1); var folder2 = mediaService.CreateMedia(-1, "Folder", 0);
folder2.Name = "test" + Guid.NewGuid(); folder2.Name = "test" + Guid.NewGuid();
Debug.WriteLine("Saving folder2 on thread: " + Thread.CurrentThread.ManagedThreadId); Debug.WriteLine("Saving folder2 on thread: " + Thread.CurrentThread.ManagedThreadId);
mediaService.Save(folder2, 0); mediaService.Save(folder2, 0);