Last active
July 29, 2021 07:42
-
-
Save MelbourneDeveloper/b8adb08622b2352e6d303db4794caa96 to your computer and use it in GitHub Desktop.
Null Object Pattern Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #nullable enable | |
| using Microsoft.VisualStudio.TestTools.UnitTesting; | |
| namespace TestProject4 | |
| { | |
| public interface IDependency { void DoSomething(); } | |
| public class NullDependency : IDependency | |
| { | |
| public void DoSomething() { } | |
| } | |
| public class Example | |
| { | |
| readonly IDependency dependency; | |
| public Example(IDependency? dependency = null) | |
| { | |
| this.dependency = dependency ?? new NullDependency(); | |
| } | |
| } | |
| [TestClass] | |
| public class UnitTest1 | |
| { | |
| [TestMethod] | |
| public void TestMethod1() | |
| { | |
| new Example(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment