Quantcast
Channel: eXpandFramework RSS
Viewing all articles
Browse latest Browse all 861

BLOG by Dennis Garavsky: Using a built-in lookup editor based on values from another table for editing simple property types

$
0
0
This is actually a variation of a solution I described in my recent A very interesting way to update and display a persistent property via a non-persistent one post, because it is again based on a non-persistent reference property filtered using the DataSourcePropertyAttribute along with a custom data source collection.
This non-persistent reference property is represented using the standard XAF's lookup PropertyEditors, which are quite convenient for providing a user with the capability to select a single value either from a database table or from an arbitrary data source. In one turn, by adding custom logic to the getter and setter of our non-persistent calculated property, we can update the stored persistent value, which does not need to be a reference to another persistent class (table), but to any type (e.g., string or integer). This may come in handy for legacy databases when you cannot really modify the schema to provide normal associations between tables.



Let's take a look at the actual code for this solution:



[Persistent("StoredValue")]
privatestringstringFieldForStore;
privatePosition_LookupPropertyForDisplay;
[NonPersistent,DataSourceProperty("LookupDataSource"),XafDisplayName("My drop-down editor based on values from another table")]
publicPositionLookupPropertyForDisplay{
get{
if(_LookupPropertyForDisplay==null&&!string.IsNullOrEmpty(stringFieldForStore)){
_LookupPropertyForDisplay=LookupDataSource.Single(v=>v.Title==stringFieldForStore);
}
return_LookupPropertyForDisplay;
}
set{
SetPropertyValue<Position>("LookupPropertyForDisplay",ref_LookupPropertyForDisplay,value);
if(!IsLoading&&!IsSaving){
stringFieldForStore=value!=null?value.Title:string.Empty;
OnChanged("stringFieldForStore");
}
}
}
privateXPCollection<Position>_lookupDataSource=null;
protectedXPCollection<Position>LookupDataSource{
get{
if(_lookupDataSource==null){
_lookupDataSource=newXPCollection<Position>(Session);
}
return_lookupDataSource;
}
}


Here we have:
1. stringFieldForStore - a hidden string persistent field, which actually stores a selected lookup editor value (the position's Title) in the database;
2. LookupPropertyForDisplay - a visible non-persistent calculated property, which is represented by the standard lookup PropertyEditor in the UI for both Windows and the Web.
3. LookupDataSource - a hidden collection property, which contains persistent objects from another table serving as a data source for the lookup editor.

Here, I would like to once again note that storing a string value instead of the object's identifier is against the best practices for database design (data denormalization). So, use this approach when you need to provide a choice from values based on another table only if there is a strong reason for this. In all other cases, it is possible to create a persistent class mapped to that table and then declare a reference property of this reference type.


Viewing all articles
Browse latest Browse all 861

Trending Articles