-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
602 additions
and
11 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 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 @@ | ||
# Caching |
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
Empty file.
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,39 @@ | ||
# Source Generator | ||
|
||
The project supports generating a DbDataReader from a class via an attribute. Add the `TableAttribute` to a class to generate the needed extension methods. | ||
|
||
```c# | ||
[Table("Status", Schema = "dbo")] | ||
public class Status | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Description { get; set; } | ||
public int DisplayOrder { get; set; } | ||
public bool IsActive { get; set; } | ||
public DateTimeOffset Created { get; set; } | ||
public string CreatedBy { get; set; } | ||
public DateTimeOffset Updated { get; set; } | ||
public string UpdatedBy { get; set; } | ||
|
||
[ConcurrencyCheck] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Computed)] | ||
[DataFieldConverter(typeof(ConcurrencyTokenHandler))] | ||
public ConcurrencyToken RowVersion { get; set; } | ||
|
||
[NotMapped] | ||
public virtual ICollection<Task> Tasks { get; set; } = new List<Task>(); | ||
} | ||
``` | ||
|
||
Extension methods are generated to materialize data command to entities | ||
|
||
```c# | ||
string email = "[email protected]"; | ||
string sql = "select * from [User] where EmailAddress = @EmailAddress"; | ||
var session = configuration.CreateSession(); | ||
var user = await session | ||
.Sql(sql) | ||
.Parameter("@EmailAddress", email) | ||
.QuerySingleAsync<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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Logging |
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,101 @@ | ||
# Parameters | ||
|
||
## Calling a stored procedure with an out parameter | ||
|
||
```csharp | ||
long total = -1; | ||
var email = "%@battlestar.com"; | ||
|
||
using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
var users = session | ||
.StoredProcedure("[dbo].[UserListByEmailAddress]") | ||
.Parameter("@EmailAddress", email) | ||
.Parameter("@Offset", 0) | ||
.Parameter("@Size", 10) | ||
.Parameter<long>(parameter => parameter | ||
.Name("@Total") | ||
.Type(DbType.Int64) | ||
.Output(v => total = v) | ||
.Direction(ParameterDirection.Output) | ||
) | ||
.Query<User>() | ||
.ToList(); | ||
``` | ||
|
||
## SQL query with a parameter | ||
|
||
```csharp | ||
var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
var email = "[email protected]"; | ||
var sql = "select * from [User] where EmailAddress = @EmailAddress"; | ||
|
||
var user = session.Sql(sql) | ||
.Parameter("@EmailAddress", email) | ||
.QuerySingle(r => new User | ||
{ | ||
Id = r.GetGuid("Id"), | ||
EmailAddress = r.GetString("EmailAddress"), | ||
IsEmailAddressConfirmed = r.GetBoolean("IsEmailAddressConfirmed"), | ||
DisplayName = r.GetString("DisplayName"), | ||
PasswordHash = r.GetString("PasswordHash"), | ||
ResetHash = r.GetString("ResetHash"), | ||
InviteHash = r.GetString("InviteHash"), | ||
AccessFailedCount = r.GetInt32("AccessFailedCount"), | ||
LockoutEnabled = r.GetBoolean("LockoutEnabled"), | ||
LockoutEnd = r.GetDateTimeOffsetNull("LockoutEnd"), | ||
LastLogin = r.GetDateTimeOffsetNull("LastLogin"), | ||
IsDeleted = r.GetBoolean("IsDeleted"), | ||
Created = r.GetDateTimeOffset("Created"), | ||
CreatedBy = r.GetString("CreatedBy"), | ||
Updated = r.GetDateTimeOffset("Updated"), | ||
UpdatedBy = r.GetString("UpdatedBy"), | ||
RowVersion = r.GetBytes("RowVersion"), | ||
}); | ||
``` | ||
|
||
## Executing an Upsert stored procedure | ||
|
||
```csharp | ||
int errorCode = -1; | ||
|
||
var userId = Guid.NewGuid(); | ||
var username = "test." + DateTime.Now.Ticks; | ||
var email = username + "@email.com"; | ||
|
||
using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
var user = session | ||
.StoredProcedure("[dbo].[UserUpsert]") | ||
.Parameter("@Id", userId) | ||
.Parameter("@EmailAddress", email) | ||
.Parameter("@IsEmailAddressConfirmed", true) | ||
.Parameter("@DisplayName", "Unit Test") | ||
.Parameter("@PasswordHash", "T@est" + DateTime.Now.Ticks) | ||
.Parameter<string>("@ResetHash", null) | ||
.Parameter<string>("@InviteHash", null) | ||
.Parameter("@AccessFailedCount", 0) | ||
.Parameter("@LockoutEnabled", false) | ||
.Parameter("@IsDeleted", false) | ||
.Return<int>(p => errorCode = p) | ||
.QuerySingle<User>(); | ||
``` | ||
|
||
## Executing a stored procedure with a return parameter | ||
|
||
```csharp | ||
int result = -1; | ||
long total = -1; | ||
|
||
var email = "[email protected]"; | ||
|
||
using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
result = session | ||
.StoredProcedure("[dbo].[UserCountByEmailAddress]") | ||
.Parameter("@EmailAddress", email) | ||
.Return<long>(p => total = p) | ||
.Execute(); | ||
|
||
``` |
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,124 @@ | ||
# Query Methods | ||
|
||
## Execute or ExecuteAsync | ||
|
||
Executes the command against a connection and returns the number of rows affected. | ||
|
||
```csharp | ||
int result = -1; | ||
long total = -1; | ||
|
||
var email = "[email protected]"; | ||
|
||
using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
result = session | ||
.StoredProcedure("[dbo].[UserCountByEmailAddress]") | ||
.Parameter("@EmailAddress", email) | ||
.Return<long>(p => total = p) | ||
.Execute(); | ||
``` | ||
|
||
|
||
## Query or QueryAsync | ||
|
||
Executes the command against the connection and converts the results to a list of objects. | ||
|
||
```csharp | ||
long total = -1; | ||
var email = "%@battlestar.com"; | ||
|
||
using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
var users = session | ||
.StoredProcedure("[dbo].[UserListByEmailAddress]") | ||
.Parameter("@EmailAddress", email) | ||
.Parameter("@Offset", 0) | ||
.Parameter("@Size", 10) | ||
.Parameter<long>(parameter => parameter | ||
.Name("@Total") | ||
.Type(DbType.Int64) | ||
.Output(v => total = v) | ||
.Direction(ParameterDirection.Output) | ||
) | ||
.Query<User>() // using source generated factor | ||
.ToList(); | ||
``` | ||
|
||
## QuerySingle or QuerySingleAsync | ||
|
||
Executes the query and returns the first row in the result as an object. | ||
|
||
```csharp | ||
var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
var email = "[email protected]"; | ||
var sql = "select * from [User] where EmailAddress = @EmailAddress"; | ||
|
||
var user = session.Sql(sql) | ||
.Parameter("@EmailAddress", email) | ||
.QuerySingle(r => new User | ||
{ | ||
Id = r.GetGuid("Id"), | ||
EmailAddress = r.GetString("EmailAddress"), | ||
IsEmailAddressConfirmed = r.GetBoolean("IsEmailAddressConfirmed"), | ||
DisplayName = r.GetString("DisplayName"), | ||
PasswordHash = r.GetString("PasswordHash"), | ||
ResetHash = r.GetString("ResetHash"), | ||
InviteHash = r.GetString("InviteHash"), | ||
AccessFailedCount = r.GetInt32("AccessFailedCount"), | ||
LockoutEnabled = r.GetBoolean("LockoutEnabled"), | ||
LockoutEnd = r.GetDateTimeOffsetNull("LockoutEnd"), | ||
LastLogin = r.GetDateTimeOffsetNull("LastLogin"), | ||
IsDeleted = r.GetBoolean("IsDeleted"), | ||
Created = r.GetDateTimeOffset("Created"), | ||
CreatedBy = r.GetString("CreatedBy"), | ||
Updated = r.GetDateTimeOffset("Updated"), | ||
UpdatedBy = r.GetString("UpdatedBy"), | ||
RowVersion = r.GetBytes("RowVersion"), | ||
}); | ||
``` | ||
|
||
## QueryValue or QueryValueAsync | ||
|
||
Executes the query and returns the first column of the first row in the result set returned by the query asynchronously. All other columns and rows are ignored. | ||
|
||
```csharp | ||
await using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
string email = "@battlestar.com"; | ||
|
||
var count = await session | ||
.Sql(builder => builder | ||
.Select<User>() | ||
.Count() | ||
.Where(p => p.EmailAddress, email, FilterOperators.Contains) | ||
) | ||
.QueryValueAsync<int>(); | ||
``` | ||
|
||
## QueryMultiple or QueryMultipleAsync | ||
|
||
Executes the command against the connection and sends the results for reading multiple results sets | ||
|
||
```csharp | ||
string email = "[email protected]"; | ||
string sql = "select * from [User] where EmailAddress = @EmailAddress; " + | ||
"select * from [Role]; " + | ||
"select * from [Priority]; "; | ||
|
||
User user = null; | ||
List<Role> roles = null; | ||
List<Priority> priorities = null; | ||
|
||
await using var session = Services.GetRequiredService<IDataSession>(); | ||
|
||
await session.Sql(sql) | ||
.Parameter("@EmailAddress", email) | ||
.QueryMultipleAsync(async q => | ||
{ | ||
user = await q.QuerySingleAsync<User>(); | ||
roles = (await q.QueryAsync<Role>()).ToList(); | ||
priorities = (await q.QueryAsync<Priority>()).ToList(); | ||
}); | ||
``` |
Oops, something went wrong.