简体   繁体   中英

Dependency Injection pass parameters by constructor

We have a project where we need to use DI and ASP Core. I'm very new to this and have a question.

I have a controller named HomeController like this:

public class HomeController : BaseController {

    private IOrderService _orderService;

    public HomeController(IOrderService orderService) {
        _orderService = orderService;
    }

    public IActionResult Index() {
         var orders = _orderService.GetMyOrders();
         return View(orders);
    }

}

The code looks like this:

public class OrderService : BaseService, IOrderService {

    public OrderService(IDataContextService dataContextService) {
        _dataContextService = dataContextService;
    }

    public List<Orders> GetMyOrders() {

        var orders = // do my code here which works fine!;

        // here i need some code do check orders for delivery so
        DeliveryService deliveryService = new DeliveryService(_dataContextService);

        // update my orders and return these orders


        return orders;

    }

}

public class DeliveryService : BaseService, IDeliveryService {

    public DeliveryService(IDataContextService dataContextService) {
        _dataContextService = dataContextService;
    }

    public void MyMethod() {

    }

}

public class BaseService {
    protected IDataContextService _dataContextService;
}

Almost all my services have a constructor like the OrderService and DeliveryService. My question is, do I have to pass the _dataContextService every time, or is there a solution within the dependency pattern?

If I understand correctly what you are asking, you are looking for an IoC container. .NET Core has built in support for dependency injection. Basically, you just indicate which implementation should be provided when an interface is requested. Then the container will instantiate the types for you. See for example https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/dependency-injection .

Hope that helps

You should keep it the way you have it and asp.net core IoC will inject it for you, but make sure it is injected per request, this will help to insantiate only one context for each request and dispose it after the request is served.

You can register the context and services in the ConfigureServices method inside the Startup class as below

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.


    // Add application services.
    services.AddTransient<HomeController>();
    services.AddTransient<IOrderService , OrderService >();
    services.AddTransient<IDeliveryService, DeliveryService>();
    services.AddScoped<IDataContextService   , YourDataContextService  >();
}

The AddScoped method will create only one instance of the object for each HTTP request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM