如何推断对象的类型并在构造过程中在泛型参数中使用该类型
是否有另一种方法可以为正在注入的记录器声明我的 ProductController ?
public class ProductController : Controller
{
private readonly LoggingInterface.ILogger<ProductController> _logger;
private readonly IProductRepository _productRepository;
public ProductController(LoggingInterface.ILogger<ProductController> logger, IProductRepository productRepository)
{
_logger = logger;
_productRepository = productRepository;
}
{
谢谢你, 斯蒂芬
Is there another way of declaring my ProductController for the logger that is being injected?
public class ProductController : Controller
{
private readonly LoggingInterface.ILogger<ProductController> _logger;
private readonly IProductRepository _productRepository;
public ProductController(LoggingInterface.ILogger<ProductController> logger, IProductRepository productRepository)
{
_logger = logger;
_productRepository = productRepository;
}
{
Thank you,
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
推理需要使用开放泛型。此样本中没有
Inference requires the use of an open generic. There are none in this sample
如果我没记错的话,你想要做的(希望你能做的)是这样的:
我认为如果你从你的设计中撤回一点,你可以获得一个相当灵活的界面。这里有三个部分——控制器、记录器以及控制器和记录器的对象,我将其称为数据传输对象。因此,您有“产品控制器”和“产品记录器”(您当前称为“产品控制器记录器”)。
假设您有这样的 DTO 对象结构:
现在,为什么不让记录器和控制器都关注 DTO,而不是让记录器关注自身与控制器呢?所以记录器就像:
... 而控制器就像:
你现在拥有的是一个将在任何 DTO 上操作的记录器和一个将在任何 DTO 上操作的控制器,并且该控制器恰好将记录器作为构造函数参数它将在与它相同的 DTO 上运行。现在,如果您愿意,您可以拥有更具体的实现:
而且
,您可以根据需要将它们具体或一般地连接起来:
请注意,我只是即时完成,所以它可能不是最佳的,但我只是想传达总体想法。您不能声明一个具有自身泛型类型的类(据我所知),但您可以让两个对相同泛型类型进行操作的类(控制器和记录器)进行交互。也就是说,IController 可以拥有一个 ILogger,并且当您实例化 IController 时,您可以强制其记录器对相同类型进行操作。
If I'm not mistaken, what you're looking to do (wishing you could do) is something like:
I think that you can get a fairly flexible interface if you pull back a little from your design. You have three parts here -- a controller, a logger, and the object of the controller and logger, which I'll call a data transfer object. So, you have "controller of product" and "logger of product" (which you're currently calling "logger of controller of product").
Let's say that you have this DTO object structure:
Now, instead of the logger concerning itself with controllers, why not have the logger and controller both concern themselves with DTOs? So logger is like:
... and controller is like:
What you have here now is a logger that will operate on any DTO and a controller that will operate on any DTO, and that controller happens to take as a constructor parameter, a logger that will operate on the same DTO that it does. Now, you can have more specific implementations if you want:
and
And, you can wire these up as specifically or generally as you please:
Please note, that I just kind of whipped this up on the fly, so it may not be optimal, but I'm just trying to convey the general idea. You can't declare a class with a generic type of itself (as far as I know), but you can have two classes interact (controller and logger) that operate on the same generic type. That is, IController can own an ILogger and when you instantiate IController, you force its logger to operate on the same type.