StrataFrame Forum

c# keyword in table using business object mapper

http://forum.strataframe.net/Topic25236.aspx

By Victor Sousa - 11/17/2009

How do I use the business object mapper on an SQL table which contains a field that is a key work in c#?

The field name is void.



I do not have control of these field names. I need to query data from an existing ERP system.



Is there anyway to use the @ prefix on the field names?



I actually do not need this field in the result set. Is there a way to instruct the BOM to use only certain fields for the BO definition?
By Peter Jones - 11/17/2009

Hi Victor,



You could create a view that omitts/renames the Void column and then use the view as the data source in the BO mapper.



You can do quite a bit in the Properties section of each column in the BO mapper but I'm pretty sure renaming columns isn't supported.

Cheers, Peter
By Greg McGuffey - 11/17/2009

Victor,



If you use a view you'll need to make is an updatable view.



You can rename a property, but I think you're going to be in trouble if the column uses a keyword. I've successfully renamed a non-keyword column to a keyword (renamed the cust_suffix column in the Customers table in sample db to "class", then bound a control to the "class" field and it loaded and saved). However, in looking at the designer code, I think you'll get into trouble if the column is a keyword.



While you can completely customize the property code, the column name is used in several places, in order to enumerate the columns in the table. Most of them are strings (like the AllFieldNames property). But in two places that I found, a keyword won't be liked. The first is in the definition of the fields enum (names like CustomersBOFieldNames for the CustomersBO). You'd end up with something like:



public enum CustomersBOFieldName

{

cust_ID,

cust_firstName,

. The second is in the definition of the _FieldEnums dictionary, which uses this enum value. Now if you remember, you could just manually edit the designer file and slap an @ in front of the enum def and you'd be good to go. Not great, but it would work.







1. Using the Bo mapper, define the code of the property, renaming it in the process.

2. In the BO file (not the designer file, but the regular one, define a property descriptor for the renamed property.



Details

When using the custom code feature, whatever you put in as the custom code is used, as provided when the BO writes out the designer file code. The easiest way to do this is to open an existing BO designer file and copy the code of another property of the same type. Paste it into the custom code area. Use the @ to escape the keyword. Using the same attributes makes sure the field is there for designers etc.



/// <Summary>

/// void field

/// </summary>

[Browsable(false)]

[BusinessFieldDisplayInEditor()]

[Description("void field")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public string @void

{

  get

  {

    return (string)this.CurrentRow["void"];

  }

  set

  {

    this.CurrentRow["void"] = value;

  }

}
By Greg McGuffey - 11/17/2009

Sorry about that, posted before I was done. Ignore previous, here's the official one.



Victor,



You can rename a property, but I think you're going to be in trouble if the column uses a keyword. I've successfully renamed a non-keyword column to a keyword (renamed the cust_suffix column in the Customers table in sample db to "class", then bound a control to the "class" field and it loaded and saved). However, in looking at the designer code, I think you'll get into trouble if the column is a keyword.



While you can completely customize the property code, the column name is used in several places, in order to enumerate the columns in the table. Most of them are strings (like the AllFieldNames property). But in three places that I found, a keyword won't be liked. The first is in the definition of the fields enum (names like CustomersBOFieldNames for the CustomersBO). You'd end up with something like:



public enum CustomersBOFieldName

{

cust_ID,

cust_firstName,

void

}




The compiler isn't going to like that keyword in the enum.



The second and third places use the field names enum. One is in the definition of the _FieldEnums dictionary and the other is in defining the property descriptors in the FieldDescriptor property. Now if you remember, you could just manually edit the designer file and slap an @ in front of the enum def and you'd be good to go. Not great, but it would work.



If you decide to try renaming the property it is pretty easy. There are two steps:



1. Using the Bo mapper, define the code of the property, renaming it in the process.

2. (renamed property doesn't match the column name in table) In the BO file (not the designer file), but the regular one, define a property descriptor for the renamed property.

   (column is a keyword, so property name is same as column name is table) put an @ in front of column name in _FieldEnums def.



Details

When using the custom code feature, whatever you put in as the custom code is used, as provided when the BO writes out the designer file code. The easiest way to do this is to open an existing BO designer file and copy the code of another property of the same type. Paste it into the custom code area. Use the @ to escape the keyword. Using the same attributes makes sure the field is there for designers etc.



/// <Summary>

/// void field

/// </summary>

[Browsable(false)]

[BusinessFieldDisplayInEditor()]

[Description("void field")]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

public string @void

{

  get

  {

    return (string)this.CurrentRow["void"];

  }

  set

  {

    this.CurrentRow["void"] = value;

  }

}




This first step you have to do.



As indicated, the second step depends on how you are renaming the property. One possibility (not your case) is that the property has a different name than the column in the table. In this case you'd have to override the GetCustomBindablePropertyDescriptors method and add a descriptor for the renamed property (see help topic "Adding Custom Field Properties"). Likely I wouldn't bother with this, unless there was a really compelling reason that the property couldn't be named as it was in the table. The most likely situation was the requirement to use a base BO that already had a property of the same name of the column.



The other possibility (your case) is that the property isn't really renamed as much as the name is escaped. In this case, you'd have to manually escape the name of the field in _FieldEnums definition, and then I think you'd be good to go. You'd have to remember to do this every time you rebuilt the BO via the BO mapper too. Like I said, not ideal.



I'd likely try the updatable view, as editing designer files like this is a bit of a hack and hard to document. But it's nice to know you have options! BigGrin
By Victor Sousa - 11/18/2009

Thank you for the replies. Before posting I did play with the view idea as well as editing the code to escape the field name in the resulting BO file. I was just hoping there was a better way.



Thanks for your time.