以下のコード サンプルを検討してください。
public interface IMyInterface
{
void SetName(string name);
string GetName();
}
public class MyInterfaceImplementor1 : IMyInterface
{
protected string Name { set; get; }
public void SetName(string name)
{
this.Name = name;
}
public virtual string GetName()
{
return this.Name;
}
}
public class MyInterfaceImplementor2 : MyInterfaceImplementor1
{
public override string GetName()
{
return String.Format("Hello! {0}", base.Name);
}
}
このための DI 構成: ( StructureMapコード スニペットが提供されます)
ObjectFactory.Configure(x =>
{
x.For<IMyInterface>()
.Use<MyInterfaceImplementor1>();
});
ObjectFactory.Configure(x =>
{
x.For<IMyInterface>()
.Use<MyInterfaceImplementor2>();
});
私のコードでは、ある時点でMyInterfaceImplementor1を使用し、別の時点でMyInterfaceImplementor2を使用しているとします。私の質問は、DI フレームワーク (StructureMap またはその他) が上記の構成をどのように解決するかということです。また、 MyInterfaceImplementor1 のインスタンスを返す場所と、 MyInterfaceImplementor2 のインスタンスをいつ返すかをどのように決定しますか? または私はここで何か間違ったことをしていますか?