简体   繁体   中英

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.

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.

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?

Would this code be considered testable? How do I test the extension methods? Is it enough to test the implementations of IDateTimeDifference interface?

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.

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?
  2. Extension methods are by their nature static and they are defined in static classes. 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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM