Decorator Issues

65 views
Skip to first unread message

wayne.b...@gmail.com

unread,
Jan 19, 2016, 5:04:32 PM1/19/16
to Autofac
Originally, you helped me out with getting all the decorators attached using open generics here. https://groups.google.com/forum/#!searchin/autofac/brantley/autofac/lV2X4hR0mak/F5g4MfhUR08J

That has been working great and I modified the code so you could pass in a list of decorators to apply (decorators decorating decorators). For completeness here is that code (in case someone wants it):

public static IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> AsClosedTypesOfWithDecorators<TLimit, TScanningActivatorData, TRegistrationStyle>(
this IRegistrationBuilder<TLimit, TScanningActivatorData, TRegistrationStyle> registration,
ContainerBuilder builder, Type commandType, params Type[] decorators) where TScanningActivatorData : ScanningActivatorData
{
if (commandType == null) throw new ArgumentNullException(nameof(commandType));
if (decorators == null || decorators.Length == 0)
return registration.AsClosedTypesOf(commandType);

string actualHandler = Guid.NewGuid().ToString();
var returnValue = registration
.AsNamedClosedTypesOf(commandType, t => actualHandler);

string lastHandler = actualHandler;
var lastDecorator = decorators.Last();
foreach (var decorator in decorators) //take all but last decorator...it cannot be keyed..
{
var newKey = Guid.NewGuid().ToString();
var decoratorRegistration = builder.RegisterGenericDecorator(
decorator,
commandType, lastHandler);
if (decorator != lastDecorator) //cannot key the last decorator...
decoratorRegistration.Keyed(newKey, commandType);
lastHandler = newKey;
}

return returnValue;
}

Anyway, the big issue at hand is conditionally registering decorators. This is a shortcoming I have been working around for a while and last April you said may focus on it after DNX settle down (https://groups.google.com/forum/#!searchin/autofac/decorator/autofac/vSU6kx7XX2M/AxhpHqddGzkJ)

However, I now have a really wild situation that I cannot resolve.
1) I register all my query/commands with decorators using the above code. All good.
2) I hand register each query/command SECOND time which effectively hides the first registration. On this I use the non-open generic RegisterType<> and RegisterDecorator<> to filter out the decorators I do not want for these specific commands/queries.

This seems to work great. I open my web application and sure enough the new registration is being applied and there are no issues.

The issue is that if I do a WEBAPI request - it continues to use the original registration and not the new one!! I have pulled my hair out on this issue.

Any ideas?


Alex Meyer-Gleaves

unread,
Mar 15, 2016, 8:54:20 AM3/15/16
to Autofac, wayne.b...@gmail.com
Hi Wayne,

I just came across this one on the group. Did you manage to sort out the Web API issue?

Thanks for sharing the method you used for registering a list of decorators. I've created an issue for the conditional registrations.


I wanted to capture this as I'm interested in improving the decorator support for 4.0.

There is already a fresh round of breaking changes with the recent switch to .NET CLI but hopefully we can get through those soon and move on to more interesting things that continually fixing build issues. =)

Cheers,

Alex.

Wayne Brantley

unread,
Mar 17, 2016, 6:21:01 PM3/17/16
to Alex Meyer-Gleaves, Autofac
Thanks for reaching out, glad to here you guys are finally digging out of .net core.
I have put lots of information in the issue you raised above.....really welcome your comments??
Reply all
Reply to author
Forward
0 new messages