Interview Questions Headline Animator

Wednesday, January 28, 2009

Are you Eager? Or are you Lazy? Or are you both on a case to case basis...??

Entity Framework, Eager Loading and Lazy Loading, my own 2 cents. For those new to Entity Framework, we are talking about the patterns that you can use to load related objects. The strategy by which you would load entity objects that are related to the one in question. There is a beautiful resource that mentions it all. I do not think there is any elaboration required in that matter. But this is not the meat of the matter.

image How do you design an application that uses Entity Framework as a data access strategy? There are a no. of things to consider. The apparent answer all over the Internet is the Repository Pattern (ref: http://blogs.infosupport.com/blogs/willemm/archive/2008/08/18/Using-ADO.NET-Entity-framework-with-the-repository-pattern.aspx, http://blogs.microsoft.co.il/blogs/kim/archive/2008/11/12/data-access-with-the-entity-framework.aspx, http://www.simonsegal.net/blog/2009/01/13/entity-framework-repository-specifications-and-fetching-strategies/). The purpose of the repository pattern is "A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code." All said and done, we write the repository to abstract the CRUD from the Application Layer. This way, simply put, we don't need to write separate routines to do CRUD behaviour on every entity type. The functions look something like this:

-------------------------------------------------------------------------------------------

/// <summary>
/// Returns the required querable object to the caller
/// </summary>
/// <typeparam name="T">Generic Object</typeparam>
/// <returns>IQueryable<T></returns>
private IQueryable<T> GetTable<T>()
{
     IQueryable<T> result = null
     string type = typeof(T).Name; 
     ObjectQuery query = Context.CreateQuery<T>(type);
     result = (
IQueryable<T>)query;
     return result;
}

This routine is called like this.

////<summary>
////Return all instances of type T.
////</summary>
////<returns></returns>
public virtual IEnumerable<T> All<T>()
{
     System.Data.Linq.
DataLoadOptions dlo = new System.Data.Linq.DataLoadOptions();
     return GetTable<T>();
}

And this in turn is called like this:

public IEnumerable<Employee> GetEmployees()
{
     IEnumerable<Employee> employees = this.dbProvider.All<Employee>();
     return employees;
}

-----------------------------------------------------------------------------------------------------------------------------

Now if you see, we have one function retrieve all types of entity objects. Now let's get to the actual topic in hand. Eager loading and lazy loading. I cannot opt for any of the mentioned patterns, because I do not have the type of the pattern that I am going to return at design time. I have already postponed that by using Generics. Just last night I learnt from Daniel Simmons that Framework 4.0 they are planning to add a property to Context object in order to opt in implicit lazy loading.

While I am still trying to figure out a good way of working around eager loading and lazy loading, as of now (before 4.0 arrives) I am firing separate queries to get all types of related objects. And interestingly, once I fire the queries for the related objects and enumerate them, the principal object at hand gets populated with related objects. So next time you want to reference the related objects, just go ahead and use the beautiful sentence completion features such as this: "txtAddressLine1.Text = employee.EmployeeAddress.First().Address.AddressLine1;"

Thursday, January 15, 2009

StyleCop and Good Code

So you are serious about your code quality huh... well StyleCop is a fantastic tool to ensure that. The best part is, it integrates with Studio 2008. It is simple.

  1. Go to This Location, click on downloads and download the latest version of StyleCop. As of now it is the version 4.3
  2. Install StyleCop. It asks you information about integration. Let it integrate with Studio for the moment.
  3. Fire up Visual Studio
  4. Click on Tools and say Run StyleCop and look at the 1001 warnings that StyleCop gives.

Now when you use StyleCop, most of the warnings are not what you really want to work upon. Like Documentation, for instance, imagewho cares whether there is a blank space line above a comment or not...? So you can go to Solutions Explorer and click on Style Cop Settings. This will bring up the "Microsoft StyleimageCop Project Settings" Dialogue box which will allow you to customize your rules set. Allow you to enable / disable rules as per your own special requirements / whims / fancies / moods... :)

It is a fun little tool and I could have written more about it but then I decided that I won't spoil the fun. You guys go ahead and explore.

Thursday, January 08, 2009

Entity Framework FAQ

Lately everyone in my team and around my bay have been talking about Entity Framework. Well since I have been an early adopter, writing my small apps using Entity Framework and fighting for it on my project, people have been coming to me to ask questions. I have been taking pride in answering them (happy to help), most of the times... until lately... since I have been working on too many things...

I just came across an FAQ which pretty much covers it all (thanks Daniel...!). I thought I will add it to my browser favourites. But then I thought that would just help me and not everyone.

Happy to help... :)

Sunday, January 04, 2009

New year resolutions

  1. Move to domain: Focus more on domain knowledge than on Technicalities. My blog will be filled more with how a technology applies in solving a business problem rather than how to solve a technical problem.
  2. I will track my investments: All these years I have been only investing to save tax. It is mandatory to invest in Insurance linked funds and so I invested. It increased my salary a little bit. I didn't look at it from a returns perspective. After I got married October last year, I was made to realize by my investment savvy wife that saving tax is just not enough. There is potential to make our lives better and that I should. That's another reason why I love my wife so much. She brings out the best in me and makes me want to get better every day
  3. I will cut costs: Yes I have lived a lavish life. Beyond my means, beyond what a sane person should do. This year I will not put money into things that I do not have to. Every extra penny that I have after spending the constant costs, I will save.
  4. I will Make a savings Plan: Every month after I get my salary, approximately 60% of my salary goes directly into Infrastructure costs (big word...!!) Rent, Maid Salaries, car EMI, House hold expenses for mom and dad and household expenses for us are infrastructure costs. The rest of the money is slowly spent over the month on small things such as eating out and such. Since I am not keeping track of where the money is going, I end up spending more. Up-front investment is required. A savings plan is required which gets implemented as soon as I get the salary.
  5. Insurance Policies: Re-activate the LIC policy that became dormant last year. Save for the new annual tax benefit investments on a monthly basis rather than on an annual basis. It is always a fight around December when I have to make the savings towards the end of the financial year.
  6. Gym: Last entire year I was extremely irregular in gym. I need to get back into discipline
  7. Guitar: At least 5 hours of cumulative practice over a week should be good.