在注入实体类的同时,实体所继承的接口也注入进去了
//注入Db
services.AddSqlSugar(
new
IocConfig()
{
ConnectionString =
"server=.;uid=sa;pwd=haosql;database=qq1"
,
DbType = IocDbType.SqlServer,
IsAutoCloseConnection =
true
});
//注入控制器
services.AddIoc(
this
,
"NETAPI"
, it => it.Name.Contains(
"Controller"
));
//注入带有Test的类
services.AddIoc(
this
,
"BizTest"
, it => it.Name.Contains(
"Test"
));
上面代码只注入了带有Test所有类,那么接口也可以用了
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { IFace interface1; public WeatherForecastController(IFace i1) { this.interface1 = i1; } [HttpGet] public DateTime Get() { var datetime = interface1;.GetDate(); return datetime; } }
原因很简单: Test1继承了IFace,只要注入的类有继承关注就能用
public class Test1:IFace { public int Id { get; set; } }
2016 © donet5.comApache Licence 2.0