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:
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:
NOTE: The AuthorsController constructor receives an IAuthorService type parameter, which you need to perform service calls inside the class. However, the AuthorsController class 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.
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.
Sign up for our free beginner training. Boost your credentials through advanced courses and certification. Register for Sitefinity training and certification.
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important