我有一个基类,我将调用 TypeBase 和几个从它派生的类,因为笑声让我们称它们为 TypeImage 和 TypeAsset。以下是代码中发生的情况:
...
TypeBase b = null;
MethodDoingStuff(passedID, ref b);
RepositoryCall(b, otherArgs);
所以在 MethodDoingStuff 我们有这样的东西:
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;
}
因此,Repository 调用的方法 sig 如下所示:
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;
}
问题出现在cn.Get<T>(id)
对 Dapper Extension 的调用中,它查找的表是基于该类型参数的,它当然将其视为 TypeBase。相关的表格数据当然是在表格 TypeImage 或 TypeAsset 或其他表格中。我的自由主要在于存储库:我可以更改方法的工作方式或引入重载。我也可以更改传递给 MethodDoingStuff() 的参数,但由于各种原因不能真正更改 MethodDoingStuff 本身。