IQueryProvider.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace InABox.Core
  5. {
  6. public interface IQueryProvider
  7. {
  8. bool ExcludeCustomProperties { get; set; }
  9. CoreTable Query(IFilter? filter = null, IColumns? columns = null, ISortOrder? sort = null, CoreRange? range = null);
  10. }
  11. public interface IQueryProvider<T> : IQueryProvider
  12. {
  13. CoreTable Query(Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, CoreRange? range = null);
  14. void Query(Filter<T>? filter, Columns<T>? columns, SortOrder<T>? sort, CoreRange? range, Action<CoreTable?, Exception?> action);
  15. void Save(T entity, string auditNote);
  16. void Save(IEnumerable<T> entities, string auditNote);
  17. void Save(T entity, string auditnote, Action<T, Exception?> callback);
  18. void Save(IEnumerable<T> entities, string auditnote, Action<IEnumerable<T>, Exception?> callback);
  19. void Delete(T entity, string auditNote);
  20. void Delete(IEnumerable<T> entities, string auditNote);
  21. void Delete(T entity, string auditnote, Action<T, Exception?> callback);
  22. void Delete(IEnumerable<T> entities, string auditnote, Action<IList<T>, Exception?> callback);
  23. }
  24. public interface IQueryProviderFactory
  25. {
  26. bool ExcludeCustomProperties { get; }
  27. IQueryProvider Create(Type T);
  28. IQueryProvider<T> Create<T>()
  29. where T : BaseObject, new()
  30. {
  31. var result = (Create(typeof(T)) as IQueryProvider<T>)!;
  32. result.ExcludeCustomProperties = ExcludeCustomProperties;
  33. return result;
  34. }
  35. }
  36. public static class QueryProviderFactoryExtensions
  37. {
  38. public static CoreTable Query<T>(this IQueryProviderFactory factory, Filter<T>? filter = null, Columns<T>? columns = null, SortOrder<T>? sort = null, CoreRange? range = null)
  39. where T : BaseObject, new()
  40. {
  41. return factory.Create<T>().Query(filter, columns, sort, range);
  42. }
  43. public static void Query<T>(this IQueryProviderFactory factory, Filter<T>? filter, Columns<T>? columns, SortOrder<T>? sort, CoreRange? range, Action<CoreTable?, Exception?> action)
  44. where T : BaseObject, new()
  45. {
  46. factory.Create<T>().Query(filter, columns, sort, range, action);
  47. }
  48. public static void Save<T>(this IQueryProviderFactory factory, T entity, string auditNote)
  49. where T : BaseObject, new()
  50. {
  51. factory.Create<T>().Save(entity, auditNote);
  52. }
  53. public static void Save<T>(this IQueryProviderFactory factory, IEnumerable<T> entities, string auditNote)
  54. where T : BaseObject, new()
  55. {
  56. factory.Create<T>().Save(entities, auditNote);
  57. }
  58. public static void Delete<T>(this IQueryProviderFactory factory, T entity, string auditNote)
  59. where T : BaseObject, new()
  60. {
  61. factory.Create<T>().Delete(entity, auditNote);
  62. }
  63. }
  64. }