I have a class that needs a way to get the value of a random integer with a maximum value. I do not want this class to depend on a specific way to get this random value (e.g. system.random). Would be better:
(A) Use a public delegate (or func)
public delegate int NextRandomInt(int maxValue); public class MyClass { public NextRandomInt NextRandomInt { get; set; } public MyClass(NextRandomInt nextRandomInt) { NextRandomInt = nextRandomInt; } }
(B) Use the public interface
public interface IRandomIntProvider { int NextInt(int maxValue); } public class MyClass { public IRandomIntProvider RandomIntProvider { get; set; } public MyClass(IRandomIntProvider randomIntProvider) { RandomIntProvider = randomIntProvider; } }
(C) Something else together.
Both methods work. I feel that using a delegate will be easier and faster to implement, but the interface is more readable and might be easier when dependency injection happens.
source share