Thursday, January 31, 2013

ASP.NET MVC

ASP.Net MVC provides an alternative to Web Forms for building web applications on the .Net platform. As an alternative to Web Forms, ASP.Net MVC takes a different approach when it comes to structuring web applications. This means user won't deals with ASPX pages and controls, postbacks or view state, or complicated event lifecycles. Instead, user has to define controllers, actions, and views.
Benefits of ASP.Net MVC
  • Closer to the protocol
    While ASP.Net Web Forms attempts to completely hide the stateless nature of HTTP, ASP.Net MVC doesn't. The model is also drastically simplified - gone are the complex page lifecycle events of Web Forms, and the abstractions over HTTP are minimal.
  • Separation of concerns
    While ASP.Net Web Forms tightly coupltes the user interface to its code-behind, ASP.Net MVC encourages a design where the user interface (the view) is kept seperate from the code that derives it (the controller).
  • Testability
    By separating applicaiton logic from the user interface, ASP.Net MVC makes it easier to test individual components in isolation. Unlike Web Forms, MVC controllers do not have a direct dependency on the infamously untestable HttpContext class and instead rely on an abstraction, which makes it far easier to write automated unit tests.
Whats new in ASP.Net MVC 3/4
MVC 3 and 4 comes with many improvements and several new features in addition to the new dependency on .Net 4. These new features include
  • The Razor view engine
  • Package management with NuGet
  • Improved extensibility
  • Global action filters
  • Dynamic language features
  • Partial page output caching
  • Ajax improvements
  • Enhancements to the validation infrastructure
  • Mobile templates
  • Web API

Wednesday, January 30, 2013

Could not load file or assembly... or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

Few time we encounter the "Could not load file or assembly... or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" error while working on ASP.Net.
Error Description:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Common' or one of its dependencies. The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Resolution: delete temporary asp.net files in -
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\

This will resolve the problem. You don't to re-install anything.

Tuesday, January 29, 2013

Use of Extension Method in NVelocity

NVelocity is a .Net-based template engine. It permits anyone to use the simple yet powerful template language to reference objects defined in .Net code. NVelocity is a port of the excellent Apache Jakarta Velocity project for .NET platform. It is a very simple, easy to learn and extensible template engine.
Library: 
  • NVelocity.dll
  • NVelocityTemplateEngine.dll
Below example shows how to incorporate dynamic content from xml file to simple text file. Consider we have "customer.xml" and  "customer.vm" files as:

customer.vm
Loop :

List of Customer Details:
#foreach($customer in $helper.MakeCollection($root.testdocument.ListOfCustomerDetail.CustomerDetail))
UID: $customer.UID
Language: $customer.Language
BusinessUnit: $customer.BusinessUnit
#end

List of Leads:
#foreach($customer in $helper.MakeCollection($root.testdocument.ListOfLeadDetail.CustomerDetail))
UID: $customer.UID
Language: $customer.Language
BusinessUnit: $customer.BusinessUnit
#end

customer.xml
<?xml version="1.0" encoding="UTF-8"?>
<testdocument>
  <ListOfCustomerDetail>
    <firstname>hello</firstname>
    <CustomerDetail>
      <UID>1</UID>
      <Language>English</Language>
      <BusinessUnit>ITD</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>2</UID>
      <Language>English</Language>
      <BusinessUnit>Eolution</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>3</UID>
      <Language>English</Language>
      <BusinessUnit>Continuty</BusinessUnit>
    </CustomerDetail>
  </ListOfCustomerDetail>
  <ListOfLeadDetail>
  <firstname>hello</firstname>
    <CustomerDetail>
      <UID>1</UID>
      <Language>English</Language>
      <BusinessUnit>ITD</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>2</UID>
      <Language>English</Language>
      <BusinessUnit>Eolution</BusinessUnit>
    </CustomerDetail>
    <CustomerDetail>
      <UID>3</UID>
      <Language>English</Language>
      <BusinessUnit>Continuty</BusinessUnit>
    </CustomerDetail>
  </ListOfLeadDetail>
</testdocument>

Here, XML document contents are converted into Json Object. This Json object then registered with the NVelocity context.

Similarly to access the extension method inside the template, we have to register Helper class instance with the NVelocity context. We have to make helper class non-static for this.
using System;
using System.Globalization;
using System.IO;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NVelocity;

namespace NvelocityTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("customer.xml");

            JObject jobject = JObject.Parse(JsonConvert.SerializeXmlNode(doc.DocumentElement).ToString());

            NVelocity.VelocityContext context = new NVelocity.VelocityContext();
            context.Put("helper", new Helper());
            context.Put("root", jobject);
          
            NVelocity.App.Velocity.Init();

            NVelocity.Template template = NVelocity.App.Velocity.GetTemplate("customer.vm");

            StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);

            template.Merge(context, writer);

            Console.WriteLine(writer.ToString());

            Console.ReadKey();
        }
    }

    public class Helper
    {
        public JArray MakeCollection(JObject value)
        {
            string text = "[" + value.ToString() + "]";
         
            return JArray.Parse(text);
        }

        public JArray MakeCollection(JArray value)
        {
            string text = value.ToString();

            return JArray.Parse(text);
        }
    }
}

Wednesday, January 2, 2013

Happy New Year


Cheers to a new year and another chance for us to get it right.