CommQ.Data.Abstractions
1.0.1-beta.4
See the version list below for details.
dotnet add package CommQ.Data.Abstractions --version 1.0.1-beta.4
NuGet\Install-Package CommQ.Data.Abstractions -Version 1.0.1-beta.4
<PackageReference Include="CommQ.Data.Abstractions" Version="1.0.1-beta.4" />
paket add CommQ.Data.Abstractions --version 1.0.1-beta.4
#r "nuget: CommQ.Data.Abstractions, 1.0.1-beta.4"
// Install CommQ.Data.Abstractions as a Cake Addin #addin nuget:?package=CommQ.Data.Abstractions&version=1.0.1-beta.4&prerelease // Install CommQ.Data.Abstractions as a Cake Tool #tool nuget:?package=CommQ.Data.Abstractions&version=1.0.1-beta.4&prerelease
CommQ.Data
A Unit of Work implementation. Uses types from System.Data.
Not an ORM.
Usage
Install the CommQ.Data
NuGet package - https://www.nuget.org/packages/CommQ.Data/
Alternatively, install the CommQ.Data.Abstractions
NuGet package in your core/domain project and install CommQ.Data
elsewhere - https://www.nuget.org/packages/CommQ.Data.Abstractions/
Creating a unit of work
Note: if you are using dependency injection, see Dependency Injection.
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 = (int)reader["Id"];
Name = (string)reader["Name"];
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 was computed. 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 was computed. 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
- No dependencies.
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 |
---|---|---|
1.3.0 | 219 | 1/6/2024 |
1.2.0 | 335 | 3/12/2023 |
1.2.0-beta.2 | 79 | 3/12/2023 |
1.2.0-beta.1 | 70 | 3/12/2023 |
1.1.0 | 311 | 3/12/2023 |
1.0.1 | 323 | 3/12/2023 |
1.0.1-beta.4 | 87 | 3/12/2023 |
1.0.1-beta.3 | 81 | 3/12/2023 |