Get needed properties of a class

.net-core c# entity-framework-core

Question

I have about 15 entities for which I have to get id and name properties. First, I check whether the properties can be pulled from local cache. If not, then I fetch them by code from DB and save in cache.

Here's my code just for two entities:

public class GetParamsService : IGetParamsService
{
    private readonly IMemoryCache _cache;
    private readonly MemoryCacheEntryOptions _cacheOptions;
    private readonly IDealTypeRepository _dealTypeRepository;
    private readonly ICurrencyRepository _currencyRepository;      

    public GetParamsService(IMemoryCache memoryCache, 
        IDealTypeRepository dealTypeRepository,
        ICurrencyRepository currencyRepository)
    {
        _cache = memoryCache;
        _cacheOptions = new MemoryCacheEntryOptions()
            .SetSlidingExpiration(TimeSpan.FromHours(2));

        _dealTypeRepository = dealTypeRepository;
        _currencyRepository = currencyRepository;
    }

    public async Task<(int idDealType, string dealTypeName)> GetDealTypeParams(
        string dealTypeCode)
    {
        if (!_cache.TryGetValue(CacheKeys.IdDealType, out int idDealType)
            | !_cache.TryGetValue(CacheKeys.DealTypeName, out string dealTypeName))
        {
            var dealType = await _dealTypeRepository
                .Get(x => x.Code == dealTypeCode, dealTypeCode);

            idDealType = dealType.IdDealType;
            dealTypeName = dealType.Name;

            _cache.Set(CacheKeys.IdDealType, idDealType, _cacheOptions);
            _cache.Set(CacheKeys.DealTypeName, dealTypeName, _cacheOptions);
        }

        return (idDealType, dealTypeName);
    }

    public async Task<(int idCurrency, string currencyName)> GetCurrencyParams(
        string currencyCode)
    {
        if (!_cache.TryGetValue(CacheKeys.IdCurrency, out int idCurrency)
            | !_cache.TryGetValue(CacheKeys.CurrencyName, out string currencyName))
        {
            var currency = await _currencyRepository
                .Get(x => x.Code == currencyCode, currencyCode);

            idCurrency = currency.IdCurrency;
            currencyName = currency.Name;

            _cache.Set(CacheKeys.IdCurrency, idCurrency, _cacheOptions);
            _cache.Set(CacheKeys.CurrencyName, currencyName, _cacheOptions);
        }

        return (idCurrency, currencyName);
    }
}

So, the methods GetDealTypeParams and GetCurrencyParams are pretty much the same and I want to make one generic method instead of many similar methods. I guess it makes sense.

The problem is that I don't know how to get "the right properties for given entity" in CacheKeys class:

public static class CacheKeys
{
    public static string IdDealType => "_IdDealType";

    public static string DealTypeName => "_DealTypeName";

    public static string IdCurrency => "_IdCurrency";

    public static string CurrencyName => "_CurrencyName";

    // ...

}

Every repository is inheritted from GenericRepository with Get method:

public class DealTypeRepository : GenericRepository<DealTypeEntity>, IDealTypeRepository
{       
    public DealTypeRepository(DbContextOptions<MyContext> dbContextOptions)
        : base(dbContextOptions)
    {

    }
}

public class GenericRepository<TEntity> where TEntity : class
{
    private readonly DbContextOptions<MyContext> _dbContextOptions;

    public GenericRepository(DbContextOptions<MyContext> dbContextOptions)
    {
        _dbContextOptions = dbContextOptions;
    }

    public async Task<TEntity> Get(Expression<Func<TEntity, bool>> predicate, string code)
    {
        try
        {
            using (var db = new MyContext(_dbContextOptions))
            {
                using (var tr = db.Database.BeginTransaction(
                    IsolationLevel.ReadUncommitted))
                {
                    var entity = await db.Set<TEntity>().AsNoTracking()
                        .FirstAsync(predicate);

                    tr.Commit();

                    return entity;
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception("Error on getting entity by code: {code}");
        }
    }
}

Can you please guide me how can I retrieve the needed properties of CacheKeys class to write a generic method ? I guess it can be easily done with reflection.

UPDATE: I'm not sure whether I have to try one generic method as every entity has Id property with its own name (for example, IdDealType for dealType, IdCurrency for currency)

1
2
10/11/2018 2:42:58 PM

Accepted Answer

Before I start: This solution assumes that all your entities follow the naming conventions you showed on your sample code.

First, it would be better if you had a repository exclusive to this service, where it would be possible to query by any entity type. There, you would use your convention to get those properties names, and you could query them by using EF.Property. As all your queries seem to be on the Code column, we can also simplify the parameters on that repo's method.

public class ParamRepository : IParamRepository
{
    private readonly DbContextOptions<MyContext> _dbContextOptions;

    public ParamRepository(DbContextOptions<MyContext> dbContextOptions)
    {
        _dbContextOptions = dbContextOptions;
    }

    public async Task<(int id, string name)> GetParamsByCode<TEntity>(string code) where TEntity : class
    {
        string entityName = typeof(TEntity).Name;
        string idProp = $"Id{entityName}";
        string nameProp = $"{entityName}Name";
        try
        {
            using (var db = new MyContext(_dbContextOptions))
            {
              var entity = await db.Set<TEntity>().AsNoTracking()
                        .Where(p => EF.Property<string>(p, "Code") == code)
                        .Select(p => new { Id = EF.Property<int>(p, idProp), Name = EF.Property<string>(p, nameProp)})
                        .FirstAsync();

              return (id: entity.Id, name: entity.Name);
            }
        }
        catch (Exception e)
        {
            throw new Exception("Error on getting entity by code: {code}");
        }
    }
}

You would also need to refactor your cache keys to be created from conventions:

public static class CacheKeys
{
    public static string GetIdKey<TEntity>() => $"_Id{typeof(TEntity).Name}";
    public static string GetNameKey<TEntity>() => $"_{typeof(TEntity).Name}Name";
}

Then, it gets easy on the GetParamsService:

public class GetParamsService
{
    private readonly IMemoryCache _cache;
    private readonly MemoryCacheEntryOptions _cacheOptions;
    private readonly IParamRepository _paramRepository;

    public GetParamsService(IMemoryCache memoryCache,
        IParamRepository paramRepository)
    {
        _cache = memoryCache;
        _cacheOptions = new MemoryCacheEntryOptions()
            .SetSlidingExpiration(TimeSpan.FromHours(2));

        _paramRepository = paramRepository;
    }

    public async Task<(int id, string name)> GetParams<TEntity>(string code) where TEntity : class
    {
        string cacheIdKey = CacheKeys.GetIdKey<TEntity>();
        string cacheNameKey = CacheKeys.GetNameKey<TEntity>();
        if (!_cache.TryGetValue(cacheIdKey, out int cacheId)
            | !_cache.TryGetValue(cacheNameKey, out string cacheName))
        {
            var param = await _paramRepository.GetParamsByCode<TEntity>(code);

            cacheId = param.id;
            cacheName = param.name;

            _cache.Set(cacheIdKey, cacheId, _cacheOptions);
            _cache.Set(cacheNameKey, cacheName, _cacheOptions);
        }

        return (cacheId, cacheName);
    }
}
2
10/15/2018 7:35:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow