CODEGATOR

.NET info worth sinking your teeth into!
Welcome to CODEGATOR Sign in | Join | Help
in Search

Martin Cook

Yet another C# developer with a blog.

An event broker solution - part 4

Last time we finished looking at the model classes that the event broker uses to represent publisher and subscribers. Today we'll look at the attribute classes that the event broker uses to locate publishers and subscribers at runtime.

Lets start with the EventPublicationAttribute class, which is used to make an association between an event and a topic at runtime. Here is what that class looks like:

[AttributeUsage(AttributeTargets.Event, AllowMultiple = true)]

public sealed class EventPublicationAttribute : Attribute

{

 

    // The topic for the associated event.

    private string topic;

 

    // Indicates whether or not to process the associated event

    //   asynchronously.

    private bool asyncEvent;

 

    public string Topic

    {

        get { return topic; }

    }

 

    public bool AsyncEvent

    {

        get { return asyncEvent; }

    }

 

    public EventPublicationAttribute(

        string topic

        )

    {

        this.topic = topic;

    }

 

    public EventPublicationAttribute(

        string topic,

        bool asyncEvent

        )

    {

        this.topic = topic;

        this.asyncEvent = asyncEvent;

    }

}


I wont describe each line of code - it's too simple for that. I will say that this attribute allows a developer to specify two things for a publisher: (1) A topic string, and (2) a flag to indicate whether or not published events should be processed asynchronously. These values will be read from the attribute at runtime by the event broker and will determine how the broker will process published events for whatever class is registered as a publisher.

The next class I want to look at today is the EventSubscriptionAttribute class, which is used to make an association between an event handler and a topic at runtime. Here is what that class looks like:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]

public sealed class EventSubscriptionAttribute : Attribute

{

 

    // The topic for the event handler.

    private string topic;

 

    // Indicates whether or not to dispatch events through the UI's

    //   synchronization context.

    private bool synchronize;

 

    public string Topic

    {

        get { return topic; }

    }

 

    public bool Synchronize

    {

        get { return synchronize; }

    }

 

    public EventSubscriptionAttribute(

        string topic

        )

    {

 

        // Sanity check the topic before using it.

        Guard.ThrowIfEmptyOrNull(topic, "topic");

 

        this.topic = topic;

    }

 

    public EventSubscriptionAttribute(

        string topic,

        bool synchronize

        )

    {

 

        // Sanity check the topic before using it.

        Guard.ThrowIfEmptyOrNull(topic, "topic");

 

        this.topic = topic;

        this.synchronize = synchronize;

    }

}

Once again this is pretty simple code so I wont explain each line. This attribute allows a developer to specify two things for a subscriber: (1) A topic string, and (2) a flag to indicate whether or not to synchronize incoming publications to the UI's synchronization context.


That's about all the time I have for blogging today. Tomorrow I'll start presenting the actual event broker class itself.

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Submit

About Martin

I work as a software engineer specializing in designing and building object-oriented business solutions for Windows platforms using C#. I have been programming professionally for roughly 20 years.

This Blog

Syndication

Terms of Service | Privacy Statement