小编Ibr*_*tty的帖子

ASP.NET Core Web API 中的属性和方法注入

如何实现属性和方法依赖注入?如何添加服务?最后,与使用构造函数注入相比,这能产生多大的影响?

是否可以使用属性/方法注入代替构造函数注入?

.Net 初学者,我们将不胜感激<3

在我使用构造函数 DI 的包装类中:

private ICustomerRepository _customerRepository; //For customer
private ICountryRepository _countryRepository; //For country
private IRegionRepository _regionRepository; //for region
private ICityRepository _cityRepository; //for city

// Constructor 
public RepositoryWrapper(OmniConnectDB context, ICustomerRepository customerRepository, IMapper mapper, ICountryRepository countryRepository, IRegionRepository regionRepository, ICityRepository cityRepository)
{
    _context = context;
    _mapper = mapper;

    _customerRepository = customerRepository;
    _countryRepository = countryRepository;
    _regionRepository = regionRepository;
    _cityRepository = cityRepository;   
}
Run Code Online (Sandbox Code Playgroud)

在服务方面:

// configure DI for Location Repositories
services.AddScoped<ICountryRepository, CountryRepository>();
services.AddScoped<IRegionRepository, RegionRepository>();
services.AddScoped<ICityRepository, CityRepository>();

// Configure DI for Customer …
Run Code Online (Sandbox Code Playgroud)

c# .net-core asp.net-core-webapi

4
推荐指数
1
解决办法
1686
查看次数

标签 统计

.net-core ×1

asp.net-core-webapi ×1

c# ×1