I'm struggling to make StructureMap use one of concrete types sharing a common interface. This is further complicated by the fact that all candidate objects are descendants of an intermediate abstract class.
public interface ICustomer
{
string Id { get; }
}
public abstract class CommonCustomer : ICustomer {
public abstract string Id { get; }
}
// Fallback type if none matched
public class BaseCustomer : CommonCustomer
{
public override string Id { get; } = "Base";
}
// Concrete type 1
public class AlphaCustomer : CommonCustomer
{
public override string Id { get; } = "Alpha";
}
// Concrete type 2
public class BravoCustomer : CommonCustomer
{
public override string Id { get; } = "Bravo";
}
What I tried so far:
Scan(x =>
{
x.TheCallingAssembly();
x.AddAllTypesOf<ICustomer>();
});
var key = "Alpha";
For<ICustomer>().Use("",
context => context.GetAllInstances<ICustomer>()
.FirstOrDefault(x => x.Id == key)).Singleton();
For<ICustomer>().UseIfNone<BaseCustomer>().Singleton();
How can I select a concrete type based on it's string property? And how do I scan through types which do not directly implement ICustomer?
CommonCustomerimplementsICustomer. - ttaaoossuuuukeychanges? It looks like you need a factory to instantiate your ICustomer. - Torbjörn Hansson