Created
February 24, 2022 22:31
-
-
Save flaviorl-net/d5b2e03586f2e11ee7167b9388f442b8 to your computer and use it in GitHub Desktop.
This file contains 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
public class ContextStrategy<TResponse, TRequest, TSearch> | |
{ | |
private IStrategy<TResponse, TRequest> _strategy; | |
private readonly Dictionary<TSearch, IStrategy<TResponse, TRequest>> _dicStrategy = new Dictionary<TSearch, IStrategy<TResponse, TRequest>>(); | |
public void AddList(TSearch op, IStrategy<TResponse, TRequest> str) | |
{ | |
_dicStrategy.Add(op, str); | |
} | |
public void SetStrategy(TSearch op) | |
{ | |
_strategy = _dicStrategy.FirstOrDefault(x => x.Key.GetHashCode() == op.GetHashCode()).Value; | |
} | |
public TResponse Execute(TRequest request) | |
{ | |
if (_strategy == null) | |
return default(TResponse); | |
var stratg = _strategy.GetType(); | |
var notify = stratg.GetMethod("Execute"); | |
var instance = Activator.CreateInstance(_strategy.GetType()); | |
return (TResponse)notify.Invoke(instance, new object[] { request }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment