Wednesday, September 5, 2012

Utility to perform a parameterized SQL Query using ADO.NET

Here's a quick utility method to perform an ADO.NET SQL query. The main tricks are the use of a regular expression to parse out the parameters and the use of a queue to conveniently get the next parameter.

Tuesday, August 28, 2012

Deep copy of C# object properties

Sometimes you have two different class types with the same properties and you want to copy all the property values from one to the other.

One way is to simply hand code one assignment statement for each field they have in common.

Another option is to use an open source mapping library like AutoMapper

Still other times you might find yourself in the middle ground of not wanting to hand code all the assignments and also not wanting to deal with a 3rd party library.

Here is an example of how you can roll your own automapper in a few lines of code.  This snippet copies like-named properties from a source to a target.  It recurses on any properies that don't have a type starting with "System." and it ignores source properties with value of null.

Friday, August 17, 2012

How do mixins work in C#?

Extension methods which target a common marker interface can be viewed as partial implementations of that interface.

If you place such extension methods in different namespaces then it is possible to utilize a technique of "Mixing In" the implementations used by a class.

This is demonstrated in code below.  The one part of this that I took a minute to get my head around was that ILogger loses the semantics of an interface.  The methods available to it are those extension methods which are visible to the compiler as dictated by the using statements.

Some links I learned from before writing this:

Thursday, July 12, 2012

What are extension methods in C#?

Here's a really basic example.

An extension method in C# is a static method defined in a static class with the this keyword placed in front of the first parameter.
public static MyStaticClass {
   public static void DescribeYourself(this int value)
       System.Console.WriteLine("I am an integer with value " + value);

public static void Main() {
   int i = 5;

I am an integer with value 5

Do you see what's going on here?  The keyword this causes that static method to effectively behave as though you added an instance method to the int class.


Now let's look at a real world example.

Database frameworks often generate code from a data model.  The code follows the pattern of a  container class with properties that expose sets of entitity classes which have properties exposing data.

The container is the gateway to the database and sets of entities to tables with their elements to rows and properties to columns.

For example:

public class Container {
    public Set<Entity> Entities { get; set; }

public class Entity {
   public int Id { get; set; }
   public string Name { get; set; }

A typical usage of the above would be:
var db = new Container();
var query = db.Entities.Where(c => c.Name == "Foo");
Console.WriteLine("The entity named Foo has Id = " + query.Single());

What if we want something that was a bit more graceful?

Something like:

var db = new Container();
var query = db.Entities.QueryByName("Foo");
Console.WriteLine("The entity named Foo has Id = " + query.Single());

The logical answer starts with, "We have to add a method to the class which defines the type of db.Entities."

Looking to the Container class we see the type of Entities is Set<Entity>.  Can we add a method to that type?  No we cannot (to this point I'm 99% sure but please correct me if I'm wrong here).

Here is where an extension method can give us what we want.

public static class EntityExtensions {
   public static IQueryable<Entity>(
                   this Set<Entity> entities, 
                   string name) {
          return entities.Where(c => c.Name == "Foo");

See how we got around that fact that we couldn't add a method to the type Set<Entity>?  The net effect as as if we did though.  It's quite cool.

Thursday, June 28, 2012

Quick way to display a grid of information in a WinForms application.

This C# snippet can be used in a WinForms application to quickly display a grid of information.

Note how the column names are defined using LINQ to Objects.