Created
June 7, 2015 21:28
-
-
Save pmhsfelix/a19a88a1995ae7cf053f to your computer and use it in GitHub Desktop.
Wrapping services in IdentityServer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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