gpt4 book ai didi

dependency-injection - 如何设置 Ninject DI 以创建 Hyprlinkr RouteLinker 实例

转载 作者:行者123 更新时间:2023-12-04 17:02:18 24 4
gpt4 key购买 nike

我有一个 MVC4 Web API 项目,我正在使用 Mark Seemann 的 Hyprlinkr生成链接资源的 Uris 组件。 (例如客户 -> 地址)。

我已经在 Dependency injection with Web API 上遵循了 Mark 的指南。 (为 Ninject 进行适当更改)位我不太清楚应该怎么做才能注入(inject) IResourceLinker进入我的 Controller 。

关注马克的guide我的IHttpControllerActivator.Create创建方法如下所示:

IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = (IHttpController) _kernel.GetService(controllerType);

request.RegisterForDispose(new Release(() => _kernel.Release(controller)));

return controller;
}

正是在这种方法中, Hyprlinkr readme建议创建 RouteLinker .不幸的是,我不确定如何使用 Ninject 注册它。

我不能像下面这样绑定(bind),因为这会导致多个绑定(bind):
_kernel.Bind<IResourceLinker>()
.ToMethod(context => new RouteLinker(request))
.InRequestScope();

我有这样的重新绑定(bind)工作:
_kernel.Rebind<IResourceLinker>()
.ToMethod(context => new RouteLinker(request))
.InRequestScope();

但我担心更改 ninject 绑定(bind)图对于每个请求都可能是一件坏事。

实现这一目标的最佳方法是什么?

根据 Paige Cook 的请求进行更新

我在这里使用重新绑定(bind):
IHttpController IHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
_kernel.Rebind<IResourceLinker>()
.ToMethod(context => new RouteLinker(request))
.InRequestScope();

var controller = (IHttpController) _kernel.GetService(controllerType);

request.RegisterForDispose(new Release(() => _kernel.Release(controller)));

return controller;
}

IHttpControllerActivator.Create每个请求都会调用。其余的绑定(bind)以标准方式进行,标准我的意思是在使用 Ninject.MVC3 生成的类中。 nuget 包。

我的 Controller 如下所示:
public class CustomerController : ApiController
{
private readonly ICustomerService _customerService;
private readonly IResourceLinker _linker;

public CustomerController(ICustomerService customerService, IResourceLinker linker)
{
_customerService = customerService;
_linker = linker;
}

public CustomerModel GetCustomer(string id)
{
Customer customer = _customerService.GetCustomer(id);
if (customer == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}

return
new CustomerModel
{
UserName = customer.UserName,
Firstname = customer.Firstname,
DefaultAddress = _linker.GetUri<AddressController>(c => c.Get(customer.DefaultAddressId)),
};
}
}

最佳答案

注册一个委托(delegate)函数给你链接器

_kernel.Bind<Func<HttpRequestMessage, IResourceLinker>>()
.ToMethod(context => (request) => new RouteLinker(request));

注入(inject)委托(delegate)
readonly Func<HttpRequestMessage, IResourceLinker> _getResourceLinker;

public controller(Func<HttpRequestMessage, IResourceLinker> getResourceLinker) {

_getResourceLinker = getResourceLinker;
}

在你的行动中使用
public async Task<Thingy> Get() {

var linker = _getResourceLinker(Request);
linker.GetUri( ... )

}

关于dependency-injection - 如何设置 Ninject DI 以创建 Hyprlinkr RouteLinker 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12895302/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com