Skip to content

Commit

Permalink
refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed May 19, 2024
1 parent 73aab1f commit 1556e3f
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 83 deletions.
5 changes: 1 addition & 4 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/OrderBy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jinget.Core.ExtensionMethods.Expressions;
using Jinget.Core.Utilities.Expressions;

namespace Jinget.Core.ExpressionToSql.Internal;
namespace Jinget.Core.ExpressionToSql.Internal;

/// <summary>
/// Provides the order by functionality used in query handling
Expand Down
2 changes: 0 additions & 2 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/Select.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Jinget.Core.ExtensionMethods.Reflection;

namespace Jinget.Core.ExpressionToSql.Internal;

public class Select<T, R> : Query
Expand Down
2 changes: 0 additions & 2 deletions 01-Core/Jinget.Core/ExpressionToSql/Internal/Where.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Jinget.Core.Exceptions;

#pragma warning disable CS8604 // Possible null reference argument.
namespace Jinget.Core.ExpressionToSql.Internal;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Exceptions;

namespace Jinget.Core.ExtensionMethods.Collections;
namespace Jinget.Core.ExtensionMethods.Collections;

public static class IQueryableExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Jinget.Core.Contracts;
using Jinget.Core.ExtensionMethods.ExpressionToSql;
using Jinget.Core.ExtensionMethods.Collections;
using Jinget.Core.ExtensionMethods.Reflection;

[assembly: InternalsVisibleTo("Jinget.Core.Tests")]
namespace Jinget.Core.ExtensionMethods.Database.SqlClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Exceptions;

namespace Jinget.Core.ExtensionMethods.ExpressionToSql;
namespace Jinget.Core.ExtensionMethods.ExpressionToSql;

public static class PagingExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Utilities.Expressions;

namespace Jinget.Core.ExtensionMethods.Expressions;
namespace Jinget.Core.ExtensionMethods.Expressions;

public static class BooleanExpressionExtensions
{
Expand All @@ -13,8 +11,10 @@ public static class BooleanExpressionExtensions
/// <summary>
/// Visit a boolean expression
/// </summary>
private static (Expression? LeftExpression, Expression? RightExpression) Visit<T>(Expression<Func<T, bool>> leftExpression,
Expression<Func<T, bool>> rightExpression, ParameterExpression parameter)
private static (Expression? LeftExpression, Expression? RightExpression) Visit<T>(
Expression<Func<T, bool>> leftExpression,
Expression<Func<T, bool>> rightExpression,
ParameterExpression parameter)
{
ReplaceExpressionVisitor leftVisitor = new(leftExpression.Parameters[0], parameter);
Expression? left = leftVisitor.Visit(leftExpression.Body);
Expand All @@ -29,31 +29,41 @@ public static class BooleanExpressionExtensions
/// should combine conditions using AND operator. if any of expressions passed as null, then the other expression will be returned
/// </summary>
/// <param name="parameterName">parameter name used in expression. for example in x=>x.Id>0, parameterName is 'x'</param>
public static Expression<Func<T, bool>> AndAlso<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2, string parameterName = "Param_0")
{
if (expr1 is null) return expr2;
if (expr2 is null) return expr1;
var parameter = Expression.Parameter(typeof(T), parameterName);

(Expression? LeftExpression, Expression? RightExpression) = Visit(expr1, expr2, parameter);
#pragma warning disable CS8604 // Possible null reference argument.
return Expression.Lambda<Func<T, bool>>(Expression.AndAlso(LeftExpression, RightExpression), parameter);
#pragma warning restore CS8604 // Possible null reference argument.
}
public static Expression<Func<T, bool>> AndAlso<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2,
string parameterName = "Param_0")
=> expr1.CreateBinaryExpression(expr2, parameterName, ExpressionType.AndAlso);

/// <summary>
/// should combine conditions using OR operator. if any of expressions passed as null, then the other expression will be returned
/// </summary>
/// <param name="parameterName">parameter name used in expression. for example in x=>x.Id>0, parameterName is 'x'</param>
public static Expression<Func<T, bool>> OrElse<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2, string parameterName = "Param_0")
public static Expression<Func<T, bool>> OrElse<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2,
string parameterName = "Param_0")
=> expr1.CreateBinaryExpression(expr2, parameterName, ExpressionType.OrElse);

static Expression<Func<T, bool>> CreateBinaryExpression<T>(
this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2,
string parameterName = "Param_0",
ExpressionType expressionType = ExpressionType.AndAlso)
{
if (expr1 is null) return expr2;
if (expr2 is null) return expr1;
var parameter = Expression.Parameter(typeof(T), parameterName);

(Expression? LeftExpression, Expression? RightExpression) = Visit(expr1, expr2, parameter);
#pragma warning disable CS8604 // Possible null reference argument.
return Expression.Lambda<Func<T, bool>>(Expression.OrElse(LeftExpression, RightExpression), parameter);
#pragma warning restore CS8604 // Possible null reference argument.
BinaryExpression binaryExpr;
if (expressionType == ExpressionType.AndAlso)
binaryExpr = Expression.AndAlso(LeftExpression, RightExpression);
else if (expressionType == ExpressionType.OrElse)
binaryExpr = Expression.OrElse(LeftExpression, RightExpression);
else
throw new Exception("Jinget Says: Only AndAlso and OrElse are supported");

return Expression.Lambda<Func<T, bool>>(binaryExpr, parameter);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Utilities.Expressions;

namespace Jinget.Core.ExtensionMethods.Expressions;
namespace Jinget.Core.ExtensionMethods.Expressions;

public static class ExpressionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Jinget.Core.Utilities.Expressions;

namespace Jinget.Core.ExtensionMethods.Expressions;
namespace Jinget.Core.ExtensionMethods.Expressions;

public static class LambdaExpressionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public static class GenericTypeExtensions
public static bool IsSubclassOfRawGeneric(this Type derivedType, Type parentType)
{
while (derivedType != null && derivedType != typeof(object))

{
var currentType = derivedType.IsGenericType ? derivedType.GetGenericTypeDefinition() : derivedType;
if (parentType == currentType)
Expand Down
23 changes: 9 additions & 14 deletions 01-Core/Jinget.Core/ExtensionMethods/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using Jinget.Core.Exceptions;
using Jinget.Core.ExtensionMethods.Reflection;
using Newtonsoft.Json;

namespace Jinget.Core.ExtensionMethods;
namespace Jinget.Core.ExtensionMethods;

public static class ObjectExtensions
{
Expand Down Expand Up @@ -48,13 +44,13 @@ TypeCode.Double or
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
{
object? value = property.GetValue(source);
if (options.IgnoreNull && value == null)
continue;
else if (options.IgnoreExpressions && value is Expression)
continue;
else if (options.IgnoreExpr2SQLOrderBys && value is List<OrderBy>)
continue;
else if (options.IgnoreExpr2SQLPagings && value is Paging)
var valueType = value.GetType();
if (
(options.IgnoreNull && value == null) ||
(options.IgnoreExpressions && value is Expression) ||
(options.IgnoreExpr2SQLOrderBys && value is List<OrderBy>) ||
(options.IgnoreExpr2SQLOrderBys && value is not null && valueType.IsGenericType && valueType.GenericTypeArguments[0].GetGenericTypeDefinition() == typeof(OrderBy<>)) ||
(options.IgnoreExpr2SQLPagings && value is Paging))
continue;
string key = property.Name;
if (!result.ContainsKey(key))
Expand All @@ -69,8 +65,7 @@ TypeCode.Double or
/// <summary>
/// Get the value of the given property
/// </summary>
public static object? GetValue(this object obj, string propertyName)
=> obj.GetType().GetProperty(propertyName)?.GetValue(obj);
public static object? GetValue(this object obj, string propertyName) => obj.GetType().GetProperty(propertyName)?.GetValue(obj);

/// <summary>
/// Convert two unrelated objects to each other.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jinget.Core.Exceptions;
using Jinget.Core.ExtensionMethods.Expressions;

namespace Jinget.Core.ExtensionMethods.Reflection;
namespace Jinget.Core.ExtensionMethods.Reflection;

public static class TypeExtensions
{
Expand Down
5 changes: 5 additions & 0 deletions 01-Core/Jinget.Core/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@
global using Microsoft.CodeAnalysis.Text;
global using Microsoft.CodeAnalysis;
global using Microsoft.CSharp;
global using Jinget.Core.ExtensionMethods.Expressions;
global using Microsoft.Extensions.DependencyInjection;
global using System.CodeDom.Compiler;
global using System.CodeDom;
global using System.Collections.Generic;
global using Jinget.Core.Utilities.Expressions;
global using System.Collections;
global using System.ComponentModel.DataAnnotations;
global using System.ComponentModel;
global using System.Data;
global using System.IO;
global using System.Linq.Expressions;
global using Jinget.Core.Exceptions;
global using Jinget.Core.ExtensionMethods.Reflection;
global using Newtonsoft.Json;
global using System.Linq;
global using System.Reflection;
global using System.Runtime.CompilerServices;
Expand Down
1 change: 0 additions & 1 deletion 01-Core/Jinget.Core/Security/AES/AESManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Security.Cryptography;
using Jinget.Core.Exceptions;

namespace Jinget.Core.Security.AES;

Expand Down
1 change: 0 additions & 1 deletion 01-Core/Jinget.Core/Security/Hashing/HashManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Security.Cryptography;
using Jinget.Core.Exceptions;
using Jinget.Core.Security.Hashing.Model;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Jinget.Core.Exceptions;
using Jinget.Core.ExtensionMethods.Reflection;
using Jinget.Core.Types;
using Newtonsoft.Json;
using Jinget.Core.Types;
using Jinget.Core.ExtensionMethods.Collections;

namespace Jinget.Core.Utilities.Expressions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;

namespace Jinget.Core.Utilities.Json;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Serialization;

namespace Jinget.Core.Utilities.Json;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using MudBlazor;

namespace Jinget.Blazor.Components.Picker;
namespace Jinget.Blazor.Components.Picker;

public class SelectedDateRangeModel
{
Expand Down
11 changes: 3 additions & 8 deletions Tests/Jinget.Core.Tests/_BaseData/Message.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Jinget.Core.Contracts;
using Jinget.Core.ExpressionToSql.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Jinget.Core.Tests._BaseData
{
Expand All @@ -18,14 +14,13 @@ public GenericRequestSampleMessage()
PageNumber = 1,
PageSize = 10,
};
OrderBy = new List<OrderBy<GenericRequestSampleMessage>>()
{
new()
OrderBy = [
new OrderBy<GenericRequestSampleMessage>()
{
Name = x=>x.Property1,
Direction=Enumerations.OrderByDirection.Descending
}
};
];
}
public string Property1 { get; set; }
public string Property2 { get; set; }
Expand Down
3 changes: 1 addition & 2 deletions Tests/Jinget.Core.Tests/_BaseData/Sample.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System;
using System.Collections.Generic;

namespace Jinget.Core.Tests._BaseData;
Expand Down

0 comments on commit 1556e3f

Please sign in to comment.