Archive

Archive for the ‘MVC’ Category

Using INT or GUID for Primary Keys? Or COMB GUID?

January 21, 2010 3 comments

Today I read an interesting article by Jimmy Nilsson (the author of .Net Enterprise Design) about using auto-generated primary keys in SQL Server. Especially for Surrogate Keys.

Of course, most of our decisions would be to use INT (or BIGINT) as data type and set IDENTITY to true. There is another solution: using GUID. [NEWID in SQL Server].

When a comparison is done between using INT and GUID as primary key, we can list out few important differences:

Pros of using GUID

  1. GUID will be unique through out the database; while INT is not
  2. @@IDENTITY can be a problem while doing INSERT through TRIGGERS. And using @@IDENTITY to get the identity for the recently added row in the table brings the created identity for the current connection.[This could be solved using SCOPE_IDENTITY (see here for an excellent explanation on this!)]
  3. GUID can be used to create primary keys before inserting the value to the database
  4. Integers are not big enough for most of the scenarios; even though you could start from smallest negative value. Anyway BIGINT can be a solution here.
  5. Using INT could be a real night mare when doing manual merge of tables.

Cons of using GUID

  1. 1. In theory GUID presents a risk of getting duplicate GUIDs; but in practice it is not. In the recent windows version (from windows 2000) the algorithm to generate GUIDs doesn’t use the MAC address of the network cars. Instead it is just a random value.
  2. GUID is 4 times larger in size.
  3. There are portability problems, not all the database have GUID as a data type.
  4. Huge overhead; 4 times larger than INT.
    Evolution of COMB GUID

So apparently overhead is the main problem here. When Jimmy Nilsson did real  inserts using GUID as primary key he found out that time taken for inserts with GUID is 30 times greater than inserts with INT. That happened for 2 reasons

1. The generated GUIDs lacked the order; they were random. As you see in the below table only the 4 is common.

C87FC84A-EE47-47EE-842C-29E969AC5131
2A734AE4-E0EF-4D77-9F84-51A8365AC5A0
70E2E8DE-500E-4630-B3CB-166131D35C21
15ED815C-921C-4011-8667-7158982951EA
56B3BF2D-BDB0-4AFE-A26B-C8F59C4B5103

2. SQL insert statement makes the indexes to be reordered and this takes a lot of time. The new ordering for indexes depended on the last bytes

Therefore Jimmy Nilsson arrived at a new algorithm and his new GUID is called as COMB GUIDs.

Jeffery Palermo in his book ASP.NET MVC in Action uses this COMB GUID for his sample code to explain NHibernate. I am using this COMB GUID for my pet project.

Other Resources:

  1. GUIDs as PRIMARY KEYs and/or the clustering key
  2. NHibernate POID Generators revealed : COMB GUID does not cause database fragmentation
  3. INT vs Unique-Identifier for ID field in database
  4. GUIDs are one of the leading causes of index fragmentation
  5. Top tips for effective database managements

MVC and Continous Learning

June 10, 2009 Leave a comment

Finally, yesterday I did some work on ASP.NET MVC 1.0 and managed to create (and run) a sample application. (I follow Scott Gu’s blog and I was eyeing at this MVC thing for a long time. But my work pressure kept me off until now). I am in a very early stage of learning ASP.NET MVC. I was following a tutorial – I have mentioned this in my previous post – from ASP.NET site and as all the tutorials are, this is incomplete. People with minimal programming experience will find this tutorial tough and not working as expected. They might think MVC is tough and complex. In reality it is.

I remember my days when I worked with Spring and Hibernate for a very small but very well structured company called iMocha. I worked there only for 2 months, but I loved the work culture and the way they do things.

Since I was working with ASP.NET web forms for about 4 years, I can’t really grasp what is the need for such a complex framework like MVC. (If I had jumped directly from Spring and Hibernate to ASP.NET MVC framework, probably I wouldnt had this doubt). Certainly this is not suitable for rapid development and small projects. I wanted to know, the real benefits of using ASP.Net MVC Framework. I got this link. (I liked Plip’s creativity: the image that shows ASP.NET with in jail bars!) Don’t just read the post; the comments are more interesting than the post.

While trying to run this application, the thought of “continous learning “ (Martin Fowler has a definition?) suddenly struck me. As a core application developer I will have to learn and embrace new technologies as they evolve. This is a long process and will continue until I quit the software field. I saw this question in Stackoverflow.

The passionate guys never quit.

Categories: MVC Tags: , ,

ASP.NET MVC 1.0 – Model Item error

June 8, 2009 3 comments

Just to get the hang of it, today I started to learn what really ASP.NET MVC is. I have Visual Studio 2008 and Framework 3.5 SP1. I installed ASP.NET MVC 1.0 from the asp.net site. I learnt that Entity Framework comes with SP1. I also wanted to explore the entity framework.
I tried this tutorial.

Also I didn’t get this _db.MovieSet.ToList().Somehow my intellisense didn’t pick up this MovieSet. I was unable to compile. After that I changed the code to _db.MovieMaster.ToList() where _db represents my entity and MovieMaster is my table. The application compiled well But when I run the application and browsed to my URL, aha, I got the following error.

The model item passed in to the dictionary is of type ‘System.Collections.Generic.List’1…but this dictionary requires a model item of type..

MVC-Error1

I searched through Google and I found a guy who has exactly asked the same question. But he didn’t get the answer for that. I even tried the free book http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx which I found in ScottGu’s blog. Really that book appeared complex to me.

After going through the searches and getting little frustrated, that thought struck me. The error appears to be like there are some handshake problem with Controller and View. Controller gives a collection object while view expects a Model item.

At first, I created the view from the Controller class when I was in the middle of the code.

public ActionResult Index()
{

return View();
}

I right clicked Index in controller and generated the View. As you see, View is just empty. The View Content dropdown in Add View pop-up was disabled. So when I created the View, I didn’t have the HTML to show the list or anything. The View was empty.

I changed my code to this:

public ActionResult Index()
{

return View(_db.MovieMaster.ToList());
}

I deleted the existing View for Index and recreated the view.

MVC-AddView

Now my View content drop down is enabled and I selected List from the dropdown. My newly created View had the entire HTML to show the List and everything worked fine!

Categories: ASP.NET, MVC