Dependent - Independent design question

May 30, 2011 at 8:54 AM

Not sure how to set this up...

In my class I got a string property named 'Description' that can have it's value set in 2 ways...

1. Direct user input by changing the displayed text

2. It can get filled by a property 'Descriptiondetails' of another class ('Descriptionfiller') and that property( 'Descriptiondetails') is a result of other properties in that class.

(so if I got this right then 'Descriptiondetails' is a Dependent of the other properties in the class 'Descriptionfiller')

 

The question is... if the property 'Description' can get filled in 2 ways (by the user and 'automatically" by changing the properties on 'Descriptionfiller') then how should I setup 'Description' ?

Is it a Dependent or an Independent. It cann't be a Dependent, I guess, because that one should only have a getter and not a setter. But if I set it up as an Independent, how can it "automagically' update itself when the properties in 'Descriptionfiller' change.... 

Any suggestions would be appreciated.

thx

Coordinator
May 30, 2011 at 3:56 PM

I would make the override independent, and leave the default dependent.

Create an Independent<string> in the DescriptionFiller class called _desciptionOverride. When the user enters text, it is stored here. If the user has not entered text, it will be null.

In the view model, Description would simply return _descriptionFiller.DescriptionOverride ?? _descriptionFiller.DescriptionDetails.

Here's some example code:

    public class DesriptionFiller
    {
        private Independent<int> _otherProperty = new Independent<int>();
        private Independent<string> _descriptionOverride = new Independent<string>();

        public int OtherProperty
        {
            get { return _otherProperty; }
            set { _otherProperty.Value = value; }
        }

        public string DescriptionOverride
        {
            get { return _descriptionOverride; }
            set { _descriptionOverride.Value = value; }
        }

        public string DescriptionDetails
        {
            get { return string.Format("Default: {0}", OtherProperty); }
        }
    }

    public class DescriptionViewModel
    {
        private readonly DesriptionFiller _desriptionFiller;

        public DescriptionViewModel(DesriptionFiller desriptionFiller)
        {
            _desriptionFiller = desriptionFiller;
        }

        public string Description
        {
            get
            {
                return _desriptionFiller.DescriptionOverride ?? _desriptionFiller.DescriptionDetails;
            }
            set
            {
                _desriptionFiller.DescriptionOverride = value;
            }
        }
    }

If you ever need to revert back to the default value, just set DescriptionOverride to null.

Jun 10, 2011 at 12:10 PM

Finally got to this bit... Got it working with that. Thanks very much!!