Похожие чаты

Yes, abstracting the logic to be reusable across multiple entities

based on dynamic properties can be achieved using a combination of generics, reflection, and expressions. The idea would be to create a generic validation service that can validate any entity based on specified unique properties.

Here's a high-level approach:

### 1. Define a Generic Repository Method

First, you'd need a way to find an entity by a given property in your repositories. A generic method could look like:

public async Task<TEntity> FindByPropertyAsync<TEntity, TValue>(Expression<Func<TEntity, bool>> predicate) where TEntity : class;

### 2. Create a Generic Validation Service

This service will use reflection to validate entities based on provided properties.

public interface IUniquePropertyValidationService
{
Task<(bool IsValid, string ErrorMessage)> ValidateUniquePropertyAsync<TEntity, TValue>(
TEntity entity,
Expression<Func<TEntity, TValue>> propertySelector,
TValue value,
int? entityId = null) where TEntity : class, new();
}

public class UniquePropertyValidationService : IUniquePropertyValidationService
{
private readonly IUnitOfWork _unitOfWork;

public UniquePropertyValidationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}

public async Task<(bool IsValid, string ErrorMessage)> ValidateUniquePropertyAsync<TEntity, TValue>(
TEntity entity,
Expression<Func<TEntity, TValue>> propertySelector,
TValue value,
int? entityId = null) where TEntity : class, new()
{
// Convert the property selector to a predicate.
var parameter = Expression.Parameter(typeof(TEntity), "x");
var body = Expression.Equal(propertySelector.Body, Expression.Constant(value, typeof(TValue)));
var lambda = Expression.Lambda<Func<TEntity, bool>>(body, parameter);

var existingEntity = await _unitOfWork.YourGenericRepository.FindByPropertyAsync(lambda);

if (existingEntity != null)
{
// If we have an entityId, we're in update mode, so we check if the found entity's ID matches the updating entity's ID.
var existingEntityId = (int)typeof(TEntity).GetProperty("Id").GetValue(existingEntity);
if (!entityId.HasValue || existingEntityId != entityId.Value)
{
var propertyName = ((MemberExpression)propertySelector.Body).Member.Name;
return (false, $"{typeof(TEntity).Name} with the same {propertyName} already exists.");
}
}

return (true, string.Empty);
}
}

### 3. Use the Service

For instance, for the currency entity:

var validationOutcome = await _uniquePropertyValidationService.ValidateUniquePropertyAsync<Currency, string>(
currencyToUpdate,
x => x.CurrencyCode,
request.CurrencyCode,
request.CurrencyId);

### Caveats:

- This approach assumes all entities have an Id property of type int. Adjustments might be necessary for different setups.
- Reflection can introduce overhead, so while this generic solution is elegant, there might be performance concerns when dealing with large volumes of data or frequent operations.
- The abstraction assumes uniqueness validation is based on a single property. If there are scenarios where uniqueness is based on combinations of properties, further adjustments would be needed.
- This is a more complex solution than having dedicated validation for each entity. Ensure the complexity aligns with your project's needs.

4 ответов

45 просмотров
Leoul-G Автор вопроса

Hey Guys what Do you think of this generic uniqueness validator, it it a good idea ?

how to insert c# code on the message like this ?

Leoul-G Автор вопроса
Houdaifa Bouamine
screenshot how to insert c# code on the message like this ?

surround the code with 3 backticks and right after(or below) the opening backticks write csharp and on the next lines the code

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта