我如何为 Ninject 提供额外的参数?

我需要自动解决我的 Windows 窗体的依赖关系。唯一的问题是我的表单构造函数也需要一个整数值。请查看代码部分中的实现。


   //Ninject bindings

   public class Bindings : NinjectModule

    {

        public override void Load()

        {

            Bind<ILogger>().To<LogToDB>();

            Bind<ICopy>().To<CopyToFolder>();            

        }

    }


  //WinForm - Form1

   public partial class Form1 : Form

    {

        public readonly ILogger _processRepository;

        public readonly Icopy _copy;

        public readonly int ValueToEdit;

        public Form1(int valueToEdit, ILogger logger, ICopy copy)

        {

            this._processRepository = logger;

            this._copy = copy;

            this.ValueToEdit = valueToEdit;

            InitializeComponent();

        }

    }

    //main

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

        IKernel kernel = new StandardKernel(new Bindings());            

        Application.Run(kernel.Get<Form1>());            

    }

我得到一个错误:Ninject.ActivationException: 'Error activating int No matching bindings are available, and the type is not self-bindable.


如何自动解析表单依赖关系并传递整数值?实际上,我使用了相同的表格来进行添加和编辑,所以在编辑时,应该设置这个编辑值。


慕尼黑5688855
浏览 75回答 2
2回答

慕容708150

我想解决这个问题最干净的方法是创建一个工厂:interface IForm1Factory{&nbsp; &nbsp; Form1 Create(int valueToEdit);}class Form1Factory{&nbsp; &nbsp; public readonly ILogger _processRepository;&nbsp; &nbsp; public readonly Icopy _copy;&nbsp; &nbsp; public Form1Factory(ILogger logger, ICopy copy)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this._processRepository = logger;&nbsp; &nbsp; &nbsp; &nbsp; this._copy = copy;&nbsp; &nbsp; }&nbsp; &nbsp; public Form1 Create(int valueToEdit)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return new Form1(valueToEdit, _processRepository, _copy);&nbsp; &nbsp; }}还有一个扩展(Ninject.Extensions.Factory)允许您自动生成工厂,例如Form1Factory基于接口的。如果您使用该扩展名,则您声明使用Bind<IForm1Factory>().ToFactory().

牧羊人nacy

using Ninject;using Ninject.Modules;using Ninject.Parameters;&nbsp;&nbsp;//Add new class&nbsp;&nbsp;public class CompositionRoot{&nbsp; &nbsp; public static IKernel _ninjectKernel;&nbsp; &nbsp; public static void Wire(INinjectModule module)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _ninjectKernel = new StandardKernel(module);&nbsp; &nbsp; }&nbsp; &nbsp; public static T Resolve<T>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _ninjectKernel.Get<T>();&nbsp; &nbsp; }&nbsp; &nbsp; public static T ResolveWithArgument<T>(ConstructorArgument argument)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return _ninjectKernel.Get<T>(argument);&nbsp; &nbsp; }}//Ninject bindingspublic class Bindings : NinjectModule{&nbsp; &nbsp; public override void Load()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Bind<ILogger>().To<LogToDB>();&nbsp; &nbsp; &nbsp; &nbsp; Bind<ICopy>().To<CopyToFolder>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}//WinForm - Form1public partial class Form1 : Form{&nbsp; &nbsp; public readonly ILogger _processRepository;&nbsp; &nbsp; public readonly Icopy _copy;&nbsp; &nbsp; public readonly int ValueToEdit;&nbsp; &nbsp; public Form1(ILogger logger, ICopy copy, int valueToEdit)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this._processRepository = logger;&nbsp; &nbsp; &nbsp; &nbsp; this._copy = copy;&nbsp; &nbsp; &nbsp; &nbsp; this.ValueToEdit = valueToEdit;&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }}//main/// <summary>/// The main entry point for the application./// </summary>[STAThread]static void Main(){&nbsp; &nbsp;//Apply the binding rule first&nbsp; &nbsp;CompositionRoot.Wire(new Bindings());&nbsp; &nbsp;&nbsp; &nbsp;//Then resolve your form dependencies this way using Ninject passing along the&nbsp;&nbsp; &nbsp;constructor arguments.&nbsp;&nbsp; &nbsp;CompositionRoot.ResolveWithArgument<Form1>(new ConstructorArgument("valueToEdit",&nbsp;&nbsp; &nbsp;1)).ShowDialog();&nbsp; &nbsp; &nbsp;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP