Continueing with the implementation of the EntityRepository

This commit is contained in:
Morten Christensen
2013-03-18 20:18:12 -01:00
parent 365dffe2db
commit 083320002b
2 changed files with 145 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Persistence.Factories;
@@ -18,10 +19,18 @@ namespace Umbraco.Core.Persistence.Repositories
internal class EntityRepository : DisposableObject, IEntityRepository
{
private readonly IDatabaseUnitOfWork _work;
private readonly IDictionary<Guid, Func<IUmbracoEntity, IUmbracoEntity>> _propertyHandler;
public EntityRepository(IDatabaseUnitOfWork work)
{
_work = work;
_propertyHandler = new Dictionary<Guid, Func<IUmbracoEntity, IUmbracoEntity>>
{
{
new Guid(Constants.ObjectTypes.Document),
UpdateIsPublished
}
};
}
/// <summary>
@@ -53,6 +62,10 @@ namespace Umbraco.Core.Persistence.Repositories
var entity = factory.BuildEntity(nodeDto);
//TODO Update HasChildren and IsPublished
if (_propertyHandler.ContainsKey(entity.NodeObjectTypeId))
{
return _propertyHandler[entity.NodeObjectTypeId](entity);
}
return entity;
}
@@ -131,6 +144,21 @@ namespace Umbraco.Core.Persistence.Repositories
#endregion
private void UpdateHasChildren(IUmbracoEntity entity)
{}
private IUmbracoEntity UpdateIsPublished(IUmbracoEntity entity)
{
var umbracoEntity = entity as UmbracoEntity;
if (umbracoEntity != null)
{
umbracoEntity.IsPublished = true;
return umbracoEntity;
}
return entity;
}
#region Sql Statements
protected virtual Sql GetBaseQuery()

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.UnitOfWork;
namespace Umbraco.Core.Services
@@ -41,6 +43,14 @@ namespace Umbraco.Core.Services
/// <returns>An <see cref="IUmbracoEntity"/></returns>
public virtual IUmbracoEntity Get(int id, bool loadBaseType = true)
{
if (loadBaseType)
{
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
return repository.Get(id);
}
}
throw new NotImplementedException();
}
@@ -56,54 +66,156 @@ namespace Umbraco.Core.Services
/// <returns>An <see cref="IUmbracoEntity"/></returns>
public virtual IUmbracoEntity Get<T>(int id, bool loadBaseType = true) where T : IUmbracoEntity
{
if (loadBaseType)
{
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
return repository.Get(id);
}
}
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual IUmbracoEntity GetParent(int id)
{
throw new NotImplementedException();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
var entity = repository.Get(id);
if (entity.ParentId == -1 || entity.ParentId == -20 || entity.ParentId == -21)
return null;
return repository.Get(entity.ParentId);
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetChildren(int id)
{
throw new NotImplementedException();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
var query = Query<IUmbracoEntity>.Builder.Where(x => x.ParentId == id);
var contents = repository.GetByQuery(query);
return contents;
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetDescendents(int id)
{
throw new NotImplementedException();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
var entity = repository.Get(id);
var query = Query<IUmbracoEntity>.Builder.Where(x => x.Path.StartsWith(entity.Path) && x.Id != id);
var entities = repository.GetByQuery(query);
return entities;
}
}
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetRootEntities(UmbracoObjectTypes objectType)
{
var objectTypeId = objectType.GetGuid();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
var query = Query<IUmbracoEntity>.Builder.Where(x => x.ParentId == -1);
var entities = repository.GetByQuery(query, objectTypeId);
return entities;
}
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetAll<T>() where T : IUmbracoEntity
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetAll(UmbracoObjectTypes objectType)
{
throw new NotImplementedException();
var objectTypeId = objectType.GetGuid();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetAll(objectTypeId);
}
}
/// <summary>
///
/// </summary>
/// <param name="objectTypeId"></param>
/// <returns></returns>
public virtual IEnumerable<IUmbracoEntity> GetAll(Guid objectTypeId)
{
throw new NotImplementedException();
using (var repository = _repositoryFactory.CreateEntityRepository(_uowProvider.GetUnitOfWork()))
{
return repository.GetAll(objectTypeId);
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual UmbracoObjectTypes GetObjectType(int id)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
public virtual UmbracoObjectTypes GetObjectType(IUmbracoEntity entity)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public virtual Type GetModelType(int id)
{
throw new NotImplementedException();
}
/// <summary>
///
/// </summary>
/// <param name="objectType"></param>
/// <returns></returns>
public virtual Type GetModelType(UmbracoObjectTypes objectType)
{
throw new NotImplementedException();