编程语言
首页 > 编程语言> > c#-Ninject工厂模式和绑定

c#-Ninject工厂模式和绑定

作者:互联网

我正在尝试实现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