How to use the Factory Design Pattern with C#

The factory method is a design pattern that can be used in your .NET projects to support dependency injection and therefore testable classes while also keeping your components decoupled from external dependencies.

You can dive deeper into the uses of the design pattern at the Wikipedia article. The video below shows a simple example of creating a testable class with an external dependency that can be mocked. It then shows how to create a factory class to hide the external dependency from a client component.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lee Dale

I am a lead software developer with a keen interest in cybersecurity . Currently studying for a MSc in Advanced Information Security and Digital Forensics.