How to write a class with dynamic properties

How to write a class with dynamic properties

Old forum URL: forums.lhotka.net/forums/t/8226.aspx


ambreshgaur posted on Tuesday, December 29, 2009

My data grid has some dynamic columns that changes depending upon critaria chosen....how can i handle such a situation in csla?

sergeyb replied on Tuesday, December 29, 2009

There is no easy way. If you know what the max number of columns is, you can just create a number of properties (Prop1, Prop2, etc...) of type of object, then create rules that would understand what each property means in order to enforce proper use input. Not a clean solution, but it will work.

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

-----Original Message-----
From: ambreshgaur [mailto:cslanet@lhotka.net]
Sent: Tuesday, December 29, 2009 5:10 AM
To: Sergey Barskiy
Subject: [CSLA .NET] How to write a class with dynamic properties

My data grid has some dynamic columns that changes depending upon critaria chosen....how can i handle such a situation in csla?

ajj3085 replied on Tuesday, December 29, 2009

I've known people that used metadata to create a class in memory, compile it, then use it.  I've always wondered if you could do this with Csla.

Not easy, but if you want to pioneer that route, the System.CodeDom.Compiler and  Microsoft.CSharp.CSharpCodeProvider namespaces would be your friend.

RockfordLhotka replied on Tuesday, December 29, 2009

ajj3085:

I've known people that used metadata to create a class in memory, compile it, then use it.  I've always wondered if you could do this with Csla.

You could do that. I do it with my MCsla prototype for SQL Server Modeling. It is a pain to get up and running (debugging is really hard), but once it is working it is really very cool.

rfcdejong replied on Tuesday, December 29, 2009

I posted a simulair question..

http://forums.lhotka.net/forums/thread/35669.aspx

Read the last reply for the solution i went for.
http://forums.lhotka.net/forums/permalink/35669/35669/ShowThread.aspx#35669

There is only some more into it if u use dynamic properties on a business object (not a readonly) cause u'll need to include the dynamic properties for checks like IsDirty or when the dynamic property change u'll need to raise a propertychanged event. Event listeners need to be decoupled and re-coupled etc etc..

The simple version would be on readonlybase.

But it do require an own abstraction layer, but u should always have such an layer :)

rsbaker0 replied on Tuesday, December 29, 2009

One other possibility is to have your object implement ICustomTypeDescriptor, and perhaps have your list class also implement ITypedList. Then you tell the grid you have an arbitrary set of properties, which will work in the case where all the objects in the list have the same set dynamic properties.

We do this in our application, but it isn't quite as challenging as what you are asking as the set of extra "dynamic" properties is constant for the duration of the application execution. (There are other wrinkles -- search this forum for ICustomTypeDescriptor and you'll see where I have posted some caveats)

RockfordLhotka replied on Tuesday, December 29, 2009

The trick with dynamic types is that you want them to work with your UI
technology of choice. Just adding a dynamic property to a type may or may
not make your UI technology actually see that property.

This is where things like ICustomTypeDescriptor come into play - so Windows
Forms (and WPF) can find your custom properties. But I don't think that
exists in Silverlight, and I don't know for sure that it is fully supported
by ASP.NET (though it should be, since this is what the DataTable uses).

The new DLR capabilities in .NET 4.0 allow some really cool dynamic stuff -
but what I don't know is whether any of the UI technologies will actually
see the dynamic properties. It is a virtual certainty that the designers
won't see them in Visual Studio, since the types wouldn't even exist at
design time... So some of the RAD features of VS may be lost in exchange for
the dynamic features of types at runtime.

rsbaker0 replied on Wednesday, December 30, 2009

RockfordLhotka:
...It is a virtual certainty that the designers
won't see them in Visual Studio, since the types wouldn't even exist at
design time... So some of the RAD features of VS may be lost in exchange for
the dynamic features of types at runtime.


Incidentally, this is also one of the major drawbacks I have found to implementing ICustomTypeDescriptor with WinForms.

If you try to use a data source that implements ICustomTypeDescriptor in the designer, not only does Visual Studio not see the extra dynamic properties, it doesn't even see the "built-in" ones. So, you can't benefit from anything at design time that would involve discovery of the properties the class implements.

Copyright (c) Marimer LLC