Skip to content

Commit

Permalink
- Add query interface seggration
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Sep 24, 2024
1 parent 497c761 commit bc52adc
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 102 deletions.
9 changes: 9 additions & 0 deletions src/Schemio.SQL/BaseSQLChildQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Schemio.SQL
{
public abstract class BaseSQLChildQuery<TQueryParameter, TQueryResult> : BaseSQLQuery<TQueryParameter, TQueryResult>, ISQLQuery, IChildQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
public abstract void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult);
}
}
9 changes: 9 additions & 0 deletions src/Schemio.SQL/BaseSQLRootQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Schemio.SQL
{
public abstract class BaseSQLRootQuery<TQueryParameter, TQueryResult> : BaseSQLQuery<TQueryParameter, TQueryResult>, ISQLQuery, IRootQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
public abstract void ResolveRootQueryParameter(IDataContext context);
}
}
44 changes: 29 additions & 15 deletions src/Schemio/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,35 @@ public Type ResultType
/// <returns></returns>
public bool IsContextResolved() => QueryParameter != null;

/// <summary>
/// Implement this method to resolve query parameter to execute query in child mode to a parent query.
/// </summary>
/// <param name="context">DataContext object passed to dataprovider.</param>
/// <param name="parentQueryResult">Query result of parent query.</param>
public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult)
{
}
///// <summary>
///// Implement this method to resolve query parameter to execute query in child mode to a parent query.
///// </summary>
///// <param name="context">DataContext object passed to dataprovider.</param>
///// <param name="parentQueryResult">Query result of parent query.</param>
//public virtual void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult)
//{
//}

/// <summary>
/// Implement this method to resolve query parameter to execute query in parent mode.
/// </summary>
/// <param name="context">DataContext object passed to dataprovider.</param>
public virtual void ResolveParameterInParentMode(IDataContext context)
{
}
///// <summary>
///// Implement this method to resolve query parameter to execute query in parent mode.
///// </summary>
///// <param name="context">DataContext object passed to dataprovider.</param>
//public virtual void ResolveParameterInParentMode(IDataContext context)
//{
//}
}

//public abstract class BaseRootQuery<TQueryParameter, TQueryResult> : BaseQuery<TQueryParameter, TQueryResult>, IQuery, IRootQuery
// where TQueryParameter : IQueryParameter
// where TQueryResult : IQueryResult
//{
// public abstract void ResolveRootQueryParameter(IDataContext context);
//}

//public abstract class BaseChildQuery<TQueryParameter, TQueryResult> : BaseQuery<TQueryParameter, TQueryResult>, IQuery, IChildQuery
// where TQueryParameter : IQueryParameter
// where TQueryResult : IQueryResult
//{
// public abstract void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult);
//}
}
58 changes: 21 additions & 37 deletions src/Schemio/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,35 @@ namespace Schemio
/// <summary>
/// Implement IQuery to fetch data using API or database.
/// </summary>
//public interface IQuery
//{
// List<IQuery> Children { get; set; }

// Type ResultType { get; }

// void ResolveParameterInParentMode(IDataContext context);

// void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult);

// bool IsContextResolved();
//}

public interface IQuery
{
List<IQuery> Children { get; set; }

Type ResultType { get; }

void ResolveParameterInParentMode(IDataContext context);

void ResolveParameterInChildMode(IDataContext context, IQueryResult parentQueryResult);

bool IsContextResolved();
}

// public interface IQuery
// {
// List<IQuery> Children { get; set; }

// Type ResultType { get; }

// bool IsContextResolved();
// }

// public interface IParentQuery : IQuery
// {
// //List<IQuery> Children { get; set; }

// //Type GetResultType { get; }

// void ResolveRootQueryParameter(IDataContext context);

// //void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult);

// //bool IsContextResolved();
// }

// public interface IChildQuery : IQuery
// {
// //List<IQuery> 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);
}
}
8 changes: 4 additions & 4 deletions src/Schemio/Impl/EventSubscriber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IChildQuery>())
query.ResolveChildQueryParameter(context, queryResult);
}
}

Expand All @@ -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<IChildQuery>())
query.ResolveChildQueryParameter(context, queryResult);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Schemio/Impl/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IRootQuery>())
query.ResolveRootQueryParameter(context);

return new QueryList(queries.Queries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerCommunicationQuery : BaseSQLQuery<CustomerParameter, CommunicationResult>
internal class CustomerCommunicationQuery : BaseSQLChildQuery<CustomerParameter, CommunicationResult>
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrderItemsQuery : BaseSQLQuery<OrderItemParameter, OrderItemResult>
internal class CustomerOrderItemsQuery : BaseSQLChildQuery<OrderItemParameter, OrderItemResult>
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
internal class CustomerOrdersQuery : BaseSQLQuery<CustomerParameter, OrderResult>
internal class CustomerOrdersQuery : BaseSQLChildQuery<CustomerParameter, OrderResult>
{
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Data;
using Dapper;

namespace Schemio.SQL.Tests.EntitySetup.EntitySchemas.Queries
{
public class CustomerQuery : BaseSQLQuery<CustomerParameter, CustomerResult>
public class CustomerQuery : BaseSQLRootQuery<CustomerParameter, CustomerResult>
{
public override void ResolveParameterInParentMode(IDataContext context)
public override void ResolveRootQueryParameter(IDataContext context)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerCommunicationQuery : BaseQuery<CustomerParameter, CommunicationResult>
internal class CustomerCommunicationQuery : BaseQuery<CustomerParameter, CommunicationResult>, 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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerOrderItemsQuery : BaseQuery<OrderItemParameter, CollectionResult<OrderItemValue>>
internal class CustomerOrderItemsQuery : BaseQuery<OrderItemParameter, CollectionResult<OrderItemValue>>, 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;
Expand Down
16 changes: 2 additions & 14 deletions tests/Schemio.Tests/EntitySetup/Queries/CustomerOrdersQuery.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
using Schemio.Tests.EntitySetup;

namespace Schemio.Tests.EntitySetup.Queries
{
internal class CustomerOrdersQuery : BaseQuery<CustomerParameter, CollectionResult<OrderValue>>
internal class CustomerOrdersQuery : BaseQuery<CustomerParameter, CollectionResult<OrderValue>>, 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.
}
Expand Down
9 changes: 2 additions & 7 deletions tests/Schemio.Tests/EntitySetup/Queries/CustomerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Schemio.Tests.EntitySetup.Queries
{
public class CustomerQuery : BaseQuery<CustomerParameter, CustomerResult>
public class CustomerQuery : BaseQuery<CustomerParameter, CustomerResult>, IRootQuery
{
public override void ResolveParameterInParentMode(IDataContext context)
public void ResolveRootQueryParameter(IDataContext context)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
Expand All @@ -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.
}
}
}

0 comments on commit bc52adc

Please sign in to comment.