jeudi 3 janvier 2019

Quickly retrieve Property Info at when property is known at compile time

I'm writing a rest framework. I'm creating validator which validate entity entries at the context level.

I'd like to 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 to do this, because I find myself writing several microcaches to solve similar problems.

Is more standardized way to cache and obtain property info?





Aucun commentaire:

Enregistrer un commentaire