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:

  1. 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 properties Name, JobTitle, and Bio.
  2. 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.
    1. Create a new interface IAuthorsService that has two methods for the List and Details views of the widget.
    2. Create the AuthorsService class which holds the logic of the methods.
  3. 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 of 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.

    To display the widget correctly on the frontend, you need to inject the controller by creating a custom controller factory.

  4. Create a custom controller factory
    When you use the ASP.NET MVC technology, you must inherit the DefaultControllerFactory class. However, you have to inherit the FrontendControllerFactory and create a new private field of type IKernel (part of the Ninject assembly).
  5. Add bindings for the factory
    You need to create a new class similar to the default Sitefinity CMS MVC widgets. The class is called InterfaceMappings and holds the bindings.
  6. Register the custom controller factory in Sitefinity CMS
    To register the controller, you create a new Global.asax global class in your SitefinityWebApp. Then, you must subscribe to the Bootstrapper class Bootstrapped event.
  7. 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.

Additional resources

External links

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

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.

Web Security for Sitefinity Administrators

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.

Foundations of Sitefinity ASP.NET Core Development

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.

Tags

Was this article helpful?