From c6199cc0be589b447fe7c12f313b101fd18e5861 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Wed, 11 Sep 2024 10:03:04 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20Entfernte=20nicht=20ben=C3=B6tigte?= =?UTF-8?q?=20`=5FkeyPropertyInfo`=20und=20aktualisierte=20`CreateAsync`?= =?UTF-8?q?=20Methode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Entfernte `_keyPropertyInfo` und die zugehörige Methode `KeyValueOf`, da sie nicht mehr benötigt wird. - Aktualisierte `CreateAsync` Methode, um direkt `createdEntity.Id` zurückzugeben. --- DigitalData.Core.Application/CRUDService.cs | 26 +-------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/DigitalData.Core.Application/CRUDService.cs b/DigitalData.Core.Application/CRUDService.cs index d737680..daadf67 100644 --- a/DigitalData.Core.Application/CRUDService.cs +++ b/DigitalData.Core.Application/CRUDService.cs @@ -1,8 +1,6 @@ using DigitalData.Core.Abstractions.Application; using DigitalData.Core.Abstractions.Infrastructure; using AutoMapper; -using System.Reflection; -using System.ComponentModel.DataAnnotations; using DigitalData.Core.DTO; using DigitalData.Core.Abstractions; using Microsoft.Extensions.Logging; @@ -22,7 +20,6 @@ namespace DigitalData.Core.Application { protected readonly TCRUDRepository _repository; protected readonly IMapper _mapper; - protected readonly PropertyInfo? _keyPropertyInfo; /// /// Initializes a new instance of the CRUDService class with the specified repository, translation service, and mapper. @@ -33,9 +30,6 @@ namespace DigitalData.Core.Application { _repository = repository; _mapper = mapper; - - _keyPropertyInfo = typeof(TEntity).GetProperties() - .FirstOrDefault(prop => Attribute.IsDefined(prop, typeof(KeyAttribute))); } /// @@ -47,7 +41,7 @@ namespace DigitalData.Core.Application { var entity = _mapper.Map(createDto); var createdEntity = await _repository.CreateAsync(entity); - return createdEntity is null ? Result.Fail() : Result.Success(KeyValueOf(createdEntity)); + return createdEntity is null ? Result.Fail() : Result.Success(createdEntity.Id); } /// @@ -120,24 +114,6 @@ namespace DigitalData.Core.Application return entity is not null; } - /// - /// Retrieves the ID value of an entity based on the defined [Key] attribute. - /// - /// The entity from which to extract the ID. - /// The ID of the entity. - protected virtual TId KeyValueOf(TEntity entity) - { - if (_keyPropertyInfo is null) - throw new InvalidOperationException($"No property with [Key] attribute found on {typeof(TEntity).Name} entity."); - - object idObj = _keyPropertyInfo?.GetValue(entity) ?? throw new InvalidOperationException($"The ID property of {typeof(TEntity).Name} entity cannot be null."); - - if (idObj is TId id) - return id; - else - throw new InvalidCastException($"The ID of {typeof(TEntity).Name} entity must be type of {typeof(TId).Name}, but it is type of {idObj.GetType().Name}."); - } - /// /// Handles exceptions that occur during CRUD operations, providing a structured string. ///