Sign In

Navigation

On This Page

Archive

<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

Categories

Blogroll

Contact

Send mail to the author(s) Email Me
MCPD
MCTS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way


Copyright ©  2010
 Creative Commons License
This work by Jeff Klawiter is, unless explicitly stated in the article,  available under the Creative Commons Attribution 3.0 United States License.

Pick a theme:
# Monday, September 14, 2009
by Jeff Klawiter - Monday, September 14, 2009 4:18:17 PM (Central Standard Time, UTC-06:00)

With the advent of ASP.NET MVC 2.0 and the new templated helpers we’re once again having to write out our entire data objects. This is something that ORM tools like LINQ to SQL and LINQ to Entities were supposed to alleviate. Even better is we must now implement a class in 3 files.

Case in point: http://blog.pagedesigners.co.nz/archive/2009/08/06/asp.net-mvc-2-ndash-buddy-classes-for-your-models.aspx

The idea of “Partial Properties” follows the convention for Partial Classes and not Partial Methods. With Partial classes you can have the same class defined in two files. This is extremely helpful with extending generated code. With Partial Methods, one partial class file defines a method and another partial class file may if it choose implement that method. If the method is never implemented then the compiler completely throws away any code that may have referenced that method.

During a PDC talk Anders said that he couldn’t see a reason why we should implement Partial Properties but it’s becoming clear to me that we are starting to have the need.

Here’s the current situation:

  1. You create your models with LINQ to SQL or LINQ to Entities.
  2. You have no control over the auto-generated code so you implement partial classes for each class made in a separate file
  3. You need to annotate the properties in the generated classes so you then have to create a buddy class
  4. You take all the properties of the generated class and type them in and add attributes to them and thus mostly defeating the purpose of #1
//GeneratedClass.Designer.cs (the generated file)
public partial class GeneratedClass
{
	private string _name;
	public string Name
	{
		get{return _name;}
		set{_name = value;}
	}
}

//GeneratedClass.cs (your file)
[MetadataType(typeof(GeneratedClass_Metadata))]
public partial class GeneratedClass
{
	
}

//GeneratedClass.cs or GeneratedClass.Meta.cs (your other file)
public partial class GeneratedClass_Metadata
{
	[Required(ErrorMessage="Name Required")]
	public string Name{get;set;}
}

In the end you end up with 2-3 files and 3 implementations of your class. You also end up with the compiler generating more classes that their only use are for attributes.

With a “Partial Property” generated code could expose all of the public properties like so

//GeneratedClass.Designer.cs (the generated file)
public partial class GeneratedClass
{
	private string _name;
	public partial string Name
	{
		get{return _name;}
		set{_name = value;}
	}
}
//GeneratedClass.cs (your file)
public partial class GeneratedClass
{
	[Required(ErrorMessage="Name Required")]
	public partial string Name;
}

This may all be moot if we can at least get some tooling support for MetaDataType. We could use a Refactor –> Generate Buddy Class. This could auto-gen the metadata class for you so all you have to do is write the attributes in. Maybe even Class Diagram support?

Comments [0] #      C# | Rant  |  kick it on DotNetKicks.com Shout it
All comments require the approval of the site owner before being displayed.
Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, strike, strong, sub, sup, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview