ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c#-Ninject工厂模式和绑定

2019-11-21 02:05:54  阅读:271  来源: 互联网

标签:factory dependency-injection ninject c


我正在尝试实现Ninject.Extensions.Factory模式,我的程序告诉我我的绑定不正确,但是我不知道为什么.我不断收到“抛出错误,激活IHashable.没有匹配的绑定可用,并且类型不是可自绑定的”异常.我的代码的相关领域如下:

public interface IHashable
{
    FileInfo File { get; }

    string ComputeHash();
}

public interface IHashableFactory
{
    IHashable GetNew(FileInfo file);
}

public class MD5ChecksumProvider : IHashable
{
    private FileInfo _file;

    public FileInfo File
    {
        get { return _file; }
    }

    public MD5ChecksumProvider(FileInfo file)
    {
        if (file == null)
            throw new ArgumentNullException("file");

        _file = file;
    }

    public string ComputeHash()
    {
        // implementation
    }
}

public class AppFileProvider : IAppFileProvider
{
    private IHashableFactory _hashFactory;

    public IHashableFactory HashProvider
    {
        get { return _hashFactory; }
    }

    public AppFileProvider(IHashableFactory hashProviderFactory)
    {
        _hashFactory = hashProviderFactory;
    }

    public string GetChecksum(FileInfo fileInfo)
    {
        var hasher = _hashFactory.GetNew(fileInfo);
        return hasher.ComputeHash();
    }
}

public class BindingProviders : NinjectModule
{
    public override void Load()
    {
        Bind<IHashable>()
            .To<MD5ChecksumProvider>();
    }
}

public class BindingFactories : NinjectModule
{
    public override void Load()
    {
        Bind<IHashableFactory>()
            .ToFactory();
    }
}

// my DI container
public sealed class Container : IDisposable
{
    private bool _isDisposed;
    private IKernel _kernel;
    private BindingFactories _bindingFactories;
    private BindingObjects _bindingObjects;
    private BindingProviders _bindingProviders;

    public Container()
    {
        _isDisposed = false;

        _bindingFactories = new BindingFactories();
        _bindingObjects = new BindingObjects();
        _bindingProviders = new BindingProviders();

        _kernel = new StandardKernel(_bindingObjects, _bindingProviders, _bindingFactories);
    }

    public T Get<T>()
    {
        return _kernel.Get<T>();
    }

    public void Dispose()
    {
        // nothing worth seeing
    }

    private void Dispose(bool disposing)
    {
        // nothing worth seeing
    }
}

// the program (composition root)
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        using (var container = new Container())
        {
            var fileProvider = container.Get<IAppFileProvider>();

            foreach (var file in files)
            {
                string hash = fileProvider.GetChecksum(storePath, file); // this line throws "Error activating IHashable. No matching bindings are available, and the type is not self-bindable.""
            }
        }
    }
}

我觉得我的绑定设置正确,但是我必须缺少明显的东西.有什么想法为什么我从上面的代码中得到异常?

解决方法:

这是由Ninject.Extensions.Factory的功能引起的.
它对待以Get开头的方法与未使用的方法有所不同.
如果将IHashableFactory.GetNew重命名为Create或Make一切正常.

here描述了“获取”功能:

The default instace provider of the extension has the convention that it tries to return an instance using a named binding whenever a method starts with “Get”. E.g. IFoo GetMySpecialFoo() is equal to

resolutionRoot.Get<IFoo>("MySpecialFoo");

由于我认为这对用户而言并不明显,并且该异常在这方面根本没有帮助,因此我提交了问题报告here

标签:factory,dependency-injection,ninject,c
来源: https://codeday.me/bug/20191121/2048543.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有