-
Notifications
You must be signed in to change notification settings - Fork 7
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
Unexpected Behavior with Generic Parameters #11
Comments
It would be helpful to have all the relevant code (eg, |
I have pushed my changes here: https://github.com/danechambers/Autofac.Extras.AggregateService/tree/v6.1.1-branch
Hope this helps! Let me know if you need more information. |
Oh, man, I'm sorry I probably wasn't speaking clearly. When I said "having it posted" I meant "right here in the issue" so when folks come in and read this...
...the code for
It may be that one of the other maintainers might be able to dive in more interactively. It might be a day or three before I can look at it. I was just hoping to potentially see something obvious without going spelunking. |
That makes complete sense. I totally get the burnout too. Thank you for the context, I really do appreciate your time. I've posted the code for namespace Autofac.Extras.AggregateService.Test
{
public interface IOpenGenericAggregate
{
IOpenGeneric<T> GetOpenGeneric<T>();
IOpenGeneric<string> GetResolvedGeneric();
IPassThroughOpenGeneric<T> PassThroughOpenGeneric<T>(IOpenGeneric<T> openGeneric);
}
} Here is the full fixture as well namespace Autofac.Extras.AggregateService.Test
{
public class AggregateServiceGenericsFixture
{
private readonly IContainer _container;
public AggregateServiceGenericsFixture()
{
var builder = new ContainerBuilder();
builder.RegisterAggregateService<IOpenGenericAggregate>();
builder.RegisterGeneric(typeof(OpenGenericImpl<>))
.As(typeof(IOpenGeneric<>));
builder.RegisterGeneric(typeof(OpenGenericPassThroughImpl<>))
.As(typeof(IPassThroughOpenGeneric<>));
_container = builder.Build();
}
/// <summary>
/// Attempts to resolve an open generic by a method call.
/// </summary>
[Fact]
public void Method_ResolveOpenGeneric()
{
var aggregateService = _container.Resolve<IOpenGenericAggregate>();
var generic = aggregateService.GetOpenGeneric<object>();
Assert.NotNull(generic);
var ungeneric = aggregateService.GetResolvedGeneric();
Assert.NotNull(ungeneric);
Assert.NotSame(generic, ungeneric);
}
[Fact]
public void Method_UnpackOpenGeneric()
{
var aggregateService = _container.Resolve<IOpenGenericAggregate>();
var generic = aggregateService.GetOpenGeneric<It.IsAnyType>();
// works
var passThroughFunc = _container.Resolve<Func<IOpenGeneric<It.IsAnyType>, IPassThroughOpenGeneric<It.IsAnyType>>>();
var genericFromPassThroughFunc = passThroughFunc(generic);
Assert.Same(generic, genericFromPassThroughFunc.OpenGeneric);
// doesn't work
var genericPassThrough = aggregateService.PassThroughOpenGeneric(generic);
Assert.Same(generic, genericPassThrough.OpenGeneric);
}
}
} Anything else I can do, please let me know. |
That helps, thanks! Things I'm thinking about as I see it now (just brainstorming)...
If you make a closed generic version of If something is amiss, it's probably going to be in this bit where the dynamic proxy is building up the list of methods and figuring out the backing implementations. I don't see that we have unit tests that cover methods that take generics as parameters so that's likely where I'd start looking. |
The problem is here, where the method handler basically says, "Add a parameter of type The existing mechanism is simple and it works, but it's not as rich as the full-on I'm not sure what the best route to fixing this is. My gut says the most robust solution would be to do something similar to what the Possibly simpler but looking more complicated, would be to build the delegate I'll see if I can get that second idea working. |
Resolve #11: Use Func<X, Y> to handle methods with parameters.
I won't have time until next week to cut a release with the fix, but it'll happen. |
Thank you again for your time on this. I definitely appreciate this project (in addition to autofac core) and all the time/effort that everybody puts into it. Looking forward to the release 🙂. |
v6.1.2 published. |
I'm not sure if this is working as designed or not, but it is something that caught me off guard. Please feel free to correct any misconceptions I may have about how the library should work.
Describe the Bug
When a method on the aggregate services interface accepts a generic parameter, a new instance of the parameter is created (or is attempted to) when invoking the method rather than using the parameter that is given to it.
Steps to Reproduce
I added a new pass-through interface and implementation
and added this method to
IOpenGenericAggregate
In
AggregateServiceGenericsFixture
, I added this registrationthen this test
However, if I use the aggregate service instead, it does not work.
Expected Behavior
I am under the impression that the aggregate service should behave as the
Func<>
does. Here is my example if you like to see my full implementation:GenericParameterExample.zip
Dependency Versions
Autofac: v6.0.0
The text was updated successfully, but these errors were encountered: