This is part 4 of my series of posts on AspectMap. If you’re new to AspectMap I’d recommend you start at the beginning and work your way through the tutorials:

What Is Multicasting?

Multicasting is most often associated with networking, where it refers to the delivery of a message to multiple destinations at once from a single source. We are using the same concept here to describe the application of a single aspect to multiple methods, using a single attribute declaration. Let’s skip to an example:

public class AnImportantClass : IDoesImportantStuff
{
  [Auditable]
  public void DoSomething()
  {
    // Does something important
  }

  [Auditable]
  public void DoSomethingMoreImportant()
  {
    // Does something more important
  }
}

We have a couple of methods here, both marked with the AuditableAttribute, which we can wire up in an AspectsRegistry as normal to do some auditing stuff. AspectMap will allow you to wire this up via a single attribute at the class level:

[Auditable]
public class AnImportantClass : IDoesImportantStuff
{
  public void DoSomething()
  {
    // Does something important
  }

  public void DoSomethingMoreImportant()
  {
    // Does something more important
  }
}

This will do the equivalent of applying the attribute to every single method on the class. This works fine for attributes without properties, but what about a scenario this, where you are passing configuration information with the attribute?

public class AnImportantClass : IDoesImportantStuff
{
  [Auditable(15)]
  public void DoSomething()
  {
    // Does something important
  }

  [Auditable(15)]
  public void DoSomethingMoreImportant()
  {
    // Does something more important
  }

  [Auditable(20)]
  public void AnExtraImportantMethod()
  {
    // This requires more detailed auditing
  }
}

You can actually define a default attribute at class level, and then override it on methods which need something more specific:

[Auditable(15)]
public class AnImportantClass : IDoesImportantStuff
{
  public void DoSomething()
  {
    // Does something important
  }

  public void DoSomethingMoreImportant()
  {
    // Does something more important
  }

  [Auditable(20)]
  public void AnExtraImportantMethod()
  {
    // This requires more detailed auditing
  }
}
Quick and easy. I hope all this information is useful to some of you, and the post on nesting and prioritising aspects will be up in the next few days.