Writing Fluent API in your applications

Fluent Interfaces / APIs are an amazing way of chaining methods and passing the input of the previous operation to the next operation. This reduces the amount of code required to declare similar objects or setting properties and configurations of the same object and makes the code more readable. Martin Fowler and Eric Evans coined the term back in 2005 (http://martinfowler.com/bliki/FluentInterface.html)

The implementation is very straight forward. The general pattern is to have chained methods each of which should be self-referential (should return object of its own type) and a terminating method to end the chain.

Following is a simplest example, a Number class which provides self-referential methods of operations returning the Number Object itself and an Equals method to terminate the chain.

    public class Number

    {

        private int _number;

        public Number(int num)

        {

            _number = num;

        }

        public Number Add(int num)

        {

            _number += num;

            return this;

        }

        public Number Subtract(int num)

        {

            _number -= num;

            return this;

        }

        public Number Multiply(int num)

        {

            _number *= num;

            return this;

        }

        public Number Divide(int num)

        {

            _number /= num;

            return this;

        }

        public int Equals()

        {

            return _number;

        }

    }

So, we can use the number class as follows by chaining the operations together.

              Console.WriteLine(

                new Number(10)

                .Add(10)

                .Subtract(5)

                .Multiply(10)

                .Divide(2)

                .Equals()

                );

Dynamically Adding DbSet Properties in DbContext for Entity Framework Code First

When working with Entity Framework Code First, the general pattern to add DbSet properties to the DbContext object is as follows. The Entity Framework will build the model according to the DbSet properties defined in the DbContext.

public class MyAppContext : DbContext

    {

        DbSet<Customer> customers { get; set; }

    }

However, for a project with even a fair bit of domain complexity, defining DbSet properties for all entities would not be possible and we need a way to dynamically read our Entity Types and add them dynamically in the DbContext. This can be achieved by implementing OnModelCreating method of the DbContext. The Entity Types can be read from either the executing assembly Assembly.GetExecutingAssembly() or by loading more than one assemblies which can be placed in a configuration file (as shown below in the example). For details about setting up the custom configuration section with your assemblies see my last post

The following code will add all Class Types to the DbContext for all the assemblies added in the configuration file, notice we are actually calling modelBuilder.Entity<T>() method through reflection.

 

public class MyAppContext : DbContext

    {

        protected override void OnModelCreating(DbModelBuilder modelBuilder)

        {

            CustomAssemblySection configSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");

 

            foreach (CustomAssembly customAssembly in configSection.Assemblies)

            {

                Assembly assembly = Assembly.Load(customAssembly.Name);

                foreach (Type type in assembly.ExportedTypes)

                {

                    if (type.IsClass)

                    {

                        MethodInfo method = modelBuilder.GetType().GetMethod("Entity");

                        method = method.MakeGenericMethod(new Type[] { type });

                        method.Invoke(modelBuilder, null);

                    }

                }

            }

            base.OnModelCreating(modelBuilder);

        }

    }

Adding Custom Configuration Section in .NET Configuration Files

In order to add a Custom Configuration Section in any of .NET configuration files like app.config or web.cofig, the framework provides classes in the System.Configuration namespace such as System.Configuration.ConfigurationSection, System.Configuration.ConfigurationElement, System.Configuration.ConfigurationElementCollection and others.

Lets say we want to add the following Custom Section in the configuration file which can hold a list of assemblies.

<CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

We need a Custom ConfigurationSection, a Custom ConfigurationElementCollection (assemblies) and a Cusom ConfigurationElement with one property which is “name” of the assembly. The following code is achieving exactly that. We have three classes CustomAssemblySection, CustomAssemblyCollection and CustomAssembly which inherit from the respective classes of System.Configuration.

Following is code for a simple Console Application with Program.cs and App.config files. The Program Main is reading the configuration and writing the assembly names on the Console.

For further reading you can refer to MSDN How To: http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Configuration;

namespace CustomConfigurationSectionConsole

{

    class Program

    {

        static void Main(string[] args)

        {

            CustomAssemblySection customSection = (CustomAssemblySection)System.Configuration.ConfigurationManager.GetSection("CustomAssemblySection");

             foreach (CustomAssemblyElement customAssembly in customSection.Assemblies)

             {

                 Console.WriteLine(customAssembly.Name);

             }

             Console.ReadLine();

        }

    }

    public class CustomAssemblySection : ConfigurationSection

    {

        [ConfigurationProperty("assemblies", IsDefaultCollection = false)]

        [ConfigurationCollection(typeof(CustomAssemblyCollection))]

        public CustomAssemblyCollection Assemblies

        {

            get

            {

                return (CustomAssemblyCollection)base["assemblies"];

            }

        }

    }

    public class CustomAssemblyCollection : ConfigurationElementCollection

    {

        protected override ConfigurationElement CreateNewElement()

        {

            return new CustomAssemblyElement();

        }

        protected override object GetElementKey(ConfigurationElement element)

        {

            return ((CustomAssemblyElement)element).Name;

        }

        public CustomAssemblyElement this[int Index]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Index);

            }

            set

            {

                if (BaseGet(Index) != null)

                {

                    BaseRemoveAt(Index);

                }

                BaseAdd(Index, value);

            }

        }

        new public CustomAssemblyElement this[string Name]

        {

            get

            {

                return (CustomAssemblyElement)BaseGet(Name);

            }

        }

    }

    public class CustomAssemblyElement : ConfigurationElement

    {

        [ConfigurationProperty("name", IsRequired = true)]

        public string Name

        {

            get

            {

                return (string)this["name"];

            }

            set

            {

                this["name"] = value;

            }

        }

    }

}

App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <configSections>

    <section name="CustomAssemblySection" type="CustomConfigurationSectionConsole.CustomAssemblySection, CustomConfigurationSectionConsole"/>

  </configSections>

    <startup>

        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />

    </startup>

  <CustomAssemblySection>

    <assemblies>

      <add name="System.Configuration"></add>

      <add name="System.Data.Entity"></add>

      <add name="System.Data"></add>

      <add name="System.Xml"></add>

      <add name="System.Xml.Linq"></add>

      <add name="System.Core"></add>

    </assemblies>

  </CustomAssemblySection>

</configuration>

The Secret Science of Motivation – Daniel Pink

Pink argues that the extrinsic motivators or incentives are the legacy of the industrial age, they only work for procedural work which does not require creative thinking. The Motivation of a 21st century information worker revolves around the following intrinsic motivational factors.

  1. AUTONOMY
  2. MASTERY
  3. PURPOSE

 

Url Shortening: Tr.im shutting down

A few days back I posted a few concerns about the URL shortening and the “tiny url’s” which are now all over the place literraly.

Now another conern comes along, so what happens if the company you are depending on for keeping all your URL’s safe which you would be posting for months or years (in future) suddenly shuts down. You loose the meaning of all your life long tweets which thousands of links. A huge amount of content will be lost with millions of broken links.

That is exactly what is going to happen to the URL’s of Tr.im, the URL shortening service which is shutting down.

Using Log Parser for analyzing IIS Logs

Microsoft Log Parser is a Command Line tool to analyze and extract data from all sorts of Log Files. It is a command line tool and does not have a fancy UI but is extremely power. The most powerful feature is the use of SQL Like query structure to extract information.

Just created the following Availability Throughput report for a couple of application that we have here. It is taking the average of the time-taken field in the IIS log over the last 4 months.

See the Log parser query.

After getting the data it is just a matter of putting it in an excel and generting the graph which I did but the Log Parser tool itself can generate a graph output as an image. There is along list of input and output formats for the tool. I just did’nt had time to look into the graph switches right now.