Wednesday, August 21, 2013

Alternatives to Activator.CreateInstance - when milliseconds matter!


Hello World!

Let me open this post with this beautiful quote:

“Time felt slower when we do nothing but wait.” 
― Toba Beta, My Ancestor Was an Ancient Astronaut

On one such occasion, while waiting for a program to do what it should, I thought of analyzing the performance through Red Gate's ANTS memory profiler and it showed that a significant bit of time was being spent in creating types in a reflective manner using Activator.CreateInstance. A quick Google for alternatives yielded some results that were nothing short of an eye opener for me! So this is sort of a "post-it" for myself and peers of various ways the community has come up with.


I. Roger Alsing's LINQ way: as explained in his post here
This methodology makes use of an ObjectCreator delegate like so:

delegate T ObjectActivator<T>(params object[] args);

Once you implement the delegate as demonstrated by Roger, the invocation is pretty simple to read:

ConstructorInfo ctor = typeof(Created).GetConstructors().First();
ObjectActivator<Created> createdActivator = GetActivator<Created>(ctor);
...
//create an instance:
Created instance = createdActivator (123, "Roger");

II. Robert Jan Boeije's extension method on System.Type as explained in his post here
His way is using the power of compiled lambda expressions. (// TODO: something to learn)
When you have this extension method in place, the call to create a new instance is as simple as this:

return typeof(T).New<T>(value);

That's it!

III. Something similar by Steve Wilkes with performance statistics and insightful comments here

IV. Dean Oliver on CodeProject - Using the DynamicMethod class in the Reflection namespace
Demonstrated here
Here, Dean uses IL.Emit to optimize creation of instances. //TODO: read on how IL emit works!

V. Andrew Rissing on CodeProject: here
Again, the magic is lambda expressions and the Func<> delegate

Whoa! I guess that's enough for now!
In summary, the lesson learnt for me is that no more Activator.CreateInstance in my code!

Happy Coding!

Tuesday, August 13, 2013

Dependency injection with Unity in ASP.NET Web API


Hello World,

I have this shiny new project that I'm working for that is based on the ASP.NET Web API and SPA architecture. Before I describe where Unity comes into picture, let be briefly outline the pieces in the setup.
If those technologies sound unfamiliar, here are a couple of getting started links for the uninitiated:
ASP.NET Web API
SPA (Single Page Application)

SPA? API - Say What Now?
                      The basic premise of this architecture setup (IMHO) is that you design your services as REST APIs exposed over HTTP and the SPA piece provides the view that calls into your API. Where SPA differs from a traditional ASP.NET MVC application is that you have a single view that renders itself from the data fetched via an Ajax request to the Web API. In a typical setup, for the SPA application, the routing and data binding is taken care of by client-side scripting (compare this to a standard MVC app where the view engine takes care of the model binding while the routing is taken care of by the server side MVC routing framework complete with mapping actions on controllers to routes on the route table.
                       There are mature, robust client side frameworks for the client side, typically known as MV* frameworks - called so because there is typically no server side controller/routing. The most popular ones (as per my research) are:

  1. Backbone.js
  2. Spine
  3. Durandal - This is one you would find if you install the (famous) Hot Towel SPA NuGet package.

These frameworks fit nicely with popular JavaScript frameworks like jQuery and clientside UI frameworks like KnockoutJS. It may sound like a steep learning curve at the outset, but believe me, once you dig in with an open mind, you'd be feeling "why wasn't I using all of this earlier?"

DI Anyone?
           When designing applications, the most common concerns are that there should be a clear separation between the domain logic and the infrastructure code - e.g. Logging, Authentication, Authorization etc. Your business object may require other components to do its job e.g. connect to a database or read configuration from a file or an LDAP server (like the Microsoft Active Directory). A good design would try to keep the domain objects loosely coupled with these dependencies so that the resulting code is more testable (dependencies like database or LDAP could be mocked) and extensible (you may swap out the actual concrete implementation by another without the client code being affected). This is where IoC (Inversion of Control) and hence Dependency Injection fit in.

Unity - thy friend 
           The Unity application block from Microsoft's Pattern and Practices team is one of the many choices for managing your dependencies. Since other products were already using it, we choose to go with Unity. To add Unity to your Web API project, the easiest way is through NuGet. Right click you project and choose Manage NuGet packages

Were going to need the reference to the following assemblies: (check this link)

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll (current version 3.0.1304.0 at the time of this writing)
Unity.WebAPI.dll

The corresponding NuGet packages are:
 Enterprise Library common
 Enterprise library policy injection
 Unity Interception extension
 Unity WebAPI

Before and After 
             A typical Web API controller class derives from the ApiController base class provided by the ASP.NET Web API framework like so:

public class StaticDataController : ApiController

Now suppose your controller needs the following services (or dependencies)
A Logger - say ILogger
A repository to talk with the data source - say IRepository

If you were not using a DI container, you would have to something like the below to get there dependencies:

        private readonly ILogger _logger;

        private readonly IRepository _websiteTemplateRepository;

        public MyDataController()
        {
            _logger = SomeServiceLocator.GetService(typeof(ILogger));
            _theRepository = SomeServiceLocator.GetService(typeof(IRepository));
        }
If that looks strange, fear not, it is the Service Locator anti-pattern

What you would rather do is have your constructor demand these as dependencies and let a DI container inject those from its registrations. So the same controller would look like this:
private readonly ILogger _logger;

        private readonly IRepository _websiteTemplateRepository;

        public MyDataController(ILogger logger, IRepository repository)
        {
            _logger = logger;
            _theRepository = repository;
        }
When using the Unity container, you just have to structure your controllers (or other classes that need the services) like above and once you have registered the dependencies into the container like so:
var container = new UnityContainer();

container.RegisterType<IService, ConcreteService>(new ContainerControlledLifetimeManager());

the container would see (through the Unity.WebAPI piece) that these dependencies need to be injected and it would oblige!
The Unity.WebAPI package adds a bootstrapper.cs class that shows you what is expected. You'll need to call the bootstrapping code from the Global.asax.cs Application_start() method and then register your container with the IDependencyResolver like this:
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);

That is pretty much it!
Further reading:
Unity - getting started
Unity - Lifetime Managers
aExpense Reference Implementation (RI)

Summary: This package contains the aExpense Reference Implementation to showcase the use of Enterprise Library application blocks. There are two versions of the reference implementation: one using Enterprise Library 5.0 and another one using Enterprise Library 6.0. The objective was to assist you not only with getting acquianted with Enterprise Library, but also with migrating from the previous version.

The most up-to-date version of the documentation and release notes is available online:
http://go.microsoft.com/fwlink/p/?LinkID=290917


Microsoft patterns & practices
http://msdn.microsoft.com/practices


Happy Coding!

Friday, August 2, 2013

Sample application to demonstrate dependency injection with Unity in an ASP.NET app


Hello World,

I've been dangling with IoC and DI for quite sometime and thought it would be nice to sum up my gotchas in a blog post. I'll try to layout this post so that it starts with the assumption that the reader is an absolute newbie to Microsoft's Enterprise Library but has some understanding of what IoC (Inversion of Control) is and what DI (Dependency Injection) containers try to achieve. For an understanding about those concepts, I highly recommend Martin Fowler's post as a starting point.

I'm writing this from a machine where I've got VS 2012 but this pretty much works on VS 2010 as well.

I. File -> New Project

Fire up Visual Studio and create a new web project. For starters, I've used the ASP.NET WebForms application template:


II. Add some code!

For the purpose of this demo, I'll build an app that fetches users from a database table and dumps them on screen. Keeping it simple will let us focus on the key learning of how Unity is setup and used in a web application.

Let's add a class library now:
Right click the solution and choose Add -> New Project and choose the type as Class Library. Let's call it as Data. We're going to define a generic repository interface and a DB context to connect to the database here. I'm not going to implement the complete thing but only the necessary parts that would convey the concepts. Take a look at this wonderful post by Jonas Gauffin to understand why a repository pattern fits the bill almost in all business applications.

We now add a repository interface as below:

public interface IRepository<TEntity, in TKey> where TEntity : class
    {
        TEntity Get(TKey id);
        TEntity GetAll();
    }

Now we derive a IUserRepository from this:

public interface IUserRepository : IRepository<User, string>
    {
        /// <summary> Finds all Users. </summary>
        /// <returns></returns>
        IEnumerable<User> FindAll();

        /// <summary> Finds User by id. </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        IEnumerable<User> FindById(string id);
    }
Next we create a IDbContext to talk to the database

public interface IDbContext : IDisposable
    {
        /// <summary>
        /// This method returns new instance of command with same instance of connection on source type
        /// This might create problem in parallel execution of multiple data readers with same instance of db connection
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        IDbCommand CreateCommand(string connectionString, string commandText = "", params IDbDataParameter[] args);

        /// <summary>
        /// This method returns new instance of command with same instance of connection on source type
        /// This might create problem in parallel execution of multiple data readers with same instance of db connection
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="commandTimeOut">The command time out.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        IDbCommand CreateCommand(string connectionString, int commandTimeOut, string commandText = "", params IDbDataParameter[] args);

        /// <summary>
        /// This method will return new instance of command with new instance of connection
        /// It is useful in case of multiple threads executing data reader and db commands
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        IDbCommand CreateNewCommand(string connectionString, string commandText = "", params IDbDataParameter[] args);

        /// <summary>
        /// This method will return new instance of command with new instance of connection
        /// It is useful in case of multiple threads executing data reader and db commands
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        /// <param name="commandTimeOut">The command time out.</param>
        /// <param name="commandText">The command text.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        IDbCommand CreateNewCommand(string connectionString, int commandTimeOut, string commandText = "", params IDbDataParameter[] args);
    }

Let us now create a DTO (data transfer object) that mirrors what we want to show up on the screen:

public class User
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string Email { get; set; }

        public DateTime DateOfBirth { get; set; }
    }

This is what the Users table definition looks like

USE [SudhanshuTest]
GO

/****** Object:  Table [dbo].[Users]    Script Date: 8/2/2013 5:11:22 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Users](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [Email] [nvarchar](100) NULL,
    [DateOfBirth] [datetime] NULL,
PRIMARY KEY NONCLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

Here is a sample IDbContext class implementation:

public class DbContext : IDbContext
    {
        private readonly IDictionary<string, SqlConnection> _connections;
        private readonly string _sqlConnection;

        [InjectionConstructor]
        public DbContext()
        {
            _sqlConnection = ConfigurationManager.ConnectionStrings["UserDb"].ConnectionString;
            _connections = new Dictionary<string, SqlConnection>();
        }

        public IDbCommand CreateCommand(string connectionString, string commandText = "", params IDbDataParameter[] args)
        {
            return CreateDbCommand(CreateConnection(connectionString), commandText, args);
        }

        public IDbCommand CreateCommand(string connectionString, int commandTimeOut, string commandText = "", params IDbDataParameter[] args)
        {
            var command = CreateNewCommand(connectionString, commandText, args);
            command.CommandTimeout = commandTimeOut;
            return command;
        }

        public IDbCommand CreateNewCommand(string connectionString, string commandText = "", params IDbDataParameter[] args)
        {
            var connection = GetConnection(_sqlConnection);
            return CreateDbCommand(connection, commandText, args);
        }

        public IDbCommand CreateNewCommand(string connectionString, int commandTimeOut, string commandText = "", params IDbDataParameter[] args)
        {
            var command = CreateCommand(connectionString, commandText, args);
            command.CommandTimeout = commandTimeOut;
            return command;
        }

        public void Dispose()
        {
            foreach (var sqlConnection in _connections)
            {
                //TODO: add open checks...
                sqlConnection.Value.Dispose();
            }
        }

        private IDbConnection CreateConnection(string connectionString)
        {
            var connection = new SqlConnection(connectionString);
            connection.Open();
            return connection;
        }

        private IDbCommand CreateDbCommand(IDbConnection connection, string commandText = "", params IDbDataParameter[] args)
        {
            var command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = commandText;

            foreach (var sqlParameter in args)
            {
                command.Parameters.Add(sqlParameter);
            }

            return command;
        }

        private IDbConnection GetConnection(string source)
        {
            SqlConnection connection;

            if (!_connections.TryGetValue(source, out connection))
            {
                connection = new SqlConnection(_sqlConnection);
                connection.Open();
                _connections.Add(source, connection);
            }
            return connection;
        }
    }

Now add another class lib project called Data.SqlServer
Next, we create a base class for our repositories that would have the scaffolding required to get a DB connection

public abstract class RepositoryBase
    {
        internal IDbContext DbContext { get; private set; }

        protected RepositoryBase(IDbContext dbContext)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException("dbContext");
            }

            DbContext = dbContext;
        }


        public T Cast<T>(object columnValue)
        {
            if (columnValue is DBNull || columnValue == null)
            {
                return default(T);
            }

            return (T)Convert.ChangeType(columnValue, typeof(T));
        }
    }

We now create a UserRepository that derives from both the IUserRepository and the the RepositoryBase above. I'll just show the method that fetches users:

public IEnumerable<User> FindAll()
        {
            var result = new List<User>();

            using (var command = DbContext.CreateCommand(_connectionStrings, "dbo.GetAllUsers"))
            {
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        result.Add(new User
                            {
                                Id = Cast<int>(reader["Id"]),
                                FirstName = Cast<string>(reader["FirstName"]),
                                LastName = Cast<string>(reader["LastName"]),
                                DateOfBirth = Cast<DateTime>(reader["DateOfBirth"]),
                                Email = Cast<string>(reader["Email"])
                            }
                        );
                    }
                }
            }

            return result;
        }
III. Wire it all up with Unity.

The easiest way to add Unity and related stuff is through NuGet. Right click you project and choose Manage NuGet packages

Were going to need the reference to the following assemblies: (check this link)

Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll (current version 3.0.1304.0 at the time of this writing)
Microsoft.Practices.Unity.Interception.dll

The corresponding NuGet packages are:
 Enterprise Library common
 Enterprise library policy injection
 Unity Interception extension
 Unity

We are going to use the wonderful UnityContainerExtension class for our purpose to cleanly encapsulate the wiring up logic. In the Data.SqlServer class library project, which contains the concrete implementations of the interfaces that would like Unity to resolve, we would add a class called Module.cs with the following code:

public class Module : UnityContainerExtension
    {
        protected override void Initialize()
        {
            Container.RegisterType<IDbContext, DbContext>(new PerHttpRequestLifetime());
            Container.RegisterType<IUserRepository, UserRepository>(new PerHttpRequestLifetime(), new InjectionConstructor(new ResolvedParameter<IDbContext>()));
        }
    }
Things to note:
We derive from UnityContainerExtension class from the Microsoft.Practices.Unity namespace
This class has a single method called Initialize which has the signature as shown above.
Notice the order of the RegisterType statements, we first specify a mapping for the IDbContext to its concrete type and then the IUserRepository.
The concrete UserRepository has a constructor that accepts a parameter of the type IDbContext. That's why we specify the "InjectionContructor" and its parameter like so:
new InjectionConstructor(new ResolvedParameter<IDbContext>())

Next we need to tell our web app to initialize and setup the container. The most used way here is the Application_start method in the Global.asax.cs file. This looks like the below;

void Application_Start(object sender, EventArgs e)
        {
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterOpenAuth();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            SetupDependencyInjection();
            // Code that runs on application startup
        }

The SetupDependencyInjection method looks like this:
private void SetupDependencyInjection()
        {
            Container = new UnityContainer();
            Container.AddExtension(new Data.SqlServer.Module());
            DependencyResolver.SetResolver(Container);
        }
DependencyResolver is a small static helper class that looks like below:

public class DependencyResolver
    {
        private static IUnityContainer _container;

        public static void SetResolver(IUnityContainer resolver) //Change to interface...
        {
            _container = resolver;
        }

        public static T Get<T>()
        {
            return _container.Resolve<T>();
        }

        public static T Get<T>(string name)
        {
            return _container.Resolve<T>(name);
        }

        public static void BuildUp(object item)
        {
            _container.BuildUp(item.GetType(), item);
        }

        public static IUnityContainer Resolver { get { return _container; } }
    }

Note that for MVC apps, you'd normally make this call:
System.Web.Mvc.DependencyResolver.SetResolver(Container);

Since we do not have an MVC scope here, the DependencyResolver comes into picture.

That's pretty much it. You should now be able to connect to the database and run this to see the users on the screen! Unity is nicely resolving your dependencies without tight coupling the interfaces and implementations.

Further reading on Unity with a superb developer's guide and reference implementation is here.

Hope this presents a nice bootstarpper introduction that will whet the appetite of the interested :). I will try posting the code on soon.

EDIT: Some information on a reference implementation for Unity 5 and 6 (from the Readme)
-----------------------------------------------------------------------------------------------
aExpense Reference Implementation (RI)

Summary: This package contains the aExpense Reference Implementation to showcase the use of Enterprise Library application blocks. There are two versions of the reference implementation: one using Enterprise Library 5.0 and another one using Enterprise Library 6.0. The objective was to assist you not only with getting acquianted with Enterprise Library, but also with migrating from the previous version.

The most up-to-date version of the documentation and release notes is available online:
http://go.microsoft.com/fwlink/p/?LinkID=290917


Microsoft patterns & practices
http://msdn.microsoft.com/practices

-----------------------------------------------------------------------------------------------
Happy Coding


Tuesday, April 2, 2013

ASP.NET Persistent session state with Riak

Hello World,

Many of us have faced this pertinent question of choosing a persistent storage for ASP.NET session state, especially if you've worked with high usage e-commerce websites. During my research for an elegant yet simple to implement solution that does not overburden the production SQL server, I landed on this post by Jeremiah Peschka.

As Jeremiah highlights, the solution proposed meets the requirements on durable and fault tolerant session state with a ton of enterprise class features.

The core tool used is an open source key value database (never knew there was something like that :-)) called Riak.

I will try to dig deep and do a follow up post on how the experience is with a basic implementation using Riak as the session store.

Happy Coding!

Finding out who is active with your MS SQL Server Database

Hello World,

I was trying to research how to quickly (and easily :-)) find out what is currently active on a particular database instance and landed on this excellent post by Brent Ozar. He describes in a short, crisp video a tool called sp_whoIsActive created by Adam Machanic. You need to run the SQL script on your master database and once done, fire it from the context of the database you are interested in:

EXEC master..sp_WhoIsActive

You would get this nice result window:



Happy Coding!

Wednesday, August 22, 2012

Some MSDN Blogs of interest

Hello World,

This is actually a note to myself about some blogs that I found interesting (or more interesting, 'cause ideally I would love to be able to read everything there is on that site! - alas a day has only 24 hours : ))

Vishal Joshi’s Tangent (ASP.NET)

WebDev Blog 

Scott Hanselman

This very interesting post on universal providers (launched with VS 2012) - here
There is off course the perpetual favorite in Scott Guthrie's blog  

Performance tidbits by Rico Mariani

Then there is MSDN Blogs home : )  

Happy Coding

Monday, August 20, 2012

VS 2010 Dark Theme et al

Hello World!


              So I got a taste of dark theme while playing around with VS 2012 RC made available by our application life cycle maintenance team. It looks cool, and to be honest the greatest cool quotient is contributed by the fact that the theme is easy on the eye. For someone who stares at monitors almost the entire waking up day, that is a big one!
 
            I got down googling as to how I could get a good (tested : ) ) dark theme for VS 2010 and got these resources:
  1. Collection of themes for Visual studio: studiostyl.es
  2. My personal favorite (thus far): Dark Studio
  3. Stackoverflow tip on how to import the theme HERE
 The reason I chose Dark Studio is very clear: keywords, comments and highlighting is legible. What I also discovered is that when you hover over an outlined section's node, the entire section is highlighted:
 
 
For the ones who care: dark themes save energy as the display takes less power to show them!
 
Happy Coding

Friday, August 17, 2012

VS 2010 Freezing during Auto Save

Hello World,

           We use Virtual Machines (VM) for some of our development work. These VMs are hosted on a server that is geographically far off (I'm in India while the VM server sits in Western Europe). Time and again there are some applications that do not quite match up with how they would behave on a local desktop. Visual Studio is one such app.
           On my VM, I had this particular issue that when VS was trying to "Auto Save" the project and solution, it would hang up and not respond for some time. I googled up to find out if I could somehow reduce the frequency of auto save and found this blog.
PS: Although the blog talks about VS 2005, the option to set the auto save frequency is located at the same menu in VS 2010. (for a change :))

Happy Coding!

Wednesday, August 15, 2012

Windows developer power tools

Hello World,

Discovered this great book - Windows Developer Power tools and its companion website. There are a host of open source tools here that would make the life of any developer better. I will try to post about tools as I discover them but there's one which caught my eye straightaway: An MSI Content viewer/extractor called Less MSIerable (have to commend the choice of name :))

It is also worth taking a look at this list of most loved tools.
I've ordered a copy of this book from here. Can't wait to lay my hands on it!

Happy Coding!

Tuesday, August 14, 2012

C#: Keeping your stored procedure names maintainable

Hello World,

Necessity is the mother of invention (oft repeated but very true) - as your hair starts greying, (and hairline begins to recede ;) ) you suddenly start to realize the weight of these adages.
One such necessity arose out of my observation that stored procedure names were scattered all across a codebase I was working with. If you ever wanted to change the name of a stored procedure used some context, you had to search the entire codebase and replace all occurances. I did not like this approach and came up with the below approach:

  1. Create a type that holds stored procedure names grouped by module
  2. Read the name of stored procs from this type everywhere else. Now if a change is required it is in one sinlge place
 A sample is given below:


   1:  /// <summary>
   2:      /// Holds the names of stored procedures used by the application
   3:      /// </summary>
   4:      public static class Commands
   5:      {
   6:          /// <summary> Holds the commands to fetch configuration data </summary>
   7:          public static class Config
   8:          {
   9:              /// <summary> The Command to Get a list of websites that are to be processed </summary>
  10:              public const string GetWebsites = "dbo.GetSomeConfig";
  11:   
  12:              /// <summary> The command to get website specific configuration by it's name </summary>
  13:              public const string GetWebsiteConfiguration = "dbo.GetSomeMoreConfig";
  14:          }
  15:   
  16:          /// <summary> Holds the commands to fetch Auth data </summary>
  17:          public static class Authentication
  18:          {
  19:              /// <summary> The command to fetch auth info </summary>
  20:              public const string GetAuthenticationType = "dbo.GetAuthType";
  21:   
  22:              /// <summary> The command to validate user </summary>
  23:              public const string ValidateUser = "dbo.ValidateUser";
  24:   
  25:                  }
  26:   
  27:          /// <summary> Holds the commands to fetch related data </summary>
  28:          public static class AnotherModule
  29:          {
  30:                        // More names here
  31:                        ...
  32:          }
  33:      }

Now code that looked like this:

   1:  var cmdConfigSettings = new SqlCommand("dbo.GetSomeConfig", connString)
Will look like this:

   1:  var cmdConfigSettings = new SqlCommand(Commands.Config.GetWebsites, connString)
Happy Coding!

AppSettingsReader class: Get typed values from app.config

Hello World,

I came across this question in one of our coffee table discussions that it should be possible using .NET framework class library to return typed values from application configuration file (app.config) instead of getting all values as string and then typecasting. Turns out, the AppSettingsReader class is tailor made for this.

The following is a sample app.config:


   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <configuration>
   3:      <appSettings>
   4:      <add key="doWork" value="true"/>
   5:      <add key="name" value="DoGood"/>
   6:      <add key="sleepFor" value="2"/>
   7:      </appSettings>
   8:  </configuration>
 
 
The sample code below shows how the values for the keys can be read as typed:


   1:  using System;
   2:  using System.Configuration;
   3:   
   4:  namespace ReaderExample
   5:  {
   6:      class Program
   7:      {
   8:          static void Main(string[] args)
   9:          {
  10:              Console.WriteLine("----------Reading Values-----------");
  11:              WriteKeyValue("doWork", typeof(System.Boolean));
  12:              WriteKeyValue("name", typeof(System.String));
  13:              WriteKeyValue("sleepFor", typeof(System.Int32));
  14:   
  15:              Console.WriteLine("----------Reading Value's Types-----------");
  16:              DisplayTypeOfValueForKey("doWork", typeof(System.Boolean));
  17:              DisplayTypeOfValueForKey("name", typeof(System.String));
  18:              DisplayTypeOfValueForKey("sleepFor", typeof(System.Int32));
  19:              Console.ReadKey();
  20:          }
  21:   
  22:          static void WriteKeyValue(string keyName, Type typeOfValue)
  23:          {
  24:              AppSettingsReader appSettingsReader = new AppSettingsReader();
  25:              var theValue = appSettingsReader.GetValue(keyName, typeOfValue);
  26:              Console.WriteLine("The value of key {0} is {1}", keyName, theValue);
  27:          }
  28:   
  29:          static void DisplayTypeOfValueForKey(string keyName, Type typeOfValue)
  30:          {
  31:              AppSettingsReader appSettingsReader = new AppSettingsReader();
  32:              var theValue = appSettingsReader.GetValue(keyName, typeOfValue);
  33:              Console.WriteLine("The type of value of key {0} is {1}", keyName, theValue.GetType());
  34:          }
  35:      }
  36:  }
Where can this be used?
Well, for a start, we all have written code such as below:

   1:  bool doWork = Convert.ToBoolean(ConfigurationManager.AppSettings["doWork"]);
We can do this in a clearer way as below:
   1:  try
   2:  {
   3:    bool? isValidBoolean = appSettingsReader.GetValue("doWork", typeof(System.Boolean)) as bool?;
   4:  }
   5:  catch (InvalidOperationException ex)
   6:  {
   7:    Console.WriteLine("Could not convert!!");
   8:  }
Now, if you check isValidBoolean.HasValue and it returns true, you know you have a valid bool value. isValidBoolean.Value would give you the actual value after you've confirmed that you have a value. One thing though: GetValue method throws an exception if it finds a value that cannot be converted to the specified type.
Does not look too promising? Wait, there's more: Let me show you an implementation using Generics where we can get typed values from app.config, raise exceptions/log if key is not found and even return a default value if the value found is not suitable:
   1:  class TypedAccessor<T>
   2:      {
   3:          // typesafe AppSettingsReader supporting default values
   4:          T _defaultValue;
   5:          bool _required;
   6:          bool _warn;
   7:          string _keyName;
   8:          readonly AppSettingsReader _reader;
   9:   
  10:          /// <summary>
  11:          /// Initializes a new instance of the <see cref="TypedAccessor&lt;T&gt;"/> class.
  12:          /// </summary>
  13:          /// <param name="reader">The reader.</param>
  14:          /// <param name="keyName">Name of the key.</param>
  15:          /// <param name="warnIfKeyNotFound">if set to <c>true</c> [warn if key not found].</param>
  16:          /// <param name="isRequired">if set to <c>true</c> [is required].</param>
  17:          /// <param name="defaultValue">The default value.</param>
  18:          public TypedAccessor(AppSettingsReader reader, string keyName, bool warnIfKeyNotFound, bool isRequired, T defaultValue)
  19:          {
  20:              // An additional parameter Logger can be added to trace what is happening here
  21:              _defaultValue = defaultValue;
  22:              _required = isRequired;
  23:              _warn = warnIfKeyNotFound;
  24:              _keyName = keyName;
  25:              _reader = reader;
  26:          }
  27:   
  28:          /// <summary>
  29:          /// Fetches this instance.
  30:          /// </summary>
  31:          /// <returns>A value of Type T</returns>
  32:          public T Fetch()
  33:          {
  34:              T value;
  35:   
  36:              if (_warn && !ConfigurationManager.AppSettings.AllKeys.Contains(_keyName))
  37:              {
  38:                  Console.WriteLine("The Key {0} is not present in the app.config file.", _keyName);
  39:                  throw new ApplicationException(string.Format("The Key {0} is not present in the app.config file.", _keyName));
  40:              }
  41:              
  42:              try
  43:              {
  44:                  value = (T)this._reader.GetValue(this._keyName, typeof(T));
  45:              }
  46:              catch (System.Exception ex)
  47:              {
  48:                  if (_warn)
  49:                  {
  50:                      // Log in a real application
  51:                      Console.WriteLine("Error encountered while fetching value of the Key {0}. Default value will be returned.", _keyName);
  52:                  }
  53:   
  54:                  if (_required)
  55:                  {
  56:                      // Log in a real application
  57:                      Console.WriteLine("The Key {0} is required, but is not present in the app.config file", _keyName);
  58:                  }
  59:   
  60:                  value = _defaultValue;
  61:              }
  62:   
  63:              return value;
  64:          }
  65:      }
How does that look? Looks very promising to me!!
More ideas on making this even more useful:
1) Most (if not all) of the config values would be of a built-in datatype, so an additional check can be added to check if a value is of a built-in type E.g. this stackoverflow discussion
2) You may want to encapsulate the logic of returning a default value to another type something like the below:
a) An enum of key names
b) A Dictionary containing the keyname, default value
c) A method on this type to return the default value for a specified key

Happy Coding

Wednesday, May 16, 2012

Using System.IO.Packaging Package class to zip a file

Hello World,

I believe all of us who work with software that writes reports would have encountered scenarios when you had a huge report e..g one month of sales data, that required to be compressed to make it portable. I was faced with one such requirement and as always I started researching on what my options were.
               A google search came up with options like the popular 7-Zip library which is un-managed code, and DotNetZip, a codeplex open source library that seemed fit for the purpose. My mandate though (from the powers that be in an IT organization :)) was to try to avoid using a third party dll.
              Now, as a .NET programmer, I would love if the framework itself provided services to pacakge and compress files rather than having to look for a third party library: and the framework does not dissappoint in this regard. There are a couple of classes that provide packaging and compression:
  1. The System.IO.Compression GZipStream class
  2. The System.IO.Packaging Package class 
A requirement from the solution I was trying to devise was that the compression should produce a file that does not require additional software to decompress (unzip) it. Though the GZipStream class offers an elegant and efficient way to package and compress files, it requires a software that understands the gzip compression scheme. Such programmes are freely available, but require downloading and installing nevertheless! That ruled out GZipStream (I'll try to post sample code for that in another post)

Notice how I'm referring to "Package" and "compression" as the two steps involved here. A "Package" is a container that holds several objects in it. Consider a directory with several files in it - that is a container, although not very portable (not too straightforward to move across network from one machine to another). I know you'll say "I can use xcopy to copy over the directory anywhere I like!" you very well can, but wouldn't it be nice if there was some way of treating the directory as a single object that holds a relationship with it's content? Unix based systems have the "tar" utility (short for tape archive) that can package several resources (files) into a single file-like object that can be treated thus. This object then is a "Package".

Now, MSDN has an example showing how to use the Package class, but then, if all samples did everything you wanted, you wouldn't have a job : ). If you run the sample (from the downloaded solution), it would package everything nicely, but does not compress anything !!

So here is how I achived the task of packaging and compressing the file: First the class that calls the methods on Package class-



   1:      /// <summary>
   2:      /// TODO: Update summary.
   3:      /// </summary>
   4:      public class ZipPackageCompressor
   5:      {
   9:   
  10:          //  -------------------------- CreatePackage --------------------------
  11:          /// <summary>
  12:          /// Creates a package zip file containing specified
  13:          /// content and resource files.
  14:          /// </summary>
  15:          /// <param name="fileToZip">
               ///    The path to the file to zip. <example>E.g. C:\myfolder\thefileToxip.csv
               ///    </example></param>
  16:          /// <param name="pathToOutput">
               ///    The path where output(zipped file) will be placed. 
               ///    <example>E.g. D:\output</example>
               /// </param>
  17:          public void CreatePackage(string fileToZip, string pathToOutput)
  18:          {
  19:              // Convert system path and file names to Part URIs. In this example
  20:              string fileName = Path.GetFileName(fileToZip);
  21:              Uri partUriDocument = PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative));
  22:              string outputFileName = Path.Combine(pathToOutput, fileName + ".zip");
  23:   
  24:              // Create the Package
  25:              // If the package file already exists, FileMode.Create will automatically 
                   // delete it first before creating a new one.
  26:              using (var package = ZipPackage.Open(outputFileName, FileMode.Create, FileAccess.ReadWrite))
  27:              {
  28:                  // Add the Document part to the Package
  29:                  PackagePart packagePartDocument = package.CreatePart(
                                                                  partUriDocument, 
                                                                  System.Net.Mime.MediaTypeNames.Text.Plain, 
                                                                  CompressionOption.Maximum);
  30:                  
  31:                  // Copy the data to the Document Part
  32:                  using (FileStream fileStream = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
  33:                  {
  34:                      CopyStream(fileStream, packagePartDocument.GetStream());
  35:                  }
  36:              }
  37:          }
  38:   
  39:   
  40:          /// <summary>
  41:          /// Copies data from a source stream to a target stream.
  42:          /// </summary>
  43:          /// <param name="source">The source stream to copy from.</param>
  44:          /// <param name="target">The destination stream to copy to.</param>
  45:          private void CopyStream(Stream source, Stream target)
  46:          {
  47:              const int bufSize = 0x1000;
  48:              byte[] buf = new byte[bufSize];
  49:              int bytesRead = 0;
  50:              while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
  51:              {
  52:                  target.Write(buf, 0, bytesRead);
  53:              }
  54:          }
  55:      }

Notice the call to package.CreatePart on line 29 with the third parameter "CompressionOption.Maximum" this is what actually compresses the file. I believe the comments in the code make it pretty straight forward to understand what is going on.

To work with the System.IO.Packaging namespace, you would need a reference to the WindowsBase assembly:


To wind up, here is a test program that calls the CreatePackage above:


   1:      public class Program
   2:      {
   3:   
   4:          public static void Main()
   5:          {
   6:              // Path to directory of files to compress and decompress.
   7:              string dirPath = AssemblyDirectory;
   8:              var zipPackageCompressor = new ZipPackageCompressor();
   9:              zipPackageCompressor.CreatePackage(
                          @"D:\Sudhanshu\Projects\FileZipper\FileZipper\Contents\MyFile_bcce1c37.csv", 
                          AssemblyDirectory);
  10:          }
  11:   
  12:          /// <summary>
  13:          /// Gets the directory from where the (EXE) assembly is executing.
  14:          /// </summary>
  15:          public static string AssemblyDirectory
  16:          {
  17:              get
  18:              {
  19:                  string assemblyPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  20:                  return assemblyPath;
  21:              }
  22:          }
  23:   
  24:      }
Happy Coding!

Thursday, April 19, 2012

C#: Watching an app.config file for changes (and responding)

Hello World,

We had this windows service that had configuration in it's app.config that governed it's behaviour. A requirement from this service was that it should watch the config file and respond to changes made without having to be re-started (a restart would re-load the app domain and re-read the config anyways)
Of several possible solutions, my approach was to watch the file for changes using .NET's FileSystemWatcher class and then consume it's OnChange event to refresh/reload config values.

Draft 1: A prototype console application that implements the above solution


   1:  class Program
   2:      {
   3:          private static bool _stopService = false;
   4:   
   5:          static void Main(string[] args)
   6:          {
   7:              Console.WriteLine("Starting Worker Process");
   8:              Thread myWorkerThread = new Thread(Run) { Name = "Run Worker Thread" };
   9:              myWorkerThread.Start();
  10:              Console.WriteLine("Press any key to stop...");
  11:              Console.ReadLine();
  12:              _stopService = true;
  13:          }
  14:   
  15:          static void Run()
  16:          {
  17:              string assemblyDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
  18:              NotifyFilters notifyFilters = NotifyFilters.LastWrite; 
  19:   
  20:              FileSystemWatcher fileSystemWatcher = new FileSystemWatcher() { 
										Path = assemblyDirectory, 
										NotifyFilter = notifyFilters, 
										Filter = "*.config" };
  21:              fileSystemWatcher.Changed += OnChanged;
  22:              fileSystemWatcher.EnableRaisingEvents = true;
  23:   
  24:              Console.WriteLine("Watching for changes...");
  25:              while (!_stopService)
  26:              {
  27:                  Thread.Sleep(5 * 1000);
  28:              }
  29:          }        
  30:   
  31:          static void OnChanged(object source, FileSystemEventArgs e)
  32:          {
  33:              Console.WriteLine("Change event handler invoked...");
  34:              Console.WriteLine("Value before refresh: {0}", ConfigurationManager.AppSettings["myKey"]);
  35:              ConfigurationManager.RefreshSection("appSettings");
  36:              Console.WriteLine("Value after change: {0}", ConfigurationManager.AppSettings["myKey"]);
  37:          }
  38:      }
 
 
The application config file looks like this:
   1:  <xml version="1.0" encoding="utf-8" ?>
   2:  <configuration>
   3:    <appSettings>
   4:      <add key="myKey" value="testValue"/>
   5:    <appSettings>
   6:  <configuration>


Notes on the implementation:

  1. By launching the FileSystemWatcher on a worker thread, I am attempting to simulate the scenario in the actual service, so ignore those bits for now
  2. Line number 22 flags the FileSystemWatcher to raise events, needless to say, if you miss setting this flag, the events would never be raised
  3. You can get details of what has triggered the event in terms of full path of the object that was watched, change type (viz. changed, deleted, created etc.) through the FileSystemEventArgs
Issues:
When run, if you modify the app.config in your program's output directory and save it, you'll see the following output:





Did you see? The OnChanged event has been raised twice for a single change. I began researching this and found that this is a known behaviour (by design) as documented stackoverflow and here
Now, the resolution (at least the one I got to work):
Draft 2: In the OnChanged event, use a try finally block to set EnableRaisingEvents to false (in the try) and then set it back to true in the finally block:

   1:  class Program
   2:      {
   3:          private static bool _stopService = false;
   4:          private static FileSystemWatcher _fileSystemWatcher;
   5:   
   6:          static void Main(string[] args)
   7:          {
   8:              Console.WriteLine("Starting Worker Process");
   9:              Thread myWorkerThread = new Thread(Run) { Name = "Run Worker Thread" };
  10:              myWorkerThread.Start();
  11:              Console.WriteLine("Press any key to stop...");
  12:              Console.ReadLine();
  13:              _stopService = true;
  14:          }
  15:   
  16:          static void Run()
  17:          {
  18:              string assemblyDirectory = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
  19:              NotifyFilters notifyFilters = NotifyFilters.LastWrite; 
  20:   
  21:              _fileSystemWatcher = new FileSystemWatcher() { 
			Path = assemblyDirectory, 
			NotifyFilter = notifyFilters, 
			Filter = "*.config" };
  22:              _fileSystemWatcher.Changed += OnChanged;
  23:              _fileSystemWatcher.EnableRaisingEvents = true;
  24:   
  25:              Console.WriteLine("Watching for changes...");
  26:              while (!_stopService)
  27:              {
  28:                  Thread.Sleep(5 * 1000);
  29:              }
  30:          }        
  31:   
  32:          static void OnChanged(object source, FileSystemEventArgs e)
  33:          {
  34:              try
  35:              {
  36:                  _fileSystemWatcher.EnableRaisingEvents = false;
  37:                  Console.WriteLine("Change event handler invoked...");
  38:                  Console.WriteLine("Value before refresh: {0}", ConfigurationManager.AppSettings["myKey"]);
  39:                  ConfigurationManager.RefreshSection("appSettings");
  40:                  Console.WriteLine("Value after change: {0}", ConfigurationManager.AppSettings["myKey"]);
  41:              }
  42:              finally
  43:              {
  44:                  _fileSystemWatcher.EnableRaisingEvents = true;
  45:              }
  46:          }
  47:      }

So now the output looks like this:



Great! Another day saved!

Happy Coding!

PS: The code formatting above is powered by Manoli.net