Re: Create Null Object
- From: "tshad" <tscheiderich@xxxxxxxxxxxxxxx>
- Date: Thu, 29 Sep 2005 11:14:08 -0700
"Oliver Sturm" <oliver@xxxxxxxxxxxx> wrote in message
news:xn0e7tfm2ba5d16000@xxxxxxxxxxxxxxxxxxxxxxx
> tshad wrote:
>
>>How do you create a null object if the Constructor finds an error and thus
>>would make the object invalid?
>>
>>If I have a Class Role and during the constructer there is something
>>wrong, I want to do something like the following:
>>
>> public Role()
>> {
>> connectionString = ConfigurationSettings.AppSettings["x"];
>> if (connectionString == "")
>> Don't create the object and pass back a null
>> }
>
> In addition to the other suggestions - this looks like a situation to me,
> where you actually detect an invalid state during object construction. In
> that case, I would probably throw an exception in the constructor.
How do you do that?
If you are already in the constructor, what tells it to send back the null
and not to create the object in the first place (or at least destroy what it
has already created).
Thanks,
Tom
>
> I see the distinguishing factors like this: if the condition is part of
> your normal application flow logic, you'll want to detect and handle it
> without exceptions - but you need to do that "from the outside", meaning
> not from the constructor. A factory method may be fine for that. But if
> the condition is something that shouldn't occur unless something went
> wrong, I'd rather throw an exception from the constructor - no need to
> introduce additional management code in the form of factory methods or
> other "outside" checks for this error situation.
>
>
> Oliver Sturm
> --
> Expert programming and consulting services available
> See http://www.sturmnet.org (try /blog as well)
.
- Prev by Date: Re: request.Headers.Clear() doesnt work correctly?
- Next by Date: Re: trim spaces before evaluating regex
- Previous by thread: Re: Create Null Object
- Next by thread: Re: How to restore the default class1.cs file in a console application?
- Index(es):
Relevant Pages
|