简体   繁体   English

如何执行依赖注入来配置 c# 中的扩展方法?

[英]How do I perform a dependency injection to configure extension methods in c#?

In c# I want to write extension methods for the DateTime struct to calculate date differences in years, month, etc.在 c# 中,我想为 DateTime 结构编写扩展方法来计算年、月等的日期差异。

For being prepared to change the calculation of the date differences I made an interface for this an passed it to a configuration method in the static extension class.为了准备更改日期差异的计算,我为此创建了一个接口,并将其传递给 static 扩展 class 中的配置方法。

In concrete:具体来说:

public static class DateTimeExtension
{

    private static IDateTimeDifference _dateDifference;


    public static void Configure(IDateTimeDifference dateDifference )
    {
        DateTimeExtension._dateDifference = dateDifference;
    }


    public static int DiffFromInYears(this DateTime dateFromIncl, DateTime dateToExcl)
    {
        return _dateDifference.InYears(dateFromIncl, dateToExcl);
    }

    public static int DiffToInYears(this DateTime dateToExcl, DateTime dateFromIncl)
    {
        return _dateDifference.InYears(dateFromIncl, dateToExcl);
    }

}

My configuration of the services looks like this:我的服务配置如下所示:

var host = Host
            .CreateDefaultBuilder()
            .ConfigureServices(
            (context, services) =>
            {
                services.AddSingleton<IDateTimeDifference>( s => new DateDifferenceFullPeriods() );
            }
            )
            .Build();

DateTimeExtension.Configure( host.Services.GetRequiredService<IDateTimeDifference>() );

I want to get as low performance penalties as possible.我希望获得尽可能低的性能惩罚。

Is there a way to enforce the user of my extension methods to call the Configure method at startup?有没有办法强制我的扩展方法的用户在启动时调用 Configure 方法?

Would this code be considered testable?这段代码会被认为是可测试的吗? How do I test the extension methods?如何测试扩展方法? Is it enough to test the implementations of IDateTimeDifference interface?测试 IDateTimeDifference 接口的实现是否足够?

In general are extension methods a good approach for this problem, considered I don't want to pass a IDateTimeDifference parameter in each difference calculation method.一般来说,扩展方法是解决这个问题的好方法,考虑到我不想在每个差异计算方法中传递 IDateTimeDifference 参数。

Are there other methods of injection more suitable?还有其他更适合的注射方法吗? Is dependency injection even necessary, if I don't need to change the difference calculation at run time?如果我不需要在运行时更改差异计算,是否需要依赖注入?

  1. Do you actually need an extension method for this?你真的需要一个扩展方法吗? Can't you just use the TimeSpan struct https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=net-5.0 to calculate time differences in days, months, years?您不能只使用TimeSpan结构https://docs.microsoft.com/en-us/dotnet/api/system.timespan?view=net-5.0来计算天、月、年的时间差吗?
  2. Extension methods are by their nature static and they are defined in static classes.扩展方法本质上是 static,它们在 static 类中定义。 On the other hand dependency injection is used to register a specific instance of a class and provide this specific instance of a class normally through an interface to other classes or methods that require it.另一方面,依赖注入用于注册 class 的特定实例,并通常通过与其他需要它的类或方法的接口来提供 class 的特定实例。 But since extension methods are defined in static classes and are static you don't need to register an instance of a static class since static classes are loaded automatically when they are required. But since extension methods are defined in static classes and are static you don't need to register an instance of a static class since static classes are loaded automatically when they are required. Generally, I don't see any logic to use dependency injection for extension methods.一般来说,我看不到任何将依赖注入用于扩展方法的逻辑。 Extension methods must rely only on their parameters and the this object.扩展方法必须只依赖于它们的参数和this object。 They should not have any other external dependency.他们不应该有任何其他的外部依赖。
  3. More on defining extension methods for structs you can read here Extension methods on a struct有关为结构定义扩展方法的更多信息,您可以在此处阅读结构上的扩展方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Linq扩展方法如何执行相等比较? - How do C# Linq extension methods perform equality comparison? 我如何在类库中为依赖注入配置StructureMap? - How do I need to configure StructureMap for Dependency Injection in a Class Library? 如何使用.Net Core 2.1配置依赖项注入的DbContext? - How do I configure a DbContext with dependency injection with .Net Core 2.1? 如何使用c#扩展方法扩展类? - How do I extend a class with c# extension methods? 扩展方法如何在C#中工作? - How do extension methods work in C#? uwp 和 c# 和 mvvm 如何做控制反转(依赖注入) - uwp with c# and mvvm how to do inversion of control (dependency injection) 如何在C#中将依赖注入与工作单元和存储库一起使用? (不是基于Web的应用程序) - How do I use Dependency Injection with Unit of Work and Repositories in C#? (Not a web based App) 如何使用参数配置mvc core 2依赖项注入,其中参数之一是依赖项? - How do I configure mvc core 2 dependency injection with parameters, where one of the parameters is a dependency? C#依赖注入 - C# Dependency Injection C#中的依赖注入 - Dependency injection in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM