Removed any manual instantiation of PetaPocoUnitOfWork as this should generally always

be done by the PetaPocoUnitOfWorkProvider to ensure that for a UOW it always uses a new database
instance, not a shared one.
This commit is contained in:
Shannon Deminick
2012-12-12 06:08:03 +05:00
parent a3938edde2
commit 1e41db6d69
5 changed files with 33 additions and 12 deletions

View File

@@ -51,9 +51,11 @@ namespace Umbraco.Core.Models
/// </summary>
public static IProfile GetCreatorProfile(this IContent content)
{
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
new PetaPocoUnitOfWork(DatabaseContext.Current.Database));
return repository.GetProfileById(content.CreatorId);
using (var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
PetaPocoUnitOfWorkProvider.CreateUnitOfWork()))
{
return repository.GetProfileById(content.CreatorId);
}
}
/// <summary>
@@ -61,9 +63,11 @@ namespace Umbraco.Core.Models
/// </summary>
public static IProfile GetWriterProfile(this IContent content)
{
var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
new PetaPocoUnitOfWork(DatabaseContext.Current.Database));
return repository.GetProfileById(content.WriterId);
using(var repository = RepositoryResolver.Current.Factory.CreateUserRepository(
PetaPocoUnitOfWorkProvider.CreateUnitOfWork()))
{
return repository.GetProfileById(content.WriterId);
}
}
}
}

View File

@@ -25,10 +25,12 @@ namespace Umbraco.Core.Persistence.UnitOfWork
/// </summary>
/// <param name="database"></param>
/// <remarks>
/// This should normally not be used directly and should be created with the UnitOfWorkProvider
///
/// The Database instance used for this unit of work should not be shared with other unit's of work, other repositories, etc...
/// as it will get disposed of when this unit of work is disposed.
/// </remarks>
public PetaPocoUnitOfWork(UmbracoDatabase database)
internal PetaPocoUnitOfWork(UmbracoDatabase database)
{
Database = database;
_key = Guid.NewGuid();

View File

@@ -30,5 +30,15 @@ namespace Umbraco.Core.Persistence.UnitOfWork
}
#endregion
/// <summary>
/// Static helper method to return a new unit of work
/// </summary>
/// <returns></returns>
internal static IDatabaseUnitOfWork CreateUnitOfWork()
{
var provider = new PetaPocoUnitOfWorkProvider();
return provider.GetUnitOfWork();
}
}
}

View File

@@ -46,16 +46,17 @@ namespace Umbraco.Tests.Persistence
{
var method = typeof(RepositoryResolver).GetMethod("ResolveByType", BindingFlags.NonPublic | BindingFlags.Instance);
var gMethod = method.MakeGenericMethod(repoType);
var repo = gMethod.Invoke(RepositoryResolver.Current, new object[] { new PetaPocoUnitOfWork(DatabaseContext.Current.Database) });
var repo = gMethod.Invoke(RepositoryResolver.Current, new object[] { PetaPocoUnitOfWorkProvider.CreateUnitOfWork() });
Assert.IsNotNull(repo);
Assert.IsTrue(TypeHelper.IsTypeAssignableFrom(repoType, repo.GetType()));
repo.DisposeIfDisposable();
}
[Test]
public void Can_Verify_UOW_In_Repository()
{
// Arrange
var uow = new PetaPocoUnitOfWork(DatabaseContext.Current.Database);
var uow = PetaPocoUnitOfWorkProvider.CreateUnitOfWork();
// Act
var repository = RepositoryResolver.Current.ResolveByType<IContentRepository>(uow);
@@ -63,6 +64,8 @@ namespace Umbraco.Tests.Persistence
// Assert
Assert.That(repository, Is.Not.Null);
Assert.That(uow.Key, Is.EqualTo(((RepositoryBase<int, IContent>)repository).UnitKey));
repository.Dispose();
}
[Test]
@@ -75,12 +78,14 @@ namespace Umbraco.Tests.Persistence
Assert.That(isSubclassOf, Is.False);
Assert.That(isAssignableFrom, Is.True);
var uow = new PetaPocoUnitOfWork(DatabaseContext.Current.Database);
var uow = PetaPocoUnitOfWorkProvider.CreateUnitOfWork();
var repository = RepositoryResolver.Current.ResolveByType<IContentRepository>(uow);
bool subclassOf = repository.GetType().IsSubclassOf(typeof (IRepository<int, IContent>));
Assert.That(subclassOf, Is.False);
Assert.That((typeof(IRepository<int, IContent>).IsInstanceOfType(repository)), Is.True);
repository.Dispose();
}
}
}

View File

@@ -667,8 +667,8 @@ namespace Umbraco.Tests.Services
[Test]
public void Can_Save_Lazy_Content()
{
var unitOfWork = new PetaPocoUnitOfWork(Umbraco.Core.DatabaseContext.Current.Database);
{
var unitOfWork = PetaPocoUnitOfWorkProvider.CreateUnitOfWork();
var contentType = ServiceContext.ContentTypeService.GetContentType("umbTextpage");
var root = ServiceContext.ContentService.GetById(1046);