英文:
What is the point of the ILoggingBuilder interface in ASP.NET Core?
问题
I looked inside the the built in WebApplicationBuilder
class and noticed the
public ILoggingBuilder Logging { get; }
property. Here is the entire ILoggingBuilder
interface:
public interface ILoggingBuilder
{
IServiceCollection Services { get; }
}
It just stores a single property so what is the point of this interface? Couldn't the WebApplicationBuilder
just store an instance of IServiceCollection
directly?
英文:
I looked inside the the built in WebApplicationBuilder
class and noticed the
public ILoggingBuilder Logging { get; }
property. Here is the entire ILoggingBuilder
interface:
public interface ILoggingBuilder
{
IServiceCollection Services { get; }
}
It just stores a single property so what is the point of this interface? Couldn't the WebApplicationBuilder
just store an instance of IServiceCollection
directly?
答案1
得分: 2
关于作用域和减少智能感知混乱的问题。如果所有东西都在 IServiceCollection 上,它会很快变得混乱。高度可配置且本身可扩展的子系统需要一个“目标”来进行扩展。这是平台用来解决这个问题的模式之一。
你将看到一些模式:
通过回调实现简单选项:
services.AddComponent((ComponentOptions options) =>
{
options.SomeFlag = true;
});
当组件上的选项数量较少,是自包含的,并且可扩展性有限时,这种模式效果很好。
从 API 返回复杂的构建器:
IComponentBuilder builder = services.AddComponent();
builder.AddExtensibleThing1();
builder.AddExtensibleThing2();
打破了流畅的 API 模式,但减少了嵌套的 Lambda 表达式。
或者
services.AddComponent((IComponentBuilder builder) =>
{
builder.AddExtensibleThing1();
builder.AddExtensibleThing2();
});
与上面类似,但是嵌套而不是返回值。以更多的回调为代价保持了流畅的 API 工作。
英文:
It's about scoping and reducing intellisense hell. If everything was on IServiceCollection it would get cluttered quickly. Subsystems that are highly configurable and are themselves extensible need a "target" to extend. This is one of the patterns the platform employs to solve this problem.
Some of the patterns you'll see:
Simple options via a callback:
services.AddComponent((ComponentOptions options) =>
{
options.SomeFlag = true;
});
This pattern works well when the number of options on component is small, self-contained and has limited extensibility.
Complex builder returned from the API:
IComponentBuilder builder = services.AddComponent();
builder.AddExtensibleThing1();
builder.AddExtensibleThing2();
Breaks the fluent API pattern but reduces nested lambdas.
OR
services.AddComponent((IComponentBuilder builder) =>
{
builder.AddExtensibleThing1();
builder.AddExtensibleThing2();
});
Similar to the above but nested instead of a return value. Keeps the fluent API working at the cost of more callbacks.
答案2
得分: 1
我查看了ILoggingBuilder的文档,它指出:
> 用于配置日志提供程序的接口。
而且,它只有一个属性,但为ILoggingBuilder
编写了许多扩展方法,因此,该接口确实用于配置日志提供程序。
英文:
I looked at the documentation for the ILoggingBuilder, and it states:
> An interface for configuring logging providers.
And yes, it has only one property, but many extension methods are written for ILoggingBuilder
, so indeed, this interface is for configuring logging providers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论