Event Centric: storing and consuming events

[Disclaimer: I don’t think this disclaimer is needed, but just to be on the safe side. The opinions expressed herein are my own personal opinions and do not represent in any way my company's view, that of any customer current or past, or any current, past or future project related to these concepts that I may participate in]

In my previous post I showed how you can evolve the way you code your domain objects (or entities) so that they can benefit from the Event Sourcing pattern, and I also showed that it’s pretty simple at its core. That post ended up being a little disconnected from the one where I show how do to cool analysis based on those events. It was on purpose, as I needed to introduce another concept, and the post was getting too big already Smile.

Lets recap briefly what happens on a domain object when we invoke an operation:...

Read full article

Event Centric: super-charge your model with domain events to enable business intelligence

I’ve shown in my previous post how interesting domain events can be mined using the Reactive Extensions for .NET. Now we need to raise those events when things happen to our domain. The typical way you’d publish events from your domain is simply adding .NET events. Say we have a Patient class, with a method Admit that causes the patient to be in the hospital and tracks the date when he was admitted:

public class Patient
{
    public DateTimeOffset? AdmittedDate { get; private set; }
    public bool IsInHospital { get; private set; }

    public void Admit(DateTimeOffset when)
    {
        if (this.IsInHospital)
            throw new InvalidOperationException("Already in hospital!");

        this.IsInHospital = true;
        this.AdmittedDate = when;
    }
}

The domain object method typically does some precondition validation (like the one above, the patient can’t be admitted twice), then mutates the state as needed. At some point the resulting object state will be persisted somehow, but it’s a good goal to have ...

Read full article

Event Centric: finding key business value by leveraging domain events and reactive extensions

Reactive Extensions (Rx) is one of the coolest additions to .NET ever. However, they have been largely ignored by the mainstream, in a significant part because (IMO) it’s seen as a UI technique, with samples that show how to handle mouse moves, drag & drop and so on. Its focus on asynchronous programming too makes it look like a niche technique that might even be worth skipping over as we wait for C# 5.0 async keyword (see Mike’s blog entry on a possible clarification of where it might fit in the async world).

There is, however, one mainstream application of reactive extensions that seems to have been missed by most: business intelligence. Here’s one concrete example: pretty soon, hospitals will face penalties for patient readmission, so you need a way to get an alert whenever patients are readmitted before a certain elapsed time (say 5 days or whatever). Another one: you want to preventively block a user’s account after 5 consecutive login failures within a minute (looks like an automated attack), or shoot an sms to the support team when failure rates for your app go above 5 crashes a day, or keep a report of top trending products in a store, and so on. ...

Read full article

How to make object initializers more useful

Quite often, it's necessary to validate an object state when it's initialized. With constructor arguments, this is easy to do as it can be done at the end or beginning of the constructor code. With object initializers, it's nearly impossible, as there's no way to know programmatically when the object initialization is finished.

There's a built-in mechanism for this that is being used in WinForms and WPF: the ISupportInitialize interface. The initialization code generated by both toolkits checks to determine if the object implements the interface. If it does, it will call BeginInit() before invoking all property setters, and EndInit() at the end. This provides a nice hook for validating all properties and maybe interrelated ones....

Read full article

Cloud-aware music done right: Spotify is the iTunes, Zune, Amazon Cloud Player and Google Music killer

Oh boy, how I’ve been waiting for something like Spotify!!! Just got an account setup (like 10’ ago) and I’m already blown away by it. Everything I’ve always wanted, truly.

To help you tell whether my evaluation is worth reading, these are the (music capable/usable) devices I own/owned and use/used actively:

  1. iPod Touch (first gen)
  2. iPad 1
  3. Zune Player
  4. T-Mobile G1
  5. Google Nexus One
  6. Galaxy Tab 7’’
  7. Galaxy Tab 10.1
  8. Windows 7 HTPC

And these are online music services I’ve used (either currently -soon to be dropped- or very recently). Note that they offer varying services and capabilities, so they cannot be compared easily, but they are all related to music, and I’m just noting the plus/minus I found for each:...

Read full article

How to improve string resources usability

(cross-posted from NETFx)

Somehow we ended up with a stagnating code generator for .resx built-in VS and we just got used to its shortcomings.

Namely:

  1. No support for formatting arguments: while you can set your resource string value to contain {0} and the like, the generator knows nothing about them. Meaning you have to write the repetitive and boring formatting code on the caller side. For example, for a resource string with Name=”User_InvalidEmail” and Value=”Provided email ‘{0}’ is invalid.”:

    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.User_InvalidEmail, email)); ...

Read full article

How to mock a dynamic object

Someone asked me how to mock a dynamic object with Moq, which might be non-obvious.

Given the following interface definition:

public interface IProject
{
  string Name { get; }
  dynamic Data { get; }
}

When you try to setup the mock for the dynamic property values, you get:

image001

 

What’s important to realize is that a dynamic object is just a plain object, whose properties happen to be resolved at runtime. Kinda like reflection, if you will: all public properties of whatever object happens to be the instance, will be resolved just fine at runtime.

Therefore, one way to mock this dynamic is to just create an anonymous type with the properties we want, and set the dynamic property to return that:...

Read full article

Making regex authoring easier to read and maintain

I’m spiking ideas on how to make my regular expressions easier to read and maintain for the dev who comes after me (that could be myself in 3 months, meaning I will surely have forgotten everything about how that crazy regex worked).

I’m aware and wary of fluent API alternatives to building regular expressions which IMO hinder readability more than anything. They are just too verbose.

So here’s a progression of options that I’m thinking of. I’d like to get your feedback on what makes for a readable pattern and how much you care about extension method pollution (since patterns are all strings, extension methods would need to “hang” there):...

Read full article

X1 vs X220 side by side specs that matter

Is it just me or the X220 is just plain better in almost all relevant fronts?

  X1 X220
Chipset/CPU Intel®   Core™ i7-2620M Processor (2.7GHz, 4MB L3 Cache) USB 3.0
    Core™ i5-2540M Processor (2.5GHz, 3MB L3 Cache)
  Core™ i5-2520M Processor (2.5GHz, 3MB Cache) Core™ i5-2520M Processor (2.5GHz, 3MB Cache)
    Core™ i5-2410M Processor (2.3GHz, 3MB L3 Cache)
  Core™ i3-2310M Processor (2.1GHz, 3MB L3 Cache) Core™ i3-2310M Processor (2.1GHz, 3MB L3 Cache)
     
Display 13.3” HD (1366x768) Super Bright (350 NITS) (non IPS, *glossy*!) Display 12.5" HD (1366x768) LED Backlit Display ...

Read full article

How to do reflection with a dynamic style

I’m not advocating *private* reflection (invoking private/internal fields, properties and methods), but we all know that there are those corner cases where you just can’t avoid it. And there’s a whole lot of scenarios when there are legitimate uses of reflection itself that can also leverage this very cool dynamic syntax (i.e. invoking generic methods where you don’t know the type at compile-time, etc.). In these cases, your code goes from pristine-looking C# to crappy unreadable reflection code.

David Ebbo has explained quite a bit already why this might be needed and how it can be used.

This is a pretty handy tool to have for those rare cases, and it’s pretty small, making it ideal for a ...

Read full article

Posted 26 May 2011 03:38 AM by Daniel Cazzulino
Filed under:
More Posts Next page »

Search

Go

This Blog

News

     

      Microsoft MVP Profile

Syndication