From 05d8b0aaf8c3f569d89ff2c0bf2b0891f461dcd0 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Fri, 9 Oct 2020 09:32:32 +0200 Subject: [PATCH] Changed to use a ReadOnlyRelation model --- src/Umbraco.Core/Models/IRelation.cs | 59 +++---------------- src/Umbraco.Core/Models/ReadOnlyRelation.cs | 35 +++++++++++ .../Repositories/IRelationRepository.cs | 2 +- .../Persistence/Factories/RelationFactory.cs | 19 +++++- .../Implement/ContentRepositoryBase.cs | 5 +- .../Implement/RelationRepository.cs | 14 +---- src/Umbraco.Web.UI.NetCore/appsettings.json | 4 +- 7 files changed, 69 insertions(+), 69 deletions(-) create mode 100644 src/Umbraco.Core/Models/ReadOnlyRelation.cs diff --git a/src/Umbraco.Core/Models/IRelation.cs b/src/Umbraco.Core/Models/IRelation.cs index 3cdea02e32..2b975ba3ae 100644 --- a/src/Umbraco.Core/Models/IRelation.cs +++ b/src/Umbraco.Core/Models/IRelation.cs @@ -4,81 +4,37 @@ using Umbraco.Core.Models.Entities; namespace Umbraco.Core.Models { - public interface IRelation : IRememberBeingDirty, IRelationReadOnly + public interface IRelation : IEntity, IRememberBeingDirty { - - /// - /// Gets or sets the integer identifier of the entity. - /// - new int Id { get; set; } - /// /// Gets or sets the Parent Id of the Relation (Source) /// [DataMember] - new int ParentId { get; set; } + int ParentId { get; set; } [DataMember] - new Guid ParentObjectType { get; set; } + Guid ParentObjectType { get; set; } /// /// Gets or sets the Child Id of the Relation (Destination) /// [DataMember] - new int ChildId { get; set; } + int ChildId { get; set; } [DataMember] - new Guid ChildObjectType { get; set; } + Guid ChildObjectType { get; set; } /// /// Gets or sets the for the Relation /// [DataMember] - new IRelationType RelationType { get; set; } + IRelationType RelationType { get; set; } /// /// Gets or sets a comment for the Relation /// [DataMember] - new string Comment { get; set; } - - /// - /// Gets the Id of the that this Relation is based on. - /// - [IgnoreDataMember] - new int RelationTypeId { get; } - } - - public interface IRelationReadOnly : IEntity - { - /// - /// Gets the integer identifier of the entity. - /// - int Id { get; } - - /// - /// Gets the Parent Id of the Relation (Source) - /// - int ParentId { get; } - - Guid ParentObjectType { get; } - - /// - /// Gets or sets the Child Id of the Relation (Destination) - /// - int ChildId { get; } - - Guid ChildObjectType { get; } - - /// - /// Gets the for the Relation - /// - IRelationType RelationType { get; } - - /// - /// Gets a comment for the Relation - /// - string Comment { get; } + string Comment { get; set; } /// /// Gets the Id of the that this Relation is based on. @@ -86,4 +42,5 @@ namespace Umbraco.Core.Models [IgnoreDataMember] int RelationTypeId { get; } } + } diff --git a/src/Umbraco.Core/Models/ReadOnlyRelation.cs b/src/Umbraco.Core/Models/ReadOnlyRelation.cs new file mode 100644 index 0000000000..f2801a93ec --- /dev/null +++ b/src/Umbraco.Core/Models/ReadOnlyRelation.cs @@ -0,0 +1,35 @@ +using System; + +namespace Umbraco.Core.Models +{ + /// + /// A read only relation. Can be used to bulk save witch performs better than the normal save operation, + /// but do not populate Ids back to the model + /// + public class ReadOnlyRelation + { + public ReadOnlyRelation(int id, int parentId, int childId, int relationTypeId, DateTime createDate, string comment) + { + Id = id; + ParentId = parentId; + ChildId = childId; + RelationTypeId = relationTypeId; + CreateDate = createDate; + Comment = comment; + } + + public ReadOnlyRelation(int parentId, int childId, int relationTypeId): this(0, parentId, childId, relationTypeId, DateTime.Now, string.Empty) + { + + } + + public int Id { get; } + public int ParentId { get; } + public int ChildId { get; } + public int RelationTypeId { get; } + public DateTime CreateDate { get; } + public string Comment { get; } + + public bool HasIdentity => Id != 0; + } +} diff --git a/src/Umbraco.Core/Persistence/Repositories/IRelationRepository.cs b/src/Umbraco.Core/Persistence/Repositories/IRelationRepository.cs index 8f17b5f622..ca40848138 100644 --- a/src/Umbraco.Core/Persistence/Repositories/IRelationRepository.cs +++ b/src/Umbraco.Core/Persistence/Repositories/IRelationRepository.cs @@ -21,7 +21,7 @@ namespace Umbraco.Core.Persistence.Repositories /// Persist multiple at once but Ids are not returned on created relations /// /// - void SaveBulk(IEnumerable relations); + void SaveBulk(IEnumerable relations); /// /// Deletes all relations for a parent for any specified relation type alias diff --git a/src/Umbraco.Infrastructure/Persistence/Factories/RelationFactory.cs b/src/Umbraco.Infrastructure/Persistence/Factories/RelationFactory.cs index 2adfe9627c..24d29cf608 100644 --- a/src/Umbraco.Infrastructure/Persistence/Factories/RelationFactory.cs +++ b/src/Umbraco.Infrastructure/Persistence/Factories/RelationFactory.cs @@ -28,7 +28,7 @@ namespace Umbraco.Core.Persistence.Factories } } - public static RelationDto BuildDto(IRelationReadOnly entity) + public static RelationDto BuildDto(IRelation entity) { var dto = new RelationDto { @@ -44,6 +44,23 @@ namespace Umbraco.Core.Persistence.Factories return dto; } + + public static RelationDto BuildDto(ReadOnlyRelation entity) + { + var dto = new RelationDto + { + ChildId = entity.ChildId, + Comment = string.IsNullOrEmpty(entity.Comment) ? string.Empty : entity.Comment, + Datetime = entity.CreateDate, + ParentId = entity.ParentId, + RelationType = entity.RelationTypeId + }; + + if (entity.HasIdentity) + dto.Id = entity.Id; + + return dto; + } } } diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs index e59acb0e81..f544d9c99b 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/ContentRepositoryBase.cs @@ -980,12 +980,11 @@ namespace Umbraco.Core.Persistence.Repositories.Implement if (!keyToIds.TryGetValue(guid, out var id)) return null; // This shouldn't happen! - return new Relation(entity.Id, id, relationType); + return new ReadOnlyRelation(entity.Id, id, relationType.Id); }).WhereNotNull(); - // Save bulk relations + // Save bulk relations< RelationRepository.SaveBulk(toSave); - } private class NodeIdKey diff --git a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs index 88231d3658..21b4ce5911 100644 --- a/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs +++ b/src/Umbraco.Infrastructure/Persistence/Repositories/Implement/RelationRepository.cs @@ -253,7 +253,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement } } - public void SaveBulk(IEnumerable relations) + public void SaveBulk(IEnumerable relations) { foreach (var hasIdentityGroup in relations.GroupBy(r => r.HasIdentity)) { @@ -265,7 +265,6 @@ namespace Umbraco.Core.Persistence.Repositories.Implement // it's the bulk inserts we care about. foreach (var relation in hasIdentityGroup) { - relation.UpdatingEntity(); var dto = RelationFactory.BuildDto(relation); Database.Update(dto); } @@ -273,16 +272,9 @@ namespace Umbraco.Core.Persistence.Repositories.Implement else { // Do bulk inserts - var entitiesAndDtos = hasIdentityGroup.ToDictionary( - r => // key = entity - { - r.AddingEntity(); - return r; - }, - RelationFactory.BuildDto); // value = DTO + var dtos = hasIdentityGroup.Select(RelationFactory.BuildDto); - - Database.InsertBulk(entitiesAndDtos.Values); + Database.InsertBulk(dtos); } } diff --git a/src/Umbraco.Web.UI.NetCore/appsettings.json b/src/Umbraco.Web.UI.NetCore/appsettings.json index 70e3d6376d..3b241a4173 100644 --- a/src/Umbraco.Web.UI.NetCore/appsettings.json +++ b/src/Umbraco.Web.UI.NetCore/appsettings.json @@ -1,6 +1,6 @@ { "ConnectionStrings": { - "umbracoDbDSN": "" + "umbracoDbDSN": "Server=(LocalDB)\\Umbraco;Database=NetCore;Integrated Security=true" }, "Serilog": { "MinimumLevel": { @@ -71,4 +71,4 @@ } } } -} +} \ No newline at end of file