logo
down
shadow

Ninject passing reference in constructor


Ninject passing reference in constructor

By : Anwar Ali
Date : November 25 2020, 03:01 PM
I hope this helps . Your program follows the Constructor Injection DI pattern which is good practice, but it has some issues which introduce coupling between your modules. The numerous calls to new should alert you.
Program should not know about IInputProcessor. This is a dependency of Form1 Form1 should not know about IInputProcessor. It is a dependency of SequenceController Form1 should not know about SequenceController, it should only know about its abstraction, ISequenceController
code :
public class Bindings : NinjectModule
    {
        public override void Load()
        {
            Bind<IConnectionToModbusCreator>().To<ConnectionToModbusCreator>();
            Bind<IInputProcessor>().To<InputProcessor>();
            Bind<ISequenceController>().To<SequenceController>();
            Bind<Form1>().ToSelf(); // in fact useless unless you add a scope, like .InSingletonScope()
        }
}

static class Program
    {
        [STAThread]
        static void Main()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form1= kernel.Get<Form1>();
            Application.Run(form1);
}

public partial class Form1 : Form
{
    private readonly ISequenceController _sequenceController;

    public Form1(ISequenceController sequenceController)
    {            
        _sequenceController = sequenceController;
        InitializeComponent();
    }

    private void Test_Click_1(object sender, EventArgs e)
    {
        _sequenceController.StartSequence();
    }


Share : facebook icon twitter icon
Ninject passing in constructor values

Ninject passing in constructor values


By : user3092480
Date : March 29 2020, 07:55 AM
Any of those help I used ToMethod in the end, which allowed me to construct the required instance with constructors in order to maintain compile time errors.
For example:
code :
.ToMethod(Func<IContext, T> method)

Bind<IWeapon>().ToMethod(context => new Sword());
Ninject, passing constructor argument to the kernel

Ninject, passing constructor argument to the kernel


By : user4147820
Date : March 29 2020, 07:55 AM
may help you . If you always want to specify the Connection when resolving a IDownloader then I think the ConstructorArgument (which is a IParameter) is what you are looking for:
code :
[Test]
public void Run_Forrest_Run()
{
    using (var kernel = new StandardKernel(new Module()))
    {
        var connection = new Connection(Guid.NewGuid().ToString());
        var downloader = kernel.Get<IDownloader>(new [] { 
            new ConstructorArgument("connection", connection) });

        Assert.That(downloader.Connection.Info, Is.EqualTo(connection.Info));
    }
}

public class Module : NinjectModule
{
    public override void Load()
    {
        Bind<ILogger>().To<ConsoleLogger>();
        Bind<IDownloader>().To<Downloader>();
    }
}
Passing ninject bound object to a class that must have constructor taking 0 params

Passing ninject bound object to a class that must have constructor taking 0 params


By : Omar Saad
Date : March 29 2020, 07:55 AM
seems to work fine There's UserRoleProvider class that is using userRepository , I've found solution for my problem.
code :
 private IUserRepository userRepo;
        public UserRoleProvider()
        {
            userRepo = DependencyResolver.Current.GetService<IUserRepository>();
        }
Passing parent constructor arguments to nested class constructor with Ninject

Passing parent constructor arguments to nested class constructor with Ninject


By : Mahmoud Ahmed
Date : March 29 2020, 07:55 AM
seems to work fine Just change your parameter to be inherited to child requests as follows:
code :
kernel.Get<ParentClass>(new ConstructorArgument("index", "MyIndex", true));
Passing parameters to the Controller Constructor using Ninject

Passing parameters to the Controller Constructor using Ninject


By : Tresor Sukami
Date : March 29 2020, 07:55 AM
I hope this helps you . why did you write a custom controller factory? This is not common when working with the Ninject.MVC3 NuGet package. A more common technique is to use the custom dependency provider that is automatically registered for you when you install this NuGet.
So here are the steps:
Related Posts Related Posts :
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org