Skip to content

Instantly share code, notes, and snippets.

@pmhsfelix
Created June 7, 2015 21:28
Show Gist options
  • Save pmhsfelix/a19a88a1995ae7cf053f to your computer and use it in GitHub Desktop.
Save pmhsfelix/a19a88a1995ae7cf053f to your computer and use it in GitHub Desktop.
Wrapping services in IdentityServer
// definition
public static class IdentityServerServiceFactoryExtensions
{
public static void Wrap<T>(this IdentityServerServiceFactory fact,
Func<T, T> wrapper,
Func<IdentityServerServiceFactory,Registration<T>> getter,
Action<IdentityServerServiceFactory,Registration<T>> setter)
where T: class
{
fact.Register(new HelperRegistration<T>(getter(fact), GetRegistrationNameFor(typeof(T))));
setter(fact, new Registration<T>(resolver =>
{
var inner = resolver.Resolve<T>(GetRegistrationNameFor(typeof(T)));
return wrapper(inner);
}));
}
private static string GetRegistrationNameFor(Type t)
{
return t.FullName;
}
}
internal class HelperRegistration<T> : Registration<T>
where T: class
{
internal HelperRegistration(Registration<T> registration, string name)
{
if (registration == null) throw new ArgumentNullException("registration");
if (name == null) throw new ArgumentNullException("name");
this.Mode = registration.Mode;
this.Type = registration.Type;
this.Factory = registration.Factory;
this.Instance = registration.Instance;
this.Name = name;
}
}
// usage
factory.Wrap(
inner => new UserServiceAdapter(inner),
fact => fact.UserService,
(fact, reg) => fact.UserService = reg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment