Dynamic Linq and Expression Parsing in .NET as a hint of C# compiler extensibility

There is a DbLinq provider for multiple non MS-SQL databases. The project is open-source and hosted on the Google Projects under MIT license. It is even more interesting, however, that it contains dynamic expression parser that allows code like this one to execute: sealed class Order {   public double SubTotal { get; set; }   public …

Extending Lokad Rules for better .NET Domain Driven Development Experience

public static void ValidateIdentity(Identity identity, IScope scope) { scope.Validate(identity.Name, “Name”, StringIs.ValidEmail); scope.Validate(identity.Pass, “Pass”, StringIs.Limited(3,64)); } Almost everything is sticking to the DRY principle here (with the ability to shape the validation in any way, since that’s plain code). Well, everything except for one thing – name of the property being checked. We duplicate it as …

How to Get Parameter Name and Argument Value From C# Lambda via IL?

Or “How NOT to Use .NET Linq Expressions in Order to Get Parameter Name and Argument Value From C# Lambda?” Yesterday I’ve managed to get overhead of 3-4 seconds per 1 million operations for the methods that did allowed to pass parameter name and argument value in one C# statement: Enforce.Argument(() => args); public static void …

How to handle .NET ReaderWriter locks efficiently?

Writing code for the concurrent execution requires, among over things, proper data access synchronization (locking) to avoid any problems. One of the most useful primitives to do that is the Slim version of ReaderWriterLock that has been introduced in .NET 3.5 ReaderWriterLockSlim represents a lock that is used to manage access to a resource, allowing …

Shortcuts for NUnit Exception Expectations

Are you tired of specifying NUnit exception expectations? [Test] [ExpectedException(typeof(InvalidOperationException))] public void Reference_Value_Is_Checked_For_Null() { // if you have R#, this should be highlighted Result.Success<object>(null); } There is a shortcut that is more compact: [Test] [Expect.ArgumentNull] public void Reference_Value_Is_Checked_For_Null() and also has nice IntelliSense: You just need to define class like this: static class Expect { …

.NET Application Block for Validation and Business Rules

A while ago I wrote about a simple .NET Validation Framework. A lot has changed since then, and more elaborate rules had to be implemented in various projects. So let me introduce you to the next version of .NET Validation and Business Rules (Micro) Application Block. You can download latest release of Lokad Shared Libraries to get started …

Some tips on writing event handling code in C# .NET

How often do you write repetitive code like this? public delegate void MessageEventHandler(object sender, MessageEventArgs e); [Serializable] public sealed class MessageEventArgs : EventArgs { // some class contents } // … event MessageEventHandler OnMessageArrived; private void RaiseOnMessageArrived(string message) { // BTW, what about thread-safety of this call? if (OnMessageArrived != null) { OnMessageArrived(this, new MessageEventArgs(message)); …

Action Policy

Simple action policy definition in .NET 3.5 is: Action<Action> In old .NET syntax this would look like: public delegate void Action(); public delegate void ActionPolicy(Action action); Basically it is just a delegate that can do something with your action. Simplest example is a policy that would let your calls to retry a couple of times …

.NET Exception Handling Action Policies Application Block

Overview In short, Action Policy is just some reusable decorator that could be easily applied to any given method call or code block to augment the execution. An Exception Handler is just a logical part of an Action Policy that determines which exceptions do we handle and how do we do that. Microsoft Exception Handling Application Block from the Enterprise …

Const vs. readonly vs. static readonly in C# .NET

Here are the differences between C# .NET const, readonly and static readonly fields. Constants: Static by default Must have compilation-time value (i.e.: you can have “A”+”B” but cannot have method calls) Can be used in attributes Are copied into every assembly that uses them (every assembly gets a local copy of values) Could be declared within functions The compiler performs …