Refactoring usage of UOW in ContentService

This commit is contained in:
sitereactor
2012-10-31 08:20:45 -01:00
parent e30cf6bc65
commit b86bfdd87b
4 changed files with 74 additions and 65 deletions

View File

@@ -9,13 +9,13 @@ namespace Umbraco.Core.Models.Rdbms
internal class DocumentTypeDto
{
[Column("contentTypeNodeId")]
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsDocumentType", OnColumns = "[contentTypeNodeId], [templateNodeId]")]
[ForeignKey(typeof(ContentTypeDto))]
[PrimaryKeyColumn(AutoIncrement = false, Name = "PK_cmsDocumentType", OnColumns = "contentTypeNodeId, templateNodeId")]
[ForeignKey(typeof(ContentTypeDto), Column = "nodeId")]
[ForeignKey(typeof(NodeDto))]
public int ContentTypeNodeId { get; set; }
[Column("templateNodeId")]
[ForeignKey(typeof(TemplateDto))]
/*[ForeignKey(typeof(TemplateDto))]*/
public int TemplateNodeId { get; set; }
[Column("IsDefault")]

View File

@@ -159,6 +159,9 @@ namespace Umbraco.Core.Persistence.Repositories
var dto = factory.BuildDto(entity);
PersistNewBaseContentType(dto.ContentTypeDto, entity);
//Inserts data into the cmsDocumentType table - currently only the ContentTypeNodeId is added/updated
dto.ContentTypeNodeId = entity.Id;
Database.Insert(dto);
//TODO Insert new DocumentType entries - NOTE only seems relevant as long as Templates resides in the DB?
//TODO Insert allowed Templates

View File

@@ -49,7 +49,7 @@ namespace Umbraco.Tests.Services
var contentService = ServiceContext.ContentService;
// Act
IContent content = contentService.GetById(1046);
var content = contentService.GetById(1046);
// Assert
Assert.That(content, Is.Not.Null);
@@ -63,16 +63,28 @@ namespace Umbraco.Tests.Services
var contentService = ServiceContext.ContentService;
// Act
IEnumerable<IContent> contents = contentService.GetByLevel(1);
var contents = contentService.GetByLevel(2);
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.EqualTo(1));
Assert.That(contents.Count(), Is.GreaterThanOrEqualTo(2));
}
[Test]
public void Can_Get_Children_Of_Content_Id()
{ }
{
// Arrange
var contentService = ServiceContext.ContentService;
// Act
var contents = contentService.GetChildren(1046);
// Assert
Assert.That(contents, Is.Not.Null);
Assert.That(contents.Any(), Is.True);
Assert.That(contents.Count(), Is.GreaterThanOrEqualTo(2));
}
public void Can_Get_All_Versions_Of_Content()
{ }
@@ -142,7 +154,11 @@ namespace Umbraco.Tests.Services
//Create and Save Content "Text Page 1" based on "umbTextpage" -> 1047
Content subpage = MockedContent.CreateTextpageContent(contentType, "Text Page 1", textpage.Id);
ServiceContext.ContentService.Save(subpage, 0);//This content object is currently not saved!? Have to figure out why
ServiceContext.ContentService.Save(subpage, 0);
//Create and Save Content "Text Page 1" based on "umbTextpage" -> 1048
Content subpage2 = MockedContent.CreateTextpageContent(contentType, "Text Page 2", textpage.Id);
ServiceContext.ContentService.Save(subpage2, 0);
}
}
}

View File

@@ -46,8 +46,7 @@ namespace Umbraco.Web.Services
/// <returns><see cref="IContent"/></returns>
public IContent CreateContent(int parentId, string contentTypeAlias)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentTypeRepository, IContentType, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentTypeRepository, IContentType, int>(_unitOfWork);
var query = Query<IContentType>.Builder.Where(x => x.Alias == contentTypeAlias);
var contentTypes = repository.GetByQuery(query);
@@ -57,7 +56,7 @@ namespace Umbraco.Web.Services
var contentType = contentTypes.First();
if (contentType == null)
throw new Exception(string.Format("No ContentType matching the passed in Alias: '{0}' was found", contentTypeAlias));
throw new Exception(string.Format("ContentType matching the passed in Alias: '{0}' was null", contentTypeAlias));
return new Content(parentId, contentType);
}
@@ -69,8 +68,7 @@ namespace Umbraco.Web.Services
/// <returns><see cref="IContent"/></returns>
public IContent GetById(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
return repository.Get(id);
}
@@ -81,8 +79,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetContentOfContentType(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.ContentTypeId == id);
var contents = repository.GetByQuery(query);
@@ -97,8 +94,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetByLevel(int level)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.Level == level);
var contents = repository.GetByQuery(query);
@@ -113,8 +109,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetChildren(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.ParentId == id);
var contents = repository.GetByQuery(query);
@@ -129,8 +124,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetVersions(int id)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var versions = repository.GetAllVersions(id);
return versions;
}
@@ -141,8 +135,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetRootContent()
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.ParentId == -1);
var contents = repository.GetByQuery(query);
@@ -156,8 +149,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetContentForExpiration()
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.Published == true && x.ExpireDate != null && x.ExpireDate.Value <= DateTime.Now);
var contents = repository.GetByQuery(query);
@@ -171,8 +163,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetContentForRelease()
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.Published == true && x.ReleaseDate != null && x.ReleaseDate.Value <= DateTime.Now);
var contents = repository.GetByQuery(query);
@@ -186,8 +177,7 @@ namespace Umbraco.Web.Services
/// <returns>An Enumerable list of <see cref="IContent"/> objects</returns>
public IEnumerable<IContent> GetContentInRecycleBin()
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.ParentId == -20);
var contents = repository.GetByQuery(query);
@@ -202,8 +192,7 @@ namespace Umbraco.Web.Services
/// <returns>True if publishing succeeded, otherwise False</returns>
public bool RePublishAll(int userId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var list = new List<IContent>();
@@ -228,7 +217,7 @@ namespace Umbraco.Web.Services
repository.AddOrUpdate(item);
}
unitOfWork.Commit();
_unitOfWork.Commit();
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
global::umbraco.library.RefreshContent();
@@ -256,8 +245,7 @@ namespace Umbraco.Web.Services
/// <returns>True if publishing succeeded, otherwise False</returns>
public bool PublishWithChildren(IContent content, int userId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
//Check if parent is published - if parent isn't published this Content cannot be published
var parent = GetById(content.ParentId);
@@ -293,7 +281,7 @@ namespace Umbraco.Web.Services
repository.AddOrUpdate(item);
}
unitOfWork.Commit();
_unitOfWork.Commit();
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
//TODO Need to investigate if it will also update the cache for children of the Content object
@@ -311,15 +299,14 @@ namespace Umbraco.Web.Services
/// <returns>True if unpublishing succeeded, otherwise False</returns>
public bool UnPublish(IContent content, int userId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var unpublished = _publishingStrategy.UnPublish(content, userId);
if (unpublished)
{
repository.AddOrUpdate(content);
unitOfWork.Commit();
_unitOfWork.Commit();
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content class.
global::umbraco.library.UnPublishSingleNode(content.Id);
@@ -362,8 +349,7 @@ namespace Umbraco.Web.Services
/// <returns>True if publishing succeeded, otherwise False</returns>
public bool SaveAndPublish(IContent content, int userId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
//Check if parent is published - if parent isn't published this Content cannot be published
var parent = GetById(content.ParentId);
@@ -389,7 +375,7 @@ namespace Umbraco.Web.Services
if (published)
{
repository.AddOrUpdate(content);
unitOfWork.Commit();
_unitOfWork.Commit();
//TODO Change this so we can avoid a depencency to the horrible library method / umbraco.content (singleton) class.
global::umbraco.library.UpdateDocumentCache(content.Id);
@@ -405,7 +391,6 @@ namespace Umbraco.Web.Services
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IContent content, int userId)
{
//var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
repository.AddOrUpdate(content);
_unitOfWork.Commit();
@@ -418,13 +403,12 @@ namespace Umbraco.Web.Services
/// <param name="userId">Id of the User saving the Content</param>
public void Save(IEnumerable<IContent> contents, int userId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
foreach (var content in contents)
{
repository.AddOrUpdate(content);
}
unitOfWork.Commit();
_unitOfWork.Commit();
}
/// <summary>
@@ -434,8 +418,7 @@ namespace Umbraco.Web.Services
/// <param name="contentTypeId">Id of the <see cref="IContentType"/></param>
public void DeleteContentOfType(int contentTypeId)
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
//NOTE What about content that has the contenttype as part of its composition?
var query = Query<IContent>.Builder.Where(x => x.ContentTypeId == contentTypeId);
@@ -447,7 +430,7 @@ namespace Umbraco.Web.Services
repository.AddOrUpdate(content);
}
unitOfWork.Commit();
_unitOfWork.Commit();
}
/// <summary>
@@ -461,10 +444,9 @@ namespace Umbraco.Web.Services
//TODO Ensure that content is unpublished when deleted
//TODO This method should handle/react to errors when there is a constraint issue with the content being deleted
//TODO Children should either be deleted or moved to the recycle bin
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
repository.Delete(content);
unitOfWork.Commit();
_unitOfWork.Commit();
}
/// <summary>
@@ -477,23 +459,34 @@ namespace Umbraco.Web.Services
{
//TODO If content item has children those should also be moved to the recycle bin
//TODO Unpublish deleted content + children
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
((Content)content).ChangeTrashedState(true);
repository.AddOrUpdate(content);
unitOfWork.Commit();
_unitOfWork.Commit();
}
/// <summary>
/// Moves an <see cref="IContent"/> object to a new location
/// Moves an <see cref="IContent"/> object to a new location by changing its parent id.
/// </summary>
/// <remarks>
/// If the <see cref="IContent"/> object is already published it will be
/// published after being moved to its new location. Otherwise it'll just
/// be saved with a new parent id.
/// </remarks>
/// <param name="content">The <see cref="IContent"/> to move</param>
/// <param name="parentId">Id of the Content's new Parent</param>
/// <param name="userId">Id of the User moving the Content</param>
public void Move(IContent content, int parentId, int userId)
{
content.ParentId = parentId;
SaveAndPublish(content, userId);
if(content.Published)
{
SaveAndPublish(content, userId);
}
else
{
Save(content, userId);
}
}
/// <summary>
@@ -501,8 +494,7 @@ namespace Umbraco.Web.Services
/// </summary>
public void EmptyRecycleBin()
{
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.ParentId == -20);
var contents = repository.GetByQuery(query);
@@ -511,7 +503,7 @@ namespace Umbraco.Web.Services
{
repository.Delete(content);
}
unitOfWork.Commit();
_unitOfWork.Commit();
}
/// <summary>
@@ -527,11 +519,10 @@ namespace Umbraco.Web.Services
var copy = ((Content) content).Clone();
copy.ParentId = parentId;
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
repository.AddOrUpdate(copy);
unitOfWork.Commit();
_unitOfWork.Commit();
return copy;
}
@@ -570,12 +561,11 @@ namespace Umbraco.Web.Services
public IContent Rollback(int id, Guid versionId, int userId)
{
//TODO Need to test if this actually works
var unitOfWork = _provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var content = repository.GetByVersion(id, versionId);
repository.AddOrUpdate(content);
unitOfWork.Commit();
_unitOfWork.Commit();
return content;
}