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