I have this method Verify_X which is called during databind for a listbox selected value. The problem is the strongly typed datasource. I want to use the abstract class BaseDataSource or an interface to call the methods supported: Parameters[] and Select(), Instead of using the most specific implementation as seen below.
This is so one method can be used for all the different types of datasources I have instead of having a method for each. They all inherit the same way.
Here is the chain of inheritance / implementation
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()
The BaseDataSource has the methods and properties I need. DseDataSource is implemented the following way:
public class DseDataSource : ProviderDataSource<SCCS.BLL.Dse, DseKey>
I know it is possible to edit the class DseDataSource, add an interface to access Parameters[] and Select(), then program against that, which allows what I want, but this requires editing the NetTiers libraries and I am curious to see if this can be done since it seemed so difficult.
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;
}
The end result would be code such as:
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;
}