User defined properties in business objects

User defined properties in business objects

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


kobruleht posted on Saturday, October 14, 2006

In my application I have about 100 tables like table of customers

CREATE TABLE Customer ( id CHAR(10), name CHAR(20), address CHAR(50) );

Data is edited in WinForm DataGrid . I have table of grid column descriptions

CREATE TABLE InputForm ( Form CHAR(20), FieldName CHAR(20), Caption CHAR(20), Width NUMERIC(2) );
INSERT INTO InputForm VALUES ( 'Customer', 'name', 'Customer Name', 100 );
INSERT INTO InputForm VALUES ( 'Customer', 'address', 'Customer Address', 200 );

My end users can add new fields and input data to those fields at runtime:

ALTER TABLE Customer ADD COLUMN EndUserSpecificField CHARACTER(20);

INSERT INTO InputForm VALUES ( 'Customer', 'EndUserSpecificField', 'Special field value', 10 );

Where to find sample how to create such application in C# ?

I looked into CSLA but havent found any sample aboutnusing dynamic business object properties.

Business object properties are defined statically only, like in Project.cs

private Guid _id;
private string _name = string.Empty;
private SmartDate _started;

etc.

Andrus.

Brian Criswell replied on Saturday, October 14, 2006

Just create a child list of custom properties.  Every object that can have custom properties would have one of these lists.

SonOfPirate replied on Monday, October 16, 2006

A good example of this can be found in the .NET Framework System.Web.UI.WebControls.WebControl class where an Attributes property is defined to allow users to add their own attributes/properties to a control.  This allows the control to be extensible in the way you are describing.  In your case, you will have to add the necessary logic to handle selecting and updating the data to/from the DB for these "added" properties just like WebControl uses the Attributes collection during rendering to append the name/value pairs to the HTML output stream.

Something else to consider - use a set of separate child tables in your DB to accomodate this rather than allowing your code to make schema changes.  It's just a preferencial thing, but I prefer to have an Attributes table listing the available/defined attributes then another table referencing the parent table (customers), the attribute and specifying the value for that instance.  This maps well to your object model as well as you can have a CustomerAttributesCollection object that is responsible for managing these values for your BO.

Hope that helps.

 

kobruleht replied on Monday, October 16, 2006

use a set of separate child tables in your DB to accomodate this rather than allowing your code to make schema changes.

Thank you. This may be great idea. However, this requires extra coding for custom attributes.

So I think I put all business object database based-attributes to a list. I think best is to create a single, universal business object which work with any tables.

It would be nice if framework has such a object which can be called from admin menu. This allows framework to edit any table, without additonal encoding. This turns framework into poweful free DBA tool which can be extended by users.

Andrus.

ajj3085 replied on Monday, October 16, 2006

kobruleht:
It would be nice if framework has such a object which can be called from admin menu. This allows framework to edit any table, without additonal encoding. This turns framework into poweful free DBA tool which can be extended by users.


That's not the goal of Csla.  In fact, objects you build using Csla many times do not map directly to the tables at all.  This is because we're modeling business behaviors, not modeling data. 

SonOfPirate replied on Monday, October 16, 2006

Andrus, what you are describing is an application or use of the framework and not something that a framework is intended to do.  You can certainly extend the framework to add your own custom classes that can be used to perform DBA tasks, but remember that the classes in a framework should be those that will be reused in multiple applications.  If the functionality you are describing is something for a single client application, then that is where the classes should be located.

And, as Andy said, your classes should be modeling behavior not simply mapping data.  So, the behavior you are talking about are DBA tasks which is fine, just be sure you want this exposed and that it belongs in a "framework".

Brian Criswell replied on Monday, October 16, 2006

kobruleht:

use a set of separate child tables in your DB to accomodate this rather than allowing your code to make schema changes.

Thank you. This may be great idea. However, this requires extra coding for custom attributes.

So I think I put all business object database based-attributes to a list. I think best is to create a single, universal business object which work with any tables.

It would be nice if framework has such a object which can be called from admin menu. This allows framework to edit any table, without additonal encoding. This turns framework into poweful free DBA tool which can be extended by users.

Andrus.


To be fair, when I suggested a list, I was suggesting a list that would pull the custom attributes from the custom attributes table.  Their would be two tables/objects.  One would include the custom attributes that the user defines for the different object types.  The other would store values against a combination of an attribute and instance of an object.  This would create a universal attribute system that could be assigned against any object.

Copyright (c) Marimer LLC