StrataFrame Forum

Data Driven Values

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

By Ger Cannoll - 5/11/2011

I want to set up a methodology where some of the data that initially gets into a BO can be taken from a table. The data in each BO could be different for the same APP that is deployed at different sites. Say I have the APP on a site in USA, then the default Currency to get onto the Customer Table for a New Cusotmer would be USD, wheresas if the APP was depolyed in Ireland for instance, the default Currency for the Customer would neeed to be set up as EURO. These are just defaults, but could then be changed before the Form is saved. This gives an idea of  whats required, but these Table driven defaults go right across most Business Objects in the Application, and there will normally be many defaults for each Business Object. This saves the customer lots of time from having to enter defaults, but also allows them the option to change the defaults if they so wish.

Ok, so that's the requirement , and am wondering whats the best way of handling this. My preference would be to have any code in the one place, to allow for deviations from the default (Instead of having something in every BUisness Object, and if theres some change, I may need to search out what needs to be done and go looking at every Buisness Object)

My Data Driven Table would look something like:

Table /BO         Field       Value
Customer                   Currency    USD
Customer                   Area           NY
RATES                        TAX           21


I would want to (just during the Adding of a New record) do a search for the relevant Table, and then populate each field with the data from the table

I would appreciate some guidance on:

1. Will above appoach work, or is there a different/better way of approaching it
2. What event would I call a 'PopulateFieldsFromDataDrivenTableOntoBO from  (Presumably just after 'Adding' a record to the BO)
3. I would like to have just 3 fields as above, but will I run into problems when I try to load a BO that is an Integer field with say data that is a String Field


e.g. Above , my DataDriven table has a value field which is Character and I will want to move say 'NY' into an Area Field (should be ok) but also want to move '21' into an Integer field...how would I handle this, other than setting up a whole pile of different filed type on the datadriven table

I expect  something like this is either already available or has been thought through already and would welcome suggstions before I go down this road myself
By Greg McGuffey - 5/11/2011

If I'm understanding you, you want to have a data table (and thus different customers of your app could have different defaults, because they are using different tables) that holds the defaults and then load those defaults when the user add record to a BO that uses this default feature.

I think it is likely a good approach. Here are some thought on it:

1. I'd load the defaults BO at startup, as this data shouldn't be changing often and it would be way faster than having to hit the table in the database every time you added a record. This assumes that the table isn't that big. A few hundred rows maybe.

2. Use the SetDefaultValues event of the BO to set these values from the defaults BO.

3. To handle the conversion problems, I can think of a few options.

    a. in the defaults BO, setup properties or methods for each of the defaults. Do a cast there. Use setter to check for valid values when defaults are configured.
        

         public string Currency { get { //-- lookup value, cast to string } set { //-- validate value, save to appropriate row in BO } }
         public int Tax { { get { //-- lookup value, cast to int} set { //-- validate value is int, save to appropriate row in BO } }


    b. Have generic methods to return specific types of values:
        

         public string GetStringDefault( string table, string field ) { //-- get value and cast to string }
         public string GetIntegerDefault( string table, string field ) { //-- get value and cast to int}


    c. Use a generic method to manage casting.
        

         public T GetDefault<T>( string table, string field )
         {
              //-- get value from BO
              string rawValue;

              //-- Cast and return
              return (T)rawValue;
         }
By Ger Cannoll - 5/11/2011

Hi Greg.

That gives me food for though and exactly what I was looking for

I'd load the defaults BO at startup,


Up to now, I've only ever used Business Objects from a the form I am in. How do you get a BO to persist , and how do you access them,  say from a BO thats opened at startup
By Greg McGuffey - 5/12/2011

I'd create a static class and have one of its properties be this BO.  Likely I'd also have a static method to explicitly load the BO, that I'd call in the application InitializingApplicaiton event handler in program.cs.

// Common class for any sort of common,
// infrequently changed data used by app. Besides defaults
// this might include config data, lookup BOs, etc.
public static class AppData
{
    // Static constructor
    static AppData
    {
        //-- Initialize BO used to access defaults.
       
AppData.Defaults = new _defaultsBO();
    }
   
    private static DefaultsBO _defaultsBO;

    // Property used to access the defaults data.
    public static DefaultsBO Defaults{ get; set; }

   // Property used to actually load defaults with
   // data. If you have other app data, I'd use this
   // method for that data too and use the
   // FillMutlipleDataTable method of BusinessLayer
   // to do it.
   public void Load()
   {
        //-- Load the defaults BO. This assumes you've
        //    setup a fill command in the BO.
        Defaults.FillAll();
   }
   
}


The AppData.Load() is called in the InitializingApplication event handler. To access a default you'd call whichever method/property of the defaults BO you decided to use. E.g. if you used the generic one, a call to get the TAX default would be AppData.Defaults.GetDefault<int>( "RATES", "TAX" ).  I'm sure there are other ways of doing this, but this is one way and hopefully will get you thinking. BigGrin