使用 Ninject 进行多重依赖注入的问题
作为免责声明,我想说的是,我仍在尝试理解整个 DI 模式,因此我想不用说我的代码可能存在重大概念性错误。
有了这个,我想做的是在以下实现中注入两个属性:
interface ISurface
{
string Use();
}
class Canvas : ISurface
{
public string Use()
{
return "canvas";
}
}
class Hardboard : ISurface
{
public string Use()
{
return "hardboard";
}
}
interface IMaterial
{
string Apply(string surface);
}
class Oil : IMaterial
{
public string Apply(string surface)
{
return "painted with oil on {0}";
}
}
class Acrylic : IMaterial
{
public string Apply(string surface)
{
return "painted with acrylic on {0}";
}
}
class Artist
{
public string Name { get; set; }
[Inject]
public IMaterial Material { get; set; }
[Inject]
public ISurface Surface { get; set; }
public string Paint()
{
return Material.Apply(Surface.Use());
}
}
class PainterModule : NinjectModule
{
public override void Load()
{
Bind<ISurface>().To<Canvas>();
Bind<IMaterial>().To<Oil>();
Bind<Artist>().ToSelf();
}
}
所以当我调用该方法时:
class Program
{
static void Main(string[] args)
{
try
{
IKernel kernel = new StandardKernel(new PainterModule());
Artist artist = kernel.Get<Artist>();
artist.Name = "Peter Gibbons";
Console.WriteLine(artist.Name + artist.Paint());
}
catch (Exception error)
{
Console.WriteLine(error.Message);
throw;
}
finally
{
Console.ReadKey(true);
}
}
}
令我惊讶的是它输出:
"Peter Gibbons painted with oil on {0}"
As a disclaimer I shall say that I'm still trying to wrap my head around the whole DI pattern therefore I guess it's needless to say that my code might probably have a major conceptual bug.
With that, what I'm trying to do is inject two properties on the following implementation:
interface ISurface
{
string Use();
}
class Canvas : ISurface
{
public string Use()
{
return "canvas";
}
}
class Hardboard : ISurface
{
public string Use()
{
return "hardboard";
}
}
interface IMaterial
{
string Apply(string surface);
}
class Oil : IMaterial
{
public string Apply(string surface)
{
return "painted with oil on {0}";
}
}
class Acrylic : IMaterial
{
public string Apply(string surface)
{
return "painted with acrylic on {0}";
}
}
class Artist
{
public string Name { get; set; }
[Inject]
public IMaterial Material { get; set; }
[Inject]
public ISurface Surface { get; set; }
public string Paint()
{
return Material.Apply(Surface.Use());
}
}
class PainterModule : NinjectModule
{
public override void Load()
{
Bind<ISurface>().To<Canvas>();
Bind<IMaterial>().To<Oil>();
Bind<Artist>().ToSelf();
}
}
So when I call the method:
class Program
{
static void Main(string[] args)
{
try
{
IKernel kernel = new StandardKernel(new PainterModule());
Artist artist = kernel.Get<Artist>();
artist.Name = "Peter Gibbons";
Console.WriteLine(artist.Name + artist.Paint());
}
catch (Exception error)
{
Console.WriteLine(error.Message);
throw;
}
finally
{
Console.ReadKey(true);
}
}
}
Surprisingly for me it outputs:
"Peter Gibbons painted with oil on {0}"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来一切都解决得很好,但是您的意思是让
Oil.Apply()
使用string.Format()
吗?这应该返回“Peter Gibbons 用油画在画布上画的”。
It looks like everything is resolving fine, but do you mean for
Oil.Apply()
to usestring.Format()
?This should return "Peter Gibbons painted with oil on canvas".