So in a nutshell I have an abstract class :
public abstract class Member
{
public string PCode { get; set; }
public string Reference { get; set; }
public DateTime? ElectedDate { get; set; }
}
And this is inherited by 3 other classes:
public class Fellow : Member
{
// some class level properties here
}
public class ForeignMember : Member
{
// some class level properties here
}
public class HonoraryMember : Member
{
// some class level properties here
}
And the usual CRUD operations are done through using a generic repository pattern:
Interface
public interface ICRMRepository<T>
{
List<T> GetAll();
T GetById(int id);
T GetByPCode(string pcode);
}
Class
public class CRMRepository<T> : ICRMRepository<T> where T : class
{
public List<T> GetAll()
{
//actions go here
}
public T GetById(int id)
{
//actions go here
}
public T GetByPCode(string pcode)
{
//actions go here
}
}
The question is if I wanted to do a query that would bring back all the Fellows, HonoraryMembers and ForeignMembers in one result set, how would I do this?
Would it be better to have an interface instead of inheritance e.g. IMember, and then the three classes use that interface? Or some solution that uses both?
2
Typically you would get ahold of an ICRMRepository<Member>
and query against that if you wanted all Members
and within your loop you’d check their actual type:
foreach(Member member in memberRepository.GetAll())
{
// do stuff here that you can do for all Members, then...
if(member is HonoraryMember)
{
var honoraryMember = member as HonoraryMember;
// do stuff here that you only do to HonoraryMembers...
}
}
In order for this to work, it’s dependent on the capabilities of your repository. Your generic Member
repository has to actually return instances of the correct class. Not all repository implementations do this, but yours could.
Most implementations use concrete types for entities, but your idea of using interfaces (IMember
) is actually worth looking into. I wrote a small application that used interfaces for my entities and I really liked it. You end up with some interesting benefits, like your Insert
method can take an instance of NewMember
and your queries can return instances of ExistingMember
(which tracks changes to itself) and both could implement the IMember
interface.
1