Tuesday, April 22, 2014

How to remove Antlr3.Runtime & WebGrease from your ASP.Net MVC web application

What is Antlr3.Runtime & WebGrease

As of a new programmer those are the dll which increase the size of their project. Before we remove these dlls we should know what are those dlls and their duties? Those are the dlls used for Bundling and Minification. By default the Visual Studio adds those nugget packages in the newly created MVC web application.

http://westdiscgolf.blogspot.in/2012/06/aspnet-mvc-4-webapi-rc-what-on-earth-is.html
http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

When should we remove Antlr3.Runtime & WebGrease

Next thing we need to understand when we should remove those and why we need to remove? If you are creating a production application, never remove as those libraries will increase the performance of our application. We may not require if we are doing any of the following.
  • Creating a PoC and mailing the same which don't deal with minification and bundling.
  • Creating a sample for blogging.

Removing Antlr3.Runtime.dll & WebGrease.dll packages

Method 1 - nuget uninstallation

Below are the steps proper steps if we have the solutions in proper condiction
  • Open nugget package manager console or UI.
  • Execute the command Uninstall-package 'package name'.The order of removal is as follows
    • Microsoft.AspNet.Web.Optimization
    • WebGrease
    • Antlr
  • Follow the common steps for method 1 & 2

Method 2 - Manual removal

Use these steps if you are not able to make it via nugget.
  • Manually remove the reference to the Antlr3.Runtime.dll, WebGrease.dll & Microsoft.AspNet.Web.Optimization from solution explorer
  • Follow the common steps for method 1 & 2

Common steps for Method 1& 2

  • Remove or exclude the App_Start\BundleConfig.cs file.
  • Remove the method call BundleConfig.RegisterBundles(BundleTable.Bundles); from Global.asax.cs
  • Remove the import using System.Web.Optimization; from all code files.
  • Remove the @Scripts.Render() and @Styles.Render method calls from your view files
  • It will make the application fail, if we are using scripts and styles. You need to explicitly add the styles and scripts to the application For example to render styles add the below line.
  • <link rel="stylesheet" href="@Url.Content("~/Content/site.css")">
  • Remove the "System.Web.Optimization" entry from views\web.config file->
    <system.web.webPages.razor> -> <namespaces>
    
  • Clean the solution and delete everything in \bin folder

Method 3 - Remove the packages folder and bin folder and ask the user to sync nugget packages

As you see the problem its about sending the sample code to other user and his easiness to run the same. If the target developer knows about nugget, let him install the nugget packages before running the sample. This is the actual solution provided the target developer have enough internet connectivity to sync nugget packages.

I have tried this in MVC 4 & 5 from VS2010 & VS 2013 without any issues.
 
Happy coding.

Tuesday, April 15, 2014

Code for yourself before you code for others

I got a strike from the quote "Code for yourself before you code for others" when I attended Google IO Extended 2012 event. Even though I was developing tools for me, I never took that as serious in determining success of a Software engineer. After 2 years of experiments related the quote, I could see that this is something important for a software professional. Now I am utilizing this quote for hunting talent for my current company.

Usually in software services companies (I would like to call as code factories), the ultimate aim is to deliver the project with working features. If the delivery mode is "code delivery" there are chances that there will be an acceptable level of quality. But in the other way "features delivery", we can never expect quality in the code. The features will be working. That's all. In such companies the hiring pattern is as follows. I know the point here is about code for ourselves, but through this I can show the importance of, one coding for himself.

Hire worker people to increase head count

This happens in big companies in below situations
  • Show their strength for marketing and to get more projects
  • Show that they are stable, still growing and not affected by recession etc.
This also happens during the period a small company tries to become a medium company or when medium company is becoming large.

Type of People

In this type of recruitment companies normally goes to campus to recruit people from even non computer science branches such as Civil, Agriculture,Accounting etc...who sometimes doesn't know how to shut down the computer.
 
But the main focus in this hiring will be common sense of the candidates, so that they can be moulded as workers who at least don't harm the software systems.
 
If we think from the candidate perspective, these people will not be having particular aim in their life except to get equal status of others and earn money. When they complete their schools, they just listen to others(relatives & friends) words and select Electronics or Mechanical engineering, instead of hearing their own mind. Their mind may be telling them to become an accountant. But as the factor of common sense and adaptability is more, they will get more marks in the exams even though they don't know anything practically about their main stream subject. Believe or not I had multiple situations where people who scored more than 80% marks in Electrical engineering department don't know, why they are not getting electric shock, when they use tester to touch wires. These people again take others words and attend the interview conducted by software development companies.
 
Also these people won't read much about theory part computer science. If they know RegEx & parsing, I would say its really great. One of the benefit for them is they can switch to any other job at any time as they don't have the bonding with computer science. This is really helpful during recession days.
 
As these type of people don't have any particular aim except money, these people can be moulded to work on anything, provided they get right instructions.

Hire own people (Relatives & Friends)

This happens in most of the companies. The manager / higher level executives inject their own people into the company with their influence in 95% cases and rest through proper channel. They do it due to various reasons
  • His friend is really good in particular technology and the company needs the talent
  • He wants a strong team who can take his orders without fail or complaints.
  • His relative / friend is not getting job in other companies after he was fired from previous job or passed out of college.
  • He needs a spy in the team so that he can know the heart beat of the team.
  • He want to take his person to abroad. Let this be the way.

Type of people

95% of the people who are hired in this way will not be meeting the hiring criteria and will be a burden to their colleagues which in turn reduce the team's productivity and demotivate the loyal people. If these candidates are really skilled they should have joined in other companies. That itself shows these people are not worth.
 
The real trouble will be to the middle level leads to whom these hired people are reporting. He cannot keep the person in the team as he is not capable of doing the tasks, but afraid to escalate as he is backed by the manager. This in turn increase pressure to the rest of the team in meeting the deliveries. The pressure eventually makes the loyal and talented people, leaving the project or company.

Hire the talent to think & innovate

Worker people alone cannot run the company. There needs some smart people to think and take decisions and finding those people is really a tough job for interviewers. Within the 30-45 minutes how to identify whether the candidate is really talented or not? The future of the company relies in the hands of these people as these people are going to think and take decisions for the company.
 
I faced the challenge so many times. Earlier I used to give various scenarios and ask them to think about alternatives. If you are an interviewer, you don't need to worry about these type of questions. Just take one of the scenario in your project and explain them in a different way. If they think smart and provide better solution than what you implemented, you can even refine your solution :) .But don't get into trouble.
 
Now I have added one more criteria to the list and its nothing but "The solutions they developed for themselves". If they have solved their own problems successfully, 50% we can say he suits to the talent criteria.

Type of people

Here comes the passion. These people will be passionate about software engineering. They will not be just programmers of particular language. They try to understand what the user needs and think about various solutions and picks up the best one. They will be continuous learners and brave enough to remove or add new tools & technologies into the project. The toughest part is they don't compromise quality to meet delivery timeline.
 
In short if a company have at least one person like this, it can have 50 worker people.

Tuesday, April 8, 2014

Showing different RAZOR Views based on the Model type in ASP.Net MVC

Problem

In real time, there will be scenarios where we need to display different model classes differently in the Views. Normally, if the controller is aware of the Model type and knows the corresponding View, it can return the proper ViewResult object via its Action() methods. But what should we do, if we have an GenericBusinessObject ie an Entity which have its properties in a dictionary or enclosed in XElement and we need to switch the View based on the type of Generic Entity?

My current project have an offline processing feature which is controlled via queues. When somebody needs to process a long running task they put their request into the queue and the queue monitor mechanism dequeue the requests whenever there is space to process that request. This is a general framework where the track developer needs to write code for queuing the request which accepts the parameter in the form of xml. The parameter decide how the processing server process that particular request. The track developer also needs to write code for the actual processing by implementing certain interfaces. Simply saying the queuing system just makes the processing delayed until the processing servers have enough capacity to process the request and the details about how to process and related logic is decoupled from the queue framework as the operation detail is passed as xml.

This system also has monitoring web application as well where the production engineers can see how many queue items are waiting in the queue, how many failed etc...The web site also have provision to see the queue request parameter xml which is viewable only to the application support engineers. A trained application support engineer can identify, what a particular queue request is intended for, by analysing the queue requests parameter. This monitoring site is implemented using ASP.Net MVC 4 with a page to see all queue requests. Things were easy until we get a requirement to display different pages based on the queue request type, so that the application support engineers don't need to analyse the xml to know what is the intention of the queue request.

To understand the issue in a simple scenario, lets take Person object with 2 properties. PersonType and Properties. Definition is given below.

    public class Person
    {
        [Display(Name = "First Name")]
        public string PersonType { getset; }
        public XElement Properties { getset; }
    }

The Properties is kept as xml as it is having dynamic schema or in simple words variable number of properties. If the PersonType is Employee the Properties can include FirstName & Company.For Student type it can have FirstName,School & Marks. 
The MVC site is supposed to call a web service to get person  object list and display all the unique person types from the obtained person list. When the user clicks on the PersonType link it should display Person objects corresponding to the person type. If the type is known such as Student display 3 columns and if its unknown just show the Person type. The URL might look like as below
site.com/Person/List?personType=Employee
or
site.com/Person/List?personType=Student

Challenges

  • If we just return the model from Action method, it cannot render as it don't know the View corresponding to the Model.
  • If we put if/switch condition based on the PersonType in the List.cshtml and render the <Table> accordingly the List.cshtml will grow to large size.
  • We can have same if/switch based on PersonType and render partial views which will limit the size of the List.cshtml. But again the partial view need to have logic for parsing the XML and display accordingly.

Solution

The better solution, I could think of is "having an abstraction for parsing the xml to create concrete model classes and give to View for just display" instead of view working on Model to find out what is relevant. So the logic of parsing xml is done in the Action and it returns typed model class object to the corresponding views. When we say abstraction, its just and interface which returns the typed objects and the corresponding view name. The implemented classes will have the logic of parsing the xml to convert to typed objects. Only consideration is about having a base class for view model classes which are returned from the interface methods or properties.

It is not the perfect solution. We can argue on merits of having partial views v/s this provider mechanism. But its worth looking at this provider mechanism as well. Below is the relevant code for this approach.

    public class ViewModelBase
    {
    }
//Typed class for student.It will differ for employee
    public class StudentViewModel:ViewModelBase
    {
        public IEnumerable<StudentModel> Students { getset; }
    }
    public class StudentModel
    {
        [Display(Name = "First Name")]
        public string FirstName { getset; }
        [Display(Name = "School")]
        public string School { getset; }
        [Display(Name = "Marks")]
        public string Marks { getset; }
    }
    public class DefaultViewModel : ViewModelBase
    {
        //Reuse the Person itself instead of DefaultModel
        public IEnumerable<Person> Persons { getset; }
    }
/////////////////Classes related to provider model.
    public interface IViewModelProvider
    {
        ViewModelBase GetViewModel(IList<Person> persons);
        string ViewName { get; }
    }
    internal partial class StudentViewModelProvider : IViewModelProvider
    {
        ViewModelBase IViewModelProvider.GetViewModel(IList<Models.Person> persons)
        {
            StudentViewModel studVMList = new StudentViewModel() { };
            studVMList.Students = persons.Select(p=>new StudentModel(){
                FirstName = p.Properties.Attribute("FirstName").Value,
                School = p.Properties.Attribute("School").Value,
                Marks = p.Properties.Attribute("Marks").Value
            });
            return studVMList;
        }
        string IViewModelProvider.ViewName
        {
            get { return "Student"; }
        }
    }
    public class DefaultViewModelProvider : IViewModelProvider
    {
        ViewModelBase IViewModelProvider.GetViewModel(IList<Models.Person> persons)
        {
            DefaultViewModel personVMList = new DefaultViewModel() { };
            personVMList.Persons = persons;
            return personVMList;
        }
        string IViewModelProvider.ViewName
        {
            get { return "Index"; }
        }
    }
/////////////MVC Specific
    public class PersonController : Controller
    {
        public ActionResult List(string personType)
        {
            IEnumerable<Person> persons = DataRepository.GetPersonsByType(personType);
            IViewModelProvider provider = ViewModelProviderFactory.GetProvider(personType);
            ViewModelBase viewModel = provider.GetViewModel(persons.ToList());
            return View(provider.ViewName, viewModel);
        }
    }

Hope this is clear. Sample can be downloaded from the below location.

Tuesday, April 1, 2014

Getting unique records based on property from list without using Distinct() in Linq

The scenario is very simple. We have a collection of Person objects. We want to know the unique PersonTypes from the collection. Below the Person class definition.

    public class Person
    {
        [Display(Name = "First Name")]
        public string PersonType { getset; }
        public XElement Properties { getset; }
    }
    public class PersonType
    {
        public string TypeName { getset; }
    }

I don't think nobody will write a loop to achieve this. The normal solution we may think of is to get the distinct Person objects using the Distinct function in Linq. But it requires an IEqualityComparer derived object to handle the comparison which is employed to find the duplicates by the Distinct function. I have explained about IEqualityComparer in one of my previous posts related to merging 2 collections without duplicates.

        static internal IEnumerable<PersonType> GetUniquePersonTypes()
        {
            IEnumerable<PersonType> personTypesList = _persons.Distinct(new PersonComparer())
                                                                                                .Select(per=>new PersonType(){TypeName=per.PersonType});
            return personTypesList;
        }
   public  class PersonComparer:IEqualityComparer<Person>
    {
        bool IEqualityComparer<Person>.Equals(Person person1, Person person2)
        {
            return person1.PersonType.Equals(person2.PersonType);
        }
 
        int IEqualityComparer<Person>.GetHashCode(Person person)
        {
            return person.PersonType.GetHashCode();
        }
    }

GroupBy to find distinct objects based on entity property

Should we really need Distinct and another class which implements IEqualityComparer? If we group the list based on the property which is key to decide duplicate and build a collection out of the group keys, won't that be same result of Distinct with comparer? Lets try it.

        static internal IEnumerable<PersonType> GetUniquePersonTypes()
        {
            IEnumerable<PersonType> personTypesList= _persons.GroupBy(x => x.PersonType)
                                                                                            .Select(group => new PersonType() 
                                                                                                                { TypeName = group.Key }
                                                                                            );
            return personTypesList;
        }

Is it working? Happy coding

Wednesday, March 26, 2014

If statement in SQLCMD

SQLCMD is a nice utility to automate the SQL scripts execution.I became the fan of it years ago when I first used it for automating database creation and version upgrade of databases. The past sql automations using sqlcmd.exe were relatively simple. Most of those were using tokens and those tokens will be replaced with actual values when the execution starts. The values for tokens will be supplied by initiating programs and those programs may be of .bat, .vbs or .ps1 files at the most it will be invoked from the installers.

Recently we came to a situation where a branching is required in SQLCMD scripting. Basically we need to change the value of the token at runtime based no a database SELECT query. At that moment I realized that sqlcmd doesn't have branching statements. Team's initial decision to mitigate it was to call the database from the initiator programs.But those will make the .bat files really heavy or may need to write another PowerShell script or exe to do the job and get the value for sqlcmd token.

But a tight google exercise unveiled the below link which explains how to imitate the branching in sqlcmd. Its nothing but creating dynamic .sql file based on query result and executing the same from its creator sql file. I don't think I need to replicate the details again. So just open the link and have fun.


Tuesday, March 18, 2014

.Net Assembly versioning in Plug-in framework

As everybody knows assembly versioning in .net is a good concept. It helps us to deliver upgraded versions very easily without affecting behaviour of old versions ie easier to maintain backward compatibility. It also helps us to sign our assemblies with strong name which means our delivered dlls are unique.

In normal cases, the assembly version contains 4 numbers separated by period "." . Those denote Major version,Minor version, Build number and Revision number respectively. When to change or increment the assembly version is a tough decision especially when it comes to applications which uses reflection extensively. As best practices some people changes the assembly version for the shipping builds only. Anyhow when we change the assembly version we need to make sure that all assemblies including dependent assemblies are loading correctly as those are signed and needs to be in same version.

In my current project we are changing the assembly version once in a year as we are using reflection in its maximum. Recently we had to deal with a versioning issue related to a plug-in architecture, we introduced last year. For simplicity I can explain the plug-in architecture using simple drawing application framework which uses IShape and its implemented classes.

Problem

The aim is to have a plug-in architecture in place where there will be a framework which will be always latest. There will be an interface IShape with a method Draw().The framework creates the object of IShape implemented classes based on configuration using reflection and calls its Draw() method. We released it in 2013 and the structure was as follows
  • Core.dll {Version:1.0.0.0}
    • Contains IShape interface
  • Framework.exe{1.0.0.0}
    • The controller class which creates the object of IShape using reflection and call Draw()
  • Impl10.dll{Verion:1.0.0.0}
    • Contains Circle class which implements IShape.Draw() method to draw circle shape.
In 2014 we are not supposed to release Impl10.dll as there is no change. But the Framework.exe might have feature improvements and it's version needs to be incremented. Similarly the version of Core.dll needs to be incremented as its always expected to be latest.

The problem starts from here. If we deliver the new version of Core and Framework with incremented assembly version 2.0.0.0 for Core.dll and Framework.dll, the Impl10.dll will not get loaded as its in old version.

Dlls present after new release.
  • Core.dll {Version:2.0.0.0}
    • Contains IShape interface
  • Framework.exe{2.0.0.0}
    • The controller class which creates the object of IShape using reflection and call Draw()
  • Impl10.dll{Verion:1.0.0.0}
    • Contains Circle class which implements IShape.Draw() method to draw circle shape
  • Impl20.dll{Version 2.0.0.0}
    • Contains Rectangle class which implements IShape.Draw() method to draw rectangle shape

Possible solutions

  1. Assembly binding redirection 
    Using this technique we are forcing the .net runtime to use latest version of Core.dll even for Impl10.dll which is pointing towards previous version of Core.dll .But needs to maintain list of all old versions.
  2. Manual Assembly resolution
    This technique loads the assembly by writing the assembly loading code in the AppDomain.AssemblyResolve event.
  3. Constant version for Core.dll
    Simple one. Don't change the version of Core.dll even there are changes for new features. So that always the classes are implementing same interface. The assembly version of implementing classes can be incremented.
We opted the 3rd solution as its simple. Attached a sample which explains the plug-in scenario.


Things to remember

When we select any of these options we need to make sure below points
  1. Never remove any method or change the method signature in interface methods. If we do so .net runtime verification will fail and it will throw method not found exceptions if we try to load old assemblies.
  2. If we want to add more methods to the interface, we need to create new interface inheriting from IShape and have new methods there. When creating objects in framework.exe make sure we cast to respective interfaces to invoke operations. Since deployed framework.exe will be always latest, it can be done easily.

Tuesday, March 11, 2014

Not able to start Android application after adding activity through new Wizard in Eclipse

I normally add new Android activities, corresponding classes and config changes manually. Recently I came to visit a link from Kerala start-up village blog where they were explaining how to create new activity in Android. They mentioned in the same way I am doing. Or probably most of the developers are doing. Then I thought of other easy ways using Eclipse IDE or the Android plug-in to generate activity xml, its class and changes in the AndroidManifest and could see Android help page which explains the same. 

Just thought of trying it out by creating an activity into one of my Android project. After creating the activity, I was not able to run the application in simulator. The error got in the Console window is given below .

[time - app name] Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
[time - app name] Please check logcat output for more details.
[time - app name] Launch canceled!

It suggests to check the logcat as well. So opened the logcat window and log reads as follows.

<time>: W/ActivityManager(407): No content provider found for permission revoke: file:///data/local/tmp/<name>.apk
<time>: I/PackageManager(407): Copying native libraries to /data/app-lib/vmdl-1401938519
<time>: W/PackageParser(407): /data/app/vmdl-1401938519.tmp (at Binary XML file line #12): <activity> does not have valid android:name

Here the console logs says that the manifest is malformed. In logcat window we could see 2 issues. One error says permission issue. And second line says activity does not have valid android:name property value. It means the activity which I added via the android wizard might have added incorrect entry to the android manifest xml file.

I started by googling the error message. Most of the SO links says its permission issue and I have to give full permission to C:\Users\<user name>\.android folder. I checked and could see it has full permission. Then I compared my faulty project's androidmanifest.xml file with other project and could see that the Android wizard which used to add activity has added the package name to the android:name. Earlier the name was .activityname. The wizard updated the activity name as packagename.activityname which caused the problem. Since the package name is already mentioned in the manifest root node, activity name cannot have the package name.

A sample has been given below

Wrong xml
<activity
            android:name="JoymonOnline.Malayalam.SwipeHost"
            android:label="@string/title_activity_swipe_host" >
</activity>

Corrected the android:name

<activity
            android:name=".SwipeHost"
            android:label="@string/title_activity_swipe_host" >
</activity>

Happy coding.