Posts

Showing posts from July, 2015

Lazy Loading

Image
Lazy loading is a concept where we delay the loading of the object until the point where we need it. Putting in simple words, on demand object loading rather than loading objects unnecessarily. For example, consider the below example where we have a simple Customer class and this Customer class has many Order objects inside it. Have a close look at the constructor of the Customer class. When the Customer object is created it also loads the Order object at that moment. So even if we need or do not need the  Order  object, it’s still loaded. But how about just loading the Customer object initially and then on demand basis load the Order object? Hide   Copy Code public class Customer { private List<Order> _Orders= null ; … … public Customer() { _CustomerName = " Shiv" ; _Orders = LoadOrders(); // Loads the order object even though //not needed } private List<Order> LoadOrders() { List<Order...

How to handle concurrency in LINQ

Image
Two Things a Good Concurrency System Should Support A good concurrency system should at least provide two important features: It should be able to detect if any concurrency violation has taken place. Once concurrency violation has been detected, it should provide various ways of how to handle the concurrency violation. LINQ Supports Optimistic Concurrency by Default LINQ entity forms the main heart of LINQ engine. All the data from database is fetched using datacontext and given to LINQ objects. Once the data is fetched in the LINQ objects, the LINQ objects get disconnected from the database. In other words, LINQ is a disconnected architecture. Due to disconnected architecture, the only type of locking we can support is optimistic locking. Good news!!! LINQ supports optimistic concurrency by default. Let’s See If Really Optimistic Concurrency Works Let’s do a small sample test to see whether LINQ really supports optimistic concurrency default. So we will...

Auto Mapper in MVC

Image
Sometimes while interacting with real time (database) entities and binding our model to them, we end up in a situation like: var dbContext = new MyDBDataContext(); var userDetails = dbContext.Users.FirstOrDefault(userId => userId.UserId == id); var user = new LearningMVC.Models.User(); if (userDetails != null ) { user.UserId = userDetails.UserId; user.FirstName = userDetails.FirstName; user.LastName = userDetails.LastName; user.Address = userDetails.Address; user.PhoneNo = userDetails.PhoneNo; user.EMail = userDetails.EMail; user.Company = userDetails.Company; user.Designation = userDetails.Designation; } return View(user);   The above mentioned code is not very hard to understand. In the above code, an instance var dbContext = new MyDBDataContext(); is created from the LINQ to SQL Context class, thereafter the user details are fetched  from a user specific table and stored in the var userDetails variable. We have an exist...

New Features in MVC 5

Image
MVC 5 comes up with many new features, I hereby summarize some of the basic features Attribute based Routings Filter Overrides  Identity Framework   Routing in MVC As MVC is all about creating loosely coupled application. Routing is one such MVC feature which decouples the URL schema of the application from the rest of the application.  In ASP.NET webforms and in previous web technologies whenever we make a browser request for a resource such as a web page, the web server expects that it exists physically on the server which it returns to the browser , either executing it and returning the HTML (in the case of dynamic pages such as aspx pages) or as such if it’s an html page . In other words there has to be One to One mapping between the URL and the requested resource such as the web form or the html page. Request handling in conventional Web Forms application 's This was the normal scenario...