15.01.2013 Views

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

Foundations of Programming - Karl Seguin

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 - Object Relational Mappers<br />

Object Relational Mappers<br />

THE OTHER OPTION INVOLVED WRITING WAY TOO MUCH SQL. - CHRIS KOCH<br />

I<br />

n chapter 3 we took our first stab at bridging the data and object world by hand-writing our own<br />

data access layer and mapper. The approach turned out to be rather limited and required quite a bit<br />

<strong>of</strong> repetitive code (although it was useful in demonstrating the basics). Adding more objects and<br />

more functionality would bloat our DAL into an enormously unmaintainable violation <strong>of</strong> DRY (don’t<br />

repeat yourself). In this chapter we’ll look at an actual O/R Mapping framework to do all the heavy lifting<br />

for us. Specifically, we’ll look at the popular open-source NHibernate framework.<br />

The single greatest barrier preventing people from adopting domain driven design is the issue <strong>of</strong><br />

persistence. My own adoption <strong>of</strong> O/R mappers came with great trepidation and doubt. You’ll essentially<br />

be asked to trade in your knowledge <strong>of</strong> a tried and true method for something that seems a little too<br />

magical. A leap <strong>of</strong> faith may be required.<br />

The first thing to come to terms with is that O/R mappers generate your SQL for you. I know, it sounds<br />

like it’s going to be slow, insecure and inflexible, especially since you probably figured that it’ll have to<br />

use inline SQL. But if you can push those fears out <strong>of</strong> your mind for a second, you have to admit that it<br />

could save you a lot <strong>of</strong> time and result in a lot less bugs. Remember, we want to focus on building<br />

behavior, not worry about plumbing (and if it makes you feel any better, a good O/R mapper will provide<br />

simple ways for you to circumvent the automated code generation and execute your own SQL or stored<br />

procedures).<br />

Infamous Inline SQL vs. Stored Procedure Debate<br />

Over the years, there’s been some debate between inline SQL and stored procedures. This debate has<br />

been very poorly worded, because when people hear inline SQL, they think <strong>of</strong> badly written code like:<br />

string sql = @"SELECT UserId FROM Users<br />

WHERE UserName = '" + userName + "'<br />

AND Password = '" + password + "'";<br />

using (SqlCommand command = new SqlCommand(sql))<br />

{<br />

return 0; //todo<br />

}<br />

Of course, phrased this way, inline SQL really does suck. However, if you stop and think about it and<br />

actually compare apples to apples, the truth is that neither is particularly better than the other. Let's<br />

examine some common points <strong>of</strong> contention.<br />

<strong>Foundations</strong> <strong>of</strong> <strong>Programming</strong> Copyright © <strong>Karl</strong> <strong>Seguin</strong> www.codebetter.com<br />

43<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!