Do you return void or object from the Add() method?

Do you return void or object from the Add() method?

Old forum URL: forums.lhotka.net/forums/t/8373.aspx


rxelizondo posted on Monday, January 18, 2010

Hello,

While looking at the Project Tracker sample application, I couldn’t help noticing that the collection methods used to add an object to a collection return void. Just to make it clear, specifically, I am referring to the following methods:


ResourceAssignments

public void AssignTo(Guid projectId)
{

}


ProjectResources

public void Assign(int resourceId)
{

}


You can see that both methods return void.

I was a little surprised to see that because up till now, I have personally always coded those methods to returned the newly added object and I figured that just about everybody else did the same thing! In fact, I am pretty sure this was the way the old CSLA used to do it on its sample projects…. maybe I am wrong about this but I think I am right.

Of course, I do realize that I can do whatever I want with the method and change it to suit my needs if I wanted to, but I guess that finding that Rocky is coding the methods like this, kind of got me thinking a little.

Is it not cool any more to return the object from the Add method? Is returning the object from the Add method a bad practice now for some unknown reason to me?

Just curious, thanks.

ajj3085 replied on Monday, January 18, 2010

I have a LineItems object, which has various Add methods to add different kinds of line items (product items, subtotal, etc.).

Each of those returns the newly added instance, because users want a prompt which allows them to set the quantity for each added item.

So I guess it depends on what you're trying to do; if you add and then you don't care anymore I'd probably return void.  But if you need to do something with the newly added instance, I don't think there should be any problem with it.  Certainly I haven't encountered any problems either way.

rxelizondo replied on Monday, January 18, 2010

Thanks Andy,

Normally, returning the object is somewhat practical to me because most of the time, I will simply take the newly created object and use it to display it in something like a windows form. So by returning the object directly from the Add method, I can add and get a reference to the object all in one shot. If I didn’t return the object from the Add method, then I would have to add extra code on the UI that searched for the newly added object.

It seems to me like returning the object can't hurt, if someone does not want the returned object then they don't have to use it!

I will assume that there is no other reason for why Rocky is not returning the object form those methods other than the project did not need it so why do it.

Like I said, I was mostly curious to see if I was missing something obvious like some kind of bad practice or something.

stefan replied on Tuesday, January 19, 2010

This goes a little OT, but:
I see issues concerning databinding here.
Isn't this technique prone to show edit level mismatches, at least in Windows Forms?
I would expect that in most situations the collection object (LineItems) is bound to a grid or something.
When directly binding the newly added object (LineItem) to a windows form, this object is bound twice at the same time. Doesn't this result in an edit level mismatch?
Am I right on this assumption?

Andy, how did you get around this?

I could think of making a copy of the new item, manipulate its properties on a separate form,
and then assign the property values one by one to the item in the list...

What do you think?

Stefan

ajj3085 replied on Tuesday, January 19, 2010

Yes, it will cause WinForms databinding problems if you do nothing.

However, what seems to work for me is if I set RaiseListChangedEvents to false for both the items binding source AND directly on the BLB object as well.

I then add the items directly to the BO and set the Quantity property after they're added.

Finally, I change RaiseListChangedEvents back to true (in the reverse order that I turned it off) and call ResetBindings( false ) on the items binding source AND the binding source for the root object.

That seems to work fine.  The grid is from Infragistics though, so I don't know if that plays a part in this setup working, but otherwise its all standard .Net stuff.

But your raise a good point; that could by why the sample doesn't show returning the newly added item.

Copyright (c) Marimer LLC