There's a few questions on SO about StructureMap and generics, and I've read a few blog posts about it, but still I'm struggling to figure out the solution to this particular scenario.
Given:
public interface ILookup
{
}
public interface ISupplier : ILookup
{
}
public interface ITenant : ILookup
{
}
public class Supplier : ISupplier
{
}
public class Tenant : ITenant
{
}
public interface ILookupRepository<TLookup> where TLookup : ILookup
{
void DoSomethingWith(TLookup lookup);
}
public class LookupRepository<TLookup> : ILookupRepository<TLookup> where TLookup : ILookup
{
public void DoSomethingWith(TLookup lookup)
{
SaveOrWhatever(lookup);
}
}
public class SomeClass
{
public void ProcessLookup(ILookup lookup)
{
// Get hold of a concrete class for ILookupRepository<TLookup> where TLookup is the type of the supplied
// lookup. For example, if ProcessLookup is passed an ISupplier I want a LookupRepository<ISupplier>.
// If it's passed an ITenant I want a LookupRepository<ITenant>.
// var repository = ???
repository.DoSomethingWith(lookup);
}
}
How do I get StructureMap to supply SomeClass.ProcessLookup with the corresponding LookupRepository<ISupplier>
or LookupRepository<ITenant>
? Is this possible without reflection? Can I alternatively get a LookupRepository<Supplier>
or LookupRepository<Tenant>
instead?
Update:
Having read Richard's initial response I've realised that my original question didn't express my problem very well (it is Friday after all!). I'm dealing with more than just one implementation of ILookup and want to be able to get the right ILookupRepository for the type I'm supplied. I've updated the question above to hopefully reflect my requirements more accurately.
Update 2:
The immediate need to crack this has passed as we've taken a slightly different approach. However I'm still interested in hearing additional suggestions to Richard's.