Cedar City Group: December 2007

Wednesday, December 19, 2007

Cool quote....

I saw this quote yesterday...it applies to software development and many other areas of life..

Talent hits a target no one else can hit, genius hits a target that no one else can see.


Happy coding....

Monday, December 17, 2007

A long time requirement finally met...

As long as I can remember, I have felt that when you use a text box to perform data entry for a database column, that the maxlength of the text box should match the definition of the column in the database. And that it should automatically change on the text box if the database design changed. I never had the gumption to actually write the code to do that, but I've always felt that it should be done.

I'm sitting here working on a fairly simple data entry form for a web application, I'm populating a text box with the value from a SubSonic field and that old urge to set the max length kicked in. I dug around in the SubSonic object model for a little bit and came up with the following code.


txtFirstName.Text = buyer.FirstName;
txtFirstName.MaxLength = buyer.GetSchema().GetColumn("firstname").MaxLength;



as my buddy Tim says Schweet!

Friday, December 14, 2007

Abstraction...

In my original post to this blog, I mentioned who the audience is. If you are an experienced developer that has it all figured out, then I'm not going to post anything here that you don't already know. However, if you are a beginner to intermediate level programmer, I hope that I can give some advice that will be helpful to you.

I've been posting about Subsonic for a while, but the question is: Why is a tool like subsonic important? The key point here is abstraction. This is a key principle in software development that has been around for years and years. It's important. At the application level, you must learn to think abstractly (I'm setting the date of birth for an employees vs. I'm updating the dob column in the employee database). The mind can only deal with so much detail, thinking abstractly helps you solve bigger problems. Object oriented programming helps in this quest.

Datasets are objects too....
Well, yeah they are, but not the kind of objects that I'm talking about here. Datasets do not provide abstraction. Think about this, when you deal with an application that is dataset oriented, what is the documentation that you refer to most often? Could it be the database schema? Your column names appear at the top level of your code. There is no abstraction. With true business objects, the top level appication has no idea how the objects are stored, the application deals with operations like, Save, Update, Delete. Much more abstract.

The problem with business objects
The downside of writing business object oriented code has always been the overhead. Business object oriented code has always taken a lot of time because of the code needed to 'translate' between the object model and the database model. Datasets seem to be quicker because there doesn't need to be any translation code. In the end, however, dataset oriented applications become brittle because they are tied to the database in an unhealthy way. What about strong typing? With datasets you must constantly typecast your data. The business object model provides strong typing which by definition improves your code.

If the abstraction argument doesn't work...
If the concept of abstraction doesn't appeal to you, then how about this. If you use true business objects, you have intellisense at the application level, even when databinding to controls.

The reason Subsonic is important....
Subsonic provides the needed abstraction while removing the overhead of writing the translation code yourself. The concept of abstraction is what is important, not the specific tool.

I'll get off of the SubSonic Kick.....

At some point but not right now.

Yesterday, I was posting some recent web site changes to a test site. For the last couple of projects that I've worked on, I've had access to SQLCompare which is a darn good product. I have encouraged several companies to buy it, though I don't have a copy of my own. Anyway, I had a bunch of database changes to make, no SQLCompare to synchronize, what do you do?

Subsonic to the rescue. With a few rules to follow, Subsonic provides a cool mechanism for upgrading your database. Subcommander provides a /version option which will create two scripts. One script will create the schema for the configured database, the other will create a script that will recreate the data. Pretty cool stuff. I 'versioned' the new database and the destination database. Run the new schema script on the destination db, then run the destination data script to re-insert the data.

Obviously, there are some rules here: you can't delete columns, or tables (without modifying the data script). But this provides a cool mechanism for developers.

How many times have you need to duplicate a database for development purposes? You want a development, testing and production version of your database. You can use backup/restore to create a new database but this provides a quick mechanism for duplicating that is very easy to understand.

There are many uses for this utility, creating your initial install scripts, version control of schema (and data!) also, though I've not checked this out yet, but a poor man's SQL Compare could be created with the combination of SubSonic and a text comparison tool like csdiff.

Pretty cool stuff, Thanks guys.

Thursday, December 13, 2007

Subsonic Rocks

Ok, I've been talking about Subsonic for a little bit. I'm working on a project now where I'm really starting to put it through its paces. I have to say that the more I work with it, the more impressed I am.

I must admit that I got sucked in by the scaffolding approach. You drop one control on a form and poof you have a data entry application. Well..almost. I was writing a form to do some simple content management. I didn't want the content to appear on the front list. With the scaffold control, it appears that you get everything. There is also a quicktable control which provides more control, but as soon as you need to add any logic to the process (hey I need to hash the password instead of storing it in clear text) at that point you are writing your own form.

On the plus side, writing your own form is much easier with Subsonic than without it. Subsonic plus partial classes in c# very easily solve the dilemma of customizing code that is generated. Typically, I need a method that retrieves an object by something other than the primary key. I typically let subsonic generate code for me into a separate data layer project. The subsonic code goes into a folder called 'generated'. I then create a second folder called 'Custom' at the same level. Any time I need to add a method to a subsonic class, I create a partial class in custom that has the same namespace and class name as the subsonic code, and add the method that I need. My new method appears on the objects as if subsonic created it. If I need to regenerate the subsonic code, my custom code remains in tact. Pretty cool stuff.

Tuesday, December 4, 2007

How things have changed....

I downloaded the Visual Studio 2008 (3.3G) and can't help but think about the first compiler that I used as a professional developer. I was so excited when we got it....Turbo Pascal 4. As far as I know, it was one of the first languages to use the IDE approach. Anyway, it arrived on 2 5.25" floppies one floppy was nothing but samples.

Sunday, December 2, 2007

SubSonic, the best thing since crunchy peanut butter

My first introduction to the MVC way of doing things was via SubSonic. The project that I was working on at the time was suffering from death by n-tier. One of the worst things that could happen while working on the project was to realize that you needed a new piece of data from the database. We were going by the book so here are the steps we had to go through:


  1. Write a stored procedure to access the tables.

  2. Write the code in the dataaccess layer to call the stored procedure (thank goodness we were using the Enterprise Library which simplified that code)

  3. Write a business layer routine to call the data access layer



Death by n-tier. I guess if the business layer added any value, I could understand it, but it didn't.

I was introduced to SubSonic. SubSonic is a data layer generator that implements the ActiveRecord pattern. In a nutshell, it automatically generates the code to present your application with strongly typed data objects and collections based on your database design. It can operate in two modes: mode one automatically updates the code datlayer code for you at compile time using their build provider. This is cool, becuase you can update your database and the changes are automatically refelected in your code the next time you compile.

I prefer mode 2 where Subsonic generates code for you and places it into a separate data layer project. You have to go through some extra steps, but this method just feels better to me. This mode is also recommended if you are going to be deploying to a shared host.

SubSonic also provides a Scaffolding control that can help you prototype your data input logic quickly.

I'm by no means an expert on this stuff. But SubSonic seems like a good tool to have in your tool box.