Vitaly's WebLog
Software development, startups, marketing

Isolating UI from Middle Tier

October 20, 2008

 

One, if not the main, principle of the good architecture is its openness against 'possible' changes. It can be viewed on different abstraction levels - some classes can be open for some changes in business logic, entire layers may be designed to be open for global changes like switching to another DB engine. As OCP states, entities should be open for extension, but not for modification. This means that your architecture should support its extension in planned ways easily without changing much code (you should write new of course). That is why seasoned developer likes to use interfaces here and there :-) And that is what dependency injection is for...

It’s a good practice to build Data Access layer in a way it is not tied to specific database. So if you use SQL server and then decide to use Oracle, you need to write new Data Access classes specific to Oracle and do not need to rewrite entire system. 

I was surprised, but it turns out that you can isolate user interface logic for web project in the same way. 

There are many JavaScript libraries that allow you to build rich Ajax based UI that lives inside your browser. The only what is needed from middle tier is to return data in some simple format (JSON, XML). So you can plan your architecture for a possible change of middle tier (ASP.NET to JSP?) if you need to. A good example of rich UI JavaScript library is ExtJS. I recommend taking look at it if you did not do that yet.

 

 

 

 

 

 

 

 

 


.NET Framework Programming Model for Add-Ins

March 24, 2008

It seems that there is still lot to learn about .NET Framework 3.5. I've found today (thanks to Daniel Moth) that is one more interesting feature was added in .NET Framework - Programming Model for Add-Ins. Basically, it allows you to build application that can be extended by add-ins. You may say that it is not a great task to build your own model. Yes, it is easy to. To build simple model. But what this new programming model offers is a complete solution. There are the following services that you can leverage:

  • Independent Versioning. You may want your host app and add-ins to version independently.
  • Discovery and Activation services
  • Isolation Levels. You may want load your add-ins in separate application domain. Separate from a host app or in seperate app domain for each add-in. There are several levels of isolation you can choose from. 
  • Lifetime management. Because of the facility above (isolation Levels) you may be not able to rely on Garbage Collection. Fortunately, there are facilities to manage lifetime of objects in this programming model.

I would definitely recommend reading a bit about this on MSDN if you are building Smart Clients sometimes. There are also some good resources on Daniel's blog that will help you to start. By the way, Daniel calls this as Managed Add-ins Framework (Great name, except of usage of 'Framework' term, that is a bit overused already) Interesting, if that is official name of this feature (I did not find mentions of MAF in official docs)?

 


Great resource on design patterns and refactoring

February 4, 2008

I've stumbled upon a great web resource containing description of design patterns, antipatterns and refactoring techniques. It is easy to read, have good illustrations and intuitive navigation. I would advice visiting it.

Design patterns are described in GOF style and there are mainly (if not only) GOF patterns. So in case you haven't this great book on your shelf when you need it, you can go to this website and find info you need. In addition to design patterns there is good description of antipatterns and refactoring how-to’s.

Code examples on this site are in C++ and Java, but it should be easy to understand them if you are proficient in C# only.

I almost forgot to leave the URL :-) Here it is: http://sourcemaking.com/


How to make incremental DB update script in VS2008

January 20, 2008

One of the most tedious tasks in database-driven software development is to make database change scripts. You meet it just as you product version becomes higher than 1. You usually have to maintain create script that creates you DB from scratch and also change script(s) that will upgrade existing databases of previous version(s) to the latest one. And those databases most probably have customer data in them that you cannot damage.

In the latest release of Visual Studio there is new type of database projects that helps maintaining your database scripts significantly. I've already wrote review of new features, but such important topic as generation of change scripts in missing in that post, so I decided to write new post on it.

From the first sight it seems that VS can generate only create scripts, which it places in /sql folder by default. Hopefully, it also can generate change script, although this feature not so easy to discover. It can be found in database compare tool. It generates scripts based on differences between two databases (between schema that you have in DB project and some DB instance). It is not well automated, so you have to copy the script it generates, review it manually, and save in a file yourself.

Some manual changes may be need in script. One good example: if you have used rename wizard to rename column in some table you may expect that data that was in that column will be kept by a script. Unfortunately, this type of change is not handled well by VS 2008. Yes, there is warning in UI, but who reads those warnings :-)

 VSDBP_RenameWarning

There is option in project properties "Perform 'smart' column name matching when you add or rename a column". Here is what MSDN says about this option:

Specifies whether to apply a heuristic when you deploy updates to determine when to rename a column instead of performing a DROP and an ADD operation. The heuristic is based on the properties of the column and the names of the source and target columns.

I hoped that setting this option will fix the problem with column rename, but unfortunately this did not work for me. I tried renaming CategoryName -> NewCategoryName and CategoryName -> Category1. In both cases this did not work. Perhaps they need to provide more info on the heuristic they use...

There is also protection from data loss. You can control it by setting in project properties. Unfortunately, changing this setting did not work for me too.

VSDBP_SchemaUpdate

VSDBP_Properties

Anyway, this is undoubtedly a great feature. And should help maintaining incremental update scripts a lot.


.NET Framework Library Source Code now available

January 17, 2008

Scott Guthrie announced that source code of .NET Framework library is now available. You can drill down to code written by Microsoft when debugging your application in Visual Studio 2008. And you even see developers comments in source code :-)

Today I'm happy to announce that this is now available for everyone to use. Specifically, you can now browse and debug the source code for the following .NET Framework libraries:

  • .NET Base Class Libraries (including System, System.CodeDom, System.Collections, System.ComponentModel, System.Diagnostics, System.Drawing, System.Globalization, System.IO, System.Net, System.Reflection, System.Runtime, System.Security, System.Text, System.Threading, etc).
  • ASP.NET (System.Web, System.Web.Extensions)
  • Windows Forms (System.Windows.Forms)
  • Windows Presentation Foundation (System.Windows)
  • ADO.NET and XML (System.Data and System.Xml)


Here is sample code snippet. This is the beginning of HttpRuntime.ShutdownAppDomain method:


Handling Database easily with Visual Studio 2008

January 12, 2008

Overview

Visual Studio 2008 Team System has new type of database projects that are much different from those we had in Visual Studio 2005. There is a number of extremely useful features that should make you development much easier. Most of this functionality was previously introduced in Visual Studio 2005 Database Edition. It was released much later than Visual Studio 2005 itself, so some developers (including me) accidentally missed its announcement. Now these tools are integrated into Visual Studio 2008 and replace old database projects.

You start from creating new schema or importing schema of existing database into VS database project, which is presented by separate scripts for each database object. Visual Studio creates quite good catalogue structure by default, but if you have large database then you may add your own folders to better organize your project. As there is separate script file for each database object, it becomes much easier to track changes when your Database Project is put under source control.

After you have imported schema of existing DB (or created new one from scratch), you can easily deploy your DB to any instance of SQL Server and work with it while offline using your private SQL server instance. Visual Studio merges all small separate scripts into several large ones, so you can also use those scripts to manually create database.

To navigate among you database objects, besides Solution Explorer, you can use Schema View pane, that displays database objects as SQL Server Management Studio or Server Explorer does. You also can filter objects if you have large database.

Perhaps the most awesome feature of new database project is design-time validation of your scripts. Visual Studio uses your local database to validate scripts as you type them. It is claimed that you must have either Developer or Enterprise edition of SQL Server locally installed, but it seems that most of the features work ok with SQL Express.

To ease the rename of database objects, Visual Studio provides refactoring wizard. It makes it really painless to rename database tables, columns, views and other objects, because it finds all places where old name was used and provides preview screen with changes for you. There are no other refactoring methods than rename now, so it is not quite clear why the only item "Rename" has been put in "Refactoring" menu. Perhaps there will be some more however...

Another new powerful feature is database unit tests. Visual Studio automates creation of unit tests for database object just as it done for C# and VB code. Good compliment for unit tests is new feature that allows you to load test data in your database. It loads randomly generated values in tables you specify. You can set-up it to load data before executing unit tests. It also can be used for performance testing to foresee how your DB will function on large volumes of data.

And finally, there is handy tool for comparing schemas and data between databases. It is buried in UI, however, and not so easy to find. See screenshots below for help.

Creating Database Project

VS2008DBP_Create Here is where you start from.

You can select to create database project using wizard. There is nothing special that can be made only in the wizard, but it allows setup your project quicker, especially if you are not too familiar with this new type of projects yet. Wizard asks you for options of your database and allows you to import schema of existing database.

VS2008DBP_Wizard_Properties VS2008DBP_Wizard_DbOptions VS2008DBP_Wizard_ImportSchema VS2008DBP_Wizard_BuildAndDeploy

Project Structure

Here is what we have after wizard finishes. Each database object creation script is placed in a separate file.

VS2008DBP_ProjectFiles_Overview      VS2008DBP_ProjectFiles_Tables

 

Design-time validation

I've intentionally made mistake in a column name. Visual Studio immediately shows error message in Error List pane. Unfortunately, error is not highlighted in code editor, but when you click on error message you get positioned on the correct place in code (this works not always, however).

I love this feature!

Another good use of validation is to check existing database for mistakes. You can import it's schema into new database project and build it to start validation.

VS2008DBP_DesignTimeValidation

Schema View Pane

You can make some changes to schema here and easily navigate through you database.

VS2008DBP_SchemaView

Refactoring

Yet one very powerful feature. If you had to rename a column in a large DB you would appreciate this new rename helper.

VS2008DBP_Refactor

VS2008DBP_Refactor_Preview 

Comparing databases

No need to use 3rd party utilities anymore. You can compare databases or database projects in Visual Studio now. Note the toolbar button that the red arrow pointing at - it starts compare wizard.

VS2008DBP_CompareSchema

Here is how results of comparison look like.

VS2008DBP_CompareSchema_compared

Loading Database with test data

Loading database with test data is very useful for unit tests and also can be used for performance testing. To load you DB with test data you add new item, Data Generation Plan, to you project.

VS2008DBP_DGP

Then you specify what tables to fill with data, how many records to add and start data load by clicking on toolbar button.

VS2008DBP_DGP_SettingsAndPreview

Unit Tests

To create Unit Tests for your stored procedure you select it in Schema View and select Create Unit Tests in context menu.

VS2008DBP_UT

Wizard asking you for a list of procedures to generate unit tests for and details abut project that will host your unit tests.

VS2008DBP_UT_Create

If you selected to create new project, Visual Studio asks you for it's properties. Note that you can automatically deploy database to your database server and load it with a test data as it is configured in your Data Generation Plan.

VS2008DBP_UT_ProjectConfiguration

And here is how result looks like. You can go on and customize your Unit Tests as you need.

VS2008DBP_UT_Generated 

Conclusion

Undoubtedly, new database projects are a great feature. What is cool is that you can set-up really solid solution for your development environment with Visual Studio 2008 and Team Foundation Server. And new features of database projects add missing parts to that. You now can test code in your database objects by running unit tests in nightly builds. And you can validate that only valid code comes to Source Controls using Continuous Integration. That all was possible with C# and VB code previously, but now you need not worry for your stored procedures too.

Even if you do not want to use database project as part of a solution, you may want to use some of its useful features on your stand-alone database:

  • You can validate stored procedures, functions and other objects in your database.
  • You may want to load you database with test data for performance testing.
  • You also can use compare wizard to see changes between your databases.
  • And, finally, you can use renaming wizard to easily rename you database objects if you need to.

Here you will find official documentation (that is quite good).