ControllerAttribute, Attributes on Public members

Topics: CAB & Smart Client Software Factory
Jun 24, 2005 at 4:35 PM
originally posted by: ptorrsmith

I notice in the CustomerBrowseView user control, the control's contoller is declared as a public member field:
Controller
public CustomerListController myController;

As a rule, I try not to expose fields as puiblic, but instead create them as private/protected and expose them as public properties.

However when I tried this approach with the ControllerAttribute, I ran into problems:

----- Approach A -----
This failed at compiletime with "Attribute 'Controller' is not valid on this declaration type. It is valid on 'field' declarations only"

private CustomerListController _myController;

Controller
public CustomerListController myController
{
get { return _myController; }
set { _myController = value; }
}


----- Approach B -----
This failed at runtime when my code tried to reference myController (null reference excception), I assume becasue the CAB framework does not know to instantiate _myController.

Controller
private CustomerListController _myController;

public CustomerListController myController
{
get { return _myController; }
set { _myController = value; }
}

...

// Fails here with myController being null
CustomerReference customer = myController.GetNextCustomer();

----- Approach C ------
This also failed at runtime when my code tried to reference _myController (null reference excception), I assume for the same reasons that the CAB framework does not know to instantiate _myController.

Controller
private CustomerListController _myController;

public CustomerListController myController
{
get { return _myController; }
set { _myController = value; }
}

...

// Fails here with _myController being null
CustomerReference customer = _myController.GetNextCustomer();



---------------

Why is this (and perhaps other) Attribute valid on 'field' declarations only? Surely being able to have this attribute (and others) on properties would be preferrable?

I notice the same on State declarations... the following will also fail at runtime with customer and _customer both having a null ref.

State // this seems to need to be a public field
protected Customer _customer;

public Customer customer
{
get { return _customer; }
set { _customer = value; }
}

But this seems to work

protected Customer _customer;

State
public Customer customer
{
get { return _customer; }
set { _customer = value; }
}


Do you have some guidelines as to what attributes can be applied to public properties and what can only be applied to public fields?

Peter
Jun 25, 2005 at 3:27 PM
originally posted by: PProvost

Yeah, we saw this a while back and it is fixed for the next code drop.

Specifically:

1. The ControllerAttribute now works with public or private members
2. The ControllerAttribute now valid on properties and fields

In fact, this is now the behavior of all the dependency injection attributes.

Thanks for the feedback.