Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Injection into private parent parent properties fails #241

Closed
AZWN opened this issue May 26, 2017 · 3 comments
Closed

Injection into private parent parent properties fails #241

AZWN opened this issue May 26, 2017 · 3 comments
Assignees
Labels
Milestone

Comments

@AZWN
Copy link

AZWN commented May 26, 2017

When the Kernel option InjectParentPrivateProperties is set to true, properties of parent parent classes are still not injected. This leads to very unexpected results when api users want to use a subclass of the default implementation an API provides.

I think, when the previously mentioned option is set, all parent class private properties should be injected, or there should be an option to allow for higher level injection.

A simple reproduction scenario might be as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Ninject;
using Ninject.Modules;
using NUnit.Framework;

namespace SelfInjectTest
{
    interface IA { }

    public class A : IA
    {
        [Inject]
        private string Name { get; set; }

        public string NameOperation()
        {
            return Name;
        }
    }

    class B : A { }

    class C : B { }

    public class Program
    {
        public static void Main(string[] args)
        {
            IKernel kernel = new StandardKernel(new NinjectSettings()
            {
                InjectParentPrivateProperties = true,
                InjectNonPublic = true
            });
            kernel.Bind<string>().ToConstant("Foo");

            var a = kernel.Get<A>();
            var b = kernel.Get<B>();
            var c = kernel.Get<C>();

            Assert.AreEqual("Foo", a.NameOperation()); // Passes
            Assert.AreEqual("Foo", b.NameOperation()); // Passes
            Assert.AreEqual("Foo", c.NameOperation()); // Fails, parent of parent property not injected
        }
    }
}
@BrunoJuchli
Copy link
Contributor

BrunoJuchli commented May 27, 2017

I agree, however i'd vote to remove this feature from Ninject entirely (in a Ninject V4). Private-property injection is basically only limited to cases where bad code is written. I think in 99% of the cases there's a better ctor-injection, or entirely different, design possible.

@AZWN
Copy link
Author

AZWN commented Jun 1, 2017

I can imagine that, but are there any plans for such a new version? If that will still take some time, I think it is still worth the effort to fix this. If you are willing to accept pull requests, I can do it myself as well.

@scott-xu
Copy link
Member

scott-xu commented Jun 3, 2017

PR is welcome

@scott-xu scott-xu self-assigned this Sep 11, 2017
@scott-xu scott-xu added this to the 4.0 milestone Sep 11, 2017
@scott-xu scott-xu added the Bug label Sep 16, 2017
@scott-xu scott-xu modified the milestones: 4.0, 3.3.0 Sep 23, 2017
glenkeane-94 pushed a commit to glenkeane-94/Nin-ject that referenced this issue Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants