Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gambarra/ExpressionExtensionSQL
https://github.com/gambarra/ExpressionExtensionSQL
builder dapper dotnet dotnet-core expression predicate sql
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/gambarra/ExpressionExtensionSQL
- Owner: gambarra
- License: mit
- Created: 2018-12-04T11:49:28.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T08:08:51.000Z (7 months ago)
- Last Synced: 2024-08-03T03:03:39.213Z (5 months ago)
- Topics: builder, dapper, dotnet, dotnet-core, expression, predicate, sql
- Language: C#
- Homepage: https://www.nuget.org/packages/ExpressionExtensionSQL/
- Size: 2.29 MB
- Stars: 40
- Watchers: 4
- Forks: 11
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- Awesome-Nuget-Packages - **ExpressionExtensionSQL**
README
ExpressionExtensionSQL - is a sample lambda expression to sql converter .NET
========================================
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/12a0ce6a259141e5949289ded24f7377)](https://app.codacy.com/app/tgambarra/ExpressionExtensionSQL?utm_source=github.com&utm_medium=referral&utm_content=gambarra/ExpressionExtensionSQL&utm_campaign=Badge_Grade_Dashboard)Packages
--------Nugget feed: https://www.nuget.org/packages/ExpressionExtensionSQL/
| Package | NuGet Stable | NuGet Pre-release | Downloads |
| ------- | ------------ | ----------------- | --------- |
[ExpressionExtensionSQL](https://www.nuget.org/packages/ExpressionExtensionSQL/) | [![ExpressionExtensionSQL](https://img.shields.io/nuget/v/ExpressionExtensionSQL.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL/) | [![ExpressionExtensionSQL](https://img.shields.io/nuget/vpre/ExpressionExtensionSQL.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL/) | [![ExpressionExtensionSQL](https://img.shields.io/nuget/dt/ExpressionExtensionSQL.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL/) |
| [ExpressionExtensionSQL.Dapper](https://www.nuget.org/packages/ExpressionExtensionSQL.Dapper/) | [![ExpressionExtensionSQL.Dapper](https://img.shields.io/nuget/v/ExpressionExtensionSQL.Dapper.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL.Dapper/) | [![ExpressionExtensionSQL.Dapper](https://img.shields.io/nuget/vpre/ExpressionExtensionSQL.Dapper.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL.Dapper/) | [![ExpressionExtensionSQL.Dapper](https://img.shields.io/nuget/dt/ExpressionExtensionSQL.Dapper.svg)](https://www.nuget.org/packages/ExpressionExtensionSQL.Dapper/) |Features
--------
ExpressionExtensionSQL is a [NuGet library](https://www.nuget.org/packages/ExpressionExtensionSQL) which you can add to your project to achieve lambda expression in SQL code. It allows your domain doesn't need to understand SQL language mainly for creating filters. Very suitable for use with [SpecificationPattern](https://en.wikipedia.org/wiki/Specification_pattern).Samples:
```
Example usage:
```
```csharp
Expression> expression = x => x.Name == "merchant1" && x.CreatedAt>=DateTime.Now;
var where = expression.ToSql();
Console.Write(where.Sql);
```
Output will be:
```
([Merchant].[Name] = @1) AND ([Merchant].[CreatedAt]>=@2)
```**where.Parameters** contains a list of parameters that can be used in ADO queries.
In many cases the name of the table or column in the database is different from the name of the entity or property. For these cases this framework allows to create a mapping through attribute or fluent mapping.
## Attribute
Class
```csharp
[TableName("tblOrder")]
public class Order {public int Id { get; set; }
public DateTime CreatedAt { get; set; }
[ColumnName("amount")]
public int TotalAmount { get; set; }
}
```
```
Example usage:
```
```csharp
Expression> expression = x => x.TotalAmount >10 && x.CreatedAt>=DateTime.Now;
var where = expression.ToSql();
Console.Write(where.Sql);
```
Output will be:
```
([tblOrder].[amount] = @1) AND ([tblOrder].[CreatedAt]>=@2)
```### Classes that have inheritance, their superclasses must be defined as abstract. Otherwise, the table name will be equivalent to that of the parent class.
```
Example usage:
```
If the classes are declared like below:
```
public class Entity {
public int Id { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Merchant : Entity {
public string Name { get; set; }
}...
Expression> expression = x => x.Id == 1;
var where = expression.ToSql();
Console.Write(where.Sql);
```
The previous usage example, will be output like this:
```
([Entity].[Id] = @1)
```But, if the **Entity** class was an abstract class, the output would be like this:
```
([Merchant].[Id] = @1)
```## Fluent Mapping
Class```csharp
public class Order {public int Id { get; set; }
public DateTime CreatedAt { get; set; }
public int TotalAmount { get; set; }
}
```
```
Example usage:
```
```csharpConfiguration.GetInstance().Entity().ToTable("tblTeste");
Configuration.GetInstance().Entity().Property(p => p.TotalAmount).ToColumn("valor");
Expression> expression = x => x.TotalAmount >10 && x.CreatedAt>=DateTime.Now;
var where = expression.ToSql();
Console.Write(where.Sql);
```
Output will be:
```
([tblTeste].[valor] = @1) AND ([tblTeste].[CreatedAt]>=@2)
```The **Configuration** class is static and can be used anywhere on your system and the configured mapping is reused for all expressions that contain the mapped entity
ExpressExtensionSQL.Dapper
--------
ExpressionExtensionSQL.Dapper is an dapper extension that allows you to use lambda expressions as filters in your Query.Samples:
```csharppublic IQueryable GetAll(Expression filter) {
var query = $@"SELECT * FROM [Order]
{{where}}"var connection = this.GetConnection();
return connection.Query(query, expression: filter);
}
```
```csharp
var result=OrderRepository.GetAll(p=>p.CreatedAt>=DateTime.Now);
```
In the above example it is important to note the use of {where}. This keyword will tell you where to include the where clause extracted from the expression.