-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from tigernetframework/max
#7 Created initial data access layer
- Loading branch information
Showing
34 changed files
with
1,458 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Tigernet.Hosting.DataAccess.Clevers; | ||
using Tigernet.Samples.RestApi.Models; | ||
|
||
namespace Tigernet.Samples.RestApi.Clevers.Interfaces; | ||
|
||
public interface IUserClever : ICleverBase<User, int> | ||
{ | ||
User Add(User user); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
namespace Tigernet.Samples.RestApi.Models | ||
using Tigernet.Hosting.Attributes.Query; | ||
using Tigernet.Hosting.Models.Common; | ||
|
||
namespace Tigernet.Samples.RestApi.Models | ||
{ | ||
public class User | ||
public class User : IEntity<int>, IQueryableEntity | ||
{ | ||
public int Id { get; set; } | ||
|
||
[SearchableProperty] | ||
public string Name { get; set; } | ||
|
||
public int Age { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ public object Ok(object data) | |
return JsonSerializer.Serialize(data); | ||
} | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/Tigernet.Hosting/Attributes/HttpMethods/Commons/HttpMethodAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/Tigernet.Hosting/Attributes/Query/SearchablePropertyAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Tigernet.Hosting.Attributes.Query | ||
{ | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public class SearchablePropertyAttribute : Attribute | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.Linq.Expressions; | ||
using Tigernet.Hosting.Extensions; | ||
using Tigernet.Hosting.Models.Common; | ||
using Tigernet.Hosting.Models.Query; | ||
|
||
namespace Tigernet.Hosting.DataAccess.Clevers; | ||
|
||
/// <summary> | ||
/// Provides base clever implementation | ||
/// </summary> | ||
/// <typeparam name="TEntity">Data type</typeparam> | ||
/// <typeparam name="TKey">Data Id type</typeparam> | ||
public class CleverBase<TEntity, TKey> : ICleverBase<TEntity, TKey> where TEntity : class, IEntity<TKey>, IQueryableEntity where TKey : struct | ||
{ | ||
public IQueryable<TEntity> DataSource { init; get; } | ||
|
||
public CleverBase() | ||
{ | ||
DataSource = Enumerable.Empty<TEntity>().AsQueryable(); | ||
} | ||
|
||
public CleverBase(IQueryable<TEntity> dataSource) | ||
{ | ||
DataSource = dataSource; | ||
} | ||
|
||
public virtual IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> expression) | ||
{ | ||
return DataSource.Where(expression); | ||
} | ||
|
||
public virtual ValueTask<IEnumerable<TEntity>> GetAsync( | ||
Expression<Func<TEntity, bool>> expression, | ||
CancellationToken cancellationToken = default | ||
) | ||
{ | ||
if (expression is null) | ||
throw new ArgumentException("Query expression cannot be null"); | ||
|
||
return new ValueTask<IEnumerable<TEntity>>(DataSource.Where(expression).ToList()); | ||
} | ||
|
||
public virtual ValueTask<IEnumerable<TEntity>> GetAsync(IEntityQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken = default) | ||
{ | ||
if (queryOptions is null) | ||
throw new ArgumentException("Query options cannot be null"); | ||
|
||
return new ValueTask<IEnumerable<TEntity>>(DataSource.Where(x => true).ApplyQuery(queryOptions).ToList()); | ||
} | ||
|
||
public virtual ValueTask<TEntity?> GetFirstAsync(IEntityQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken = default) | ||
{ | ||
if (queryOptions is null) | ||
throw new ArgumentException("Query options cannot be null"); | ||
|
||
queryOptions.AddPagination(1, 1); | ||
return new ValueTask<TEntity?>(DataSource.Where(x => true).ApplyQuery(queryOptions).FirstOrDefault()); | ||
} | ||
|
||
public virtual ValueTask<TEntity?> GetByIdAsync(TKey id, CancellationToken cancellationToken = default) | ||
{ | ||
return new ValueTask<TEntity?>(Task.FromResult(DataSource.FirstOrDefault(x => x.Id.Equals(id)))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Linq.Expressions; | ||
using Tigernet.Hosting.Models.Common; | ||
using Tigernet.Hosting.Models.Query; | ||
|
||
namespace Tigernet.Hosting.DataAccess.Clevers; | ||
|
||
/// <summary> | ||
/// Defines base clever interface | ||
/// </summary> | ||
public interface ICleverBase<TEntity, TKey> where TEntity : class, IEntity<TKey>, IQueryableEntity where TKey : struct | ||
{ | ||
/// <summary> | ||
/// Gets entity set as queryable collection without decryption | ||
/// </summary> | ||
/// <param name="expression">Predicate expression</param> | ||
/// <returns>Queryable collection of entity</returns> | ||
IQueryable<TEntity> Get(Expression<Func<TEntity, bool>> expression); | ||
|
||
/// <summary> | ||
/// Gets entity set after querying | ||
/// </summary> | ||
/// <param name="expression">Predicate expression</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>Set of entities</returns> | ||
ValueTask<IEnumerable<TEntity>> GetAsync(Expression<Func<TEntity, bool>> expression, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets a set of entities by filter | ||
/// </summary> | ||
/// <param name="queryOptions">Complex query options</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>Set of entities after query</returns> | ||
ValueTask<IEnumerable<TEntity>> GetAsync(IEntityQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets a set of entities by filter | ||
/// </summary> | ||
/// <param name="queryOptions">Complex query options</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>Set of entities after query</returns> | ||
ValueTask<TEntity?> GetFirstAsync(IEntityQueryOptions<TEntity> queryOptions, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets entity by Id | ||
/// </summary> | ||
/// <param name="id">Id of entity</param> | ||
/// <param name="cancellationToken">Cancellation token</param> | ||
/// <returns>Entity if found, otherwise null</returns> | ||
ValueTask<TEntity?> GetByIdAsync(TKey id, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Tigernet.Hosting.Models.Common; | ||
|
||
namespace Tigernet.Hosting.Extensions; | ||
|
||
/// <summary> | ||
/// Provides extensions specifically for entities | ||
/// </summary> | ||
public static class EntityExtensions | ||
{ | ||
/// <summary> | ||
/// Checks whether given type is entity | ||
/// </summary> | ||
/// <param name="type">Type to check</param> | ||
/// <returns>True if given type is entity, otherwise false</returns> | ||
/// <returns><see langword="true" /> if given type is entity; <see langword="false" /> otherwise.</returns> | ||
private static bool IsEntity(this Type type) | ||
{ | ||
return type.InheritsOrImplements(typeof(IQueryableEntity)); | ||
} | ||
|
||
/// <summary> | ||
/// Gets direct child entities from a type | ||
/// </summary> | ||
/// <param name="type">Type to get direct child entities</param> | ||
/// <returns>Set of direct child entities</returns> | ||
/// <exception cref="ArgumentException">If type is null</exception> | ||
public static IEnumerable<Type> GetDirectChildEntities(this Type type) | ||
{ | ||
if (type == null) | ||
throw new ArgumentException("Cannot get direct children for null type"); | ||
|
||
if (!type.IsEntity()) | ||
return Enumerable.Empty<Type>(); | ||
|
||
// Get children | ||
var result = type.GetProperties().Where(x => x.PropertyType.IsClass && x.PropertyType.IsEntity()).Select(x => x.PropertyType).ToList(); | ||
|
||
return result.Distinct(); | ||
} | ||
} |
Oops, something went wrong.