gpt4 book ai didi

c# - Ninject 工厂模式和绑定(bind)

转载 作者:行者123 更新时间:2023-11-30 16:09:46 24 4
gpt4 key购买 nike

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

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.""
}
}
}
}

我觉得我的绑定(bind)设置正确,但我一定遗漏了一些明显的东西。任何想法为什么我从上面的代码中得到异常?

最佳答案

这是由 Ninject.Extensions.Factory 的一个特性引起的。它处理以 Get 开头的方法与那些没有的不同。如果重命名 IHashableFactory.GetNewCreateMake一切正常。

描述了“Get”特征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");


因为我认为这对用户来说并不明显,并且在这方面异常(exception)根本没有帮助,我已经提交了一个问题报告 here

关于c# - Ninject 工厂模式和绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27097754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com