Last active
December 11, 2015 11:28
-
-
Save akimboyko/4594189 to your computer and use it in GitHub Desktop.
Inject dependency into PostSharp aspect with instance scope using factory method
There is a problem with parralel unittests of this implementation because of static factory method
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
// More about aspect life time | |
// http://www.sharpcrafters.com/blog/post/Day-10-Aspect-Lifetime-Scope-Part-2.aspx | |
[Serializable] | |
public class InjectionAspectWithCallbackAttribute : OnMethodBoundaryAspect, IInstanceScopedAspect | |
{ | |
private ILogger Logger { get; set; } | |
public static Func<ILogger> InjectLogger { get; set; } | |
#region OnMethodBoundaryAspect overrides | |
public override void OnEntry(MethodExecutionArgs args) | |
{ | |
Logger.Log("On Entry"); | |
} | |
#endregion | |
#region IInstanceScopedAspect Members | |
public object CreateInstance(AdviceArgs adviceArgs) | |
{ | |
return MemberwiseClone(); | |
} | |
public void RuntimeInitializeInstance() | |
{ | |
if (InjectLogger == null) | |
{ | |
throw new Exception("CreateSomeDependency is null"); | |
} | |
Logger = InjectLogger(); | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment