Tags: , | Categories: nservicebus, messaging Posted by balazs.zoltan on 2/19/2010 8:27 PM | Comments (0)

I’m starting to look into messaging frameworks so I’m putting it here for later reference.

SOA (Service Oriented Architecture)

From all the definitions that I would say that SOA can be defineed as follows: “an architectural discipline that uses loosely-coupled services to construct complex composite applications.”

Service Bus (SB), Enterprise Service Bus (ESB)

A service bus could be defined as a number of input/output channels that processes messages. The Enterprise Service Bus is considered a platform to realize SOA. The ESB coordinates the messaging between services, based on different events and business conditions.

Event driven architecture (EDA)

From wikipedia: Event driven architecture is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.

Publish/Subscribe (pub/sub)

From wikipedia: Publish/subscribe (or pub/sub) is an asynchronous messaging paradigm where senders (publishers) of messages are not programmed to send their messages to specific receivers (subscribers). Rather, published messages are characterized into classes, without knowledge of what (if any) subscribers there may be. Subscribers express interest in one or more classes, and only receive messages that are of interest, without knowledge of what (if any) publishers there are. This decoupling of publishers and subscribers can allow for greater scalability.

Tags: | Categories: .net Posted by balazs.zoltan on 2/13/2010 4:00 PM | Comments (0)

In .NET 2.0 we had something like:

delegate int Sum(int x, int y);
public static void Main()
{
	Sum sum = delegate(int x, int y) { return x + y; };
	Console.WriteLine(sum(1, 2));
}

The delegate type must be defined outside the method body, because you can’t create a type within a method. Along came .NET 3.5 with the new generic delegate types: Action and Func. The signatures of the Action<T>, Func<T> and Predicate<T> delegate types:

// Action is a delegate type with void return parameter
public delegate void Action<T>(T obj);
public delegate void Action<T1, T2>(T1 arg1, T2 arg2);

// Func is a delegate type with a generic return parameter
public delegate TResult Func<T, TResult>(    T arg);
public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);

// For completeness sake:
public delegate bool Predicate<T>(T obj);

Both the Action and Func types have signatures declared to support up to 4 arguments. So now we are able to write (using anonymous delegates, observe that no named delegate was declared):

// the long form would be 
// sum  = new Func<int, int, int>(delegate(int x, int y) { return x + y; });
Func<int, int, int> sum = delegate(int x, int y) { return x + y; };
Console.WriteLine(sum(1, 2));

Transforming into lambda expressions

The format of the lambda expressions is: (parameter  list) => { statements; }. So the example above becomes:

Func<int, int, int> sum = (x, y) => x + y;
// an extra parenthesis may help readability
Func<int, int, int> sum = ((x, y) => x + y);

Reading lambda expressions is easier if you think in the terms of a method call and return value (some say that the => sign should be read “goes to”).

One more generic delegate

The Predicate<T> generic delegate represents any method that returns a bool and accepts a single typed parameter (a method that evaluates some criteria). Now let’s see an example first using the Predicate<T> delegate, an anonymous delegate and finally a lambda expression:

        Item[] items = 
        { 
            new Item {Name = "a"}, new Item {Name = "b"}, 
            new Item {Name = "c"}
        };
 
        // using Predicate generic delegate
        Item findUsingPredicate = Array.Find(
            items, 
            FindUsingPredicate);
    
        // using anonymous delegate
        Item findUsingDelegate = Array.Find(
            items, 
            delegate(Item item) { return item.Name == "b"; });

        // using lambda expression
        Item findUsingLambda = Array.Find(
            items, 
            item => item.Name == "b");

where FindUsingPredicate method is defined as follows:

    static bool FindUsingPredicate(Item item)
    {
        return item.Name == "b";
    }

In a nutshell this would be it.