Home » Source Code » Polymorphic Databinding for .NET

Polymorphic Databinding for .NET

liuwei
2016-05-19 11:12:32
The author
View(s):
Download(s): 0
Point (s): 1 
Category Category:
AllAll

Description

Defensive Datasource

Some ASP.NET controls, such as DataGrid, expect all the elements of the DataSource collection to have the same type when you call the control's DataBind method. Sometimes, the restriction seems to be a bit looser: if the collection is not an ITypedList, then for each property of the first element in the collection, the control will expect to find that property on every element in the collection. Either way, it's inconvenient, and it happens even if you have AutoGenerateColumns = false.

Consider the case when you want to display the properties defined in a superclass for a set of subclass instances. Some people call this "polymorphic databinding" because the elements of the collection are heterogeneous but derived from a common supertype (aka BaseType). If you try to simply bind an IEnumerable container to your DataGrid, you will get an exception at runtime. The problem may present as a System.Reflection.TargetInvocationException from System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component). The error message may be "Property accessor 'SomeProperty' on object 'SomeNamespace.SomeClass' threw the following exception: 'Object does not match target type.'" Alternatively, "Property accessor 'SomeProp' on object 'SomeNS.SomeClass' threw the following exception: 'Invalid object name 'SomeOtherClass'.'"

Implementing ITypedList is an ordeal, and implementing one-off simple representations of a supertype's data gets old quickly. Other possible solutions for this problem include Really complex databinding. Alternatively, you can use my DefensiveDatasource library, which I've tried to optimize for the simpler common case.

This library includes NUnit tests and an NAnt build file. A demo was once available and perhaps will be available again in the future: Demo.aspx source pretty printed

A simple solution

The problem with DataGrid can't be solved without access to its source code. The obvious work-around is to write a lightweight class that exposes the properties against which you wish to DataBind. You'll need a new class with a set of fields (or a reference to the object you wish you could bind against), a set of properties, and probably a copy constructor. Don't forget to update this new class as appropriate when you make changes to the other class.

DefensiveDatasource.Chameleon does all that drudgery for you using Reflection/Codegen. It's simple to implement and requires no maintenance effort.

How-to

  1. Reference the DefensiveDatasource DLL from your project
  2. Optionally, place using seanfoy; at the top of your source file
  3. Replace code that says dataGrid.Datasource = someEnumerable; by dataGrid.Datasource = new DefensiveDatasource(someEnumerable);
Sponsored links

File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.96 kB
apply-version.nant3.17 kB2008-07-07 06:09
AssemblyInfo.cs346.00 B2008-07-07 06:19
AssemblyInfo.cs.tmpl374.00 B2008-07-07 05:15
01.96 kB
01.96 kB
DefensiveDatasource.dll11.00 kB2008-07-07 06:20
DefensiveDatasource.pdb25.50 kB2008-07-07 06:20
DefensiveDatasource.xml7.11 kB2008-07-07 06:20
Demo.aspx.dll4.00 kB2008-07-07 06:20
Demo.aspx.pdb11.50 kB2008-07-07 06:20
Demo.aspx.xml134.00 B2008-07-07 06:20
TestDefensiveDatasource.dll11.50 kB2008-07-07 06:20
TestDefensiveDatasource.pdb33.50 kB2008-07-07 06:20
TestDefensiveDatasource.xml3.60 kB2008-07-07 06:20
Demo.aspx758.00 B2008-07-07 03:25
DefensiveDatasource.build6.59 kB2008-07-07 06:01
DefensiveDatasource.cs17.92 kB2008-07-07 06:02
DefensiveDatasource.csproj2.91 kB2008-07-07 06:16
DefensiveDatasource.sln1.34 kB2008-07-07 03:25
Demo.aspx758.00 B2008-07-07 03:25
Demo.aspx.cs914.00 B2008-07-07 03:25
gpl.txt34.32 kB2007-07-02 07:00
01.96 kB
nunit.framework.dll44.00 kB2008-07-07 03:25
LICENSE.txt738.00 B2008-07-07 03:33
TestDefensiveDatasource.cs14.10 kB2008-07-07 06:03
...
Sponsored links

Comments

(Add your comment, get 0.1 Point)
Minimum:15 words, Maximum:160 words
  • 1
  • Page 1
  • Total 1

Polymorphic Databinding for .NET (70.63 kB)

Need 1 Point(s)
Your Point (s)

Your Point isn't enough.

Get 22 Point immediately by PayPal

Point will be added to your account automatically after the transaction.

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

LOGIN

Don't have an account? Register now
Need any help?
Mail to: support@codeforge.com

切换到中文版?

CodeForge Chinese Version
CodeForge English Version

Where are you going?

^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!
OK

Warm tip!

CodeForge to FavoriteFavorite by Ctrl+D