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:
Name
JobTitle
Bio
IAuthorsService
AuthorsService
Ninject
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.
AuthorsController
IAuthorService
To display the widget correctly on the frontend, you need to inject the controller by creating a custom controller factory.
DefaultControllerFactory
FrontendControllerFactory
IKernel
InterfaceMappings
Global.asax
Bootstrapper
Bootstrapped
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.
Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.
This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.
This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important