Passing constructor arguments when loading a view

Topics: CAB & Smart Client Software Factory
Mar 1, 2006 at 9:00 AM
originally posted by: aadams99

I need to load a view that has arguments that need to be passed to the view's constructor, how is this done?

EventSubscription(EventTopicNames.EmployeeDepartmentDetailSelected, Thread = ThreadOption.UserInterface)
public void ViewDepartmentDetail(object sender, EventArgs<EmployeeDepartmentDS.EmployeeDepartmentRow> e)
{
//DepartmentDetailView view = new DepartmentDetailView(e.Data);
using (DepartmentDetailView view = WorkItem.Items.AddNew<DepartmentDetailView>())
{
view.ShowDialog();
WorkItem.Items.Remove(view);
view.Dispose();
}
}
Mar 1, 2006 at 9:23 AM
originally posted by: BradWilsonMSFT

EventSubscription(EventTopicNames.EmployeeDepartmentDetailSelected, Thread = ThreadOption.UserInterface)
public void ViewDepartmentDetail(object sender, EventArgs<EmployeeDepartmentDS.EmployeeDepartmentRow> e)
{
DepartmentDetailView view = new DepartmentDetailView(e.Data);
WorkItem.Items.Add(view);
view.ShowDialog();
WorkItem.Items.Remove(view);
}
Mar 1, 2006 at 10:48 AM
originally posted by: aadams99

Ok -- looking closer -- I found that by using the 'Add', as apposed to 'AddNew' or 'AddNew<>' the presenter that goes along with this dialog form does not get created (the create new never gets called). But I can't pass and object to the 'AddNew'(s) -- just a type. Is there a work around?
Mar 1, 2006 at 1:38 PM
originally posted by: aadams99

Didn't quite work --
Mar 1, 2006 at 2:48 PM
originally posted by: BradWilsonMSFT

Where is the CreateNew attribute?

The only difference between AddNew and Add is that when you call AddNew(), ObjectBuilder decides which constructor to call and calls it. When you call Add(), you the developer have already decided which constructor to call, because you already created it. If you had some extra constructor with a CreateNew on it, it won't get called (since the object already exists).
Mar 2, 2006 at 8:55 AM
originally posted by: aadams99

The dialog (form) is a smart part and has a contstructor. There is a presenter that is accosiated with this smartpart. The CreateNew doesn't get called in this case because i'm only adding and not addingnew to the workitem.

SmartPart
public partial class DepartmentDetailView : Form
{
DepartmentDetailViewPresenter _presenter;

public DepartmentDetailView()
{
InitializeComponent();
}


// This is normally called when I add the View to the workitem by using the Item.AddNew. But in this case it doesn't get called because I'm just doing a Item.Add
CreateNew
public DepartmentDetailViewPresenter Presenter
{
set
{
Guard.ArgumentNotNull(value, "Presenter");

_presenter = value;
_presenter.View = this;
}
get
{
return _presenter;
}
}

// Constructor
public DepartmentDetailView(EmployeeDepartmentDS.EmployeeDepartmentRow employeeDepartmentRow)
{
this.employeeDepartmentRow = employeeDepartmentRow;
this.employeeDepartmentDT = (EmployeeDepartmentDS.EmployeeDepartmentDataTable)employeeDepartmentRow.Table;

InitializeComponent();

employeeDepartmentRowBindingSource.DataSource = employeeDepartmentRow;
}

:
:
:
}
Mar 2, 2006 at 9:48 AM
originally posted by: BradWilsonMSFT

Dependency injection definitely works when using WorkItem.Items.Add(). This simple app demonstrates that it does work as expected. Here is the output from running it:

14 > csc test.cs /r:Microsoft.Practices.CompositeUI.dll /r:Microsoft.Practices.ObjectBuilder.dll

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

15 > .\test.exe

In Class1 Constructor
In Class2 Constructor
In Class1.C2 Setter

And here is the contents of test.cs:

using System;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.ObjectBuilder;

public class Class1
{
public Class1()
{
Console.WriteLine("In Class1 Constructor");
}

CreateNew
public Class2 C2
{
set { Console.WriteLine("In Class1.C2 Setter"); }
}
}

public class Class2
{
public Class2()
{
Console.WriteLine("In Class2 Constructor");
}
}

public class App : CabApplication<WorkItem>
{
STAThread
public static void Main()
{
new App().Run();
}

protected override void Start()
{
Class1 c1 = new Class1();
RootWorkItem.Items.Add(c1);
}
}
Mar 2, 2006 at 10:37 AM
originally posted by: aadams99

Your right. I figured out what was wrong. In the constructor of the smartpart I was using the presenter, well, the presenter is not initialized until I do a Item.Add, which is done after the new -- so I will not be able to use presenter logic at the constructor level.