Skip to content

Commit

Permalink
- pre-release code for v.2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Oct 31, 2024
1 parent 5d03992 commit 5842e63
Show file tree
Hide file tree
Showing 29 changed files with 151 additions and 271 deletions.
108 changes: 51 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ As explained above, You can configure a query in `Parent` or `Child` (nested) mo
i. Parent Query
To define a `parent` or `root` query which is usually configured at level 1 to query the root entity, derive from `aseRootQuery<TQueryParameter, TQueryResult>`
To define a `parent` or `root` query which is usually configured at level 1 to query the root entity, derive from `BaseQuery<TQueryParameter, TQueryResult>`
* `TQueryParameter` is basically the class that holds the `inputs` required by the root query for execution. It is an implementation of `IQueryParameter` type.
* `TQueryResult` is the result that will be returned from executing the root query. It is an implementation of `IQueryResult` type.
Expand All @@ -153,12 +153,12 @@ In `parent` mode, the query parameter is resolved using the `IDataContext` param
> See example `CustomerQuery` implemented to be configured and run in parent mode below.
> ```
>internal class CustomerQuery : BaseRootQuery<CustomerParameter, CustomerResult>
>internal class CustomerQuery : BaseQuery<CustomerParameter, CustomerResult>
> {
> public override void ResolveRootQueryParameter(IDataContext context)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Executes as Parent or Level 1 query.
> // The query parameter is resolved using IDataContext parameter of data provider class.
> // The query parameter is resolved using IDataContext parameter of data provider class. Parent query result will be null.
>
> var customer = (CustomerContext)context;
> QueryParameter = new CustomerParameter
Expand All @@ -171,7 +171,7 @@ In `parent` mode, the query parameter is resolved using the `IDataContext` param
ii. Child Query
To define a `child` or `dependant` query which is usually configured as child at level below the root query to query, derive from `BaseChildQuery<TQueryParameter, TQueryResult>`
To define a `child` or `dependant` query which is usually configured as child at level below the root query to query, derive from `BaseQuery<TQueryParameter, TQueryResult>`
* `TQueryParameter` is basically the class that holds the `inputs` required by the child query for execution. It is an implementation of `IQueryParameter` type.
* `TQueryResult` is the result that will be returned by executing the child query. It is an implementation of `IQueryResult` type.
Expand All @@ -181,9 +181,9 @@ In `child` mode, the query parameter is resolved using the `query result` of the
> See example `CustomerCommunicationQuery` implemented to be configured and run as child or nested query to customer query below. Please see `CustomerSchema` definition above for parent/child configuration setup.
>```
> internal class CustomerCommunicationQuery : BaseChildQuery<CustomerParameter, CommunicationResult>
> internal class CustomerCommunicationQuery : BaseQuery<CustomerParameter, CommunicationResult>
> {
> public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Execute as child to customer query.
> // The result from parent customer query is used to resolve the query parameter of the nested communication query.
Expand All @@ -205,16 +205,16 @@ In `child` mode, the query parameter is resolved using the `query result` of the
- `Schemio.EntityFramework` - provides implementation of IQueryEngine to execute `Entity Framework` queries.
`Query using Schemio.SQL`
The SQL query needs to implement `BaseSQLRootQuery<TQueryParameter, TQueryResult>` or `BaseSQLChildQuery<TQueryParameter, TQueryResult>` based on parent or child implementation.
The SQL query needs to implement `BaseSQLQuery<TQueryParameter, TQueryResult>`.
And, requires implementing `public abstract CommandDefinition GetCommandDefinition()` method to return `command definition` for query to be executed with `Dapper` supported QueryEngine.
See below example `CustomerQuery` implemented as Root SQL query
>```
> internal class CustomerQuery : BaseSQLRootQuery<CustomerParameter, CustomerResult>
> internal class CustomerQuery : BaseSQLQuery<CustomerParameter, CustomerResult>
> {
> public override void ResolveRootQueryParameter(IDataContext context)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Executes as root or level 1 query.
> // Executes as root or level 1 query. parent QueryResult should be null.
> var customer = (CustomerContext)context.Entity;
> QueryParameter = new CustomerParameter
> {
Expand All @@ -237,9 +237,9 @@ See below example `CustomerQuery` implemented as Root SQL query
>
See below example `CustomerOrderItemsQuery` implemented as child SQL query.
>```
>internal class CustomerOrderItemsQuery : BaseSQLChildQuery<OrderItemParameter, OrderItemResult>
>internal class CustomerOrderItemsQuery : BaseSQLQuery<OrderItemParameter, OrderItemResult>
> {
> public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Execute as child query to order query taking OrderResult to resolve query parameter.
> var ordersResult = (OrderResult)parentQueryResult;
Expand All @@ -263,16 +263,16 @@ See below example `CustomerOrderItemsQuery` implemented as child SQL query.
>```
`Query using Schemio.EntityFramework`
The SQL query needs to implement `BaseSQLRootQuery<TQueryParameter, TQueryResult>` or `BaseSQLChildQuery<TQueryParameter, TQueryResult>` based on parent or child implementation.
The SQL query needs to implement `BaseSQLQuery<TQueryParameter, TQueryResult>`.
And, requires implementing `public abstract IEnumerable<IQueryResult> Run(DbContext dbContext)` method to implement query using `DbContext` using entity framework.
See below example `CustomerQuery` implemented as Root Entity framework query
>```
> internal class CustomerQuery : BaseSQLRootQuery<CustomerParameter, CustomerResult>
> internal class CustomerQuery : BaseSQLQuery<CustomerParameter, CustomerResult>
> {
> public override void ResolveRootQueryParameter(IDataContext context)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Executes as root or level 1 query.
> // Executes as root or level 1 query. parent QueryResult shuld be null.
> var customer = (CustomerContext)context.Entity;
> QueryParameter = new CustomerParameter
> {
Expand All @@ -296,9 +296,9 @@ See below example `CustomerQuery` implemented as Root Entity framework query
>
See below example `CustomerOrderItemsQuery` implemented as child Entity framework query.
>```
>internal class CustomerOrderItemsQuery : BaseSQLChildQuery<OrderItemParameter, OrderItemResult>
>internal class CustomerOrderItemsQuery : BaseSQLQuery<OrderItemParameter, OrderItemResult>
> {
> public override void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> protected override void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult)
> {
> // Execute as child query to order query taking OrderResult to resolve query parameter.
> var ordersResult = (CustomerOrderResult)parentQueryResult;
Expand Down Expand Up @@ -394,33 +394,41 @@ var provider = new DataProvider(new CustomerSchema(), Schemio.EntityFramework.Qu
### Using IOC for registrations
With ServiceCollection, you should call the `services.UseSchemio()` method for IoC registration.
#### Registrations
To configure Data provider with SQL Query engine, use fluent registration apis as shown below -
```
services.UseSchemio<Customer>(With.Schema<Customer>(c => new CustomerSchema())
.AddEngine(c => new QueryEngine(new SQLConfiguration { ConnectionSettings = new ConnectionSettings {
Providername = "System.Data.SqlClient",
ConnectionString ="Data Source=Powerstation; Initial Catalog=Customer; Integrated Security=SSPI;"
}}))
.LogWith(c => new Logger<IDataProvider<Customer>>(c.GetService<ILoggerFactory>())));
With ServiceCollection, you need to register the below dependencies.
```
To configure Data provider with Entity Framework Query engine, use fluent registration apis shown as below -
```
services.AddDbContextFactory<CustomerDbContext>(options => options.UseSqlServer(YourSqlConnection), ServiceLifetime.Scoped);

services.AddLogging();

services.UseSchemio<Customer>(With.Schema<Customer>(c => new CustomerSchema())
.AddEngine(c => new QueryEngine<CustomerDbContext>(c.GetService<IDbContextFactory<CustomerDbContext>>()))
.LogWith(c => new Logger<IDataProvider<Customer>>(c.GetService<ILoggerFactory>())));

// Register core services
services.AddTransient(typeof(IQueryBuilder<>), typeof(QueryBuilder<>));
services.AddTransient(typeof(ITransformExecutor<>), typeof(TransformExecutor<>));
services.AddTransient(typeof(IDataProvider<>), typeof(DataProvider<>));
services.AddTransient<IQueryExecutor, QueryExecutor>();

// Register instance of ISchemaPathMatcher - Json, XPath or Custom.
services.AddTransient(c => new XPathMatcher());

// Enable logging
services.AddLogging();

//For Dapper SQL engine.
services.AddTransient<IQueryEngine>(c => new QueryEngine(new SQLConfiguration { ConnectionSettings = new ConnectionSettings {
Providername = "System.Data.SqlClient",
ConnectionString ="Data Source=Powerstation; Initial Catalog=Customer; Integrated Security=SSPI;"
}});

// For entity framework engine.
services.AddDbContextFactory<CustomerDbContext>(options => options.UseSqlServer(YourSqlConnection), ServiceLifetime.Scoped);
services.AddTransient<IQueryEngine>(c => new QueryEngine<CustomerDbContext>(c.GetService<IDbContextFactory<CustomerDbContext>>());

// Register schema definitions. eg CustomerSchema
services.AddTransient<IEntitySchema<Customer>, CustomerSchema>();
```
`Please Note:` You can combine multiple query engines and implement different types of queries to execute on different supported platforms.
To use Data provider, Inject IDataProvider<T> using constructor & property injection method or explicity Resolve using service provider ie. `IServiceProvider.GetService(typeof(IDataProvider<>))`
#### Data Provider (DI)
To use Data provider, Inject IDataProvider<T> where T is IEntity, using constructor & property injection method or explicity Resolve using service provider ie. `IServiceProvider.GetService(typeof(IDataProvider<Customer>))`
## Extend Schemio
### Custom Query Engine
Expand Down Expand Up @@ -483,23 +491,8 @@ public class QueryEngine<T> : IQueryEngine where T : DbContext
For Parent Query base implementation, see example below.
```
public abstract class BaseSQLRootQuery<TQueryParameter, TQueryResult>
: BaseRootQuery<TQueryParameter, TQueryResult>, ISQLQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
/// <summary>
/// Get query delegate with implementation to return query result.
/// Delegate returns a collection from db.
/// </summary>
/// <returns>Func<DbContext, IEnumerable<IQueryResult>></returns>
public abstract IEnumerable<IQueryResult> Run(DbContext dbContext);
}
```
For Child Query implementation, see example below.
```
public abstract class BaseSQLChildQuery<TQueryParameter, TQueryResult>
: BaseChildQuery<TQueryParameter, TQueryResult>, ISQLQuery
public abstract class BaseSQLQuery<TQueryParameter, TQueryResult>
: BaseQuery<TQueryParameter, TQueryResult>, ISQLQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
Expand All @@ -511,6 +504,7 @@ public abstract class BaseSQLChildQuery<TQueryParameter, TQueryResult>
public abstract IEnumerable<IQueryResult> Run(DbContext dbContext);
}
```
### Custom Schema Language
You can provide your own schema language support for use in entity schema definition to map sections of object graph.
Expand Down
17 changes: 0 additions & 17 deletions src/Schemio.EntityFramework/BaseSQLChildQuery.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Schemio.EntityFramework
{
public abstract class BaseSQLRootQuery<TQueryParameter, TQueryResult>
: BaseRootQuery<TQueryParameter, TQueryResult>, ISQLQuery
public abstract class BaseSQLQuery<TQueryParameter, TQueryResult>
: BaseQuery<TQueryParameter, TQueryResult>, ISQLQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
Expand Down
6 changes: 6 additions & 0 deletions src/Schemio.EntityFramework/Schemio.EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 0 additions & 9 deletions src/Schemio.SQL/BaseSQLChildQuery.cs

This file was deleted.

2 changes: 0 additions & 2 deletions src/Schemio.SQL/BaseSQLQuery.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Data;
using Dapper;

namespace Schemio.SQL
{
Expand Down
9 changes: 0 additions & 9 deletions src/Schemio.SQL/BaseSQLRootQuery.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/Schemio.SQL/Schemio.SQL.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup>

<ItemGroup>
Expand Down
19 changes: 0 additions & 19 deletions src/Schemio/BaseChildQuery.cs

This file was deleted.

28 changes: 27 additions & 1 deletion src/Schemio/BaseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Schemio
/// </summary>
/// <typeparam name="TQueryParameter"></typeparam>
/// <typeparam name="TQueryResult"></typeparam>
public abstract class BaseQuery<TQueryParameter, TQueryResult> : IQuery
public abstract class BaseQuery<TQueryParameter, TQueryResult> : IQuery, IChildQuery, IRootQuery
where TQueryParameter : IQueryParameter
where TQueryResult : IQueryResult
{
Expand All @@ -32,5 +32,31 @@ public Type ResultType
/// </summary>
/// <returns></returns>
public bool IsContextResolved() => QueryParameter != null;

/// <summary>
/// Implement to resolve query parameter.
/// </summary>
/// <param name="context">root context.</param>
/// <param name="parentQueryResult">query result from parent query (when configured as nested query). Can be null.</param>
protected abstract void ResolveQueryParameter(IDataContext context, IQueryResult parentQueryResult);

/// <summary>
/// Implement to resolve query parameter for nested queries
/// </summary>
/// <param name="context">root context</param>
/// <param name="parentQueryResult">query result from parent query.</param>
public void ResolveChildQueryParameter(IDataContext context, IQueryResult parentQueryResult)
{
ResolveQueryParameter(context, parentQueryResult);
}

/// <summary>
/// Implement to resolve query parameter for first level queries.
/// </summary>
/// <param name="context">root context</param>
public void ResolveRootQueryParameter(IDataContext context)
{
ResolveQueryParameter(context, null);
}
}
}
18 changes: 0 additions & 18 deletions src/Schemio/BaseRootQuery.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Schemio/IEntitySchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Schemio
/// Implement to configure schema path mappings for an Entity.
/// </summary>
/// <typeparam name="TEntity">Entity type</typeparam>
public interface IEntitySchema<TEntity> where TEntity : IEntity
public interface IEntitySchema<TEntity> : IEntitySchema where TEntity : IEntity
{
/// <summary>
/// Entity schema mappings.
Expand Down
6 changes: 6 additions & 0 deletions src/Schemio/IEntitySchema1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Schemio
{
public interface IEntitySchema
{
}
}
Loading

0 comments on commit 5842e63

Please sign in to comment.