Linked Questions

Popular Questions

I'm writing a rest framework, I have a validator for entity entries which I'd like to validate on save changes at the context level.

I'm looking for a quick way to obtain property infos which will be known at compile time.

I'm obtaining the property info like so currently.

var propertyInfo = typeof(ConversationSubscription)
    .GetProperty(nameof(ConversationSubscription.IsSubscribed));

this.RuleFor(x => x.Entity<ConversationSubscription>().IsSubscribed)
    .Must(this.BeSubscriber)
    .When(x => ValidatorByStateExtensions.Was(x, propertyInfo, x.EntityEntryManager));

Granted, most of my validator are stateless so they could be registered as singletons which would avoid the problem, However some of my validators aren't.

I could create a map in the DI Container to obtain the properyInfo based on the type e.g

public interface IPropertyTypeMapContainer
{
     void RegisterType(Type type);
     PropertyInfo GetInfoByName(Type type, string propertyName);
}

But I don't like this, because I find myself writing several micro-caches to solve similar problems.

Is more standardized way to cache and obtain PropertyInfo?

Related Questions