using a SmartPart as a regular UserControl a view

Topics: CAB & Smart Client Software Factory
Sep 2, 2006 at 5:02 PM
originally posted by: sklett

I've got a problem that I'm sure is due to my misuse of the CAB.
I have create a SmartPart called "RecentResultsView" - In the Load event for the RecentResultView I make a call to the presenter to update the View's controls. Pretty typical I think.

In another View, I'll call it "MainView" I have added this SmartPart as if it were a regular UserControl. When I load MainView in the designer I get the dreaded "Red Banner Designer Screen of Death" and it states:
"Object reference not set to an instance of an object" and the supplied stack trace indicates this happens in the RecentResultsView_Load() method.

So the Presenter reference hasn't been handled by the DI.

So I know that cause (I think), but what is the solution? Is this why the CAB gods created the ZoneWorkspace? Would this solve my problem? I've had a hard time getting the ZoneWorkspace to work for me, I always give up pretty quick as I haven't NEEDED it yet, but maybe my time has come?

Is ZoneWorkspace my solution?
Sep 3, 2006 at 10:00 AM
originally posted by: mamu300B

Hi!

your are right with the reason for the exception you get. DI hasn't run, so no presenter is injected.

One solution would be to move the presenter's update code from the Load to the setter method (or anywhere else you use DI to get the presenter) where the presenter is injected/created.
By the way this would be an even more common and recommended approach for setting the presenter's view property.

-Matthias
Sep 3, 2006 at 10:08 AM
originally posted by: sklett

Morning!.. err, Afternoon?

I don't follow you exactly. If I understand correctly you are suggestion that I move "_presenter.OnViewLoaded()" from View.OnLoad() to the presenter property setter in the view code?

This would mean that the presenter would set the view's controls when it is built by the ObjectBuilder vs when the view is shown. Am I correct? in which case, isn't there a chance that DI'ed services and other dependencies wouldn't be build yet?

You last sentence makes me think I'm missing something here. Can you expand on the last statement please?
Sep 4, 2006 at 2:19 AM
originally posted by: mamu300B

Sorry, when my words weren't clear enough.

My idea comes from the SCSF AppraiserWorkbench sample. When you have a look on how they create the presenter you will find something like that:

SmartPart
public sealed partial class AppraisalDetailView : UserControl, IAppraisalDetailView
{
private AppraisalDetailViewPresenter _presenter;
CreateNew
public AppraisalDetailViewPresenter Presenter
{
set
{
Guard.ArgumentNotNull(value, "Presenter");
_presenter = value;
_presenter.View = this;
}
get
{
return _presenter;
}
}
...
}

And if you really have to call "_presenter.OnViewLoaded()" in the view's OnLoad, why not make a check for null reference?

>>This would mean that the presenter would set the view's controls when it is built by the ObjectBuilder vs when the view is shown. Am I correct?

exactly. see code above.

>>in which case, isn't there a chance that DI'ed services and other dependencies wouldn't be build yet?

it depends. but usually that will not happen.

Hope this makes things more clear.

-Matthias