Skip to content

Instantly share code, notes, and snippets.

@flaviorl-net
Created February 24, 2022 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaviorl-net/d5b2e03586f2e11ee7167b9388f442b8 to your computer and use it in GitHub Desktop.
Save flaviorl-net/d5b2e03586f2e11ee7167b9388f442b8 to your computer and use it in GitHub Desktop.
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