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

ExportDecorator with When.InjectedInto won't fire #192

Closed
mgth opened this issue Aug 30, 2018 · 8 comments
Closed

ExportDecorator with When.InjectedInto won't fire #192

mgth opened this issue Aug 30, 2018 · 8 comments
Labels

Comments

@mgth
Copy link

mgth commented Aug 30, 2018

If i do not put the condition Decorator is applied, but when When.InjectedInto is used the decorator is never applied.
I'm using a Func<param, IService> in the constructor.
Doing this with standard export works as expected but not with decorator.

@ipjohnson ipjohnson added the bug label Aug 31, 2018
@ipjohnson
Copy link
Owner

@mgth I was able to replicate the bug. I'll see if I can fix it this weekend at some point and do a beta release.

@ipjohnson
Copy link
Owner

@mgth I've released a pre-release version to nuget. Can you test it out?

@mgth
Copy link
Author

mgth commented Sep 2, 2018

Sorry, it still fail for me. But this is special case where I must use Container.Inject(obj);
Another thing is the decorator itself does not get injected.

@ipjohnson
Copy link
Owner

@mgth you’ll need to put together a sample showing the issue.

@mgth
Copy link
Author

mgth commented Sep 3, 2018

    interface IService { }
    class MyService : IService { }
    class MyServiceDecorator : IService { }

    interface IMyClass { }
    class MyClass : IMyClass
    {
        [Import]
        public Func<IService> GetService { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var container = new DependencyInjectionContainer();

            container.Configure( c =>
            {
                c.Export<MyClass>().As<IMyClass>().ImportMembers();
                c.Export<MyService>().As<IService>();
                c.ExportDecorator(typeof(MyServiceDecorator)).As(typeof(IService)).When.InjectedInto<IMyClass>();
            });

            var myClass = new MyClass();
            container.Inject(myClass);

            // there getService will return a MyService when it should return a MyServiceDecorator
            var service = myClass.GetService();
        }
    }

when this will return a MyServiceDecorator as expected
var myClass = (MyClass)container.Locate<IMyClass>();

@ipjohnson
Copy link
Owner

@mgth the reason it's not work the way you want it to is because the Inject method is an extension and isn't a first class feature in the container. No strategy is created for an injected type so the default implementation for WhenInjectedInto won't work.

I'm not inclined to make a change to the default behavior because it could have unintended side effects but you could do a custom condition like this.

@mgth
Copy link
Author

mgth commented Sep 3, 2018

Ok, that works. Thank you much for your precious time.

@ipjohnson
Copy link
Owner

@mgth glad I was able to address your issue. I'm going to close this as fixed, if you run into anything more open an issue and I'll try and address it.

I'll plan on doing an official release near the end of this month.

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

2 participants