Design is easy with - Object role stereotypes
For a while we started to do design sessions. Nothing fancy but if you did not do it before is not trivial, try it. And to be more clear by this, I mean : you know what the product owner wants, now you want with your team to come up with a design which will solve the problem.
Decompose the problem into small parts and tackle them in isolation. This what you will find everywhere. But how will I apply this in my situation ? How I can find the right granularity ? Where do I start my design ? With what?
If I could know thins before reading Object Design, Roles, Responsibilities, and Collaborations a lot of things were different. If you have read this book skip this article if not keep going. How ?
Starting with:
In this big machinery every objects has a purpose, a role to play in the specific context. If two objects play the same role they can be interchanged.
Role Stereotypes – help to focus on object’s responsibilities. A well defined object supports a clearly defined role. Because the goal is to build consistent and easy to use objects it’s advantageous to stereotype them ignoring specifics of behavior and thinking at a higher level.
Stereotyping - design gets descriptive:
One object can play more than one role in the given context and that is ok if just one role is considered major. If one object is used in 2 contexts for different purposes then the object pays 2 distinct roles and it clearly violates single responsibility principle.
Design using role stereotypes the following system: a building 50 levels has 2 elevators, one is at 47 coming down, another is at 1st going to 9th floor. You just went out of your apartment, al level 11 and intend to visit your friend at 32th.
Once you are done with this try to do the old way and compare them
Stereotyping gave us the clear vision of – Single Responsibility Principle. Objects became smaller with descriptive names. Testing for simple objects is a breeze, code easy to change and ready for new functionality. Overall – confidence and predictability.
This is what wiki is saying wiki
MSMQ is essentially a messaging protocol that allows applications running on separate servers/processes to communicate in a fail-safe manner. A queue is a temporary storage location from which messages can be sent and received reliably, as and when conditions permit. This enables communication across heterogeneous networks and between computers which may not always be connected. By contrast, sockets and other network protocols assume that direct connections always exist.
.net has System.Messaging which gives us access to Msmq API. As a developer you have 2 ways to make your application to use messaging infrastructure.
As the name is mentioning the application will use a message adapter in an infinite loop to pool the message queue for messages. When there is a message it will be dequeued and processed.
public class PoolingConsumer : IDisposable
{
readonly MessageQueue _channel;
public PoolingConsumer(string queueName)
{
_channel = new MessageQueue(queueName);
}
public Message Receive()
{
return _channel.Receive();
}
public void Dispose()
{
_channel.Dispose();
}
}
When you want to process a message as soon as it arrives asynchronously then go event driven. This is the fastest way when you don’t need transactions. Is better suited for monitoring, notifications.
public class EventDrivenConsumer : IDisposable
{
readonly MessageQueue _channel;
public EventDrivenConsumer(string queueName)
{
_channel = new MessageQueue(queueName);
}
public void RegisterHandler(Action<Message> handler)
{
_channel.ReceiveCompleted += (sender, args) =>
{
var queue = sender as MessageQueue;
handler(queue.EndReceive(args.AsyncResult));
queue.BeginReceive();
};
_channel.BeginReceive();
}
public void Dispose()
{
_channel.Dispose();
}
}
[Test]
public void ReceiveShouldPullAMessageFromTheQueue()
{
const string receiver = @".\Private$\receiver";
new MessageQueue(receiver).Send(new Message());
var message = new PoolingConsumer(receiver).Receive();
Assert.IsNotNull(message);
}
[Test]
public void HandleGetsCalledWhenAMessageIsAvailableInTheQueue()
{
const string receiver = @".\Private$\eventingendpoint";
Message output = null;
var endpoint = new EventDrivenConsumer(receiver);
endpoint.RegisterHandler(msg => { output = msg; });
new MessageQueue(receiver).Send(new Message());
Thread.Sleep(10);
Assert.IsNotNull(output);
}
This is more of a quick reference for me. In case you like it feedback will be appreciated.
This is probably the first time when I realized that NoSQL is an option.
Expressing this in a formal language I need some persistent storage around our application that will expose following fast/atomic operations
Before getting to Couchbase I’ve started my journey from Memcached, Redis, Riak. The evaluation criteria was
Tooling, being on .net platform I had just to nuget Enym library and here we are :) Simple, no ?
Code sample
public class Sessions
{
private readonly MembaseClient _client;
private readonly TimeSpan _lifetime;
public Sessions(MembaseClient client, TimeSpan lifetime)
{
_client = client;
_lifetime = lifetime;
}
public void Track(string ticket)
{
_client.Store(StoreMode.Set,
ticket, ticket,
_lifetime);
}
public bool ExtendIfAlive(string ticket)
{
object value = string.Empty;
return _client.TryGet(ticket,
DateTime.Now.Add(_lifetime),
out value);
}
public void Release(string ticket)
{
_client.Remove(ticket);
}
}
Not much and nothing fancy. Well, is still in backlog but it’s a solution. The whole code is on github
Resources couchbase-sdk-net-tutorial membase-getting-started