代码拉取完成,页面将自动刷新
Make the default DI container inject fields and properties easily . (based on .netstandard2.0)
PM> Install-Package Autowired.Core
编写服务类,并添加[AppService]特性
[AppService]
public class MyService
{
//functions
}
在Setup的ConfigureServices方法中注册应用服务
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//注册应用服务
services.AddAppServices();
}
其他类中注入使用,比如Controller中
public class HomeController : Controller
{
[Autowired]
MyUserService myUserService;
public HomeController(AutowiredService autowiredService)
{
autowiredService.Autowired(this);
}
}
如果服务的生命周期是Scoped,需要注入IServiceProvider
public class HomeController : Controller
{
[Autowired]
MyUserService myUserService;
public HomeController(AutowiredService autowiredService, IServiceProvider provider )
{
autowiredService.Autowired(this, provider);
}
}
[AppService(ServiceLifetime.Transient)]
public class MyService
{
//functions
}
服务代码:
public interface IService{}
[AppService]
public class MyService:IService
{
//functions
}
注入代码:
[Autowired]
IService myUserService;
public interface IService1{}
public interface IService2{}
[AppService]
public class MyService:IService1,IService2
{
//functions
}
这个时候可以默认注入类型可以是IService1,即第一个接口类型,如果要注入的IService2,可以把IService2放在第一位,也可以如下声明:
[AppService(typeof(IService2))] //对应IService2
[AppService] //对应IService1
public class MyService:IService1,IService2{
//functions
}
public interface IService{}
[AppService("ser1")]
public class Service1:IService{
//service1的实现
}
[AppService("ser2")]
public class Service2:IService{
//service2的实现
}
注入的时候,我们可以根据identifier指定具体哪个实现
[Autowired("ser1")]
IService service1;
[Autowired("ser2")]
IService service2;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。