使用 Unity IOC 的 Web API - 我的 DBContext 依赖性是如何解决的?
Web API with Unity IOC - How is my DBContext Dependecy resolved?
我需要帮助了解 Unity 和 IOC 的工作原理。
我的 UnityContainer 中有这个
var container = new UnityContainer();
// Register types
container.RegisterType<IService, Service>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
然后在我的 Web API 控制器中,我了解到 IService 是由 Unity 注入的,因为它是一个注册类型。
public class MyController : ApiController
{
private IService _service;
//------- Inject dependency - from Unity 'container.RegisterType'
public MyController(IService service)
{
_service = service;
}
[HttpGet]
public IHttpActionResult Get(int id)
{
var test = _service.GetItemById(id);
return Ok(test);
}
}
我的服务界面
public interface IService
{
Item GetItemById(int id);
}
我的服务实现有自己的构造函数,它接受一个 EntityFramework DBContext 对象。 (EF6)
public class Service : IService
{
private MyDbContext db;
// --- how is this happening!?
public IService(MyDbContext context)
{
// Who is calling this constructor and how is 'context' a newed instance of the DBContext?
db = context;
}
public Item GetItemById(int id)
{
// How is this working and db isn't null?
return db.Items.FirstOrDefault(x => x.EntityId == id);
}
}
它工作的原因是 MyDbContext
有一个无参数的构造函数(或者它有一个包含 unity 可以解析的参数的构造函数),因为默认情况下 unity 可以解析具体类型而无需注册。
引用自this reference:
When you attempt to resolve a non-mapped concrete class that does not have a matching registration in the container, Unity will create an instance of that class and populate any dependencies.
您还需要了解自动布线的概念。
当容器尝试解析 MyController
时,它检测到它需要解析映射到 Service
的 IService
。当容器尝试解析 Service
时,它检测到它需要解析 MyDbContext
。此过程称为自动装配,并递归完成,直到创建整个对象图。
我需要帮助了解 Unity 和 IOC 的工作原理。
我的 UnityContainer 中有这个
var container = new UnityContainer();
// Register types
container.RegisterType<IService, Service>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);
然后在我的 Web API 控制器中,我了解到 IService 是由 Unity 注入的,因为它是一个注册类型。
public class MyController : ApiController
{
private IService _service;
//------- Inject dependency - from Unity 'container.RegisterType'
public MyController(IService service)
{
_service = service;
}
[HttpGet]
public IHttpActionResult Get(int id)
{
var test = _service.GetItemById(id);
return Ok(test);
}
}
我的服务界面
public interface IService
{
Item GetItemById(int id);
}
我的服务实现有自己的构造函数,它接受一个 EntityFramework DBContext 对象。 (EF6)
public class Service : IService
{
private MyDbContext db;
// --- how is this happening!?
public IService(MyDbContext context)
{
// Who is calling this constructor and how is 'context' a newed instance of the DBContext?
db = context;
}
public Item GetItemById(int id)
{
// How is this working and db isn't null?
return db.Items.FirstOrDefault(x => x.EntityId == id);
}
}
它工作的原因是 MyDbContext
有一个无参数的构造函数(或者它有一个包含 unity 可以解析的参数的构造函数),因为默认情况下 unity 可以解析具体类型而无需注册。
引用自this reference:
When you attempt to resolve a non-mapped concrete class that does not have a matching registration in the container, Unity will create an instance of that class and populate any dependencies.
您还需要了解自动布线的概念。
当容器尝试解析 MyController
时,它检测到它需要解析映射到 Service
的 IService
。当容器尝试解析 Service
时,它检测到它需要解析 MyDbContext
。此过程称为自动装配,并递归完成,直到创建整个对象图。