How to create a WinForms MDI application? - part 1
This is the first post of several posts which will describe my idea of building a non-complex application based on WinForms MDI model. This application will consist of three layers Data Access Layer, Business Logic Layer and UI Layer which is the end application. I’ll use latest Microsoft technologies to develop it .Net 3.5, LINQ, WinForms etc. The application will serve a car service. It will allow managing cars, signing them fleet spot checks and building a maintenance report. I’ll provide the full application code at the last post of this series.
Note that all in this series of posts is only my opinion and idea of how to do such application and it may be not acceptable for any real application no matter that I was used similar application architecture in a real project which was successful!
(All posts in this series: Post 1, Post 2, Post 3, Post 4, Source Code)
I’ll present application database scheme and Data Access Layer of the application in this post. The database consists of two tables Car and CarMaintenance. CarMaintenance is connected to Car via a foreign key between Car’s RegistrationNumber and CarMaintenance’s RegistrationNumber. RegistrationNumber field is the primary field of the primary table Car. Normally, it is good to have a separated ID for the table record but initially, I’ve started using car’s registration number as a primary key and I’ll keep it for this sample. It is clear that it is accepted in some countries like mine (Bulgaria) where the number is connected to the car and it is very rare to change it but it is not accepted for UK where one can change his/her car’s registration number without any problem. But as I said it is a sample application and I apologize for such small realization problems. The database is created on SQL Server Express edition which comes with Visual Studio 2008.
First of all we need to create the database scheme. It can be done using any DB management tool of your wish. For this sample I’ll use Visual Studio 2008. Below is the database scheme diagram. I set standard table view of each table in order to be able to show all information of table meta data. It can be done going to table context menu/table view/standard. Another nice option of the diagram designer is a copy diagram to clipboard option accessible through diagram context menu. I used it in order to be able to save the diagram as an image and to add the image to the post.
Now we are ready to build up our Data Access Layer. Firstly we need to create a new project in VS 2008. I named my project CarsMaintenance.DataAccess. After that I’ll create a LINQ to SQL classes model. It can be done right clicking on the project/Add/New Item/Data/LINQ to SQL classes. Now I have a dbml file where I can add any of my db tables. It can be done just drag and drop table from Server Explorer tab. Before to do that I need to have a connection. That is why I go to the Server Explorer tab and add a connection to the database I have. Probably, you have the connection if you build up the database your own. When we add both of the tables the designer will generate C# code for the model and it will place it in <model name>.designer.cs. In my case I named my model just Model.dbml and this file is named Model.designer.cs. If you are interested you can go through the file and to observe it. Note that the generated class inherits from System.Data.Linq.DataContext
Represents the main entry point for the LINQ to SQL framework.
The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an “identity cache” that guarantees that entities retrieved more than one time are represented by using the same object instance.
In general, a DataContext instance is designed to last for one “unit of work” however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.
I’ll use generated class in my case named ModelDataContext in the Business Logic Layer to perform operations over database. Below is the model picture
It is all for today. I’ll present application’s Business Logic Layer in the next post of the series.


July 22nd, 2008 at 4:07 pm
[...] is the second post in the series. In the first one I wrote an overview of the sample application. In two words it will be three layer application [...]