From bc52adc484b1001861211e077aa5f0132ce5293c Mon Sep 17 00:00:00 2001 From: Ninja Date: Wed, 25 Sep 2024 00:57:45 +0100 Subject: [PATCH] - Add query interface seggration --- src/Schemio.SQL/BaseSQLChildQuery.cs | 9 +++ src/Schemio.SQL/BaseSQLRootQuery.cs | 9 +++ src/Schemio/BaseQuery.cs | 44 +++++++++----- src/Schemio/IQuery.cs | 58 +++++++------------ src/Schemio/Impl/EventSubscriber.cs | 8 +-- src/Schemio/Impl/QueryBuilder.cs | 4 +- .../Queries/CustomerCommunicationQuery.cs | 4 +- .../Queries/CustomerOrderItemsQuery.cs | 4 +- .../Queries/CustomerOrdersQuery.cs | 4 +- .../EntitySchemas/Queries/CustomerQuery.cs | 5 +- .../Queries/CustomerCommunicationQuery.cs | 9 +-- .../Queries/CustomerOrderItemsQuery.cs | 9 +-- .../Queries/CustomerOrdersQuery.cs | 16 +---- .../EntitySetup/Queries/CustomerQuery.cs | 9 +-- 14 files changed, 90 insertions(+), 102 deletions(-) create mode 100644 src/Schemio.SQL/BaseSQLChildQuery.cs create mode 100644 src/Schemio.SQL/BaseSQLRootQuery.cs diff --git a/src/Schemio.SQL/BaseSQLChildQuery.cs b/src/Schemio.SQL/BaseSQLChildQuery.cs new file mode 100644 index 0000000..8605ab8 --- /dev/null +++ b/src/Schemio.SQL/BaseSQLChildQuery.cs @@ -0,0 +1,9 @@ +namespace Schemio.SQL +{ + public abstract class BaseSQLChildQuery : BaseSQLQuery, ISQLQuery, IChildQuery + where TQueryParameter : IQueryParameter + where TQueryResult : IQueryResult + { + public abstract void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult); + } +} \ No newline at end of file diff --git a/src/Schemio.SQL/BaseSQLRootQuery.cs b/src/Schemio.SQL/BaseSQLRootQuery.cs new file mode 100644 index 0000000..a44f3d9 --- /dev/null +++ b/src/Schemio.SQL/BaseSQLRootQuery.cs @@ -0,0 +1,9 @@ +namespace Schemio.SQL +{ + public abstract class BaseSQLRootQuery : BaseSQLQuery, ISQLQuery, IRootQuery + where TQueryParameter : IQueryParameter + where TQueryResult : IQueryResult + { + public abstract void ResolveRootQueryParameter(IDataContext context); + } +} \ No newline at end of file diff --git a/src/Schemio/BaseQuery.cs b/src/Schemio/BaseQuery.cs index ae25f6f..533737d 100644 --- a/src/Schemio/BaseQuery.cs +++ b/src/Schemio/BaseQuery.cs @@ -33,21 +33,35 @@ public Type ResultType /// public bool IsContextResolved() => QueryParameter != null; - /// - /// Implement this method to resolve query parameter to execute query in child mode to a parent query. - /// - /// DataContext object passed to dataprovider. - /// Query result of parent query. - public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) - { - } + ///// + ///// Implement this method to resolve query parameter to execute query in child mode to a parent query. + ///// + ///// DataContext object passed to dataprovider. + ///// Query result of parent query. + //public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + //{ + //} - /// - /// Implement this method to resolve query parameter to execute query in parent mode. - /// - /// DataContext object passed to dataprovider. - public virtual void ResolveParameterInParentMode(IDataContext context) - { - } + ///// + ///// Implement this method to resolve query parameter to execute query in parent mode. + ///// + ///// DataContext object passed to dataprovider. + //public virtual void ResolveParameterInParentMode(IDataContext context) + //{ + //} } + + //public abstract class BaseRootQuery : BaseQuery, IQuery, IRootQuery + // where TQueryParameter : IQueryParameter + // where TQueryResult : IQueryResult + //{ + // public abstract void ResolveRootQueryParameter(IDataContext context); + //} + + //public abstract class BaseChildQuery : BaseQuery, IQuery, IChildQuery + // where TQueryParameter : IQueryParameter + // where TQueryResult : IQueryResult + //{ + // public abstract void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult); + //} } \ No newline at end of file diff --git a/src/Schemio/IQuery.cs b/src/Schemio/IQuery.cs index 04838b2..0f0afbf 100644 --- a/src/Schemio/IQuery.cs +++ b/src/Schemio/IQuery.cs @@ -3,51 +3,35 @@ namespace Schemio /// /// Implement IQuery to fetch data using API or database. /// + //public interface IQuery + //{ + // List Children { get; set; } + + // Type ResultType { get; } + + // void ResolveParameterInParentMode(IDataContext context); + + // void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult); + + // bool IsContextResolved(); + //} + public interface IQuery { List Children { get; set; } Type ResultType { get; } - void ResolveParameterInParentMode(IDataContext context); - - void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult); - bool IsContextResolved(); } - // public interface IQuery - // { - // List Children { get; set; } - - // Type ResultType { get; } - - // bool IsContextResolved(); - // } - - // public interface IParentQuery : IQuery - // { - // //List Children { get; set; } - - // //Type GetResultType { get; } - - // void ResolveRootQueryParameter(IDataContext context); - - // //void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult); - - // //bool IsContextResolved(); - // } - - // public interface IChildQuery : IQuery - // { - // //List Children { get; set; } - - // //Type GetResultType { get; } - - // //void ResolveRootQueryParameter(IDataContext context); - - // void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult); + public interface IRootQuery : IQuery + { + void ResolveRootQueryParameter(IDataContext context); + } - // //bool IsContextResolved(); - // } + public interface IChildQuery : IQuery + { + void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult); + } } \ No newline at end of file diff --git a/src/Schemio/Impl/EventSubscriber.cs b/src/Schemio/Impl/EventSubscriber.cs index 351c05e..33baf8e 100644 --- a/src/Schemio/Impl/EventSubscriber.cs +++ b/src/Schemio/Impl/EventSubscriber.cs @@ -28,8 +28,8 @@ public void OnEventHandler(IDataContext context, ExecutorResultArgs args) if (unresolved.ParentQueryResultType != result.GetType()) continue; - foreach (var query in unresolved.Queries) - query.ResolveParameterInChildMode(context, queryResult); + foreach (var query in unresolved.Queries.Cast()) + query.ResolveChildQueryParameter(context, queryResult); } } @@ -48,8 +48,8 @@ public void OnEventHandler(IDataContext context, ExecutorResultArgs args) !unresolved.ParentQueryResultType.IsAssignableFrom(result.GetType())) continue; - foreach (var query in unresolved.Queries) - query.ResolveParameterInChildMode(context, queryResult); + foreach (var query in unresolved.Queries.Cast()) + query.ResolveChildQueryParameter(context, queryResult); } } } diff --git a/src/Schemio/Impl/QueryBuilder.cs b/src/Schemio/Impl/QueryBuilder.cs index c1ef5ca..3e674a1 100644 --- a/src/Schemio/Impl/QueryBuilder.cs +++ b/src/Schemio/Impl/QueryBuilder.cs @@ -20,8 +20,8 @@ public IQueryList Build(IDataContext context) { var queries = GetMappedQueries(entitySchema.Mappings.ToList(), context); - foreach (var query in queries.Queries) - query.ResolveParameterInParentMode(context); + foreach (var query in queries.Queries.Cast()) + query.ResolveRootQueryParameter(context); return new QueryList(queries.Queries); } diff --git a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerCommunicationQuery.cs b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerCommunicationQuery.cs index 283e950..208e403 100644 --- a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerCommunicationQuery.cs +++ b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerCommunicationQuery.cs @@ -3,9 +3,9 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries { - internal class CustomerCommunicationQuery : BaseSQLQuery + internal class CustomerCommunicationQuery : BaseSQLChildQuery { - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Execute as child to customer query. var customer = (CustomerResult)parentQueryResult; diff --git a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrderItemsQuery.cs b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrderItemsQuery.cs index 741982a..3f7ac22 100644 --- a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrderItemsQuery.cs +++ b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrderItemsQuery.cs @@ -3,9 +3,9 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries { - internal class CustomerOrderItemsQuery : BaseSQLQuery + internal class CustomerOrderItemsQuery : BaseSQLChildQuery { - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Execute as child to order query. var ordersResult = (OrderResult)parentQueryResult; diff --git a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrdersQuery.cs b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrdersQuery.cs index 6ddcf16..1b6f630 100644 --- a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrdersQuery.cs +++ b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerOrdersQuery.cs @@ -3,9 +3,9 @@ namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries { - internal class CustomerOrdersQuery : BaseSQLQuery + internal class CustomerOrdersQuery : BaseSQLChildQuery { - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Execute as child to customer query. var customer = (CustomerResult)parentQueryResult; diff --git a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerQuery.cs b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerQuery.cs index cf06c9e..5bf6ade 100644 --- a/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerQuery.cs +++ b/tests/Schemio.SQL.Tests/EntitySetup/EntitySchemas/Queries/CustomerQuery.cs @@ -1,11 +1,10 @@ -using System.Data; using Dapper; namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries { - public class CustomerQuery : BaseSQLQuery + public class CustomerQuery : BaseSQLRootQuery { - public override void ResolveParameterInParentMode(IDataContext context) + public override void ResolveRootQueryParameter(IDataContext context) { // Executes as root or level 1 query. var customer = (CustomerContext)context.Entity; diff --git a/tests/Schemio.Tests/EntitySetup/Queries/CustomerCommunicationQuery.cs b/tests/Schemio.Tests/EntitySetup/Queries/CustomerCommunicationQuery.cs index 9a81040..d3b0231 100644 --- a/tests/Schemio.Tests/EntitySetup/Queries/CustomerCommunicationQuery.cs +++ b/tests/Schemio.Tests/EntitySetup/Queries/CustomerCommunicationQuery.cs @@ -1,13 +1,8 @@ namespace Schemio.Tests.EntitySetup.Queries { - internal class CustomerCommunicationQuery : BaseQuery + internal class CustomerCommunicationQuery : BaseQuery, IChildQuery { - public override void ResolveParameterInParentMode(IDataContext context) - { - // Does not execute as root or level 1 queries. - } - - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Execute as child to customer query. var customer = (CustomerResult)parentQueryResult; diff --git a/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrderItemsQuery.cs b/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrderItemsQuery.cs index 738e207..0b06502 100644 --- a/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrderItemsQuery.cs +++ b/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrderItemsQuery.cs @@ -1,13 +1,8 @@ namespace Schemio.Tests.EntitySetup.Queries { - internal class CustomerOrderItemsQuery : BaseQuery> + internal class CustomerOrderItemsQuery : BaseQuery>, IChildQuery { - public override void ResolveParameterInParentMode(IDataContext context) - { - // Does not execute as root or level 1 queries. - } - - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Execute as child to order query. var ordersResult = (OrderCollectionResult)parentQueryResult; diff --git a/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrdersQuery.cs b/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrdersQuery.cs index 13cb6b3..2f762b1 100644 --- a/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrdersQuery.cs +++ b/tests/Schemio.Tests/EntitySetup/Queries/CustomerOrdersQuery.cs @@ -1,20 +1,8 @@ -using Schemio.Tests.EntitySetup; - namespace Schemio.Tests.EntitySetup.Queries { - internal class CustomerOrdersQuery : BaseQuery> + internal class CustomerOrdersQuery : BaseQuery>, IChildQuery { - public override void ResolveParameterInParentMode(IDataContext context) - { - // Executes as root or level 1 query. - var customer = (CustomerContext)context; - QueryParameter = new CustomerParameter - { - CustomerId = customer.CustomerId - }; - } - - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) + public void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult) { // Does not execute as child to any query. } diff --git a/tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs b/tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs index 7dcb28e..5cdda38 100644 --- a/tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs +++ b/tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs @@ -2,9 +2,9 @@ namespace Schemio.Tests.EntitySetup.Queries { - public class CustomerQuery : BaseQuery + public class CustomerQuery : BaseQuery, IRootQuery { - public override void ResolveParameterInParentMode(IDataContext context) + public void ResolveRootQueryParameter(IDataContext context) { // Executes as root or level 1 query. var customer = (CustomerContext)context.Entity; @@ -13,10 +13,5 @@ public override void ResolveParameterInParentMode(IDataContext context) CustomerId = customer.CustomerId }; } - - public override void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult) - { - // Does not execute as child to any query. - } } } \ No newline at end of file