Skip to content

Commit

Permalink
- Rename IEntityContext to IEntityRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Nov 21, 2024
1 parent 67438ac commit 8104198
Show file tree
Hide file tree
Showing 23 changed files with 42 additions and 55 deletions.
14 changes: 7 additions & 7 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public class CustomerQuery : SQLQuery<CustomerResult>
protected override Func<IDbConnection, Task<CustomerResult>> GetQuery(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
var customer = (CustomerRequest)context.Request;
return connection => connection.QueryFirstOrDefaultAsync<CustomerResult>(new CommandDefinition
(
Expand Down Expand Up @@ -189,7 +189,7 @@ public class CustomerQuery : SQLQuery<CustomerResult>
protected override Func<DbContext, Task<CustomerResult>> GetQuery(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query. parentQueryResult will be null.
var customer = (CustomerContext)context.Entity;
var customer = (CustomerRequest)context.Request;
return async dbContext =>
{
Expand Down Expand Up @@ -251,9 +251,9 @@ public class CustomerWebQuery : WebQuery<CustomerResult>
protected override Func<Uri> GetQuery(IDataContext context, IQueryResult parentApiResult)
{
// Executes as root or level 1 api.
var customerContext = (CustomerContext)context.Entity;
var customerRequest = (CustomerRequest)context.Request;
return () => new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId), UriKind.Absolute);
return () => new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerRequest.CustomerId), UriKind.Absolute);
}
/// <summary>
Expand Down Expand Up @@ -465,10 +465,10 @@ iv. Example registration: Multiple Engines

To use Data provider, Inject `IDataProvider<T>` where T is IEntity, using constructor & property injection method or explicitly Resolve using service provider ie. `IServiceProvider.GetService(typeof(IDataProvider<Customer>))`

##### ii. Call DataProvider.GetData(IEntityContext context) method.
You need to call the `GetData()` method with an instance of parameter class derived from `IEntityContext` interface.
##### ii. Call DataProvider.GetData(IEntityRequest context) method.
You need to call the `GetData()` method with an instance of parameter class derived from `IEntityRequest` interface.

The `IEntityContext` provides a `SchemaPaths` property, which is a list of schema paths to include for the given request to fetch aggregated data.
The `IEntityRequest` provides a `SchemaPaths` property, which is a list of schema paths to include for the given request to fetch aggregated data.
- When `no` paths are passed in the parameter then entire aggregated entity for all configured queries is returned.
- When list of schema paths are included in the request then the returned aggregated data entity only includes query results from included queries.

Expand Down
6 changes: 3 additions & 3 deletions src/Schemio.Core/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ namespace Schemio.Core
{
internal class DataContext : IDataContext
{
public DataContext(IEntityContext entityContext)
public DataContext(IEntityRequest request)
{
Entity = entityContext;
Request = request;
Cache = new Dictionary<string, object>();
}

public Dictionary<string, object> Cache { get; set; }
public IEntityContext Entity { get; set; }
public IEntityRequest Request { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Schemio.Core/IDataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Schemio.Core
{
public interface IDataContext : IEntityContextCache
{
IEntityContext Entity { get; }
IEntityRequest Request { get; }
}
}
2 changes: 1 addition & 1 deletion src/Schemio.Core/IDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Schemio.Core
{
public interface IDataProvider<TEntity> where TEntity : IEntity
{
TEntity GetData(IEntityContext context);
TEntity GetData(IEntityRequest request);
}
}
2 changes: 1 addition & 1 deletion src/Schemio.Core/IEntityContextValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace Schemio.Core
{
public interface IEntityContextValidator
{
public void Validate(IEntityContext context);
public void Validate(IEntityRequest context);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Schemio.Core
{
public interface IEntityContext
public interface IEntityRequest
{
/// <summary>
/// Entity schema paths for data retrieval.
Expand Down
2 changes: 1 addition & 1 deletion src/Schemio.Core/ISchemaPathMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface ISchemaPathMatcher
/// <summary>
/// Determines whether there is a match for given input path vs configured paths for entity's object graph.
/// </summary>
/// <param name="inputPath">Input path from IEntityContext.SchemaPaths</param>
/// <param name="inputPath">Input path from IEntityRequest.SchemaPaths</param>
/// <param name="configuredPaths">Configured paths from EntityConfiguration<TEntity></param>
/// <returns></returns>
bool IsMatch(string inputPath, ISchemaPaths configuredPaths);
Expand Down
17 changes: 2 additions & 15 deletions src/Schemio.Core/Impl/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public DataProvider(
this.entityBuilder = entityBuilder;
}

public TEntity GetData(IEntityContext entityContext)
public TEntity GetData(IEntityRequest request)
{
var context = new DataContext(entityContext);
var context = new DataContext(request);
return GetData(context);
}

Expand All @@ -53,19 +53,6 @@ internal TEntity GetData(IDataContext context)
var watch = System.Diagnostics.Stopwatch.StartNew();
var queries = queryBuilder.Build(context);

//foreach (var item in queries.Queries)
//{
// Console.WriteLine("L1 Query To Execute: " + item.GetType().Name);

// foreach (var item2 in item.Children)
// {
// Console.WriteLine("L2 Query To Execute: " + item2.GetType().Name);

// foreach (var item3 in item2.Children)
// Console.WriteLine("L3 Query To Execute: " + item3.GetType().Name);
// }
//}

watch.Stop();
logger?.LogInformation("Query builder executed in " + watch.ElapsedMilliseconds + " ms");

Expand Down
4 changes: 2 additions & 2 deletions src/Schemio.Core/Impl/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private QueryList GetMappedQueries(IReadOnlyCollection<Mapping<T, IQueryResult>>

private IEnumerable<IQuery> FilterByPaths(IDataContext context, IEnumerable<Mapping<T, IQueryResult>> mappings)
{
var matchedMappings = context.Entity.SchemaPaths != null
? mappings.Where(mapping => context.Entity.SchemaPaths.Any(Path => schemaPathMatcher.IsMatch(Path, mapping.SchemaPaths)))
var matchedMappings = context.Request?.SchemaPaths != null
? mappings.Where(mapping => context.Request.SchemaPaths.Any(Path => schemaPathMatcher.IsMatch(Path, mapping.SchemaPaths)))
.ToList()
: mappings;

Expand Down
6 changes: 3 additions & 3 deletions src/Schemio.Core/XML/XMLDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public XMLDataProvider(IEntityContextValidator entityContextValidator,
/// </summary>
/// <param name="context"></param>
/// <returns>XDocument</returns>
public virtual XDocument GetData(IEntityContext context)
public virtual XDocument GetData(IEntityRequest context)
{
// Log Request context.
LogRequest(context);
Expand Down Expand Up @@ -63,8 +63,8 @@ private XDocument TransformToXmlDocument(T dataSource)
return doc;
}

private void LogRequest(IEntityContext context) => logger.LogInformation(context.GetType().Name);
private void LogRequest(IEntityRequest context) => logger.LogInformation(context.GetType().Name);

private void ValidateRequest(IEntityContext context) => entityContextValidator.Validate(context);
private void ValidateRequest(IEntityRequest context) => entityContextValidator.Validate(context);
}
}
6 changes: 3 additions & 3 deletions tests/Schemio.API.Tests/E2E.Tests/E2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Teardown()
[Test]
public void TestDataProviderToFetchWholeContractWhenNamesAreNull()
{
var customer = dataProvider.GetData(new CustomerContext
var customer = dataProvider.GetData(new CustomerRequest
{
CustomerId = Endpoints.Ids.CustomerId
});
Expand Down Expand Up @@ -80,7 +80,7 @@ public void TestDataProviderToFetchWholeContractWhenNamesAreNull()
[Test]
public void TestDataProviderToFetchPartialCustomerOrdersContractWhenNamesAreIncluded()
{
var customer = dataProvider.GetData(new CustomerContext
var customer = dataProvider.GetData(new CustomerRequest
{
CustomerId = Endpoints.Ids.CustomerId,
SchemaPaths = ["customer.orders.items"]
Expand Down Expand Up @@ -120,7 +120,7 @@ public void TestDataProviderToFetchPartialCustomerOrdersContractWhenNamesAreIncl
[Test]
public void TestDataProviderToFetchPartialCustomerCommunicationContractWhenNamesAreIncluded()
{
var customer = dataProvider.GetData(new CustomerContext
var customer = dataProvider.GetData(new CustomerRequest
{
CustomerId = Endpoints.Ids.CustomerId,
SchemaPaths = ["customer.communication"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Schemio.API.Tests.EntitySetup
{
internal class CustomerContext : IEntityContext
internal class CustomerRequest : IEntityRequest
{
public int CustomerId { get; set; }
public string[] SchemaPaths { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public CustomerWebQuery() : base(Endpoints.BaseAddress)
protected override Func<Uri> GetQuery(IDataContext context, IQueryResult parentApiResult)
{
// Executes as root or level 1 api.
var customerContext = (CustomerContext)context.Entity;
var customerContext = (CustomerRequest)context.Request;

return () => new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId), UriKind.Absolute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override Func<Uri> GetQuery(IDataContext context, IQueryResult parentA
{
// Execute as nested api to order parent api taking OrderResult to resolve api parameter.
var orders = (CollectionResult<OrderResult>)parentApiResult;
var customerContext = (CustomerContext)context.Entity;
var customerContext = (CustomerRequest)context.Request;

return () => new Uri(string.Format(Endpoints.BaseAddress + Endpoints.OrderItems, customerContext.CustomerId, orders.Select(o => o.OrderId).ToCSV()), UriKind.Absolute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public override IEnumerable<Mapping<Customer, IQueryResult>> GetSchema()
}
}

internal class EntityContext : IEntityContext
internal class EntityContext : IEntityRequest
{
public int CustomerId { get; set; }
public string[] SchemaPaths { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CustomerQuery : BaseQuery<CustomerRecord>
public override void ResolveQuery(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
var customer = (CustomerContext)context.Request;
QueryParameter = new
{
customer.CustomerId
Expand Down
2 changes: 1 addition & 1 deletion tests/Schemio.Core.Tests/EntitySetup/CustomerContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Schemio.Core.Tests.EntitySetup
{
internal class CustomerContext : IEntityContext
internal class CustomerContext : IEntityRequest
{
public int CustomerId { get; set; }
public string[] SchemaPaths { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions tests/Schemio.EntityFramework.Tests/E2E.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Setup()
[Test]
public void TestDataProviderToFetchWholeEntityWhenPathsAreNull()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1
});
Expand Down Expand Up @@ -68,7 +68,7 @@ public void TestDataProviderToFetchWholeEntityWhenPathsAreNull()
[Test]
public void TestDataProviderToFetchEntityWhenPathsContainsCommunication()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1,
SchemaPaths = new[] { "Customer/Communication" }
Expand Down Expand Up @@ -102,7 +102,7 @@ public void TestDataProviderToFetchEntityWhenPathsContainsCommunication()
[Test]
public void TestDataProviderToFetchEntityWhenPathsContainsOrderItems()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1,
SchemaPaths = new[] { "Customer/orders/order/items/item" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Schemio.EntityFramework.Tests.EntitySetup
{
internal class CustomerContext : IEntityContext
internal class CustomerRequest : IEntityRequest
{
public int CustomerId { get; set; }
public string[] SchemaPaths { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CustomerQuery : SQLQuery<CustomerRecord>
protected override Func<DbContext, Task<CustomerRecord>> GetQuery(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query. parentQueryResult will be null.
var customer = (CustomerContext)context.Entity;
var customer = (CustomerRequest)context.Request;

return async dbContext =>
{
Expand Down
8 changes: 4 additions & 4 deletions tests/Schemio.SQL.Tests/E2E.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void Setup()
[Test]
public void TestDataProviderToFetchWholeEntityWhenPathsAreNull()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1
});
Expand Down Expand Up @@ -69,7 +69,7 @@ public void TestDataProviderToFetchWholeEntityWhenPathsAreNull()
[Test]
public void TestDataProviderToFetchEntityWhenPathsContainsOrderItems()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1,
SchemaPaths = new[] { "Customer/orders/order/items/item" }
Expand Down Expand Up @@ -105,7 +105,7 @@ public void TestDataProviderToFetchEntityWhenPathsContainsOrderItems()
[Test]
public void TestDataProviderToFetchEntityWhenPathsContainsCommunication()
{
var customer = _provider.GetData(new CustomerContext
var customer = _provider.GetData(new CustomerRequest
{
CustomerId = 1,
SchemaPaths = new[] { "Customer/Communication" }
Expand Down Expand Up @@ -139,7 +139,7 @@ public void TestDataProviderToFetchEntityWhenPathsContainsCommunication()
[Test]
public void TestDataProviderToCacheResultForResultsWithAttributeApplied()
{
var context = new DataContext(new CustomerContext
var context = new DataContext(new CustomerRequest
{
CustomerId = 1
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Schemio.SQL.Tests.EntitySetup
{
internal class CustomerContext : IEntityContext
internal class CustomerRequest : IEntityRequest
{
public int CustomerId { get; set; }
public string[] SchemaPaths { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CustomerQuery : SQLQuery<CustomerRecord>
protected override Func<IDbConnection, Task<CustomerRecord>> GetQuery(IDataContext context, IQueryResult parentQueryResult)
{
// Executes as root or level 1 query.
var customer = (CustomerContext)context.Entity;
var customer = (CustomerRequest)context.Request;

return connection => connection.QueryFirstOrDefaultAsync<CustomerRecord>(new CommandDefinition
(
Expand Down

0 comments on commit 8104198

Please sign in to comment.