CommQ.Data
1.0.1-beta.3
See the version list below for details.
dotnet add package CommQ.Data --version 1.0.1-beta.3
NuGet\Install-Package CommQ.Data -Version 1.0.1-beta.3
<PackageReference Include="CommQ.Data" Version="1.0.1-beta.3" />
paket add CommQ.Data --version 1.0.1-beta.3
#r "nuget: CommQ.Data, 1.0.1-beta.3"
// Install CommQ.Data as a Cake Addin #addin nuget:?package=CommQ.Data&version=1.0.1-beta.3&prerelease // Install CommQ.Data as a Cake Tool #tool nuget:?package=CommQ.Data&version=1.0.1-beta.3&prerelease
CommQ.Data
A Unit of Work implementation. Uses types from System.Data. Includes some helper classes to make database access more convenient.
Not an ORM.
Creating a unit of work
var factory = new UnitOfWorkFactory(connectionString);
await using var uow = await factory.CreateAsync();
A database transaction is started upon creation of the unit of work. The transaction isolation can be controlled by using the appropriate override.
await using var uow = await factory.CreateAsync(IsolationLevel.Serializable);
Using the unit of work
var dbWriter = uow.CreateWriter(); // IDbWriter
// ... write to the database using dbWriter
await uow.SaveChangesAsync();
If SaveChangesAsync
is not called before the scope of the using statement ends, the transaction will be rolled back.
Using IDbWriter
Use IDbWriter to execute queries that modify data. Getting an instance of IDbWriter requires an IUnitOfWork.
await dbWriter.CommandAsync("DELETE FROM dbo.MyTable");
Include parameters by including the second argument.
await dbWriter.CommandAsync("UPDATE dbo.MyTable SET Name = @Name WHERE Id = @Id", parameters =>
{
parameters.Add("@Id", SqlDbType.Int).Value = 1;
parameters.Add("@Name", SqlDbType.VarChar, 200).Value = "John";
});
IDbReader
Use IDbReader
to execute queries that do not modify data. It can be used with IUnitOfWork
to read data within the scope of the transaction, or can be used standalone without a unit of work.
Creating a standalone IDbReader
var factory = new DbReaderFactory(connectionString);
await using var dbReader = await factory.CreateAsync();
Creating an IDbReader
using a unit of work
var dbReader = uow.CreateReader();
Reading data using IDbReader
To read scalar values from the database:
var count = await dbReader.ScalarAsync<int>("SELECT COUNT(*) FROM dbo.MyTable");
To read arbitrary data using IDataReader
IDataReader reader = await dbReader.RawAsync("SELECT u.Name, a.City FROM dbo.Users u JOIN dbo.Address a on a.UserId = u.Id WHERE u.Id = @UserId", parameters =>
{
parameters.Add("@UserId", SqlDbType.Int).Value = 1;
})
For convenience, IDataReader
has methods to read classes that implement IDbReadable
and have parameterless constructors.
// Can be some type that maps to a table or is a result of a query
public class User : IDbReadable<User>
{
public int Id { get; set; }
public string Name { get; set; }
public User Read(IDataReader reader)
{
Id = reader["Id"] as int? ?? throw new InvalidCastException("Id was null");
Name = reader["Name"] as string ?? throw new InvalidCastException("Name was null");
return this;
}
}
IDbReader
can then be used as follows to retrieve objects.
User user = await dbReader.SingleAsync<User>("SELECT * FROM dbo.Users WHERE Id = 2");
IEnumerable<User> users = await dbReader.EnumerableAsync<User>("SELECT * FROM dbo.Users");
Dependency Injection
A singleton IUnitOfWorkFactory
can be added to the DI container.
builder.Services.AddSingleton<IUnitOfWorkFactory>(_ =>
{
var connectionString = builder.Configuration.GetConnectionString("Default");
return new UnitOfWorkFactory(connectionString);
});
To use IDbReader
without a unit of work, a singleton IDbReaderFactory
can be added to the DI container
builder.Services.AddSingleton<IDbReaderFactory>(_ =>
{
var connectionString = builder.Configuration.GetConnectionString("Default");
return new DbReaderFactory(connectionString);
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- CommQ.Data.Abstractions (>= 1.0.1-beta.3)
- Microsoft.Data.SqlClient (>= 5.1.0)
-
net6.0
- CommQ.Data.Abstractions (>= 1.0.1-beta.3)
- Microsoft.Data.SqlClient (>= 5.1.0)
-
net7.0
- CommQ.Data.Abstractions (>= 1.0.1-beta.3)
- Microsoft.Data.SqlClient (>= 5.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0-beta.3 | 61 | 5/5/2024 |
2.0.0-beta.2 | 61 | 3/31/2024 |
2.0.0-beta.1 | 54 | 3/30/2024 |
1.3.0 | 170 | 1/6/2024 |
1.2.0 | 246 | 3/12/2023 |
1.2.0-beta.2 | 77 | 3/12/2023 |
1.1.0 | 200 | 3/12/2023 |
1.0.1 | 221 | 3/12/2023 |
1.0.1-beta.4 | 79 | 3/12/2023 |
1.0.1-beta.3 | 78 | 3/12/2023 |
1.0.1-beta.2 | 78 | 3/11/2023 |
1.0.1-beta.1 | 75 | 3/11/2023 |
1.0.0 | 212 | 3/11/2023 |