Use constructor dependency injections
One of the most common patterns in programming is the Inversion of Control (IoC) design pattern. You can also use this pattern with ASP.NET MVC.
The advantages of using the IoC design pattern are the following:
- Reduces class coupling
- Increases code reusing
- Improves code maintainability
- Improves application testing
In the following article you will learn how to use the Dependency Injection over the constructor of the controller. You will create a custom MVC widget for the Author dynamic type. You can download the whole Constructor injection sample from the GitHub repository and follow the procedure below.
Perform the following:
-
Implement the View model
View models hold the presentation layer and are pure containers for properties. View models do not hold any business logic. The code sample below demonstrates how to create a View model for an author with propertiesName,JobTitle, andBio. -
Implement the service layers The service layer holds the domain model logic and works with the Sitefinity CMS managers. The services may also have business logic depending on the level of the separation of concerns you want to achieve.
- Create a new interface
IAuthorsServicethat has two methods for the List and Details views of the widget. - Create the
AuthorsServiceclass which holds the logic of the methods.
- Create a new interface
-
Implement the controller
Services need to be included in the controller to separate the logic from the data access. Thus, the services create a new dependence in the controller constructor, which is resolved using the Dependency Injection and the help ofNinject.NOTE: The
AuthorsControllerconstructor receives anIAuthorServicetype parameter, which you need to perform service calls inside the class. However, theAuthorsControllerclass does not implement the default constructor (a constructor with no parameters) that any controller must have to work with ASP.NET MVC. Therefore, if you drag a widget implemented without a default constructor on a Sitefinity CMS page and view it on the frontend, an error similar to the following will be displayed: No paremeterless constructor defined for this object.To display the widget correctly on the frontend, you need to inject the controller by creating a custom controller factory.
-
Create a custom controller factory
When you use the ASP.NET MVC technology, you must inherit theDefaultControllerFactoryclass. However, you have to inherit theFrontendControllerFactoryand create a new private field of typeIKernel(part of theNinjectassembly). -
Add bindings for the factory
You need to create a new class similar to the default Sitefinity CMS MVC widgets. The class is calledInterfaceMappingsand holds the bindings. -
Register the custom controller factory in Sitefinity CMS
To register the controller, you create a newGlobal.asaxglobal class in your SitefinityWebApp. Then, you must subscribe to theBootstrapperclassBootstrappedevent. -
Restart your application.
RESULT: As a result, you add a dependency injection for the controller of the custom widget. Thus, you can continue working on the actions of the controller and implementing your views.