Friday, May 31, 2013

Using ViewModel pattern in MVC 4 website, .net

It is very helpful to use ViewModel and to implement in an website with MVC 4 is pretty easy. Found this resource very helpful for using ViewModel in MVC4:

Will publish my post on same soon.

Thursday, May 30, 2013

Double question mark operator ??, .NET

The double question mark operator is used to provide default value for nullable type. e.g. done? is a nullable type and to provide a default value to it, we use done??false. It will return false when done is null. MVC 4 website using SQL Server Express instead of SQL Compact Edition

I was working on a sample MVC 4 application and on first run it used SQL Server Express by default for the database. Later I created seed data and set my config file to use SQL Server Compact Edition instead of SQL Server Express. I deleted .mdf from the APP_DATA folder and tried everything I could but application was still using the same old data instead of seed data.
My initializer code:
public class DBInitializer: DropCreateDatabaseIfModelChanges<xxxx>
Here is the small trick I used to fix this issue.

Update the seed data initializer to:
public class DBInitializer: DropCreateDatabaseAlways
and run the application, now your previous data base gets drop and new database using the new configuration will load up. Though its a work around but it fixed the issue. Once you got your new database working you can update the initializer to DropCreateDatabaseIfModelChanges in order to avoid creating database every single time. 

Friday, May 24, 2013

Preventing javascript as a query parameter, ASP

Suppose your web page is using this controller action

public string Browse(string queryParam)
     string message = HttpUtility.HtmlEncode(" = " + queryParam);
     return message;

Here HttpUtility.HtmlEncode utility is used to avoid illegal user input. For example, if someone deliberately uses: /home/user?queryParam=<script>window.location=''</script> then this utility will prevent such access.

Unit of Work and Repository Patterns, Entity Framework

Recently I started working on learning with MVC 4 and couple of other interesting stuff for my current assignment. There has been significant improvement in data access layer in C# since the time I last used it (probably 1 year back).

Here are the links of couple of helpful posts:
a) Unit of Work design pattern:
b) Repository Pattern

Sunday, May 5, 2013

Updating forked repository with parent repository, Github

Suppose you have a team working on a repository name "Base" and a particular team member wants to work on specific module. He forks the Base repository to his account. Later he needs to get latest changes done by developers on Base repository to his repository. Here are the steps to follow:

a) In Github, you name repositories like 'origin', 'remote' etc. Add Base repository as remote to your repo.
git remote add teambranch git://
Here teambranch is the name of remote and you can get the git link info from the base repo web ui.

b) Use git fetch to download objects and ref from another repository
git fetch teambranch
It will fetch all branches of remote into remote-tracking branches.

c) Rewrite your master branch
git rebase teambranch/master
And your repository will be updated.

I am still learning Git and will post more helpful post as come across. Also I read couple of post to achieve this merging task but still don't have complete understanding on working of  various git commands. Will keep on posting as I will learn.