2015-01-30 1 views
2

검색하고 검색했지만 아주 간단한 것처럼 보이지만 아무 것도 찾을 수 없습니다.포스트 샤프는 애스펙트 그룹을 적용하는 간단한 방법을 제공합니까?

기본적으로 나는 보통 같은 방법으로 함께 적용되는 4 가지 또는 5 가지 양상을 가지고 있지만, 또한 많은 장소에서 개별적으로 요구되기 때문에 분리되어 있어야합니다.

postsharp는 '메소드에이 부분을 추가하는 것은이 네 가지 다른 측면을 함께 추가하는 것과 동일합니다'라고 말할 수 있습니까?

나는 aspect를 하나의 aspect로 결합 할 수 있다는 것을 알고 있지만, 결합 된 aspect와 non-combined aspect에서 중복 된 코드를 원하지 않는다. 그리고 똑같이 메소드 위에 선언 된 aspect의 전체 스택을 엉망으로 만들고 싶지는 않다.

누군가가 큰 도움이 될만한 것을 제안 할 수 있다면. 미리 감사드립니다. 당신이 볼 수 있듯이

[Serializable] 
public class CombinedAspect : MethodLevelAspect, IAspectProvider 
{ 
    public IEnumerable<AspectInstance> ProvideAspects(object targetElement) 
    { 
     yield return new AspectInstance(targetElement, new FirstAspect()); 
     yield return new AspectInstance(targetElement, new SecondAspect()); 
    } 
} 

는,이 인터페이스는 매우 강력한 도구가 될 수 있습니다

답변

2

가장 쉬운 옵션은 자리 측면에 IAspectProvider 인터페이스를 구현하는 것입니다. 또한 실제로 선언문에 어떤면이 있는지 여부를 알고 싶을 수도 있습니다. 이것은 IAspectRepositoryService 서비스를 사용하여 수행 할 수 있습니다 :

[Serializable] 
public class CombinedAspect : MethodLevelAspect, IAspectProvider 
{ 
    private int count = 0; 

    public IEnumerable<AspectInstance> ProvideAspects(object targetElement) 
    { 
     IAspectRepositoryService repositoryService = PostSharpEnvironment.CurrentProject.GetService<IAspectRepositoryService>(); 

     if (!repositoryService.HasAspect(targetElement, typeof(FirstAspect))) 
      yield return new AspectInstance(targetElement, new FirstAspect()); 

     if (!repositoryService.HasAspect(targetElement, typeof(SecondAspect))) 
      yield return new AspectInstance(targetElement, new SecondAspect()); 
    } 
} 

그러나,이 서비스는 PostSharp 4.0 이후 사용할 수 있습니다.

+0

감사합니다. 이것은 훌륭하게 작동합니다. – KFR42