Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.






July 2009 - Posts

ASP.NET MVC 2 Preview 1 is released!

ASP.NET MVC 2 first preview is released to public now and you can download it from Microsoft download site. Here is short overview of technical requirements and main new features of ASP.NET MVC 2 Preview 1.

System requirements

  • Windows 7, Windows Vista, Windows Server 2003 or Windows Server 2008,
  • .Net Framework SP 1,
  • Visual Studio 2008, Visual Studio 2008 SP1, or Visual Web Developers 2008 SP1

New features

  • areas support – allows to group and partition functionality in large ASP.NET MVC applications,
  • DataAnnotation validation support – ASP.NET MVC 2 is able to use same data annotation mechanism as ASP.NET Dynamic Data and .NET RIA Services do,
  • strongly typed UI helpers – let you use strong-typed lambda expressions when referencing the view template’s model object,
  • UI Helper templating support – new methods to bind custom editors and displays to model properties,
  • other cool features – find out by yourself :)


kick it on pimp it Progg it 顶 Shout it
vote it on
Posted: Jul 31 2009, 11:59 AM by DigiMortal | with 13 comment(s)
Filed under: ,
Fighting against GhostDoc abusing

GhostDoc by SubMain is great tool for documenting source code. It is able to generate documentation from methods and properties names. It analyzes names and then offers appropriate description. GhostDoc is also able to use description of interface member that current property or method implements. And it takes only one simple key press.

Well, GhostDoc is great tool and I think it makes developers life easier. But not always. Don’t blame GhostDoc – the real problem is (like always) between chair and keyboard!

How to abuse GhostDoc

GhostDoc is like every good invention – one can use it and abuse it. Like a gun – you can use it to protect yourself or your home or country and you can use it to kill someone just for fun. Here is one real-life scenario.

One of the most painful things one developer can do is documenting code using GhostDoc automatically generated code with out worrying if documentation helps other developers or not. Image the class that is full of auto-generated documentation like this.

/// <summary>

/// Sets the employee phone.

/// </summary>

/// <param name="value">The value.</param>

public void SetEmployeePhone(string value)


    phoneLabel.Text = value;


Looking at method name I get exactly the same information that documentation sais - this method sets employee phone somewhere. But what is the phone here? Is it phone number or phone model or phone type? We know only that there is phone and we know that it has value. But where this value goes? Who knows…

How bad documentation affects development?

For this short method I was able to ask three questions. Let’s suppose we have 20 methods documented like this – it makes 60 questions. This amount of questions refers to serious problem of understandability of documentation. Doesn’t matter what is written in documentation – are there notes like above or some spicy yellow news about stars – it is worthless and useless for other developers.

Developers who actively abuse GhostDoc are real pain in the ass for those developers who respect their co-workers and want to write quality software. Faulty or crappy code documentation is not only useless but also dangerous because it slows down debugging process – if developer notices that there is documentation available then he or she starts reading it. Every such crappy documentation stops the reader and asks for attention.

How to avoid bad documentation?

There is no good way unless one writes brilliant GREP or PowerShell scripts to detect weird documentation blocks by patterns. Besides automatic discovery of crappy documentation you can also use most expensive resource – man power. To fight against literal anarchy in code documentation I am considering the following process:

  1. Establish rules for code documentation.
  2. Make these rules available to all developers.
  3. Look also at code documentation during code reviews.
  4. Allow developers to remove crappy documentation as soon as they find one.
  5. Make compiler generate warnings or errors when documentation is missing.

If you have suggestions then please feel free to drop a comment here.

kick it on pimp it Progg it 顶 Shout it
Modeling people and organizations: Class Party

Classes for people and organizations are often modeled incorrectly in object models. These faults cause heavy implementation problems. In this posting I will analyze some legacy models, introduce class Party as generalization of people and organizations and provide some implementation details. Information here is based on my own experience, like always.

Legacy models

Pretty often we can see something like these in legacy object models. I added associations with address classes too because this way you get better picture of problems I will describe.

Person and Company: The Legacy Person and Company: The Legacy

Both of these models introduce some pretty bad problems when we are going to implement these models. Major problem is that we have two classes – Person and Company – and we want to use them in same contexts. Their separation introduces additional association on the left model and one additional class with association on the right model. Let’s see one scenarios where problems occur.

Handling Person and Company as one

It is pretty common requirement. We may sell products to persons and companies, so they can be both our customers. We need simple dropdown list on order form to let sales person select customer. This is easy thing to do (although the solution looks awful to me):

    'P' + CAST(id AS varchar(8)) AS customer_id,
    last_name + ', ' + first_name AS name


    'C' + CAST(id AS VARCHAR(8)) AS customer_id,


This SQL contains something I don’t like. Take a look at these ID-s. These ID-s must contain something we can use when we save customer data. We have to know which table has this ID so we can ask person or company and then assign it to appropriate property of order.

  1. Get customer ID
  2. Determine customer type
    1. If customer is Person
      1. Extract ID
      2. Ask Person with specified ID
      3. Assign it to Person property of Order
    2. If customer is Company
      1. Extract ID
      2. Ask Company with specified ID
      3. Assign it to Company property of Order
  3. Save Order

Well… we got it done but taste of something awful is in mouth.

Generalizing people and organizations: class Party

People and organizations can be handled as parties of different deals. Ordering, buying, signing a contract – all these activities are made by different parties. We can generalize people and organizations using class named Party. You can read more about it and other related models from The Data Model Resource Book – Universal Data Models. I suggest you to buy these books because they help you a lot when you analyze or model your applications.

Here you can see class diagram where Person and Category are generalized using Party.

Class Party generalizes Person and Company

Class Party helps us a lot. All associations that are common for people and organizations we can define between Party and other classes. This way we can handle people and organizations as they are same.

Implementing Party, Person and Company

You maybe notices that Party has property called DisplayName. DisplayName is important design detail. Besides unique identifier we need also something to show as name when we list parties (we want to select customer from dropdown).


Here is the example of tables in database that support our generalization. Tables shown here are simple and give you the point how to do it.

Database structure for parties, persons and companiesTable party is joined to other tables using one-to-one relationship. Party defines the value of primary key, other tables just use it.

We can also see that display name is field of table party. Why? I have found it very convenient if we can see the name of party when we need to manage data in tables manually. DBA is able to solve different issues faster when he or she has all the information that describes rows in table.

Of course, not using the display name in database level is also allowed. Just make your decision considering your current context and needs.


On the code level we have to do some tricks. We define DisplayName as abstract property in Party. Person and Company both have override for this property. Setter of overridden properties is empty because we don’t want to let users change DisplayName manually.

public abstract class Party


    public virtual int Id { get; set; }

    public abstract string DisplayName { get; internal set; }



public class Person : Party


    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }


    public override string DisplayName




            return LastName + ", " + FirstName;


        internal set { }




public class Company : Party


    public virtual string Name { get; set; }


    public override string DisplayName




            return Name;


        internal set { }



Now we have successfully implemented basic structure of people and organizations in our code. I have used the same solution actively over past couple of years and I am pretty happy with it.

Visual Studio 2010: Database projects

Visual Studio 2008 introduced us database projects. These projects are also present in Visual Studio 2010. I noticed some differences already when building my photo gallery application. Let’s see how these projects work under Visual Studio 2010.

Why database projects?

Database projects are good for more than one reason. Main reason is simple – all database objects are defined in same solution where is code that is using this database. Because database project is project like the others then it is possible to add it to code repository – you get versioning this way.

You can build and deploy database projects so you get SQL-scripts and up to date database on your database server. And… I’ve heard that database projects are also able to incremental deployments.

Structure of database projects

Mygallery2010: Database project In my last posting about my photo gallery system, Packaging my gallery code, I wrote about how to separate files from web project to other projects. One of there projects was database project called MyGallery2010.Database.

You can see my database project structure on right. Currently I have definitions for tables, primary keys and foreign keys. I think it is enough for now. When project gets some more complex requirements on database then I will add new features.

One difference I noticed is that empty database project is created without deep folder structure in Visual Studio 2010. Visual Studio 2008 database projects created also folders for tables, keys and so on. Visual Studio 2010 let’s you decide how do you want to organize the definitions of your database projects.

The internal structure of database projects is very rich. Take a look at properties section – there are settings for SQL command variables, deployments, permissions and other settings. You can also add database references to Visual Studio database projects.

And one thing more – take a look at Post-Deployment and Pre-Deployment folders. You can add there special SQL-scripts that are run before and after database deployment.

Adding database objects to project

After creating new database project it is easy to start adding definitions for database objects. You can right-click on database project and select database object type from menu as show on following screenshot fragment.

Visual Studio 2010: Database menu

You can also use "Add New Item” menu selection to add new database object definition from database objects menu as shown on following screenshot.

Visual Studio 2010: Add new database object

After adding new object definition Visual Studio 2010 creates file for it and adds example definition to it. You can use it to build up your own object definition. Building your database using Visual Studio database projects guarantees pretty well that you pay attention to every object you define. I think it may decrease also bugs in database definitions.

Building and deploying database

Building and deploying database projects is not different from other projects. What is different is output.

MyGallery2010: Build results of database project

You can see that before any deployments database scripts are validated and also object dependencies are analyzed. It means that before deployment the order of changes is already clear and deployment scripts have no errors.

kick it on pimp it Progg it 顶 Shout it
Agile Principles, Patterns, and Practices in C# – book review

Agile Principles, Patterns, and Practices in C# (Robert C. Martin Series) 
Agile Principles, Patterns, and Practices in C# (Robert C. Martin Series)
Agile Principles, Patterns, and Practices in C# by Robert C. Martin and Micah Martin describes how to write software using C#. Book covers also most important design patterns and object-oriented development principles. There are very good, close to reality examples for every topic and that makes this book pretty easy to read and understand.

Everything in this book is organized logically. Knowledge given and explained in one section is well used in real context during all following sections. I think it makes this book interesting as you develop your skills step-by-step without jumping from one context to another.

Authors build couple of simple applications in this book to illustrate programming and testing practices. It is interesting process because they start with pretty simple code and then refactor it continuously, write tests and apply patterns to get well designed applications.

You can find also examples about application packaging, packages analysis and different metrics that give you overview of parts of applications. Book contains all equations and gives you exampler of calculations based on code presented by authors.

I think this book is a good starting material for everybody who wants to start serious software development.

Editorial review from Amazon

With the award-winning book Agile Software Development: Principles, Patterns, and Practices, Robert C. Martin helped bring Agile principles to tens of thousands of Java and C++ programmers. Now .NET programmers have a definitive guide to agile methods with this completely updated volume from Robert C. Martin and Micah Martin, Agile Principles, Patterns, and Practices in C#. This book presents a series of case studies illustrating the fundamentals of Agile development and Agile design, and moves quickly from UML models to real C# code. The introductory chapters lay out the basics of the agile movement, while the later chapters show proven techniques in action. The book includes many source code examples that are also available for download from the authors' Web site. Readers will come away from this book understanding *Agile principles, and the fourteen practices of Extreme Programming *Spiking, splitting, velocity, and planning iterations and releases *Test-driven development, test-first design, and acceptance testing *Refactoring with unit testing *Pair programming *Agile design and design smells *The five types of UML diagrams and how to use them effectively *Object-oriented package design and design patterns *How to put all of it together for a real-world project Whether you are a C# programmer or a Visual Basic or Java programmer learning C#, a software development manager, or a business analyst, Agile Principles, Patterns, and Practices in C# is the first book you should read to understand agile software and how it applies to programming in the .NET Framework.

Table of contents

About the Authors

Section: I Agile Development

Chapter 1. Agile Practices
Chapter 2. Overview of Extreme Programming
Chapter 3. Planning
Chapter 4. Testing
Chapter 5. Refactoring
Chapter 6. A Programming Episode

Section: II Agile Design

Chapter 7. What Is Agile Design?
Chapter 8. The Single-Responsibility Principle (SRP)
Chapter 9. The Open/Closed Principle (OCP)
Chapter 10. The Liskov Substitution Principle (LSP)
Chapter 11. The Dependency-Inversion Principle (DIP)
Chapter 12. The Interface Segregation Principle (ISP)
Chapter 13. Overview of UML for C# Programmers
Chapter 14. Working with Diagrams
Chapter 15. State Diagrams
Chapter 16. Object Diagrams
Chapter 17. Use Cases
Chapter 18. Sequence Diagrams
Chapter 19. Class Diagrams
Chapter 20. Heuristics and Coffee

Section: III The Payroll Case Study

Chapter 21. COMMAND and ACTIVE OBJECT: Versatility and Multitasking
Chapter 22. TEMPLATE METHOD and STRATEGY: Inheritance versus Delegation
Chapter 23. Facade and Mediator
Chapter 24. Singleton and Monostate
Chapter 25. Null Object
Chapter 26. The Payroll Case Study: Iteration 1
Chapter 27. The Payroll Case Study: Implementation

Section: IV Packaging the Payroll System

Chapter 28. Principles of Package and Component Design
Chapter 29. Factory
Chapter 30. The Payroll Case Study: Package Analysis
Chapter 31. Composite
Chapter 32. Observer: Evolving into a Pattern
Chapter 33. Abstract Server, Adapter, and Bridge
Chapter 34. PROXY and GATEWAY: Managing Third-Party APIs
Chapter 35. Visitor
Chapter 36. State
Chapter 37. The Payroll Case Study: The Database
Chapter 38. The Payroll User Interface: MODEL VIEW PRESENTER

Appendix A. A Satire of Two Companies
Appendix B. What Is Software?

kick it on pimp it Progg it 顶 Shout it
Vote it
Blogging Heroes: Interviews with 30 of the World's Top Bloggers

Blogging Heroes: Interviews with 30 of the World's Top Bloggers  
Analysis Patterns: Reusable Object Models
Blogging Heroes: Interviews with 30 of the World's Top Bloggers by Michael A. Banks is book that every blogger should read. It is not another book that is just somehow covering popular topic and makes easy money to author. I think this is serious book, really.

Interviews are very interesting. Selected bloggers are very different people and each one of them came to blogging differently. Not all of these people are commercial bloggers, some of them have very popular blogs with no advertisements.

As bloggers are different, so are their blogging tips and tricks different. You can read about many ways how to find audience to your blog, how to write blog postings and how to offer very good reading and community experience to your readers.

One thing they all agree is: blogging is about passion – without it there is no point to keep a blog. I think this book is must-be for every blogger out there!

Table of contents



1. Dave Taylor: The Intuitive Life Business Blog.
2. Chris Anderson: The Long Tail.
3. Gina Trapani: Lifehacker.
4. Ina Steiner: AuctionBytes.
5. Mary Jo Foley: All about Microsoft.
6. David Rothman: TeleRead.
7. Frank Warren: PostSecret.
8. Mike Masnick: Techdirt.
9. Mark Frauenfelder:
10. Robert Scoble: Scobleizer.
11. Peter Rojas: Engadget.
12. John Neff: Autoblog.
13. Ken Fisher: Ars Technica.
14. Deborah Petersen: Life in the Fast Lane.
15. Joel Comm:
16. Brian Lam: Gizmodo.
17. Kristin Darguzas: ParentDish.
18. Chris Grant: Joystiq.
19. Scott McNulty: The Unofficial Apple Weblog.
20. Philipp Lenssen: Google Blogoscoped.
21. Brad Hill: Weblogs, Inc.
22. Steve Rubel: Micro Persuasion.
23. Rebecca Lieb: ClickZ.
24. Deidre Woollard: Luxist.
25. Gary Lee: An Internet Marketing Web Site.
26. Richard MacManus: Read/WriteWeb.
27. Eric T.: Internet Duct Tape.
28. Victor Agreda, Jr.: DIY Life.
29. Steve Garfield: Steve Garfield’s Video Blog.
30. Grant Robertson: Download Squad.

Further Reading.

ASP.NET MVC Calendar component

I am writing simple ASP.NET MVC system that I plan to publish with source code pretty soon. I needed some simple calendar component that doesn’t require ASP.NET server-side form to work. I found different JavaScript based calendars and only one pretty old calendar that was specially written for ASP.NET MVC. I updated it and here is the source and binary downloads for Visual Studio 2008 and ASP.NET MVC 1.0.

ASP.NET MVC calendar Well, this component is written for ASP.NET MVC Preview and with my modifications it also works for MVC 1.0 too. I was not able to get contact with author so I published files here. I changed no names of namespaces and classes, so it is easy to merge my changes to current code if author wishes it.

Image on the right side shows how the calendar looks like with default settings. Calendar has its own CSS file so it is very easy to change the look of it. Currently the calendar needs some modifications and I’m trying my modifications there as soon as possible.

You can use calendar on your views like this:


var dates = new DateTime[2];

dates[0] = new DateTime(2009, 7, 12);

dates[1] = new DateTime(2009, 7, 29);


<%= Html.Calendar("id",DateTime.Now,dates,"Controller","Action")%>

Seems convenient, at least to me. If I make any significant modifications I will write new posting here that describes new features.


Visual Studio 2008 Project with binaries
Size: 28KB

kick it on pimp it Progg it 顶 Shout it
vote it on
NHibernate Linq 1.0 is here!

Ayende @ Rahien just announces NHibernate.Linq 1.0.

Cite: “NHibernate Linq support is based on the existing, proven in production, Linq provider in NHibernate Contrib. The plans to overhaul that and merge that into NHibernate’s proper for the next release are still active, but the project team feels most strongly that production quality Linq support is something that we ought to provide for our users now.”

Let’s go and try it! :)

OpenForum – new ASP.NET MVC based forum

There is new project in CodePlex called OpenForum. With source code you get also some samples you can run on your machine. OpenForum is pretty new and simple. There are no advanced forum features and currently it is suitable for smaller forums. You can change the look of OpenForum and write your own user repositories. Let’s see how OpenForum looks like.

As a first thing there is nice sample forum. It uses default ASP.NET MVC web application template. There is separate menu choice “Forum” on template. When you click there you can see forum postings.

OpenForum: Forum postsOpenForum: Posts list opens when you click on Forums tab.
Click on image to enlarge.

When you click on posting title then thread view of posting and its replays will be show.

OpenForum: Thread view
OpenForum: Thread view – post and replies.
Click on image to enlarge.

And here is the example of writing new forum post. There is limited WYSIWYG editor and I think it works for most forums where image uploads and more complex post formatting is not needed.

OpenForum: Writing new post
OpenForum: Writing new post.
Click on image to enlarge.

Setting forum up was pretty easy task to do. By default it uses same users database that ASP.NET offers so you have all features for users management available. Visitors can log in or register as users through site user interface.

I think OpenForum, like it is currently, works fine for smaller companies and support forums where only basic forum functionalities are needed. Of course, before putting it up for public use expects you to heavy security testing because current version is still beta.

Packaging my gallery code

In this posting I describe how I prepare my gallery application for automated testing. As a first thing I plan to write some unit tests because some of my classes have now logic to be tested. The other thing is database that is living outside my Visual Studio solution. Let’s make now first steps in my gallery code packaging.

This far I made things work on new technologies introduced by Visual Studio 2010. I tried to use my own code and I made some design modifications. Now I feel like I am back at safe waters and my next steps on model and UI are pretty much predictable. As there is not much code written already it is good time to make some modifications to my solution’s packaging. You can read more about my gallery project from MyGallery2010 page. Also feel free to drop a comment if you agree or disagree with me. :)

Separating code to packages

MyGallery2010: Current package Currently I have all my stuff in same project. It was good when I tried out new features because I was able to do it very fast without separating pieces of code to different libraries. When we look at screenshot on right we can see that there are all things in same project:

  • business classes like GalleryItem, Album and Photo,
  • MyGalleryEntities model and data context class MyGalleryEntities.

What we cannot see here is the database. But it exists and Visual Studio supports also SQL Server database projects.

Also I want to start writing tests for my gallery classes and this is the main motivation to separate current code to different libraries and projects. So, looking and current project structure and considering needs described above I think about following projects in solution:

  1. ASP.NET MVC Web Application – web application with models, views, controllers etc.
  2. MyGallery2010.Core – class library that contains business classes and interfaces.
  3. MyGallery2010.Database – SQL Server 2008 database project that contains definitions for database objects like tables, primary keys and foreign keys.
  4. MyGallery2010.Core.Tests.Unit – test project with unit tests for MyGallery2010.Core.

Is it enough?

MyGallery2010: New packaging structure In this point I stop because this projects structure is enough for now. Of course I can think further and separate more things to libraries but I don’t really want to make things too complex right now as there is no need for that. I know guys who create mega projects structure even for small and simple desktop applications. This far life has shown one thing – overhead caused by heavy packaging eats a lot of time and slows down development. Also it produces no additional value.

So, let’s make no pointless work we don’t need in this point. Let’s get this simple packaging work done and let’s see then what modifications are next most important ones. I am pretty sure that these modifications introduce needs to some more changes in current code and maybe even in packaging structure.

Restructuring projects

When restructuring your project, never make too big steps because otherwise you have big and complex modifications chain to solve. Changes caused by restructuring have usually bigger impact on code that changing some classes. This is why it is good to go with it in earlier stages in project but not before you can see what kind of packages you really need.

You can find out more from these books: Code Complete 2 and Working Effectively with Legacy Code (I have read both of these books and I strongly recommend them to developers).

kick it on pimp it Progg it 顶 Shout it
More Posts Next page »