apieceoffruit

QuerySystem

Sep 7th, 2020
846
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public class CameraQueryCollection : List<CameraQuery>
  2.     {
  3.         public void Add(string name, Func<SmartCamera, bool> query) => this.Add(ToQuery(name, query));
  4.  
  5.         public void Add(string name, Func<IEnumerable<SmartCamera>, IEnumerable<SmartCamera>> filter) =>
  6.             Add(ToQuery(name, filter));
  7.        
  8.         CameraQuery ToQuery(string name,Func<SmartCamera,bool> query) => new ExpressionQuery{ Description = name, Expression = query};
  9.  
  10.         CameraQuery ToQuery(string name, Func<IEnumerable<SmartCamera>, IEnumerable<SmartCamera>> filter) =>
  11.             new FilterQuery { Description = name, Expression = filter};
  12.  
  13.         IEnumerable<SmartCamera> Execute(IEnumerable<SmartCamera> cameras, CameraQuery query) => query.Execute(cameras);
  14.  
  15.         IEnumerable<SmartCamera> Execute(IEnumerable<SmartCamera> cameras, int index) => this[index].Execute(cameras);
  16.     }
  17.    
  18.     [Serializable]
  19.     public abstract class CameraQuery
  20.     {
  21.        
  22.         public string Description;
  23.        
  24.         public abstract IEnumerable<SmartCamera> Execute(IEnumerable<SmartCamera> cameras);
  25.     }
  26.  
  27.     public class ExpressionQuery : CameraQuery
  28.     {
  29.  
  30.         public Func<SmartCamera, bool> Expression;
  31.         public override IEnumerable<SmartCamera> Execute(IEnumerable<SmartCamera> cameras) => cameras.Where(Expression);
  32.     }
  33.  
  34.     public class FilterQuery : CameraQuery
  35.     {  
  36.         public Func<IEnumerable<SmartCamera>,IEnumerable<SmartCamera>> Expression;
  37.  
  38.         public override IEnumerable<SmartCamera> Execute(IEnumerable<SmartCamera> cameras) =>
  39.             Expression.Invoke(cameras);
  40.     }
RAW Paste Data