Do we need interfaces for dependency injection?

后端 未结 4 479
青春惊慌失措
青春惊慌失措 2020-12-14 06:29

I have an ASP.NET Core application. The application has few helper classes that does some work. Each class has different signature method. I see lot of .net core examples on

4条回答
  •  庸人自扰
    2020-12-14 06:54

    I won't try to cover what others have already mentioned, using interfaces with DI will often be the best option. But it's worth mentioning that using object inheritance at times may provide another useful option. So for example:

    public class Storage
     {
        public virtual Task Download(string file)
        {
        }
     }
    
    
    public class DiskStorage: Storage
     {
        public override Task Download(string file)
        {
        }
     }
    

    and registering it like so:

    services.AddScoped();
    

提交回复
热议问题