2

I am creating a repository as follows:

public class StagingRepository
{
    private IRepository<CurrentTransformer> CurrentTransformerRepository { get; set; }
    private IRepository<Equipment> EquipmentRepository { get; set; }
    private IRepository<Line> LineRepository { get; set; }
    public StagingRepository()
    {
        this.CurrentTransformerRepository = new Repository<CurrentTransformer>();
        this.EquipmentRepository = new Repository<Equipment>();
        this.LineRepository = new Repository<Line>();
    }
}

All this works... however what I am trying to add is a method within the class that returns one of the Repositories above based on a string value.

public IRepository<Object> SetRepo(string TableName)
{
    switch (TableName)
    {
        case "Line":
            return LineRepository;
            break;
    }
}

I know the above does not work but it gives you an idea of what I am trying to accomplish. My goal would be I should be able to setup something like this in my code to call the repository:

StagingRepository _repo = new StagingRepository();
var myList = _repo.SetRepo("Line").All(); //All is a function of IRepository

Any Help would be greatly appreciated.

1 Answer 1

3

You can make IRepository covariant:

public interface IRepository<out T>

This way, you can always use IRepository<Derived> in place of IRepository<Base> when returning from a method. In your case, your code will now compile and work:

public IRepository<object> SetRepo(string TableName)
{
    switch (TableName)
    {
        case "Line":
            return LineRepository;
            break;
    }
}

because Line is derived from object, so you can return IRepository<Line> for IRepository<object>.

This however won't work if you use T as an argument - type can not be both covariant and contravariant. In such case, consider moving non-generic methods from IRepository<T> to a non-generic version IRepository and make your repositories implement both. This way you could have:

public IRepository SetRepo(string TableName)
// ...

By the way, the name SetRepo is misleading - the function doesn't set anything, it rather "gets" an appropriate repository.

6
  • @kevinc I've added more explanations. Is it OK now?
    – BartoszKP
    Aug 27, 2015 at 16:37
  • I tried to put in IRepository<out t> however it will not allow due to I have other methods that are requiring T
    – kevin c
    Aug 27, 2015 at 16:40
  • @kevinc Indeed, that's a problem - a type can't be both covariant and contravariant. I'll update my answer.
    – BartoszKP
    Aug 27, 2015 at 16:42
  • @kevinc Sorry, nothing better than this (not counting reflection-overkill approaches) came to my mind.
    – BartoszKP
    Aug 27, 2015 at 17:25
  • I guess I do not understand what you mean by making generic and non-generic. Can you help?
    – kevin c
    Sep 2, 2015 at 18:13

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.