Tuesday 10 June 2014

ASP.NET vNext


Details of the new version of the ASP.NET have being announced. Additional details about it can be found over the Internet e.g. here. Since chances are high that many of the proposed changes should make it to the actual product in its current form or a modified form, I thought it would be a good idea to try out the new version.
I was a little surprised when I created a new web project in Visual Studio 2014 CTP - You can try it out too if you have an account on Azure, there is an VM image available in the Image Gallery by the name "Visual Studio Professional 14 CTP". Notable things are :
  1. There is no web.config. Much of the configuration in the web.config file used to target the hosting of application in IIS, IIS Express or Cassini. Now that ASP.NET vNext aims to break free from IIS (refer to "Helios"), much of it not needed anyways. Rest of the configuration can be specified in "config.json" file - yes, it contains a JSON object and Visual Studio does a neat job of helping us with Intellisense. By default it has a connectionstring that wires up the default connection to a database hosted on "(localdb)\mssqllocaldb".
  2. There are not many options available on the dialog box that opens up when you click "Properties" of the context menu of the web project. Most of it is moved into "project.json" file which stores (again) a JSON object. Apart from project properties, it also keeps a list of nuGet package references.
  3. There is an interesting reference in the project named ".NET Framework 4.5" - you are free to change it to ".NET Core Framework 4.5" though which is targeted towards hosting on cloud (read Azure). So, I guess in future we will not be required to install .NET framework on the target machine as the necessary framework assemblies will be available along with the application's deployment folder. I wonder how would that impact the GAC though.
  4. You can host an ASP.NET vNext application in IIS, IIS Express, or even self host it in a console application. Imagine taking your application in a USB and hosting it on any available laptop by just plugging the USB stick and launching a command file (which can produce by using Publish feature of ASP.NET vNext). As an experiment, I created an ASP.NET vNext Console application (named "ConsoleApp2") - added a class "startup.cs", updated "Program.cs" to start the web server and added a simple controller/view in the application : it works like a charm :)
  5. Default database model for authentication/authorization has been pruned to just 2 tables - AspNetUsers and AspNetUserClaims. AspNetUsers, as the name suggests, is about the users and their credentials. AspNetUserClaims is most probably targeted to store rest of the details like role etc. as a claim. It is much simpler and covers most of the bases.
  6. There is only one base class for controllers i.e. Controller. No ApiController or MvcController etc. In fact, you don't even need to do that if you want. e.g. You can add a POCO class like NonStandardController and add a public method (action) that returns whatever you want to return.
  7. Everything can be configured through "Configure" method of "Startup" class. e.g. DirectoryBrowsing can be enabled by invoking UseDirectoryBrowser method on IBuilder instance. In fact, there is a way to configure the ASP.NET application to use one (or more) middleware classes to return response. e.g.
     
 And here is how you register your own Middleware.
 
 
Note: Instead of writing code from scratch I referred to the code samples available at Entropy. It is a great place to start when experimenting with ASP.NET vNext.
 
 

Monday 9 June 2014

Clone .NET objects

Making clones of objects is quite a regular requirement in .NET projects. Some of the typical scenarios are:

  1. Provide undo functionality in user forms in:
    • Desktop applications e.g. Windows Forms application, WPF application
    • Rich client applications e.g. Silverlight (I know it is most probably not going to have a new version but SL5 will be supported till 2021)
  2. Duplicate a record retrieved from data store

There are a few tried and tested methods which can help in solving this.

Method # 1:
Use Automapper. That is probably the easiest way. e.g. Suppose we have classes like these:

public class FormA
{
        public string FormName { get; set; }

       // public string TestString { get; set; }

        public FormB FormB { get; set; }
}

public class FormB
{
        public string FormName { get; set; }

}

Application code can be like following:

Mapper.CreateMap();
Mapper.CreateMap();
FormA f = new FormA() { FormName = "Test", FormB = new FormB() { FormName = "test2" } };
FormA f1 = new FormA();
Mapper.Map(f, f1);
Console.WriteLine(f1.FormName);
Console.WriteLine(f1 == f);

Console.WriteLine(f1.FormB == f.FormB);

Method# 2:
Use serialization process with any of the preferred formatters (XML, Binary, JSON etc.). Plenty of examples are available on the Internet for this e.g. here. If we use binary formatter, we can preserve value of private members too as binary serialization preserves type fidelity.

Method#3:
Write a generic copy method that Use Reflection to copy values of properties, members etc. It will turn out to be a very involved implementation as it will need to handle null values of members, and type of members (arrays, lists, enums etc.) but it will also provide flexibility to handle any case.

Method#4:
Implement ICloneable to implement the cloning logic.