It's a design question. I have a business object, and 5 business object types that are derived from it.
I also will have a class which has BindingList as a member. I will have 5 classes derived from it.
Since covariance doesn't work here, how would you structure the design to minimize code repetition? I could of course chuck the BindingList and go with DataTable in which case the problem is eliminated.
But since everyone raves about BindingList I would LOVE to see how you guys would approach this.
SOLUTION (based on Pavel Minaev's answer):
public class SampleBase
{
protected string m_seq;
protected string m_id;
protected string m_weight;
protected string m_units;
public SampleBase(string seq, string id, string weight, string units)
{
Seq = seq;
Id = id;
Weight = weight;
Units = units;
}
public SampleBase() { }
public string Seq
{
get { return m_seq; }
set { m_seq = value; }
}
public string Id
{
get { return m_id; }
set { m_id = value; }
}
public string Weight
{
get { return m_weight; }
set { m_weight = value; }
}
public string Units
{
get { return m_units; }
set { m_units = value; }
}
}
public class FwdSample : SampleBase
{
protected string m_std_id;
public FwdSample() { }
public FwdSample (string seq, string id, string weight, string units, string std_id ) : base(seq, id, weight, units)
{
StdId = std_id;
}
public string StdId
{
get { return m_std_id; }
set { m_std_id = value; }
}
}
//End of Sample Classes
public abstract class RunBase<T> where T : SampleBase , new()
{
protected BindingList<T> m_samples;
public RunBase() {}
public void Add(T sample)
{
m_samples.Add(sample);
}
public void Update(int index, T sample)
{
m_samples[index] = sample;
}
public void Delete(int index)
{
m_samples.RemoveAt(index);
}
public BindingList<T> Samples
{
get { return m_samples; }
}
}
public class FwdRun : RunBase<FwdSample>
{
public FwdRun()
{
m_samples = new BindingList<FwdSample>();
}
}