Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Last active December 11, 2015 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akimboyko/4594189 to your computer and use it in GitHub Desktop.
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
// 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