使用 Ninject 进行多重依赖注入的问题

发布于 2024-10-06 07:42:29 字数 1778 浏览 6 评论 0原文

作为免责声明,我想说的是,我仍在尝试理解整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

维持三分热 2024-10-13 07:42:29

看起来一切都解决得很好,但是您的意思是让 Oil.Apply() 使用 string.Format() 吗?

class Oil : IMaterial
{
    public string Apply(string surface)
    {
        return string.Format("painted with oil on {0}", surface);
    }
}

这应该返回“Peter Gibbons 用油画在画布上画的”。

It looks like everything is resolving fine, but do you mean for Oil.Apply() to use string.Format()?

class Oil : IMaterial
{
    public string Apply(string surface)
    {
        return string.Format("painted with oil on {0}", surface);
    }
}

This should return "Peter Gibbons painted with oil on canvas".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文