Implementation of static dependency injection methods

In this old code that I am trying to update, they implemented dependency injection as follows:

public class Program
{
    private IProgramRepository programRepository;

     public Program(IProgramRepository repository)
        {
             this.programRepository = repository;
        }

     public Program() : this(new EN_Program()) { }

now in this program class all methods are static, so all static methods actually have 2 methods:

    public static List<Program> GetProgramsByUser(int userId)
    {
        return GetProgramsByUser(userId, GetDefaultRepository());
    }
    private static List<Program> GetProgramsByUser(int userId, IProgramRepository repo)
    {
        return repo.GetProgramsByUser(userId);
    }

Now I read this, among other things, about the DI implementation:

. . , " , . ". Product.cs EN_Program. IProgramRepository (EN_Program). IProgramRepository, Product.cs . , , .

, DI, - (Program.cs), .

, ? DI, ?

. , (Program.cs), Program.cs ,

+5
2

. Injection Dependency, . , Anti-pattern Injection Injection.

Injection Dependency, . - , .

, , - . :

  • , .
  • , , ( ). , , EN_Program .
+7

factory (). factory IProgramRepository. , factory.

public static List<Program> GetProgramsByUser(int userId)
{
    return GetProgramsByUser(userId, GetDefaultRepository());
}

. "".

private static List<Program> GetProgramsByUser(int userId, IProgramRepository repo)
{
    return repo.GetProgramsByUser(userId);
}

(DI) (IoC), . Perhapse DI, IoC !

+1

All Articles