looking a way to combine these methods in generic way on same return type

.net c# entity-framework-core generics list

Question

I have got below three methods and they are returning same return type and the only difference among these three methods is that the data is getting retrieved from different tables.

Below are those methods.

public IQueryable<SpaceFunctionType> GetSpaceFunctionForA170(string airFlowSource)
{
    return _dbContext.A170.Where(s => s.AirflowSourceName == airFlowSource)
                          .Select(c => new SpaceFunctionType
                          {
                              Category = c.Category,
                              SpaceFunction = c.SpaceFunction
                          });
}

public IQueryable<SpaceFunctionType> GetSpaceFunctionForA621(string airFlowSource)
{
    return _dbContext.A621.Where(s => s.AirflowSourceName == airFlowSource)
                          .Select(c => new SpaceFunctionType
                          {
                              Category = c.Category,
                              SpaceFunction = c.SpaceFunction
                          });
}

public IQueryable<SpaceFunctionType> GetSpaceFunctionForEquipment(string airFlowSource)
{
    return _dbContext.Equipments.Where(s => s.EquipmentSourceName == airFlowSource)
                          .Select(c => new SpaceFunctionType
                          {
                              Category = c.Category,
                              SpaceFunction = c.SpaceFunction
                          });
}

Could any one please help me suggest any ideas on this how to combine these methods in a generic way. I am using Entity Framework Core.

1
0
1/21/2020 10:29:14 PM

Accepted Answer

EF DbContext has a .Set<TDomain>() method that returns the set of objects based on the type. You can literally write-

public IQueryable<SpaceFunctionType> Get<TDomain>(string airFlowSource) where TDomain: IA
{
    return _dbContext.Set<TDomain>().Where(s => s.EquipmentSourceName == airFlowSource)
                          .Select(c => new SpaceFunctionType
                          {
                              Category = c.Category,
                              SpaceFunction = c.SpaceFunction
                          });
}

Now to add common functionality, add an interface -

public interface IA {
    ... AirflowSourceName 
}

Derive from interface -

public class A170: IA
{}

public class A621 : IA 
{}

public class Equipments: IA
{}

more details https://docs.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext.set?view=entity-framework-6.2.0

UPDATE Since they are different properties. You can inject a expression lambda to compare for you.-

public IQueryable<SpaceFunctionType> Get<TDomain>(Expression<Func<TDomain, bool>> predicate) where TDomain: IA
{
    return _dbContext.Set<TDomain>().Where(predicate)
                          .Select(c => new SpaceFunctionType
                          {
                              Category = c.Category,
                              SpaceFunction = c.SpaceFunction
                      });
}

and call like this -

.Get<A70>(x => x.AirflowSourceName == "forcename");

or

.Get<Equipments>(x => x.EquipmentSourceName == "forcename");

UPDATE 2

The interface need to be updated as -

public interface IA {
    ... Category
    ... SpaceFunction
}

Without this, the Select clause will not know that there are properties with such names, but since these are common to all, adding them to interface should not be a problem..

2
1/22/2020 9:43:19 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