Dynamic business objects

Dynamic business objects

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


rfcdejong posted on Monday, June 22, 2009

In a new project we'll have to make dynamic views:

A user configuration in the database will cause a view to have controls.
This control has to be databinded to a dynamic property.
That property will have dynamic authorization and dynamic validation.

It means that we don't know much at designtime...

- Dynamic authorization isn't a problem at all;
- Simple dynamic validation rules aren't a problem as well;
- Dependend dynamic validation rules will be harder, but not impossible;
- Dynamic properties are another thing.

I'm thinking about using the codedom to create dynamic business objects, generate an assembly in memory depending on the content defined by the user in the database. It might also be a mix of non-generic / pre-defined properties together with the dynamic properties.

How would u guys do this? Is there anyone with any practical experience?

SonOfPirate replied on Monday, June 22, 2009

I suppose a lot of it depends on what information is in your configuration file, how the end-user manages the dynamic content, etc.

I recently finished a project with similar requirements.  We opted not to go the code-dom route only becuase we needed to support a GUI that allowed the user to manipulate the view.  Plus, we wanted an extensibility point.

In the end, we went with the idea of a compiled pseudo-base class that would be instantiated at run-time along with an XML document that contained all of the dynamic content which would be deserialized into the base class.  We can declare property values, etc. in the XML file which then override the preset values from the base class when deserialized into the instance.

For extensibility, we actually allow the XML document to specify what "base" class to use - similar to defining a code behind page in ASP.NET.  We first read the XML document, find what class to instantiate, create an instance of that class, then deserialize the rest of the XML into the class.  This gives us an -in-memory instance of the desired class.  Plus, we can easily extend or customize the application by changing the "base" class used with the view.

Another nice feature is that we can use the same XML to render the view in our GUI at "design" time.

It's actually a bit complicated, but hopefully this gives you an idea of the approach.

 

RockfordLhotka replied on Monday, June 22, 2009

It may be possible to create a subclass of Csla.Core.BusinessBase (not generic) that implements ICustomTypeDescriptor (I think that is the interface), along with the functionality in BusinessBase<T>, to create a dynamic object class.

I know people have tried doing this, and I think some have succeeded.

Obviously you'd need to do the same for ReadOnlyBase.

SonOfPirate replied on Monday, June 22, 2009

Ah, good point, Rocky.  I wasn't thinking of it in those terms.  I worked for a company not too long ago that allowed the user to define custom "attributes" on business objects.  We did accomplish this using a property bag and a custom type descriptor that would make the object "appear" as if the custom attributes were properties on the object.  So, when the business object was bound to a grid, for instance, it would have columns for the custom attributes right alongside the hard-coded properties from the business object.

Our approach at the time was to create a subclass of BusinessBase, as Rocky indicated, with an interface defining the access point for defining and managing the attributes and a custom type descriptor doing the work.  I can probably dig up some old code if requested.

 

CodeSmartNotHard replied on Wednesday, June 24, 2009

Hi,

We have some similar requirements where we need to add dynamic properties.  Would you mind digging up some code to demonstrate this?

Thanks,

Mike

rfcdejong replied on Thursday, June 25, 2009

We haven't used it yet, but found some interesting links after hearing about ICustomTypeDescriptor:

http://msdn.microsoft.com/en-us/magazine/cc163816.aspx

http://zcoder.blogspot.com/2007/11/icustomtypedescriptor.html

 

I think that'll work for us.

rfcdejong replied on Friday, August 21, 2009

This topic is from 2 months ago and meanwhile i added dynamic properties support in our subclasses with validation and authorization on it.

I didn't use ICustomTypeDescriptor but instead i used ITypedList, the only problem is that this isn't supported in Silverlight (yet) so no dynamic properties in Silverlight :(
The reason for implementing ITypedList is that somehow the infragistics grid doesn't support ICustomTypeDescriptor.

In our ObjectFactory subclass (colleagues tent to name it DataFactory) dynamic properties can be registered, loaded and readed.

I wrote a custom PropertyDescriptor which checks the type of the PropertyInfo and incase it's a DynamicPropertyInfo then my code is called and else csla code. Not using the FieldManager nor it's data, since the FieldManager is designed for the type of the business object not a instance.

It's a bit complex, but not as complex as using the codedom with emits.


RegisterDynamicProperty in CSLA would be a nice feature

Copyright (c) Marimer LLC