我如何在vNext依赖注入创造构造认购 [英] How do I create Constructor Subscription in vNext Dependency Injection
问题描述
我使用的是一次性vNext应用试验。我试图做的是创建一个名为DataService的',这会返回一些数据,然后创建一个使用DI采取一种IDataService参数作为构造对象的虚拟类。
I am experimenting with a throwaway vNext application. What I'm trying to do is create a dummy class called 'DataService' which will return some data, and then create objects which use DI to take an IDataService parameter as the constructor.
所以,我IDataService / DataService的定义是:
So my IDataService/DataService definitions are:
public interface IDataService
{
DateTime Created { get; }
}
public class DataService : IDataService
{
private DateTime created = DateTime.Now;
public DateTime Created
{
get { return created; }
}
}
在我的启动I类注册此为单:
In my startup class I register this as a singleton:
public void Configure(IApplicationBuilder app)
{
app.UseServices(services =>
{
services.AddSingleton<Data.IDataService, Data.DataService>();
});
而在我创建了一个类,我添加了IDataService接口作为构造依赖性:
And in a class I created, I add the IDataService interface as a constructor dependency:
internal class Constraint : IRouteConstraint
{
private readonly IDataService _dataService;
public Constraint (IDataService dataService)
{
_dataService = dataService;
}
public bool Match(....)
{
//call dataservice for match
}
}
与构造函数依赖这里的类是我想用它来获得在MVC动态路由的IRouteConstraint - 启动类code的最后一部分是这样的:
The class here with the constructor dependency is an IRouteConstraint which I'm trying to use to get dynamic routes in MVC - the final part of the startup class code is this:
app.UseMvc(routes => {
routes.MapRoute(name: "TestRoute", template: "{*constraints}", defaults: new { controller = "Test", action = "Index" }, constraints: new { constraint = new Constraint() }); //<--- how to construct using DI?
});
问题是,因为它缺少构造函数约束()类不能被创建。所有的例子显示使用DI和控制器类的实例通过MVC的处理,以便为AUTOMAGIC我们不会第一眼看到一部分的控制器。
The issue is that the Constraint() class can't be created because it is missing the constructor. All the examples show a Controller using DI and the instantiation of the Controller classes are handled by MVC so that is part of the 'automagic' we don't see at first glance.
所以我的问题是:
- 我如何实例化约束对象,以便DI提供的DataService单的执行?
- 是我的问题,因为我在UseMvc方法配置方法尝试这一点,或者是我的问题比这更根本的?
我想我的思念越来越DI作为一个对象工厂提供我的对象,而不是试图以声明他们创造的某种方式。
I figure I'm missing some way of getting DI to act as an object factory to provide my objects instead of trying to declaratively creating them.
推荐答案
如果你想通过解决依赖注入的约束,你需要注册,第一:
If you're wanting to resolve your Constraint through the Dependency Injection, you'll need to register it, first:
services.AddTransient<Constraint>();
一旦你的IApplicationBuilder,您可以直接访问服务:
Once you have the IApplicationBuilder, you can access the services directly:
app.ApplicationServices.GetRequiredService<Constraint>();
当然,如果你不想要添加约束
键入服务列表中,您仍然可以访问 IDataService
以同样的方式。
Of course, if you don't want to add your Constraint
type to the services list, you can still access the IDataService
the same way.
这里假设你有使用Microsoft.Framework.DependencyInjection;
在你启动的顶部声明;给你的其他的语法,我相信你做的。
This assumes you have using Microsoft.Framework.DependencyInjection;
declared at the top of your Startup; given your other syntax, I believe that you do.
这篇关于我如何在vNext依赖注入创造构造认购的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!