Be a Git Ninja deck

It was a lot fun presenting at the Boston code camp 21.  The slide deck with all the links is below:

It was a lot fun presenting at the Boston code camp 21.  The slide deck with all the links is below:…

Read More

Micro-ORM Code Camp 6 Presentation Links

Presentation from Code Camp 6 (run index.html from the presentation folder)

MicroStackOverflow demo project from the presentation

StackExchange public data dump

StackExchange data explorer

Forked Data importer for StackExchange 

Why Micro-ORM?

My prologue post about the talk

That annoying insert problem by Sam Saffron

Select N+1 problem (stack overflow)

Dapper Links:

Dapper Site

Dapper Extensions

Dapper Simple Crud

PetaPoco Links:

PetaPoco link

PetaPoco  on github

Simple.Data Links:

Simple.Data on github

Introducing simple data post

Simple Data documentation

Simple.Data.sample

Simple.Data.Pad (similar to LINQ pad)

Mark Rendle on Herding code pod cast

Miscellaneous  links:

Hanselminutes from with Rob Conery and Sam Saffron talking about Micro-ORM

Similar presentation

Jon Skeet on Dynamic Gotchas

Presentation from Code Camp 6 (run index.html from the presentation folder) MicroStackOverflow demo project from the presentation StackExchange public data dump StackExchange data explorer Forked Data importer for StackExchange  Why Micro-ORM? My prologue post about the talk That annoying insert problem by Sam Saffron Select N+1 problem…

Read More

Micro-ORM, Why should you care?

Before we look at anything, we must try to understand what an ORM is. It stands for object relational mapper. It tries to solve a very common problem of impedance mismatch between the set based SQL world and the loops based programming world. (This impedance mismatch has given birth to NoSQL databases (MongoDb, Couchbase, RavenDB,etc). ORM takes sets of results returned by running the SQL and maps it to POCOs (plain old CLR objects). It also tends to have some SQL like capacities that can used to send the modified POCOs to the database to manipulate data. The typical ORMs used in the .Net world include NHibernate, EF (EntityFramework), and a whole bunch of micro ORMs such as Dapper, Massive, PetaPoco ,Simple.Data, OrmLite from ServiceStack ,etc.

These tools gained ground very quickly.  Unfortunately, that spread the myth of “You don’t have to know SQL if you use ORM”. Can one say, “I am going to write something in my favorite web framework and I’m not going to care about HTTP and HTML.”?   Abstraction makes things easier to deal with but does not replace the need for understanding the underlying layer. While accessing the data, if we know our SQL well enough, we can work with these ORMs in a better way or at least not abuse them.  Otherwise, we are at a high risk of spiraling down the hell hole of Select N +1 problems along with others.

Why use Micro-ORMs again?  What’s wrong with EF?

Sam Saffron says:

“It is that the one-size-fits-all approach does not offer a snug fit. When ORMs compete, they often use a big table to describe every feature they have that the competition lacks. Unfortunately, this is a big driver for bloat and complexity, it leaves us developers confused.”

(You can read  the entire post here )

This is coming from a guy I have great respect for. He is one of the creators  of MiniProfiler and Dapper (and some other stuff), one of my favorite Micro-ORMs. He also has done Speed Asp.net MVC edition series on Tekpub.com. I could not agree more with Sam here. It does seem like EF is trying to please everyone by jamming in a ton of unnecessary features .

The maintenance of EDMX(glorified XML) is usually a pain. Code first in EF is there, but there is no stored procedure support. This big “enterprisy” framework tries to do too many things.

It usually does a very good job working through the expression trees to generate SQL to fetch the correct data, but the generated SQL can be hard to understand, let alone controlling it. You will have to use profilers like EFProfiler, MiniProfiler or SQL profiler to keep an eye on the queries being generated. The query initialization in EF tends to be slower the first time. It is also hard to debug.

Why not let the database do some of the work of query optimizations? SQL server’s (if you are using SQL server) query optimizer is exceptional. Microsoft has put a lot of research into it.  Stored procedures can give that extra performance boost by precompiling the SQL.

Performance is a critical factor when it comes to data access. We don’t want to have nasty queries consuming too many resources and taking too long to execute. Especially if you are running it on services like Windows Azure which has a “pay as you go” model. (You pay only for the resources you used).  Good performance can directly result in saving money.

If you have a Select N+1 problem, the app could be hitting the database a thousand times to get one result, which can be converted to one simple query returning all the results. It is very easy to create this type of problem with EF.  Sometimes, the developers are not even aware of it.

In addition to all this, EF can create leaky abstractions.  In your typical Asp.net MVC application, you can make it hit the database from Razor View, Controller and business logic or service layer. Everything can get tightly coupled and may end up with duplicated logic.  It is hard to stop this kind of stuff. 

Micro-ORMs, on the other hand , work on the principle of less is more. They generally have a small set of features that work very well. So where is it all going? Are we going to be stuck in SQL land all the time? On Hanselminutes, Rob Conery, nailed the answer to perfection by saying "the complexity (of big ORMs) is resolving to something simple (micro ORMS)."

My take on it is that it is good to have options, have a good  understanding of them and use the right tool for the job.

This is a prologue to my talk on Micro ORMs at Connecticut dot net user group’s Code Camp. I have put together a project on github to demonstrate Dapper, Massive, Simple.Data and PetaPoco.

Before we look at anything, we must try to understand what an ORM is. It stands for object relational mapper. It tries to solve a very common problem of impedance mismatch between the set based SQL world and the loops based programming world. (This impedance mismatch has given birth to…

Read More

Auditing with Entity Framework and repository pattern

There is always a need to keep track of changes in the production database. We may want to create a log or maintain a history of these changes. There are multiple ways of doing this. One way is to have triggers on a table that creates these records along with who made the change and when was it made.

I was going to implement this with Entity Framework. I came across this good post about it. It starts with DDL to create an audit table, then the entity, and how to make use of the change tracker. The DbEntityEntry in the change tracker gives us the old and the new value of an entity. It shows how to do the logging by overriding the SaveChanges method on the DbContext.

If you are using the repository pattern, it won’t work as it is. When you add or attach an entity, the changes get detected. As a result, in the GetAuditRecordsForChange method, the original state and the current state of an entity will be the same and it will not create any logs. The details of how detect changes in EF works can be found here.

In order to resolve this, we need to delay the changes to be detected until the save method is called. The way to do this is by turning off the AutoDetectChangesEnabled to false before a method that triggers the detect changes. Here’s how it looks like :

 public virtual void Add(T entity)
 {
    DataContext.Configuration.AutoDetectChangesEnabled = false;
    DbSet.Add(entity);
 }

Now everything works fine and logs are properly created.

There is always a need to keep track of changes in the production database. We may want to create a log or maintain a history of these changes. There are multiple ways of doing this. One way is to have triggers on a table that creates these records along with…

Read More

Installing Windows 8 on a MacBook

After dealing with continuous hardware and drivers issues on Lenovo IdeaPad y560 ( for more than 2 years!), I decided to sell it off and get a MacBook pro. After all, Apple just makes good hardware and provides very good support through the Apple store. Of course, I’d need and want to put Windows 8 pro on it. There are plenty of “How to” blog posts about this. If you just Google “installing windows 8 on mac” with Bing, you will find quite a few. Based on my experience and research, I decided to put together my own post with some tips.

Caution: Apple and Microsoft don’t provide official support for this yet.

The recipe has the following ingredients besides, of course, a MacBook

Windows 8 pro
A USB drive (min 20 gigs)
A windows 7 pc for some of the prep work
An ISO Maker like Magic ISO
Windows 7 USB DVD download tool ( this helps in creating a bootable USB)
2 GB free space DropBox or SkyDrive ( not required but handy)

On Windows 7 PC :

I bought a DVD from Amazon and created a .iso file. MSDN subscribers can directly download a .iso file of Windows 8 ( or any supported OS).That’s how it used to be when I had MSDN. I am not sure if it is still true.

You will need to format the USB drive using FAT32 file system formatter. You will find out why in the next section.

On Mac :

You can open boot camp assistant and select the second option, like below, to download Windows support software on the USB drive.

This is the slowest part of the process. This support software requires FAT32 USB drive and is about 1 Gig in size. If the drive is not FAT32 formatted, it just errors out saying it cannot copy the support software after waiting for the whole download to complete. Apple can improve things a bit here. Users need a precise error message as soon as possible in the process, not after waiting for half an hour.

On Windows 7 PC again:

The USB drive is required for the Windows 8 installation. You can either copy the support software to the DropBox folder or the SkyDrive or another USB drive. After this, open the Windows 7 Download tool ,select the .iso file of Windows 8 installation, and make the bootable USB.

On Mac :

You can then go back to boot camp assistant and select Install Windows 7 option as below:

After hitting continue, it will take you to the screen below where you can select the partition size.

You can then reboot the machine with bootable USB drive attached.

It should get into Windows 8 setup. If not, then reboot again and hold

the option
button down.

(source: Apple.com)

Once you get into Windows 8 setup, select the partition you created and format it to NTFS. After installing Windows 8, the setup will reboot the machine and you will directly get into Windows 8. (Make sure to remove the USB drive otherwise you will get into Windows 8 setup again) .

What? Where is Mac? Did I lose it? I messed it up!! Panic!

Don’t worry.. Smile

Here’s the trick. Shutdown the computer, HOLD the wonderful option button down and press the power button, then you should see something like below.

The reason you see this is because Windows 8 bios startup does not recognize Mac. It takes over and directly brings you to Windows 8. After the trick above, Mac bios startup takes over and recognizes that there is an OS installed with Boot Camp. It then shows the option like the image above. After this, every time you reboot and hold the option button down, you will see the OS options properly.

Once you get into Windows 8, very few things will be working properly. No wireless internet yet. No worries, you haven’t installed any drivers yet. If you have a Cat 5 cable lying around, you can connect to the internet with it. Install the DropBox / SkyDrive utility and download the Windows support software. If you have it copied on a separate USB drive, copy it over. You can go get it from from Mac(takes a long time to download).

After installing the drivers, everything works like a charm! Smile I love the multi touch mouse pad experience! It works really well with Windows 8. The only thing I found that was a bit annoying was that the display was not the brightest even if it was set to brightest. It would become bright for a second and then become dim again. This is because of “Adaptive Brightness” feature of Windows. It is easy to fix. This post from HowToGeek.com describes how to do it by changing the power options.

Here are some of the reviews I found interesting for running Windows 8 on MacBook:

http://www.anandtech.com/show/6008/windows-8-on-the-retina-display-macbook-pro

http://gizmodo.com/5889404/windows-8-on-a-macbook-air-beautiful-and-kinda-broken

http://www.pcmag.com/article2/0,2817,2393005,00.asp

Apple provides good support for Boot camp. You can find useful information here.

Thanks to my friend Prathamesh Kale for boot camp screen shots. I needed a mac without windows installed because options in boot camp change once it is installed.

After dealing with continuous hardware and drivers issues on Lenovo IdeaPad y560 ( for more than 2 years!), I decided to sell it off and get a MacBook pro. After all, Apple just makes good hardware and provides very good support through the Apple store. Of course, I’d need and…

Read More