dependecy injection

This is one of my favorite patterns out there! Use this if you have a large codebase or multiple consumers of a library.

Use case: You have some code, and multiple Consumers (Webpage, Phone Application & Desktop Application) which want to use the contained Logic (so you do not copy paste the same code three times). Now you want to show progress for a long running operation in the DownloadLargeFileWorkflow. What to do:

  • You create a IProgress interface
  • Each of your Consumers implements this interface
  • You pass the instance which implements this interface to the shared code.

Ready! Now you can use the same code for all three very different consumers.

Now how to implement this?

public interface IProgress
    void ShowProgress();
    void HideProgress();

public class DownloadLargeFileWorkflow
    private IProgress _progress;
    pulic DownloadLargeFileWorkflow(IProgress progress)
        _progress = progress;

    public void Execute()
        //do long running operation

In your consumer specific code you can now construct the DownloadLargeFileWorkflow with your specific implementation of the IProgress interface.


Leave a Reply to mvvm pattern in c# | Techgemechtel Cancel reply

Your email address will not be published. Required fields are marked *