1

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
    In the future, please do not cross-post questions between Stack Exchange sites. If your question is off-topic where you originally post it, moderators can migrate it to the other site. – Thomas Owens Apr 15 '14 at 16:44
  • I wasn't aware that you could migrate questions. Thanks – user3096080 Apr 15 '14 at 16:51
1

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
    Thanks Scott, the first option looks just the ticket! I'll give this a try – user3096080 Apr 15 '14 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.