Changed to use a ReadOnlyRelation model

This commit is contained in:
Bjarke Berg
2020-10-09 09:32:32 +02:00
parent 731a189fbe
commit 05d8b0aaf8
7 changed files with 69 additions and 69 deletions

View File

@@ -4,81 +4,37 @@ using Umbraco.Core.Models.Entities;
namespace Umbraco.Core.Models
{
public interface IRelation : IRememberBeingDirty, IRelationReadOnly
public interface IRelation : IEntity, IRememberBeingDirty
{
/// <summary>
/// Gets or sets the integer identifier of the entity.
/// </summary>
new int Id { get; set; }
/// <summary>
/// Gets or sets the Parent Id of the Relation (Source)
/// </summary>
[DataMember]
new int ParentId { get; set; }
int ParentId { get; set; }
[DataMember]
new Guid ParentObjectType { get; set; }
Guid ParentObjectType { get; set; }
/// <summary>
/// Gets or sets the Child Id of the Relation (Destination)
/// </summary>
[DataMember]
new int ChildId { get; set; }
int ChildId { get; set; }
[DataMember]
new Guid ChildObjectType { get; set; }
Guid ChildObjectType { get; set; }
/// <summary>
/// Gets or sets the <see cref="RelationType"/> for the Relation
/// </summary>
[DataMember]
new IRelationType RelationType { get; set; }
IRelationType RelationType { get; set; }
/// <summary>
/// Gets or sets a comment for the Relation
/// </summary>
[DataMember]
new string Comment { get; set; }
/// <summary>
/// Gets the Id of the <see cref="RelationType"/> that this Relation is based on.
/// </summary>
[IgnoreDataMember]
new int RelationTypeId { get; }
}
public interface IRelationReadOnly : IEntity
{
/// <summary>
/// Gets the integer identifier of the entity.
/// </summary>
int Id { get; }
/// <summary>
/// Gets the Parent Id of the Relation (Source)
/// </summary>
int ParentId { get; }
Guid ParentObjectType { get; }
/// <summary>
/// Gets or sets the Child Id of the Relation (Destination)
/// </summary>
int ChildId { get; }
Guid ChildObjectType { get; }
/// <summary>
/// Gets the <see cref="RelationType"/> for the Relation
/// </summary>
IRelationType RelationType { get; }
/// <summary>
/// Gets a comment for the Relation
/// </summary>
string Comment { get; }
string Comment { get; set; }
/// <summary>
/// Gets the Id of the <see cref="RelationType"/> that this Relation is based on.
@@ -86,4 +42,5 @@ namespace Umbraco.Core.Models
[IgnoreDataMember]
int RelationTypeId { get; }
}
}

View File

@@ -0,0 +1,35 @@
using System;
namespace Umbraco.Core.Models
{
/// <summary>
/// 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
/// </summary>
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;
}
}

View File

@@ -21,7 +21,7 @@ namespace Umbraco.Core.Persistence.Repositories
/// Persist multiple <see cref="IRelation"/> at once but Ids are not returned on created relations
/// </summary>
/// <param name="relations"></param>
void SaveBulk(IEnumerable<IRelationReadOnly> relations);
void SaveBulk(IEnumerable<ReadOnlyRelation> relations);
/// <summary>
/// Deletes all relations for a parent for any specified relation type alias

View File

@@ -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;
}
}
}

View File

@@ -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

View File

@@ -253,7 +253,7 @@ namespace Umbraco.Core.Persistence.Repositories.Implement
}
}
public void SaveBulk(IEnumerable<IRelationReadOnly> relations)
public void SaveBulk(IEnumerable<ReadOnlyRelation> 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);
}
}

View File

@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"umbracoDbDSN": ""
"umbracoDbDSN": "Server=(LocalDB)\\Umbraco;Database=NetCore;Integrated Security=true"
},
"Serilog": {
"MinimumLevel": {
@@ -71,4 +71,4 @@
}
}
}
}
}