• Getting Started
    • Prerequisites
    • Download and Installation
    • Change Log
  • Developer Guide
    • EvenCart Plugins
      • Development Environment Setup
      • Plugin Structure
      • EvenCart MVC
      • Dependency Injection
      • Domain Entities
      • Database Versions
      • Plugin Settings
      • Data Provider Plugin
      • Payment Processing Plugin
      • Shipping Provider Plugin
      • Authentication Provider
      • Widget Plugin
    • EvenCart API
      • Authentication
      • Requests & Responses
      • API EndPoints
    • Caching
  • Designer Guide
    • How to create a theme
    • Extended Tags
      • Layout Tag
      • Widget Tag
      • Json Tag
      • Css Tag
      • Js Tag
      • Bundle Tag
      • Partial Tag
      • Control Tag
      • Route Tag
      • Global Tag
      • Component Tag
    • Extended Filters
    • Global Objects
  • Packaging & Distribution

Dependency Injection

The following document describes about the IoC container used by EvenCart and how we can register and resolve the services from within our plugin.

Registering the services

EvenCart uses DryIoc as it's IoC container and exposes an interface EvenCart.Core.Infrastructure.IDependencyContainer. If we wish to register services with the container during application startup, create a class that implements this interface in our plugin project.

For example, if we are writing a payment processing plugin and want our payment processing services to registered and resolved with the container, we should create a class named DependencyContainer.cs and register our services with it's interface within RegisterDependencies method.

 public class DependencyContainer : IDependencyContainer
    {
        public void RegisterDependencies(IRegistrator registrar)
        {
            registrar.Register<ICustomPaymentService, CustomSliderService>(Reuse.ScopedOrSingleton);
            /*
            more registrations
            */
        }

        public int Priority { get; } = 0;
    }

Resolving Services within Controller

The services registered within IDependencyContainer implementation are registered during application startup. The services are automatically resolved when injected within controller.

public class PluginController : FoundationController
{
    private readonly ICustomPaymentService _customPaymentService;
    public PluginController(ICustomPaymentService customPaymentService)
    {
        _customPaymentService = customPaymentService;
        //customPaymentService is resolved by ASP.NET Core automatically
    }
}

Resolving services outside controller

There may be times we may wish to resolve certain services outside controller. EvenCart exposes a static class EvenCart.Core.Infrastructure.DependencyResolver that provides methods to resolve our services.

@using EvenCart.Core.Infrastructure;
...
var paymentService = DependencyResolver.Resolve<ICustomPaymentService>();
...

To know more about scopes and registrations, see the DryIoc documentation.

Next steps

Now that you know about the dependency injection, we can now see creation of domain entities

ON THIS PAGE
  • Registering the services
  • Resolving Services within Controller
  • Resolving services outside controller
  • Next steps

Related Pages

  • Plugin Structure
  • EvenCart MVC
  • Domain Entities
  • Database Versions
  • Plugin Settings
© 2022 Sojatia Infocrafts Pvt. Ltd. All rights reserved. Powered by onlyDoc.