XAF0004: Implement XAF controller constructors correctly
- 2 minutes to read
Severity: Warning
XAF has the following requirements for Controllers:
- The Controller class and its constructor should be public if the constructor has no parameters. Otherwise, XAF does not treat this class as a Controller and does not load it.
- If the Controller class or its constructor is not public or has parameters, you should create the Controller explicitly and add it to the ShowViewParameters.Controllers collection. Refer to the following topic for more information: How to refresh data in the View after the Open Related Record action is executed.
Limitations
This diagnostic works only for classes derived from the DevExpress.ExpressApp.Controller class.
This diagnostic does not work for abstract classes and constructors that have ActivatorUtilitiesConstructorAttribute.
Examples
Invalid Code
using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl;
namespace MySolution.Module.Controllers{
public class ExampleClass1 : Controller {
// The constructor must be public
ExampleClass1 () {} // Warning: XAF0004
}
public class ExampleClass2 : Controller {
public ExampleClass2 (IXafApplicationProvider appProvider){} // Warning: XAF0004
}
// ...
}
Valid Code
using DevExpress.ExpressApp;
using DevExpress.Persistent.BaseImpl;
namespace MySolution.Module.Controllers {
public class ExampleClass1 : Controller {
// This constructor meets the requirements
public ExampleClass1() {}
}
// Even though the Controller class and its constructor are not public, this implementation meets the requirements
class ExampleClass2 : Controller {
internal ExampleClass2(string theParameter) { }
}
// ...
ShowViewParameters showViewParameters = /*...*/;
showViewParameters.Controllers.Add(new ExampleClass2("MyParameter"));
// ...
}