Posts Tagged ‘.NET Framework’

Is it possible to disable forms authentication for specific sub directories of an application?

Yes we can disable forms authentications for specific sub directory using below code in config files

<location path="Folder" allowOverride=”false”>
                <allow users="*" />
 Hope this help !

Deep and Shallow Copying, Difference of Shallow and Deep Copying

December 16, 2014 Leave a comment

What is Shallow Copy?

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object. If any of the fields of the object are references to other objects, just the reference addresses are copied i.e., only the memory address is copied.

What is Deep Copy?

A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.

When creating copies of arrays or objects one can make a deep copy or a shallow copy. This explanation uses arrays.

Recall array variables in Java are references or pointers. A shallow copy can be made by simply copying the reference.

public class Ex{
private int[] data;

public Ex(int[] values){
data = values;

public void showData(){
System.out.println( Arrays.toString(data) );

The above code shows shallow copying. data simply refers to the same array as vals.


This can lead to unpleasant side effects if the elements of values are changed via some other reference.

public class UsesEx{
public static void main(String[] args){
int[] vals = {-5, 12, 0};
Ex e = new Ex(vals);
e.showData(); // prints out [-5, 12, 0]
vals[0] = 13;
e.showData(); // prints out [13, 12, 0]
// Very confusiin, because I didn’t intentionally change anything about the
// object e refers to.

A deep copy means actually creating a new array and copying over the values.

public class Ex{
private int[] data;

public Ex(int[] values){
data = new int[values.length];
for(int i = 0; i < data.length; i++)
data[i] = values[i];

public void showData(){
System.out.println( Arrays.toString(data) );

The above code shows deep copying.


Changes to the array values refers to will not result in changes to the array data refers to.

Tips to improve Entity Framework Performance

March 12, 2013 14 comments

LINQ to Entity is a great ORM for querying and managing database. It offers a lot of things, so it is mandatory to know about performance of it. These are right up to a certain point as LINQ comes with its own penalties. There are some tips and tricks that we should keep in mind while designing and query database using entity framework ORM. Here is a list of some tips that I would like to share with you.

  1. Avoid to put all the DB Objects into One Single Entity Model

    Entity Model specifies a single unit of work, not all our database. If we have many database objects that are not connected to one another or these(log tables, objects used by batch processes etc.) are not used at all. Hence these objects are consuming space in the memory and cause performance degrades. So try to make separate entity models of related database objects.

  2. Disable change tracking for entity if not needed

    Whenever you retrieve the data only for reading purpose, not for modification then there is no need of object tracking. So disable object tracking by using MergeOption as below:

    NorthwindDataContext context = new NorthwindDataContext();

    context.tblCities.MergeOption = MergeOption.NoTracking;

    This option allows us to turn off the object cache and unnecessary identity management of the objects.

  3. Use Pre-Generating Views to reduce response time for first request

    When the object of ObjectContext is created first time in the application, the entity framework creates a set of classes that is required to access the database. This set of classes is called view and if your data model is large then creating the view may delay the web application response to the first request for a page. We can reduce this response time by creating view at compile time by using T4 template or EdmGen.exe command-line tool.

  4. Avoid fetching all the fields if not required

    Avoid fetching not required fields from the database. Suppose I have table of Customer with 20 fields and I am interested only in three fields – CustomerID, Name, Address then fetch only these three fields instead of fetching all the fields of the Customer table.

    //Bad Practice

    var customer =

    (from cust in dataContext.Customers

    select cust).ToList();

    //Good Practice

    var customerLite =

    (from cust in dataContext.Customers

    select new {

    customer. CustomerID,



    }). ToList ();

  5. Choose appropriate Collection for data manipulation

    In linq we have Var, IEnumerable, IQueryable, IList type collection for data manipulation. Each collection has its importance and performance impact on the query, so beware of using all these collection for data manipulation.

  6. Use Compiled Query wherever needed

    Make a query to compiled query if it is frequently used to fetch records from the database. This query is slow in first time but after that it boost the performance significantly. We use Compile method of CompiledQuery class for making compiled query.

    Suppose you required to retrieve customers details again and again based on city then make this query to compiled query like as

    // create the entity object

    NorthwindEntities mobjentity = new NorthwindEntities();

    //Simple Query

    IQueryable lstCus = from customer in mobjentity.tblCustomers

    where customer.City == “Delhi”

    select customer;

    //Compiled Query

    Func> compiledQuery

    = CompiledQuery.Compile>(

    (ctx, city) =>from customer in ctx.Customers

    where customer.City == city

    select customer);

    In above query we are passing the string parameter city for filtering the records.

  7. Retrieve only required number of records

    When we are binding data to grid or doing paging, retrieve only required no of records to improve performance. This can achieved by using Take,While and Skip methods.

    // create the entity object

    NorthwindEntities mobjentity = new NorthwindEntities();

    int pageSize=10,startingPageIndex=2;

    List lstCus = mobjentity.tblCustomers.Take(pageSize)

    .Skip(startingPageIndex * pageSize)


  8. Avoid using Contains

    In LINQ, we use contains method for checking existence. It is converted to “WHERE IN” in SQL which cause performance degrades.

  9. Avoid using Views

    Views degrade the LINQ query performance costly. These are slow in performance and impact the performance greatly. So avoid using views in LINQ to Entities.

  10. Debug and Optimize LINQ Query

    If you want to debug and optimize your query then LINQ Pad is a great tool for this purpose. I am a big fan of LINQ Pad. It is very useful for query construction, debugging and optimization.

    IQueryable lstCus = from customer in mobjentity.tblCustomers

    where customer.City == “Delhi”

    select customer;


    Dump method of LINQ Pad give the result of above query in the result window.

Understanding MVC (Model View Controller) architecture

November 28, 2012 4 comments

This article is intended to provide basic concept and fundamentals of MVC (Model View Controller) architecture workflow for beginners.


“M” “V” “C” stands for “MODEL” “VIEW” “CONTROLLER” , MVC is an architecture to develop web applications in a different manner than the traditional web development , web applications developed with MVC is even more SEO (Search Engine Friendly ) friendly.

Developing MVC application requires Microsoft .net framework 3.5 or higher.

MVC interaction with browser:

Like a normal web server interaction, MVC application also accept request and respond web browser same way.

Inside MVC architecture:

Whole MVC architecture is based on Microsoft .net framework 3.5 and in addition uses LINQ to SQL Server.

What is a Model?

  1. MVC model is basically a C# or class
  2. A model is accessible by both controller and view
  3. A model can be used to pass data from Controller to view.
  4. A view can use model to display data in page.

What is a View?

  1. View is an ASPX page without having a code behind file
  2. All page specific HTML generation and formatting can be done inside view
  3. One can use Inline code (server tags ) to develop dynamic pages
  4. A request to view (ASPX page) can be made only from a controller’s action method

What is a Controller?

  1. Controller is basically a C# or class which inherits system.mvc.controller
  2. Controller is a heart of whole MVC architecture
  3. Inside Controller’s class action methods can be implemented which is responsible for responding to browser OR calling view’s.
  4. Controller can access and use model class to pass data to view’s
  5. Controller uses ViewData to pass any data to view

MVC file structure & file naming standards

MVC uses a standard directory structure and file naming standards which is very important part of MVC application development.

Inside the ROOT directory of the application there must be 3 directories each for model, view and Controller.

Apart from 3 directories there must have a Global.asax file in root folder. And a web.config like a traditional application.

  • Root [directory]
    •  Controller [directory]
      • Controller CS files
    •  Models [directory]
      • Model CS files
    •  Views [directory]
      • View CS files
    • Global.asax
    • Web.config MVC Execution life cycle

Here is how MVC architecture executes the requests to browser and objects interactions with each other.

A step by step process is explained below: [Refer figure as given below]

Step 1: Browser request

Browser request happens with a specific URL. Let’s assume that user entering URL like: []/home/index/

Step 2: Job of Global.asax – MVC routing

The specified URL will first get parsed via application_start() method inside Global.asax file. From the requested URL it will parse the Controller, Action and ID.

So for []/home/index/:

Controller = home

Action = index()

ID = empty — we have not specified ID in []/home/index/, so it will consider as empty string

Step 3: Controller and Action methods

MVC now find the home controller class in controller directory. A controller class contains different action methods,

There can be more than one action method, but MVC will only invokes the action method which is been parsed from the URL, its index() in our case.

So something like: homeController.index() will happen inside MVC controller class.

Invoking action method can return plain text string OR rendered HTML by using view.

Step 4: Call to View (ASPX page)

Invoking view will return view() . a call to view will access the particular ASPX page inside the view directory and generate the rendered HTML from the ASPX and will respond back to the browser.

In our case controller was home and action was index(). So calling view() will return a rendered HTML from the ASPX page located at /views/home/index.aspx.

This is it, the whole process ends here. So this is how MVC architecture works.