Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Data Record Class Design Question
Message
 
To
31/08/2009 13:21:42
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
General information
Forum:
ASP.NET
Category:
Class design
Miscellaneous
Thread ID:
01420594
Message ID:
01421945
Views:
34
>>>>>>I'm creating a set of classes to handle data:
>>>>>>
>>>>>>
>>>>>>class CompanyInfo : DataRecordBase
>>>>>>{
>>>>>>    private string _CompanyName = "";
>>>>>>    public string CompanyName
>>>>>>    {
>>>>>>        get { return _CompanyName; }
>>>>>>        set { _CompanyName = value; }
>>>>>>    }
>>>>>>
>>>>>>    public override bool SaveChanges()
>>>>>>    {
>>>>>>      if(Validate())
>>>>>>      {
>>>>>>         // Code here to save
>>>>>>      }
>>>>>>    }
>>>>>>    public override void Load()
>>>>>>    {
>>>>>>    }
>>>>>>
>>>>>>    private bool Validate()
>>>>>>    {
>>>>>>        bool bRetVal = true;
>>>>>>
>>>>>>        if (_CompanyName == "")
>>>>>>        {
>>>>>>        }
>>>>>>
>>>>>>        return bRetVal;
>>>>>>    }
>>>>>>}
>>>>>>
>>>>>>
>>>>>>They are all based off the abstract class DataRecordBase. The question is this - If the Validate method returns false, because a property is not complete, what should happen? I don't want to raise an exception?
>>>>>>
>>>>>>Since this is a non-visual class I don't want to pop up a message in this class. I'm thinking that Validate should set a class variable to an appropriate message, and maybe set a public IsValid flag to false.
>>>>>>
>>>>>>Then, in the form or class that uses it, if SaveChanges returns False then I know that the message property would be populated with something like "The Customer Name cannot be empty".
>>>>>
>>>>>I'm never in favour of public variables. You could use an out parameter to pass back the error message (or even an array of error messages) so something like:
>>>>>        public bool SaveChanges(out string msg)
>>>>>        {
>>>>>            if (!Validate())
>>>>>            {
>>>>>                msg = "This went wrong";
>>>>>                return false;
>>>>>            }
>>>>>            else return true; 
>>>>>        }
Called with
string str;
>>>>>            if (!CompanyInfoInstance.SaveChanges(out str))
>>>>>            {
>>>>>                MessageBox.Show(str);
>>>>>            }
BTW, if this is WinForms have you thought of using the ErrorProvider class in the UI?
>>>>>Regards,
>>>>>Viv
>>>>
>>>>I guess I could, but this class just represents a data record. It's not visual. That's why I thought a property defined in the base would work.
>>>
>>>But surely the error has to be surfaced in a visual way eventually - and the UI layer is the obvious (possibly only sensible) place for that?
>>
>>Yes, but this class and those like it are not UI components. They are a data record represented in a class. So when an issues/exception occurs, how do you inform the UI?
>
>You raise an event. Am I missing the basic question of the thread?
>Tim

Not at all. That was my original idea, but others have been advocating a different error handling approach.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform