I have a base class I'll call TypeBase and several classes derived from it, for grins lets call them TypeImage and TypeAsset. Here's what happens in the code:
...
TypeBase b = null;
MethodDoingStuff(passedID, ref b);
RepositoryCall(b, otherArgs);
So in MethodDoingStuff we have something like this:
public bool MethodDoingStuff (long passedID, ref TypeBase b)
{
object concrete = DbCallThatGetsSubclass(passedID);//returns a subclass
of TypeBase(in reality more than 2 possibilities)
TypeBase tb = (TypeBase)concrete;
b=tb;
return true;
}
So the method sig for the Repository call looks like this:
public virtual T FindByID<T>( T typeInstance, long id) where T : TypeBase
{
T item = (T)Activator.CreateInstance(typeof(T));
using (IDbConnection cn = Connection)
{
item = cn.Get<T>(id);
}
return item;
}
The problem arises in that cn.Get<T>(id)
is a call to a Dapper Extension, the Table it looks for is based on that type parameter, which of course it sees as TypeBase. The relevant table data is of course in the tables TypeImage or TypeAsset, or whatever. My freedom mostly lies in the Repository: I can change how the method works or introduce overloads. I can also change what arguments are passed into MethodDoingStuff() but for a variety of reasons can't really change MethodDoingStuff itself.
You are casting the type to the base class. So when you use in with Dapper it sees TypeBase. This is a problem with inheritance. Using a interface might help you solve the problem.