pashov.net/code|February 2015

Monthly Archives: February 2015

.NET Coding

Dynamic objects in C#

Published by:

I know that it is not what strongly typed language is supposed to support but sometimes we need to add properties on the fly as in JavaScript. Here are two alternative ways to do that:

One way is to use the ExpandoObject that is being shipped in System.Dynamic. We can add properties (of any type) on the fly and to consume them afterwards. Consider the following example:

        dynamic dynamicObject = new ExpandoObject();
        dynamicObject.myCat = cat;
        dynamicObject.myCar = car;
        dynamicObject.justAString = "I am just a string";
        dynamicObject.justAnInteger = 42;

If we try to consume a property that is not present we get an exception. No need to say that you have to say good bye to intellisense.

The second way is to implement a class with an indexer and an underlying dictionary as the data store. You can add properties on the fly with a slightly different syntaxis. Consider the following example:

public class ViewBag
{
    public ViewBag()
    {
        dict = new Dictionary();
    }

    private Dictionary dict;

    public object this[string index]
	{
		get 
        {
            object val=null;
            dict.TryGetValue(index,out val);
            return val;
        }
		set 
        {
            if (dict.ContainsKey(index))
            {
                throw new Exception();
            }
            else
            {
                dict.Add(index,value);
            }
        }
}

and that is how we add properties

        var viewBag = new ViewBag();
        viewBag["mycar"] = car;
        viewBag["myCat"] = cat;
        viewBag["justAString"] = "I am just a string";
        viewBag["justAnInteger"] = 42;

I have named it viewBag purposefully because the actual MVC ViewBag is built this way.

I must admit that it is mainly border scenarios where you might need those but you never know.

Others

Is everything made yet?

Published by:

Sometimes i hear people saying that there is not much room for innovation as pretty much everything is being invented already in one form or another. Bad excuse.

I’ve been a bitcoin believer for quite a while now. Bitcoin in my opinion is THE innovation of the first decade of the 21-st century. However, is the bitcoin idea radically new? Not at all. If you take a look at Satoshi Nakamoto’s (a pseudonym for the bitcoin founder(s)) paper that introduced the bitcoin protocol in 2008 to the world, you will see that the references are at least 10 years old. Further, if you visit the first reference (very understandable and short read), you will see that bitcoin was conceived at least in year 1998. The ideas that bitcoin consists of as cryptography, distributed networks and so on, date further back.

The reason i am saying this is that you don’t have to invent the wheel to be an inventor. You can:
– apply an old idea in practice
– combine old ideas in an ingenious way
– make an old thing better
– market a good idea in an innovative way
– see an innovative application of an idea or a thing
– make more with less

and so on…

I am sure the list can go forever. What is hard is to believe that everyone can be an inventor and adopt that state of mind.

.NET Coding

Construct Dynamic Filters with C#

Published by:

Filtering data with C# is pretty straight-forward when using LINQ – you get results with one-liners as

var result=cars.Where(a=>a.carMake=="Fiat" && a.speed>100);

However, we know the filtering properties (carMake and speed) and the filtering conditions (equals with carMake and greater than with speed) in advance. The more interesting case, however, is when we don’t know the filtering properties, their number and the filtering criteria in advance but have to construct them in real time. Consider the case when you have data objects with many properties and allow the user on the front end to filter it according to his/her needs. In this case Dynamic Expression Construction comes to rescue.
LINQ extension methods work with predicates (or anonymous functions). Fortunately, C# provides very powerful api to build Expression trees, which can be compiled to predicates in runtime and passed to the LINQ extension methods afterwards.
Let’s see an example. Suppose that we have a list of cars and we want to filter them dynamically for whatever purpose – here is our Car object

 
public class Car
    {
        public string Model { get; set; }
        public string Make { get; set; }
        public string CountryOfOrigin { get; set; }
        public string CountryOfProduction { get; set; }

        public int MaxMilesPerHour { get; set; }
        public int NumberOfSeats { get; set; }
        public int MilesPerGallon { get; set; }
        public double WeightInKg { get; set; }

        public decimal BasePrice { get; set; }
        public decimal FullPrice { get; set; }
    }

With 10 properties, we can filter a collection of cars in many possible ways.
Next we need a class that would represent a filter – so we need a property name for comparison, value to be compared to and operation (equal, less than etc). Let’s call it ExpressionFilter

public class ExpressionFilter
    {
        public string PropertyName { get; set; }
        public object Value { get; set; }
        public Comparison Comparison{ get; set; }
    }

Comparison is nothing more than a enum with possible operations

public enum Comparison
    {
        Equal,
        LessThan,
        LessThanOrEqual,
        GreaterThan,
        GreaterThanOrEqual,
        NotEqual,
        Contains, //for strings
        StartsWith, //for strings
        EndsWith //for strings
    }

Next comes the real thing – we need a class which can take a list of ExpressionFilters, do its thing and return an Expression tree composed of these filters, which we can use as a parameter in any LINQ extension method after compiling it. Let’s have a class that constructs And expression trees. We will call it ConstructAndExpressionTree

 
public static Expression<Func<T, bool>> ConstructAndExpressionTree<T&gt(List<ExpressionFilter> filters)
        {
            if (filters.Count == 0)
                return null;

            ParameterExpression param = Expression.Parameter(typeof(T), "t");
            Expression exp = null;

            if (filters.Count == 1)
            {
                exp = ExpressionRetriever.GetExpression<T>(param, filters[0]);
            }
            else
            {
                exp = ExpressionRetriever.GetExpression<T>(param, filters[0]);
                for (int i = 1; i < filters.Count; i++)
                {
                    exp = Expression.And(exp, ExpressionRetriever.GetExpression<T>(param, filters[i]));
                }
            }

            return Expression.Lambda<Func<T, bool>>(exp, param);
        }

The class is generic so it can be used with any type. The code is pretty straight-forward -> we start with the ParameterExpression which is the t=> part of the predicate. Then we iterate through the filters and start building the actual expression called exp here. You probably notice that we use a helper method ExpressionRetriever.GetExpression to construct the separate expressions (like a.speed>100), which we stick with Or binary operator aferwards. Here is the code of ExpressionRetriever.GetExpression:

public static class ExpressionRetriever
    {
        private static MethodInfo containsMethod = typeof(string).GetMethod("Contains");
        private static MethodInfo startsWithMethod = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
        private static MethodInfo endsWithMethod = typeof(string).GetMethod("EndsWith", new Type[] { typeof(string) });

        public static Expression GetExpression<T>(ParameterExpression param, ExpressionFilter filter)
        {
            MemberExpression member = Expression.Property(param, filter.PropertyName);
            ConstantExpression constant = Expression.Constant(filter.Value);
            switch (filter.Comparison)
            {
                case Comparison.Equal:
                    return Expression.Equal(member, constant);
                case Comparison.GreaterThan:
                    return Expression.GreaterThan(member, constant);
                case Comparison.GreaterThanOrEqual:
                    return Expression.GreaterThanOrEqual(member, constant);
                case Comparison.LessThan:
                    return Expression.LessThan(member, constant);
                case Comparison.LessThanOrEqual:
                    return Expression.LessThanOrEqual(member, constant);
                case Comparison.NotEqual:
                    return Expression.NotEqual(member, constant);
                case Comparison.Contains:
                    return Expression.Call(member, containsMethod, constant);
                case Comparison.StartsWith:
                    return Expression.Call(member, startsWithMethod, constant);
                case Comparison.EndsWith:
                    return Expression.Call(member, endsWithMethod, constant);
                default:
                    return null;
            }
        }
    }

Expression class provides methods for the standard operations as equal, not equal, less than etc. but notice also the last three cases where we use Expression.Call to call the string methods Contains, StartsWith and EndsWith. Here lies a big potential for extension as you can use whatever methods you like defined on any type. You only should get their MethodInfo through reflection.
So sticking it all together (i skip the part where i populate the cars collection):

      static void Main(string[] args)
        {
            var filters = new List<ExpressionFilter> 
            { 
                new ExpressionFilter
                {
                    PropertyName="CountryOfOrigin",
                    Comparison=Comparison.StartsWith,
                    Value="Fr"
                },
                new ExpressionFilter
                {
                    PropertyName="MaxMilesPerHour",
                    Comparison=Comparison.GreaterThanOrEqual,
                    Value=190
                }
            };

            var expressionTree= ExpressionBuilderHelper.ExpressionBuilder.ConstructAndExpressionTree<Car>(filters);
            var anonymousFunc = expressionTree.Compile();
            var result = cars.Where(anonymousFunc);
        }

You can see that the result is a car that has MaxMilesPerHour>190 and CountryOfOrigin that starts with ‘Fr’
result