当前位置 : 主页 > 手机开发 > 其它 >

依赖注入 – 如何设置Ninject DI以创建Hyprlinkr RouteLinker实例

来源:互联网 收集:自由互联 发布时间:2021-06-22
我有一个MVC4 Web API项目,我利用Mark Seemann的 Hyprlinkr组件生成Uris链接资源. (客户 – 地址,例如). 我已经在Dependency injection with Web API上遵循了Mark的指南(对Ninject进行了适当的更改),我无法理
我有一个MVC4 Web API项目,我利用Mark Seemann的 Hyprlinkr组件生成Uris链接资源. (客户 – >地址,例如).

我已经在Dependency injection with Web API上遵循了Mark的指南(对Ninject进行了适当的更改),我无法理解将IResourceLinker注入控制器应该做些什么.

在Mark的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中注册.

我不能像下面那样绑定,因为这会导致多个绑定:

_kernel.Bind<IResourceLinker>()
    .ToMethod(context => new RouteLinker(request))
    .InRequestScope();

我有这样的重新绑定:

_kernel.Rebind<IResourceLinker>()
    .ToMethod(context => new RouteLinker(request))
    .InRequestScope();

但我担心更改ninject绑定图对每个请求都可能是一件坏事.

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

根据Paige Cook的请求更新

我在这里使用重新绑定:

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.其余的绑定是以标准方式进行的,标准是指在使用Ninject.MVC3 nuget包生成的类中.

我的控制器看起来像这样:

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)),
                };
    }
}
注册一个委托函数来为您提供链接器

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

注入代表

readonly Func<HttpRequestMessage, IResourceLinker> _getResourceLinker;

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

    _getResourceLinker = getResourceLinker;
}

用于您的行动

public async Task<Thingy> Get() {

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

}
网友评论