|
The null object pattern.
public class Customer{}
public class NullCustomer : Customer{}
Then instead of assigning a customer reference to null you assign it to the null customer, etc. Then you can do all of your work without null checks. There are some instances where this works really, really, well. However, if you are chaining functions it is probably some sort of workflow so there maybe another solution that issue.
|
|
|
|
|
There is a way that you could do this, but it would be much more trouble than it was worth, and it would definitely open up a whole world of hurt. Effectively, what would need to be implemented is an IL rewriter to manage and rewrite the chain internally. Then you could create a fluid interface that would need to be recognised as the item that needs rewriting.
|
|
|
|
|
I respect a man who prefers power drills and small explosives where others would wimp out and use tweezers.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
When I get the chance, I may just have a go at it. Need some free time first.
|
|
|
|
|
Not very elegant, even if you could. Nice to get all clever (over engineering is, surely, an anti-pattern) with code but sometime in the future some poor sap will have to maintain what you have put together so keep it simple and verbose.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
I've heard of chaining programs and subprograms - using hpl on the HP 9825, for instance - but short circuit evaluation was never considered.
Having never optimized a program for multi-processor environments, I have no idea how to do this, but you could launch a separate process for each of your levels of test, then cancel all if any one of them fails. It might not be useful, but it would certainly give the grunts whose job it is to debug things something interesting to do.
Will Rogers never met me.
|
|
|
|
|
Out of curiosity, what would you do in the else section of such a call?
|
|
|
|
|
Else? There is no else.
Just blindly plow on!
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
There are a few ways you could do this, but one thing I've been meaning to write a tip/trick on (as soon as I figure out how it can be done) is the expression tree approach. You'd do this:
var result = SafeChain(A.b.c.d.e.f.g.h.i.j.k);
The parameter would be passed as an expression tree, which would then be evaluated in steps, making sure to check for nulls along the way. The first null would cause null to be returned, otherwise the result value would be returned. I imagine you could use the same approach with your method chaining / expression trees.
|
|
|
|
|
Actually to be passed as an expresion tree you'd have to write:
var result = SafeChain(() =>A.b.c.d.e.f.g.h.i.j.k);
But I agree that this is the way to do it.
|
|
|
|
|
In C# you can implement this k<ind of chaining using extension methods and expressions trees. The extension method (somewhat combined version of the two above):
public static U NotNull<T, U>(this T myObject, Expression<Func<U>> expression)
where U : class
{
if (myObject == null)
return null;
else
{
var func = expression.Compile();
return func();
}
}
Usage (returns either null if there's a null in the chain, or the value of MySubProperty, with almost the same syntax as above):
var value = myObject.NotNull(() => myObject.MyProperty).NotNull(() => myProperty.MySubProperty);
The only downside I found that in this case you have to declare a variable of type MyProperty for use in the second lambda expression. Maybe there's a way around it, but I didn't manage to find one as of yet. That can be changed though if the input remains myObject and the expression consists of the full path i. e. () => myObject.MyProperty.MySubProperty.
|
|
|
|
|
I figured out something that works. See my message below. It is based off of yours, with an extra parameter for the expression.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
Chris Maunder wrote: Am I procrastinating?
Yep! But then, you probably don't want to fix RootAdmin - it's down according to http://www.downforeveryoneorjustme.com/rootadmin.com[^]
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.
|
|
|
|
|
Maybe this IS the fix for the site.
|
|
|
|
|
public static U NotNull<T, U>(this T myObject, Expression<Func<T, U>> expression) where U : class
{
if (myObject == null)
{
return null;
}
try
{
var func = expression.Compile();
return func(myObject);
}
catch (Exception)
{
return null;
}
}
Based off of a suggestion above.
I tested this, and it seems to work.
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
If only there was some sort of site where people could write articles on exactly this issue[^]!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes!
|
|
|
|
|
That settles it for me, I want voting back.
People say nothing is impossible, but I do nothing every day.
|
|
|
|
|
I am humbled and ashamed.
And have just found an endless opportunity for way, way more procrastination.
cheers,
Chris Maunder
The Code Project | Co-founder
Microsoft C++ MVP
|
|
|
|
|
Have you looked at this CP article by Dimitri Nesteruk[^]
It looks very useful.
"I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68).
"I don't need to shoot my enemies, I don't have any." - Me (2012).
|
|
|
|
|
You were beat to the punch.
|
|
|
|
|
So I was. Great minds think alike but in my case I'll pass up on the citation.
Hopefully it'll convince Muppet Maunder to read it and see if helps him.
"I do not have to forgive my enemies, I have had them all shot." — Ramón Maria Narváez (1800-68).
"I don't need to shoot my enemies, I don't have any." - Me (2012).
|
|
|
|
|
In C++ you could do this with an 'instance' call like this:
if (this != NULL)
{
return this->real_stuff;
}
else
{
return default_stuff;
} As others have mentioned, in C# you can do this with extension methods:
namespace Test
{
public class Widget
{
}
public static class WidgetExtensions
{
static public void Method(this Widget _this)
{
if (_this != null)
{
Console.WriteLine("real widget");
}
else
{
Console.WriteLine("null widget");
}
}
}
class Program
{
static void Main(string[] args)
{
Widget widget = null;
widget.Method();
widget = new Widget();
widget.Method();
}
}
} results in the output
null widget
real widget
Software Zen: delete this;
|
|
|
|
|
The first code block doesn't make any sense to me but this line
var result = Test.NotNull(myObject).NotNull(myObject.MyProperty).IsPositive(myObject.MyProperty.Id); can be represented like this, can't you?
var result = (myObject == null ? false: myObject.MyProperty == null ? false : IsPositive(myObject.MyProperty.Id); Just bouncing off an idea
P.S. You can do it much simpler by using exceptions, I believe
Cheers
|
|
|
|
|