リストボックスで選択された値のデータバインド中に呼び出されるこのメソッドVerify_Xがあります。問題は、厳密に型指定されたデータソースです。以下に示すように、最も具体的な実装を使用する代わりに、抽象クラス BaseDataSource またはインターフェイスを使用して、サポートされているメソッドを呼び出したい: Parameters[] および Select()。
これは、それぞれにメソッドを用意する代わりに、私が持っているさまざまなタイプのデータソースすべてに 1 つのメソッドを使用できるようにするためです。それらはすべて同じ方法で継承されます。
継承・実装の連鎖はこちら
public class DseDataSource : ProviderDataSource<SCCS.BLL.Dse, DseKey>
public abstract class ProviderDataSource<Entity, EntityKey> : BaseDataSource<Entity, EntityKey>, ILinkedDataSource, IListDataSource
where Entity : SCCS.BLL.IEntityId<EntityKey>, new()
where EntityKey : SCCS.BLL.IEntityKey, new()
public abstract class BaseDataSource<Entity, EntityKey> : DataSourceControl, IListDataSource, IDataSourceEvents
where Entity : new()
where EntityKey : new()
BaseDataSource には、必要なメソッドとプロパティがあります。DseDataSource は次の方法で実装されます。
public class DseDataSource : ProviderDataSource<SCCS.BLL.Dse, DseKey>
クラス DseDataSource を編集し、Parameters[] および Select() にアクセスするためのインターフェイスを追加してから、それに対してプログラムすることが可能であることはわかっています。とても難しそうだったのでできます。
public static string Verify_DSE(string valueToBind, DseDataSource dataSource)
{
if (ListContainsValue(dataSource.GetEntityList(), valueToBind)) return valueToBind;
CustomParameter p = dataSource.Parameters["WhereClause"] as CustomParameter;
if (p != null)
{
p.Value = "IsActive=true OR Id=" + valueToBind;
dataSource.Select();
return valueToBind;
}
return string.Empty;
}
private static bool ListContainsValue(IEnumerable list, string value)
{
if (value.Length == 0) return true;
foreach (object o in list)
{
IEntity entity = o as IEntity;
if (entity != null)
{
if (entity.Id.ToString() == value)
return true;
}
}
return false;
}
最終結果は次のようなコードになります。
public static string Verify(string valueToBind, object dataSource)
{
//what is the correct way to convert from object
BaseDataSource baseInstance = dataSource as BaseDataSource;
if baseInstance != null)
{
if (ListContainsValue(baseInstance.GetEntityList(), valueToBind)) return valueToBind;
CustomParameter p = baseInstance.Parameters["WhereClause"] as CustomParameter;
if (p != null)
{
p.Value = "IsActive=true OR Id=" + valueToBind;
baseInstance.Select();
return valueToBind;
}
}
return string.Empty;
}