I have a controller that has 3 methods with the same method body. The only difference is the method type. I have a generic type interface that I'm injecting into the controller. I'm injecting the interface because I'm using an IoC (StructureMap).
The problem is that the return type of the controller methods doesn't match the generic type of the method in my interface. (GetStatistics1 and GetStatistics2)
For demonstration purposes, I changed the 3rd method (GetStatistics3). I have casted the return type from IEnumerable<T> to IEnumerable<Type3>. The code compiles, but I'm not sure if that is the most efficient or eloquent solution.
The code is below:
Interface:
public interface IStatistics<T>
{
IEnumerable<T> ExecStatistics_SP(DateTime ? date,
int ? xd, string iap, int? statisticsType );
}
Controller:
public class StatisticsController : ApiController
{
private readonly IStatistics<T> _repo;
public StatisticsController(IStatistics<T> repo)
{
_repo = repo;
}
public IEnumerable<Type1> GetStatistics1(string iap, DateTime date, int? statisticsType, int xd)
{
return _repo.ExecStatistics_SP(date,xd,iap,statisticsType).AsEnumerable();
}
public IEnumerable<Type2> GetStatistics2(string aip, DateTime date, int? statisticsType, int xd)
{
return = _repo.ExecStatistics_SP(date, xd, iap, statisticsType).AsEnumerable();
}
public IEnumerable<Type3> GetStatistics3(string iap, DateTime date, int? statisticsType, int xd)
{
returnValue = _repo.ExecStatistics_SP(date,xd,iap,statisticsType).AsEnumerable();
return (IEnumerable<Type3>) returnValue;
}
}