Working with Partial Types

Applies to:
Microsoft .NET Framework 2.0
Microsoft Visual Studio 2005
Microsoft Visual C# 2.0
Microsoft Visual Basic 2005

Summary: This article talks about Partial types which is one of the new feature of Microsoft .NET Framework 2.0, this feature is available with both the languages C# 2.0 and Visual Basic 2005.

Contents
Abstract
Introduction to Partial Types
Benefits of Partial Types
How Partial Types work
Traditional approach
Partial type - Code example

Abstract

With the launch of .NET 1.0/1.1 and Visual Studio .NET 2002/2003 Microsoft introduces a concept of "Windows Form Designer generated code" for Windows forms, which was encapsulated within the region named "Windows Form Designer generated code".



"Windows Form Designer generated code" contains the core information of the objects which developer drag and drop on the surface of the form, for example, a button, text box, label control etc.

Very often, developers accidentally modify the code within this region which causes the windows forms to appear incorrectly. And since this code is auto-generated it is tedious to write or modify this code. This is why it is a recommendation not to expand the "Windows Form Designer generated code" region and also don’t do any sort of modification in that. At least if you want to see your forms appearing properly.

Sometimes developers wish if they could have clean code editor window. With the release of .NET 2.0 and Visual Studio 2005 Microsoft changed this behavior and in Visual Studio 2005, the designer-generated code is no longer visible in the Code editor window.



This does not mean that it is removed from the .NET 2.0 platform and VS .NET 2005; instead, it is hidden within Solution Explorer. Which can be seen by expanding the Form1.cs or Form1.vb, and then you will see the designer generated code Form1.Designer.cs or Form1.Designer.vb embedded underneath that form. Each form will have its own designer generated code which can be seen by expanding that form.

Microsoft achieved and implemented this by using Partial types which is a brand new feature of .NET 2.0 Platform and Visual Studio 2005.

Introduction to Partial Types

Microsoft offers this feature to separate the code generated by visual designer, and so by using Partial Types the UI code can be hidden from the developers and they do not need to be worried for the problems like they were facing earlier because of embedded “Windows Forms designer generated code” with in the code editor window. While it is a good programming practice to maintain all source code for a type in a single file, sometimes a type becomes large enough and so difficult to maintain.

Partial types allow classes, structs, and interfaces to be broken into multiple pieces; stored in different source files for easier development and maintenance. A new type modifier called partial is used when defining a type in multiple parts

Traditional approach

Let's understand how to implement Partial type on your own with Visual Studio 2005. I have designed a sample application without Partial types i.e. traditional approach.

This is a Console application, I named it Traditional approach. Where all the functionality is embedded in the single class file, this traditional approach is also known as Single Unit.

http://www.programmersheaven.com/articles/images/vidya/partialtype/image003.gif Figure 3 Code example - Traditional approach or Single uint


Benefits of Partial Types

  • Partial types allow a clean separation of business logic and the user interface. For example, the code generated by Visual Studio 2005 for a Windows Form is kept separate from the business logic of application.
  • Using partial types, the UI code can be hidden from the developer, who usually has no need to access it anyway.
  • This will prevent developers from accidentally modifying the code that is generated by the visual designer.
  • Partial types will also make debugging easier, as the code is partitioned into separate files.

Partial type - Code example

Partial types mean that your class definition can be split into multiple physical files. The working mechanism of partial type is based on the following:

  • Each source file contains a section of the class definition, and all parts are combined when the application is compiled.
  • During compile time, it simply groups all the various partial classes and treats them as a single entity.
Now I will demonstrate how to implement partial types for the same above given code. To demonstrate this I have creaed a project named Partial Types.

I added two class files named Partial1.cs and Partial2.cs in the project and after that the IDE will look like the image below:



The class definitions of Partial1.cs and Partial2.cs will be like as shown in the figure given below. The main and basic difference between the traditional and partial type implementation of classes if that while implementing partial types class definition have a new .NET 2.0 specific keyword named partial(C#) or Partial(VB .NET)



Doing this will not just hide the class definition in solution explorer, rather it will be seen as appear in the Figure -4.

To hide these you have to use another .NET 2.0 specific feature called MSBuild, this is a new build engine. MSBuild defines what you are going to build and how you are going to build it. The build is defined in the XML file format. (May be I will write another detailed article on MSBuild).

To hide the partial classes in Solution Explorer, you need to modify MSBuild of the project and for that you will need to use the project specific .vbproj or.csproj file.

Open the .vbproj or.csproj file with Notepad and navigate to the <ItemGroup> node which will look like as shown in the image below: (If you have the build the project/solution)



Modify the code (as shown in the figure below) inside the <ItemGroup> node to specify that under whom these partial classes will appear and will be associated with; during compilation.



After modifying the code in notepad save the changes and close notepad, and switch to the Visual Studio solution, you will be notified by a message box saying “The project ‘Partial Type’ has been modified outside the environment.” Click on Reload. Now go to the Solution Explorer and you will see that the classes named Partial1.cs and Partial2.cs are appearing inside the collapsed Program.cs, i.e. not seen as were appearing before as shown in Figure- 4.

To view the hidden classes (partial types) you need to expand the Program.cs and then only you can see the Partial1.cs and Partial2.cs embedded inside. It will appear as shown in the figure below.



This is how you can utilise the partial types and the logic can be hidden-protected; you can also prevent mishaps and provide useful code-layer abstraction at the same time.




About the author
Vidya Vrat Agarwal, a Microsoft .NET Purist, and an MCT, MCSD.NET, MCAD.NET, MCSD. He is also a Life Member of Computer Society of India (CSI). He started working on Microsoft.NET with its beta release. He has been involved in software development, consultation, corporate trainings, T3 programs and technical writing for Microsoft .NET for various corporate clients.

He lives with his beloved wife and lovely daughter nick named Pearly. He believes that nothing will turn into a reality without them. In technologies the part which really fascinates him is the architecture of that particular technology. He is the follower of the concept No Pain, No Gain.

Presently he is working with LionBridge Technologies in Mumbai - India, where he work with Microsoft Dept. on Visual Studio 2005 (Whidbey) and SQL Server 2005 (Yukon) and his business card reads Subject Matter Expert (SME). His technical profile holds .NET Framework, VC#, VB.NET, WebServices, Remoting, SQL Server, COM, DCOM, COM+, MTS, MSMQ. Visual Studio 2005 (Whidbey), SQL Server 2005 (Yukon) and Longhorn.

His blog can be read at http://dotnetpassion.blogspot.com





 

Other Views

corner

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.