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

ResolveMany if singleton decorators decorates the first item only #254

Closed
dadhi opened this issue Apr 5, 2020 · 3 comments · Fixed by #255
Closed

ResolveMany if singleton decorators decorates the first item only #254

dadhi opened this issue Apr 5, 2020 · 3 comments · Fixed by #255
Assignees
Labels
bug Something isn't working
Milestone

Comments

@dadhi
Copy link
Owner

dadhi commented Apr 5, 2020

https://dotnetfiddle.net/E0b9WO
This should work.

@dadhi dadhi added the bug Something isn't working label Apr 5, 2020
@dadhi dadhi self-assigned this Apr 5, 2020
@dadhi dadhi added this to the v4.1.5 milestone Apr 5, 2020
dadhi added a commit that referenced this issue Apr 5, 2020
@dadhi
Copy link
Owner Author

dadhi commented Apr 5, 2020

Apparently RegisterDelegate works in v4.1.3 but broken in v4.1.4.
But RegisterDelegateDecorator is broken in both.

https://dotnetfiddle.net/E0b9WO

using System;
using System.Linq;
using DryIoc;
					
public class Program
{
	public static void Main()
	{
		var c = new Container();
		
		c.Register<I, A>(Reuse.Singleton);
		c.Register<I, B>(Reuse.Singleton);
		
		// works in 4.1.3 but doesn't in 4.1.4
		c.RegisterDelegate<I, I>(i => new D(i), setup: Setup.Decorator);
		
		// does not work
		//c.RegisterDelegateDecorator<I>(_ => i => new D(i));
		
		var ii = c.ResolveMany<I>().ToArray();
		
		Console.WriteLine(ii[0].GetType() + ", " + ii[1].GetType());
		Console.WriteLine(((D)ii[0]).Inner.GetType() + ", " + ((D)ii[1]).Inner.GetType());
	}
	
	interface I {}
	class A : I {}
	class B : I {}
	class D : I
	{
		public D(I i) { Inner = i; }
		
		public I Inner { get; set; }
	}
}

dadhi added a commit that referenced this issue Apr 5, 2020
@hennys
Copy link

hennys commented Apr 6, 2020

It also seems like 4.1.3 does not work either in case that the delegate is registered as Singleton as well: c.RegisterDelegate<I, I>(i => new D(i), Reuse.Singleton, setup: Setup.Decorator);

So if you want the Decorator service to be a Singelton as well as the Decorated service, there no apparent way around that.

https://dotnetfiddle.net/ZcBKn5

using System;
using System.Linq;
using DryIoc;
					
public class Program
{
	public static void Main()
	{
		var c = new Container();
		
		c.Register<I, A>(Reuse.Singleton);
		c.Register<I, B>(Reuse.Singleton);
		
		c.RegisterDelegate<I, I>(i => new D(i), Reuse.Singleton, setup: Setup.Decorator);
		
		var ii = c.ResolveMany<I>().ToArray();
		
		Console.WriteLine(ii[0].GetType() + ", " + ii[1].GetType());
		Console.WriteLine(((D)ii[0]).Inner.GetType() + ", " + ((D)ii[1]).Inner.GetType());

		var jj = c.ResolveMany<I>().ToArray();
		
		Console.WriteLine("Same: " + object.ReferenceEquals(ii[0], jj[0]));
}
	
	interface I {}
	class A : I {}
	class B : I {}
	class D : I
	{
		public D(I i) { Inner = i; }
		
		public I Inner { get; set; }
	}
}

@dadhi
Copy link
Owner Author

dadhi commented Apr 6, 2020

@hennys It is funny how this bug is so diverse and persistent. Thanks for the reporting. I will look.

dadhi added a commit that referenced this issue Apr 6, 2020
added UpdateItemOrShrinkUnsafe and updated Copy to use Array.Copy
optimize decorator sorting for 2 items
@dadhi dadhi linked a pull request Apr 10, 2020 that will close this issue
dadhi added a commit that referenced this issue Apr 12, 2020
dadhi added a commit that referenced this issue Apr 13, 2020
* added test for #254

* ignoring the failed test

* Cherry pick commit 'c7f698c'

* Cherry pick commit '285214f'

* Cherry pick commit '7dec0b9'

* Cherry pick commit '0a2091a'

* Cherry pick commit '517c44f'

* Cherry pick commit '5d07bc6'

* Cherry pick commit '41eb818'

* #254 - make the obsoletion of RegisterDelegateDecorator in the next major version more obvious

* added: simplest case FactoryMethod constructor and updated the usages

* simplifying ParameterServiceInfo.Of

* removing the code doing nothing

* added: test with singleton decorator for #254

added UpdateItemOrShrinkUnsafe and updated Copy to use Array.Copy
optimize decorator sorting for 2 items

* revert back to the Match because using UpdateItemOrShrinkUnsafe is pointless

* #254 reimplement RegisterDelegateDecorator in terms of RegisterDelegate

* cleanup unused things

* dead code cleanup

* sharing the RequestStack for the Validate

* optimizing out double GetParameters when parameters are already resolved by ConstructorWithResolvableArguments

added the missing validation check for the property injection #242

* enable back ignored NCrunch test including ReducedStackoverflow

* small cleanup

* added sorting benchmark and implemented decorator sorting for more than 2

* small comments cleanup

* -2kb by removing unnecassery creation of FactoryMethod

* fixed: #254

* improving the error messages

* release notes for the v4.1.4

* updated benchmarks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants