diff --git a/src/Umbraco.Core/Models/IRelation.cs b/src/Umbraco.Core/Models/IRelation.cs new file mode 100644 index 0000000000..b37357f8bc --- /dev/null +++ b/src/Umbraco.Core/Models/IRelation.cs @@ -0,0 +1,38 @@ +using System.Runtime.Serialization; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Models +{ + public interface IRelation : IAggregateRoot + { + /// + /// Gets or sets the Parent Id of the Relation (Source) + /// + [DataMember] + int ParentId { get; set; } + + /// + /// Gets or sets the Child Id of the Relation (Destination) + /// + [DataMember] + int ChildId { get; set; } + + /// + /// Gets or sets the for the Relation + /// + [DataMember] + IRelationType RelationType { get; set; } + + /// + /// Gets or sets a comment for the Relation + /// + [DataMember] + string Comment { get; set; } + + /// + /// Gets the Id of the that this Relation is based on. + /// + [IgnoreDataMember] + int RelationTypeId { get; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/IRelationType.cs b/src/Umbraco.Core/Models/IRelationType.cs new file mode 100644 index 0000000000..8fc12eb6a1 --- /dev/null +++ b/src/Umbraco.Core/Models/IRelationType.cs @@ -0,0 +1,41 @@ +using System; +using System.Runtime.Serialization; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Models +{ + public interface IRelationType : IAggregateRoot + { + /// + /// Gets or sets the Name of the RelationType + /// + [DataMember] + string Name { get; set; } + + /// + /// Gets or sets the Alias of the RelationType + /// + [DataMember] + string Alias { get; set; } + + /// + /// Gets or sets a boolean indicating whether the RelationType is Bidirectional (true) or Parent to Child (false) + /// + [DataMember] + bool IsBidirectional { get; set; } + + /// + /// Gets or sets the Parents object type id + /// + /// Corresponds to the NodeObjectType in the umbracoNode table + [DataMember] + Guid ParentObjectType { get; set; } + + /// + /// Gets or sets the Childs object type id + /// + /// Corresponds to the NodeObjectType in the umbracoNode table + [DataMember] + Guid ChildObjectType { get; set; } + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Models/Relation.cs b/src/Umbraco.Core/Models/Relation.cs index edcca14d82..ec7c3741a6 100644 --- a/src/Umbraco.Core/Models/Relation.cs +++ b/src/Umbraco.Core/Models/Relation.cs @@ -11,15 +11,15 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - public class Relation : Entity, IAggregateRoot + public class Relation : Entity, IAggregateRoot, IRelation { //NOTE: The datetime column from umbracoRelation is set on CreateDate on the Entity private int _parentId; private int _childId; - private RelationType _relationType; + private IRelationType _relationType; private string _comment; - public Relation(int parentId, int childId, RelationType relationType) + public Relation(int parentId, int childId, IRelationType relationType) { _parentId = parentId; _childId = childId; @@ -28,7 +28,7 @@ namespace Umbraco.Core.Models private static readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo(x => x.ParentId); private static readonly PropertyInfo ChildIdSelector = ExpressionHelper.GetPropertyInfo(x => x.ChildId); - private static readonly PropertyInfo RelationTypeSelector = ExpressionHelper.GetPropertyInfo(x => x.RelationType); + private static readonly PropertyInfo RelationTypeSelector = ExpressionHelper.GetPropertyInfo(x => x.RelationType); private static readonly PropertyInfo CommentSelector = ExpressionHelper.GetPropertyInfo(x => x.Comment); /// @@ -69,7 +69,7 @@ namespace Umbraco.Core.Models /// Gets or sets the for the Relation /// [DataMember] - public RelationType RelationType + public IRelationType RelationType { get { return _relationType; } set diff --git a/src/Umbraco.Core/Models/RelationType.cs b/src/Umbraco.Core/Models/RelationType.cs index cf3448c62d..e1f5b4a388 100644 --- a/src/Umbraco.Core/Models/RelationType.cs +++ b/src/Umbraco.Core/Models/RelationType.cs @@ -11,7 +11,7 @@ namespace Umbraco.Core.Models /// [Serializable] [DataContract(IsReference = true)] - public class RelationType : Entity, IAggregateRoot + public class RelationType : Entity, IAggregateRoot, IRelationType { private string _name; private string _alias; diff --git a/src/Umbraco.Core/Persistence/Factories/RelationFactory.cs b/src/Umbraco.Core/Persistence/Factories/RelationFactory.cs index 081a17beb5..8aac081c33 100644 --- a/src/Umbraco.Core/Persistence/Factories/RelationFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/RelationFactory.cs @@ -3,18 +3,18 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - internal class RelationFactory : IEntityFactory + internal class RelationFactory : IEntityFactory { - private readonly RelationType _relationType; + private readonly IRelationType _relationType; - public RelationFactory(RelationType relationType) + public RelationFactory(IRelationType relationType) { _relationType = relationType; } #region Implementation of IEntityFactory - public Relation BuildEntity(RelationDto dto) + public IRelation BuildEntity(RelationDto dto) { var entity = new Relation(dto.ParentId, dto.ChildId, _relationType) { @@ -29,7 +29,7 @@ namespace Umbraco.Core.Persistence.Factories return entity; } - public RelationDto BuildDto(Relation entity) + public RelationDto BuildDto(IRelation entity) { var dto = new RelationDto { diff --git a/src/Umbraco.Core/Persistence/Factories/RelationTypeFactory.cs b/src/Umbraco.Core/Persistence/Factories/RelationTypeFactory.cs index 3fdb6febe3..81020fa593 100644 --- a/src/Umbraco.Core/Persistence/Factories/RelationTypeFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/RelationTypeFactory.cs @@ -3,11 +3,11 @@ using Umbraco.Core.Models.Rdbms; namespace Umbraco.Core.Persistence.Factories { - internal class RelationTypeFactory : IEntityFactory + internal class RelationTypeFactory : IEntityFactory { #region Implementation of IEntityFactory - public RelationType BuildEntity(RelationTypeDto dto) + public IRelationType BuildEntity(RelationTypeDto dto) { var entity = new RelationType(dto.ChildObjectType, dto.ParentObjectType, dto.Alias) { @@ -21,7 +21,7 @@ namespace Umbraco.Core.Persistence.Factories return entity; } - public RelationTypeDto BuildDto(RelationType entity) + public RelationTypeDto BuildDto(IRelationType entity) { var dto = new RelationTypeDto { diff --git a/src/Umbraco.Core/Persistence/Mappers/RelationMapper.cs b/src/Umbraco.Core/Persistence/Mappers/RelationMapper.cs index f67c022f62..5d596fefe0 100644 --- a/src/Umbraco.Core/Persistence/Mappers/RelationMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/RelationMapper.cs @@ -10,6 +10,7 @@ namespace Umbraco.Core.Persistence.Mappers /// Represents a to DTO mapper used to translate the properties of the public api /// implementation to that of the database's DTO as sql: [tableName].[columnName]. /// + [MapperFor(typeof(IRelation))] [MapperFor(typeof(Relation))] public sealed class RelationMapper : BaseMapper { diff --git a/src/Umbraco.Core/Persistence/Mappers/RelationTypeMapper.cs b/src/Umbraco.Core/Persistence/Mappers/RelationTypeMapper.cs index 610667cf05..b417b7fb53 100644 --- a/src/Umbraco.Core/Persistence/Mappers/RelationTypeMapper.cs +++ b/src/Umbraco.Core/Persistence/Mappers/RelationTypeMapper.cs @@ -11,6 +11,7 @@ namespace Umbraco.Core.Persistence.Mappers /// implementation to that of the database's DTO as sql: [tableName].[columnName]. /// [MapperFor(typeof(RelationType))] + [MapperFor(typeof(IRelationType))] public sealed class RelationTypeMapper : BaseMapper { private static readonly ConcurrentDictionary PropertyInfoCacheInstance = new ConcurrentDictionary(); diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationRepository.cs index 80162e15b8..de7ff3ba66 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationRepository.cs @@ -2,7 +2,7 @@ namespace Umbraco.Core.Persistence.Repositories { - public interface IRelationRepository : IRepositoryQueryable + public interface IRelationRepository : IRepositoryQueryable { } diff --git a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationTypeRepository.cs index e4ca156a94..5fa35df140 100644 --- a/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/Interfaces/IRelationTypeRepository.cs @@ -2,7 +2,7 @@ namespace Umbraco.Core.Persistence.Repositories { - public interface IRelationTypeRepository : IRepositoryQueryable + public interface IRelationTypeRepository : IRepositoryQueryable { } diff --git a/src/Umbraco.Core/Persistence/Repositories/RelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/RelationRepository.cs index 3ee0c6fa8b..b93de7e585 100644 --- a/src/Umbraco.Core/Persistence/Repositories/RelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/RelationRepository.cs @@ -2,6 +2,7 @@ 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.Caching; using Umbraco.Core.Persistence.Factories; @@ -13,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories /// /// Represents a repository for doing CRUD operations for /// - internal class RelationRepository : PetaPocoRepositoryBase, IRelationRepository + internal class RelationRepository : PetaPocoRepositoryBase, IRelationRepository { private readonly IRelationTypeRepository _relationTypeRepository; @@ -31,7 +32,7 @@ namespace Umbraco.Core.Persistence.Repositories #region Overrides of RepositoryBase - protected override Relation PerformGet(int id) + protected override IRelation PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); @@ -49,12 +50,12 @@ namespace Umbraco.Core.Persistence.Repositories //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 - entity.ResetDirtyProperties(false); + ((TracksChangesEntityBase)entity).ResetDirtyProperties(false); return entity; } - protected override IEnumerable PerformGetAll(params int[] ids) + protected override IEnumerable PerformGetAll(params int[] ids) { if (ids.Any()) { @@ -73,10 +74,10 @@ namespace Umbraco.Core.Persistence.Repositories } } - protected override IEnumerable PerformGetByQuery(IQuery query) + protected override IEnumerable PerformGetByQuery(IQuery query) { var sqlClause = GetBaseQuery(false); - var translator = new SqlTranslator(sqlClause, query); + var translator = new SqlTranslator(sqlClause, query); var sql = translator.Translate(); var dtos = Database.Fetch(sql); @@ -122,9 +123,9 @@ namespace Umbraco.Core.Persistence.Repositories #region Unit of Work Implementation - protected override void PersistNewItem(Relation entity) + protected override void PersistNewItem(IRelation entity) { - entity.AddingEntity(); + ((Entity)entity).AddingEntity(); var factory = new RelationFactory(entity.RelationType); var dto = factory.BuildDto(entity); @@ -132,18 +133,18 @@ namespace Umbraco.Core.Persistence.Repositories var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; - entity.ResetDirtyProperties(); + ((ICanBeDirty)entity).ResetDirtyProperties(); } - protected override void PersistUpdatedItem(Relation entity) + protected override void PersistUpdatedItem(IRelation entity) { - entity.UpdatingEntity(); + ((Entity)entity).UpdatingEntity(); var factory = new RelationFactory(entity.RelationType); var dto = factory.BuildDto(entity); Database.Update(dto); - entity.ResetDirtyProperties(); + ((ICanBeDirty)entity).ResetDirtyProperties(); } #endregion diff --git a/src/Umbraco.Core/Persistence/Repositories/RelationTypeRepository.cs b/src/Umbraco.Core/Persistence/Repositories/RelationTypeRepository.cs index 900129121e..79fba9ea8d 100644 --- a/src/Umbraco.Core/Persistence/Repositories/RelationTypeRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/RelationTypeRepository.cs @@ -2,6 +2,7 @@ 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.Caching; using Umbraco.Core.Persistence.Factories; @@ -13,7 +14,7 @@ namespace Umbraco.Core.Persistence.Repositories /// /// Represents a repository for doing CRUD operations for /// - internal class RelationTypeRepository : PetaPocoRepositoryBase, IRelationTypeRepository + internal class RelationTypeRepository : PetaPocoRepositoryBase, IRelationTypeRepository { public RelationTypeRepository(IDatabaseUnitOfWork work) : base(work) @@ -27,7 +28,7 @@ namespace Umbraco.Core.Persistence.Repositories #region Overrides of RepositoryBase - protected override RelationType PerformGet(int id) + protected override IRelationType PerformGet(int id) { var sql = GetBaseQuery(false); sql.Where(GetBaseWhereClause(), new { Id = id }); @@ -41,12 +42,12 @@ namespace Umbraco.Core.Persistence.Repositories //on initial construction we don't want to have dirty properties tracked // http://issues.umbraco.org/issue/U4-1946 - entity.ResetDirtyProperties(false); + ((TracksChangesEntityBase)entity).ResetDirtyProperties(false); return entity; } - protected override IEnumerable PerformGetAll(params int[] ids) + protected override IEnumerable PerformGetAll(params int[] ids) { if (ids.Any()) { @@ -65,10 +66,10 @@ namespace Umbraco.Core.Persistence.Repositories } } - protected override IEnumerable PerformGetByQuery(IQuery query) + protected override IEnumerable PerformGetByQuery(IQuery query) { var sqlClause = GetBaseQuery(false); - var translator = new SqlTranslator(sqlClause, query); + var translator = new SqlTranslator(sqlClause, query); var sql = translator.Translate(); var dtos = Database.Fetch(sql); @@ -115,9 +116,9 @@ namespace Umbraco.Core.Persistence.Repositories #region Unit of Work Implementation - protected override void PersistNewItem(RelationType entity) + protected override void PersistNewItem(IRelationType entity) { - entity.AddingEntity(); + ((Entity)entity).AddingEntity(); var factory = new RelationTypeFactory(); var dto = factory.BuildDto(entity); @@ -125,18 +126,18 @@ namespace Umbraco.Core.Persistence.Repositories var id = Convert.ToInt32(Database.Insert(dto)); entity.Id = id; - entity.ResetDirtyProperties(); + ((ICanBeDirty)entity).ResetDirtyProperties(); } - protected override void PersistUpdatedItem(RelationType entity) + protected override void PersistUpdatedItem(IRelationType entity) { - entity.UpdatingEntity(); + ((Entity)entity).UpdatingEntity(); var factory = new RelationTypeFactory(); var dto = factory.BuildDto(entity); Database.Update(dto); - entity.ResetDirtyProperties(); + ((ICanBeDirty)entity).ResetDirtyProperties(); } #endregion diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index 2fb2222528..5be3681fd1 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -1184,7 +1184,7 @@ namespace Umbraco.Core.Services //NOTE This 'Relation' part should eventually be delegated to a RelationService if (relateToOriginal) { - RelationType relationType = null; + IRelationType relationType = null; using (var relationTypeRepository = _repositoryFactory.CreateRelationTypeRepository(uow)) { relationType = relationTypeRepository.Get(1); diff --git a/src/Umbraco.Core/Services/IRelationService.cs b/src/Umbraco.Core/Services/IRelationService.cs new file mode 100644 index 0000000000..32e7d88f2a --- /dev/null +++ b/src/Umbraco.Core/Services/IRelationService.cs @@ -0,0 +1,215 @@ +using System; +using System.Collections.Generic; +using Umbraco.Core.Models; +using Umbraco.Core.Models.EntityBase; + +namespace Umbraco.Core.Services +{ + public interface IRelationService + { + /// + /// Gets a by its Id + /// + /// Id of the + /// A object + IRelation GetById(int id); + + /// + /// Gets a by its Id + /// + /// Id of the + /// A object + IRelationType GetRelationTypeById(int id); + + /// + /// Gets a by its Alias + /// + /// Alias of the + /// A object + IRelationType GetRelationTypeByAlias(string alias); + + /// + /// Gets all objects + /// + /// Optional array of integer ids to return relations for + /// An enumerable list of objects + IEnumerable GetAllRelations(params int[] ids); + + /// + /// Gets all objects by their + /// + /// to retrieve Relations for + /// An enumerable list of objects + IEnumerable GetAllRelationsByRelationType(RelationType relationType); + + /// + /// Gets all objects by their 's Id + /// + /// Id of the to retrieve Relations for + /// An enumerable list of objects + IEnumerable GetAllRelationsByRelationType(int relationTypeId); + + /// + /// Gets all objects + /// + /// Optional array of integer ids to return relationtypes for + /// An enumerable list of objects + IEnumerable GetAllRelationTypes(params int[] ids); + + /// + /// Gets a list of objects by their parent Id + /// + /// Id of the parent to retrieve relations for + /// An enumerable list of objects + IEnumerable GetByParentId(int id); + + /// + /// Gets a list of objects by their child Id + /// + /// Id of the child to retrieve relations for + /// An enumerable list of objects + IEnumerable GetByChildId(int id); + + /// + /// Gets a list of objects by their child or parent Id. + /// Using this method will get you all relations regards of it being a child or parent relation. + /// + /// Id of the child or parent to retrieve relations for + /// An enumerable list of objects + IEnumerable GetByParentOrChildId(int id); + + /// + /// Gets a list of objects by the Name of the + /// + /// Name of the to retrieve Relations for + /// An enumerable list of objects + IEnumerable GetByRelationTypeName(string relationTypeName); + + /// + /// Gets a list of objects by the Alias of the + /// + /// Alias of the to retrieve Relations for + /// An enumerable list of objects + IEnumerable GetByRelationTypeAlias(string relationTypeAlias); + + /// + /// Gets a list of objects by the Id of the + /// + /// Id of the to retrieve Relations for + /// An enumerable list of objects + IEnumerable GetByRelationTypeId(int relationTypeId); + + /// + /// Gets the Child object from a Relation as an + /// + /// Relation to retrieve child object from + /// Optional bool to load the complete object graph when set to False + /// An + IUmbracoEntity GetChildEntityFromRelation(IRelation relation, bool loadBaseType = false); + + /// + /// Gets the Parent object from a Relation as an + /// + /// Relation to retrieve parent object from + /// Optional bool to load the complete object graph when set to False + /// An + IUmbracoEntity GetParentEntityFromRelation(IRelation relation, bool loadBaseType = false); + + /// + /// Gets the Parent and Child objects from a Relation as a "/> with . + /// + /// Relation to retrieve parent and child object from + /// Optional bool to load the complete object graph when set to False + /// Returns a Tuple with Parent (item1) and Child (item2) + Tuple GetEntitiesFromRelation(IRelation relation, bool loadBaseType = false); + + /// + /// Gets the Child objects from a list of Relations as a list of objects. + /// + /// List of relations to retrieve child objects from + /// Optional bool to load the complete object graph when set to False + /// An enumerable list of + IEnumerable GetChildEntitiesFromRelations(IEnumerable relations, bool loadBaseType = false); + + /// + /// Gets the Parent objects from a list of Relations as a list of objects. + /// + /// List of relations to retrieve parent objects from + /// Optional bool to load the complete object graph when set to False + /// An enumerable list of + IEnumerable GetParentEntitiesFromRelations(IEnumerable relations, + bool loadBaseType = false); + + /// + /// Gets the Parent and Child objects from a list of Relations as a list of objects. + /// + /// List of relations to retrieve parent and child objects from + /// Optional bool to load the complete object graph when set to False + /// An enumerable list of with + IEnumerable> GetEntitiesFromRelations( + IEnumerable relations, + bool loadBaseType = false); + + /// + /// Relates two objects that are based on the interface. + /// + /// Parent entity + /// Child entity + /// The type of relation to create + /// The created + IRelation Relate(IUmbracoEntity parent, IUmbracoEntity child, IRelationType relationType); + + /// + /// Relates two objects that are based on the interface. + /// + /// Parent entity + /// Child entity + /// Alias of the type of relation to create + /// The created + IRelation Relate(IUmbracoEntity parent, IUmbracoEntity child, string relationTypeAlias); + + /// + /// Checks whether any relations exists for the passed in . + /// + /// to check for relations + /// Returns True if any relations exists for the given , otherwise False + bool HasRelations(IRelationType relationType); + + /// + /// Checks whether any relations exists for the passed in Id. + /// + /// Id of an object to check relations for + /// Returns True if any relations exists with the given Id, otherwise False + bool IsRelated(int id); + + /// + /// Saves a + /// + /// Relation to save + void Save(IRelation relation); + + /// + /// Saves a + /// + /// RelationType to Save + void Save(IRelationType relationType); + + /// + /// Deletes a + /// + /// Relation to Delete + void Delete(IRelation relation); + + /// + /// Deletes a + /// + /// RelationType to Delete + void Delete(IRelationType relationType); + + /// + /// Deletes all objects based on the passed in + /// + /// to Delete Relations for + void DeleteRelationsOfType(IRelationType relationType); + } +} \ No newline at end of file diff --git a/src/Umbraco.Core/Services/RelationService.cs b/src/Umbraco.Core/Services/RelationService.cs index 29a42da115..e205fa3c8c 100644 --- a/src/Umbraco.Core/Services/RelationService.cs +++ b/src/Umbraco.Core/Services/RelationService.cs @@ -9,7 +9,7 @@ using Umbraco.Core.Persistence.UnitOfWork; namespace Umbraco.Core.Services { - public class RelationService + public class RelationService : IRelationService { private readonly IDatabaseUnitOfWorkProvider _uowProvider; private readonly RepositoryFactory _repositoryFactory; @@ -28,7 +28,7 @@ namespace Umbraco.Core.Services /// /// Id of the /// A object - public Relation GetById(int id) + public IRelation GetById(int id) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { @@ -41,7 +41,7 @@ namespace Umbraco.Core.Services /// /// Id of the /// A object - public RelationType GetRelationTypeById(int id) + public IRelationType GetRelationTypeById(int id) { using (var repository = _repositoryFactory.CreateRelationTypeRepository(_uowProvider.GetUnitOfWork())) { @@ -54,11 +54,11 @@ namespace Umbraco.Core.Services /// /// Alias of the /// A object - public RelationType GetRelationTypeByAlias(string alias) + public IRelationType GetRelationTypeByAlias(string alias) { using (var repository = _repositoryFactory.CreateRelationTypeRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.Alias == alias); + var query = new Query().Where(x => x.Alias == alias); return repository.GetByQuery(query).FirstOrDefault(); } } @@ -68,7 +68,7 @@ namespace Umbraco.Core.Services /// /// Optional array of integer ids to return relations for /// An enumerable list of objects - public IEnumerable GetAllRelations(params int[] ids) + public IEnumerable GetAllRelations(params int[] ids) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { @@ -81,7 +81,7 @@ namespace Umbraco.Core.Services /// /// to retrieve Relations for /// An enumerable list of objects - public IEnumerable GetAllRelationsByRelationType(RelationType relationType) + public IEnumerable GetAllRelationsByRelationType(RelationType relationType) { return GetAllRelationsByRelationType(relationType.Id); } @@ -91,11 +91,11 @@ namespace Umbraco.Core.Services /// /// Id of the to retrieve Relations for /// An enumerable list of objects - public IEnumerable GetAllRelationsByRelationType(int relationTypeId) + public IEnumerable GetAllRelationsByRelationType(int relationTypeId) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.RelationTypeId == relationTypeId); + var query = new Query().Where(x => x.RelationTypeId == relationTypeId); return repository.GetByQuery(query); } } @@ -105,7 +105,7 @@ namespace Umbraco.Core.Services /// /// Optional array of integer ids to return relationtypes for /// An enumerable list of objects - public IEnumerable GetAllRelationTypes(params int[] ids) + public IEnumerable GetAllRelationTypes(params int[] ids) { using (var repository = _repositoryFactory.CreateRelationTypeRepository(_uowProvider.GetUnitOfWork())) { @@ -118,11 +118,11 @@ namespace Umbraco.Core.Services /// /// Id of the parent to retrieve relations for /// An enumerable list of objects - public IEnumerable GetByParentId(int id) + public IEnumerable GetByParentId(int id) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.ParentId == id); + var query = new Query().Where(x => x.ParentId == id); return repository.GetByQuery(query); } } @@ -132,11 +132,11 @@ namespace Umbraco.Core.Services /// /// Id of the child to retrieve relations for /// An enumerable list of objects - public IEnumerable GetByChildId(int id) + public IEnumerable GetByChildId(int id) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.ChildId == id); + var query = new Query().Where(x => x.ChildId == id); return repository.GetByQuery(query); } } @@ -147,11 +147,11 @@ namespace Umbraco.Core.Services /// /// Id of the child or parent to retrieve relations for /// An enumerable list of objects - public IEnumerable GetByParentOrChildId(int id) + public IEnumerable GetByParentOrChildId(int id) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.ChildId == id || x.ParentId == id); + var query = new Query().Where(x => x.ChildId == id || x.ParentId == id); return repository.GetByQuery(query); } } @@ -161,12 +161,12 @@ namespace Umbraco.Core.Services /// /// Name of the to retrieve Relations for /// An enumerable list of objects - public IEnumerable GetByRelationTypeName(string relationTypeName) + public IEnumerable GetByRelationTypeName(string relationTypeName) { List relationTypeIds = null; using (var repository = _repositoryFactory.CreateRelationTypeRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.Name == relationTypeName); + var query = new Query().Where(x => x.Name == relationTypeName); var relationTypes = repository.GetByQuery(query); if (relationTypes.Any()) { @@ -175,7 +175,7 @@ namespace Umbraco.Core.Services } if (relationTypeIds == null) - return Enumerable.Empty(); + return Enumerable.Empty(); return GetRelationsByListOfTypeIds(relationTypeIds); } @@ -185,12 +185,12 @@ namespace Umbraco.Core.Services /// /// Alias of the to retrieve Relations for /// An enumerable list of objects - public IEnumerable GetByRelationTypeAlias(string relationTypeAlias) + public IEnumerable GetByRelationTypeAlias(string relationTypeAlias) { List relationTypeIds = null; using (var repository = _repositoryFactory.CreateRelationTypeRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.Alias == relationTypeAlias); + var query = new Query().Where(x => x.Alias == relationTypeAlias); var relationTypes = repository.GetByQuery(query); if (relationTypes.Any()) { @@ -199,7 +199,7 @@ namespace Umbraco.Core.Services } if (relationTypeIds == null) - return Enumerable.Empty(); + return Enumerable.Empty(); return GetRelationsByListOfTypeIds(relationTypeIds); } @@ -209,11 +209,11 @@ namespace Umbraco.Core.Services /// /// Id of the to retrieve Relations for /// An enumerable list of objects - public IEnumerable GetByRelationTypeId(int relationTypeId) + public IEnumerable GetByRelationTypeId(int relationTypeId) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.RelationTypeId == relationTypeId); + var query = new Query().Where(x => x.RelationTypeId == relationTypeId); return repository.GetByQuery(query); } } @@ -224,7 +224,7 @@ namespace Umbraco.Core.Services /// Relation to retrieve child object from /// Optional bool to load the complete object graph when set to False /// An - public IUmbracoEntity GetChildEntityFromRelation(Relation relation, bool loadBaseType = false) + public IUmbracoEntity GetChildEntityFromRelation(IRelation relation, bool loadBaseType = false) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); return _entityService.Get(relation.ChildId, objectType, loadBaseType); @@ -236,7 +236,7 @@ namespace Umbraco.Core.Services /// Relation to retrieve parent object from /// Optional bool to load the complete object graph when set to False /// An - public IUmbracoEntity GetParentEntityFromRelation(Relation relation, bool loadBaseType = false) + public IUmbracoEntity GetParentEntityFromRelation(IRelation relation, bool loadBaseType = false) { var objectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); return _entityService.Get(relation.ParentId, objectType, loadBaseType); @@ -248,7 +248,7 @@ namespace Umbraco.Core.Services /// Relation to retrieve parent and child object from /// Optional bool to load the complete object graph when set to False /// Returns a Tuple with Parent (item1) and Child (item2) - public Tuple GetEntitiesFromRelation(Relation relation, bool loadBaseType = false) + public Tuple GetEntitiesFromRelation(IRelation relation, bool loadBaseType = false) { var childObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ChildObjectType); var parentObjectType = UmbracoObjectTypesExtensions.GetUmbracoObjectType(relation.RelationType.ParentObjectType); @@ -265,7 +265,7 @@ namespace Umbraco.Core.Services /// List of relations to retrieve child objects from /// Optional bool to load the complete object graph when set to False /// An enumerable list of - public IEnumerable GetChildEntitiesFromRelations(IEnumerable relations, bool loadBaseType = false) + public IEnumerable GetChildEntitiesFromRelations(IEnumerable relations, bool loadBaseType = false) { foreach (var relation in relations) { @@ -280,7 +280,7 @@ namespace Umbraco.Core.Services /// List of relations to retrieve parent objects from /// Optional bool to load the complete object graph when set to False /// An enumerable list of - public IEnumerable GetParentEntitiesFromRelations(IEnumerable relations, + public IEnumerable GetParentEntitiesFromRelations(IEnumerable relations, bool loadBaseType = false) { foreach (var relation in relations) @@ -297,7 +297,7 @@ namespace Umbraco.Core.Services /// Optional bool to load the complete object graph when set to False /// An enumerable list of with public IEnumerable> GetEntitiesFromRelations( - IEnumerable relations, + IEnumerable relations, bool loadBaseType = false) { foreach (var relation in relations) @@ -319,7 +319,7 @@ namespace Umbraco.Core.Services /// Child entity /// The type of relation to create /// The created - public Relation Relate(IUmbracoEntity parent, IUmbracoEntity child, RelationType relationType) + public IRelation Relate(IUmbracoEntity parent, IUmbracoEntity child, IRelationType relationType) { //Ensure that the RelationType has an indentity before using it to relate two entities if(relationType.HasIdentity == false) @@ -343,7 +343,7 @@ namespace Umbraco.Core.Services /// Child entity /// Alias of the type of relation to create /// The created - public Relation Relate(IUmbracoEntity parent, IUmbracoEntity child, string relationTypeAlias) + public IRelation Relate(IUmbracoEntity parent, IUmbracoEntity child, string relationTypeAlias) { var relationType = GetRelationTypeByAlias(relationTypeAlias); if(relationType == null || string.IsNullOrEmpty(relationType.Alias)) @@ -365,11 +365,11 @@ namespace Umbraco.Core.Services /// /// to check for relations /// Returns True if any relations exists for the given , otherwise False - public bool HasRelations(RelationType relationType) + public bool HasRelations(IRelationType relationType) { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.RelationTypeId == relationType.Id); + var query = new Query().Where(x => x.RelationTypeId == relationType.Id); return repository.GetByQuery(query).Any(); } } @@ -383,7 +383,7 @@ namespace Umbraco.Core.Services { using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { - var query = new Query().Where(x => x.ParentId == id || x.ChildId == id); + var query = new Query().Where(x => x.ParentId == id || x.ChildId == id); return repository.GetByQuery(query).Any(); } } @@ -392,7 +392,7 @@ namespace Umbraco.Core.Services /// Saves a /// /// Relation to save - public void Save(Relation relation) + public void Save(IRelation relation) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) @@ -406,7 +406,7 @@ namespace Umbraco.Core.Services /// Saves a /// /// RelationType to Save - public void Save(RelationType relationType) + public void Save(IRelationType relationType) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) @@ -420,7 +420,7 @@ namespace Umbraco.Core.Services /// Deletes a /// /// Relation to Delete - public void Delete(Relation relation) + public void Delete(IRelation relation) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) @@ -434,7 +434,7 @@ namespace Umbraco.Core.Services /// Deletes a /// /// RelationType to Delete - public void Delete(RelationType relationType) + public void Delete(IRelationType relationType) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationTypeRepository(uow)) @@ -448,12 +448,12 @@ namespace Umbraco.Core.Services /// Deletes all objects based on the passed in /// /// to Delete Relations for - public void DeleteRelationsOfType(RelationType relationType) + public void DeleteRelationsOfType(IRelationType relationType) { var uow = _uowProvider.GetUnitOfWork(); using (var repository = _repositoryFactory.CreateRelationRepository(uow)) { - var query = new Query().Where(x => x.RelationTypeId == relationType.Id); + var query = new Query().Where(x => x.RelationTypeId == relationType.Id); var list = repository.GetByQuery(query).ToList(); foreach (var relation in list) @@ -465,15 +465,15 @@ namespace Umbraco.Core.Services } #region Private Methods - private IEnumerable GetRelationsByListOfTypeIds(IEnumerable relationTypeIds) + private IEnumerable GetRelationsByListOfTypeIds(IEnumerable relationTypeIds) { - var relations = new List(); + var relations = new List(); using (var repository = _repositoryFactory.CreateRelationRepository(_uowProvider.GetUnitOfWork())) { foreach (var relationTypeId in relationTypeIds) { int id = relationTypeId; - var query = new Query().Where(x => x.RelationTypeId == id); + var query = new Query().Where(x => x.RelationTypeId == id); relations.AddRange(repository.GetByQuery(query).ToList()); } } diff --git a/src/Umbraco.Core/Services/ServiceContext.cs b/src/Umbraco.Core/Services/ServiceContext.cs index f587beb638..fa6f8a876f 100644 --- a/src/Umbraco.Core/Services/ServiceContext.cs +++ b/src/Umbraco.Core/Services/ServiceContext.cs @@ -23,7 +23,7 @@ namespace Umbraco.Core.Services private Lazy _packagingService; private Lazy _serverRegistrationService; private Lazy _entityService; - private Lazy _relationService; + private Lazy _relationService; private Lazy _treeService; private Lazy _sectionService; private Lazy _macroService; @@ -51,8 +51,8 @@ namespace Umbraco.Core.Services IFileService fileService, ILocalizationService localizationService, PackagingService packagingService, - IEntityService entityService, - RelationService relationService, + IEntityService entityService, + IRelationService relationService, ISectionService sectionService, IApplicationTreeService treeService) { @@ -64,7 +64,7 @@ namespace Umbraco.Core.Services _localizationService = new Lazy(() => localizationService); _packagingService = new Lazy(() => packagingService); _entityService = new Lazy(() => entityService); - _relationService = new Lazy(() => relationService); + _relationService = new Lazy(() => relationService); _sectionService = new Lazy(() => sectionService); _treeService = new Lazy(() => treeService); } @@ -130,7 +130,7 @@ namespace Umbraco.Core.Services _entityService = new Lazy(() => new EntityService(provider, repositoryFactory.Value, _contentService.Value, _contentTypeService.Value, _mediaService.Value, _dataTypeService.Value)); if (_relationService == null) - _relationService = new Lazy(() => new RelationService(provider, repositoryFactory.Value, _entityService.Value)); + _relationService = new Lazy(() => new RelationService(provider, repositoryFactory.Value, _entityService.Value)); if (_treeService == null) _treeService = new Lazy(() => new ApplicationTreeService(cache)); @@ -172,7 +172,7 @@ namespace Umbraco.Core.Services /// /// Gets the /// - public RelationService RelationService + public IRelationService RelationService { get { return _relationService.Value; } } diff --git a/src/Umbraco.Tests/Persistence/Repositories/RelationRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/RelationRepositoryTest.cs index c727e04fd4..ad8d001596 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/RelationRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/RelationRepositoryTest.cs @@ -211,7 +211,7 @@ namespace Umbraco.Tests.Persistence.Repositories { // Act - var query = Query.Builder.Where(x => x.ParentId == 1046); + var query = Query.Builder.Where(x => x.ParentId == 1046); int count = repository.Count(query); // Assert @@ -230,7 +230,7 @@ namespace Umbraco.Tests.Persistence.Repositories { // Act - var query = Query.Builder.Where(x => x.RelationTypeId == 2); + var query = Query.Builder.Where(x => x.RelationTypeId == 2); var relations = repository.GetByQuery(query); // Assert diff --git a/src/Umbraco.Tests/Persistence/Repositories/RelationTypeRepositoryTest.cs b/src/Umbraco.Tests/Persistence/Repositories/RelationTypeRepositoryTest.cs index 1cea1b4bb3..8be923f935 100644 --- a/src/Umbraco.Tests/Persistence/Repositories/RelationTypeRepositoryTest.cs +++ b/src/Umbraco.Tests/Persistence/Repositories/RelationTypeRepositoryTest.cs @@ -201,7 +201,7 @@ namespace Umbraco.Tests.Persistence.Repositories { // Act - var query = Query.Builder.Where(x => x.Alias.StartsWith("relate")); + var query = Query.Builder.Where(x => x.Alias.StartsWith("relate")); int count = repository.Count(query); // Assert @@ -220,7 +220,7 @@ namespace Umbraco.Tests.Persistence.Repositories // Act var childObjType = new Guid(Constants.ObjectTypes.DocumentType); - var query = Query.Builder.Where(x => x.ChildObjectType == childObjType); + var query = Query.Builder.Where(x => x.ChildObjectType == childObjType); var result = repository.GetByQuery(query); // Assert