LunchPail 2.1.1
dotnet add package LunchPail --version 2.1.1
NuGet\Install-Package LunchPail -Version 2.1.1
<PackageReference Include="LunchPail" Version="2.1.1" />
paket add LunchPail --version 2.1.1
#r "nuget: LunchPail, 2.1.1"
// Install LunchPail as a Cake Addin #addin nuget:?package=LunchPail&version=2.1.1 // Install LunchPail as a Cake Tool #tool nuget:?package=LunchPail&version=2.1.1
LunchPail
LunchPail is a .NET Standard compliant Unit of Work implementation for ADO.NET. The UoW is the workhorse of the data context, so the project was dubbed "lunch pail" as a reference to blue collar athletes.
Getting Started
- Register the context within your IoC container (.NET Core shown below using SQL Server):
//startup.cs
public void ConfigureServices(IServiceCollection services)
{
//rest of code...
//context
services.AddTransient<IDbConnectionFactory>(options =>
{
var builder = new SqlConnectionStringBuilder(Configuration.GetConnectionString("DefaultConnection"));
return new DbConnectionFactory(() =>
{
var conn = new SqlConnection(builder.ConnectionString);
conn.Open();
return conn;
});
});
services.AddScoped<IDbContext, DbContext>();
//repositories (we'll add this later)
}
- Create a domain class to represents your database object.
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
- Create a repository with a dependency on
IDbContext
public interface IProductRepository
{
Task<Product> Read (int id);
}
public class ProductRepository : DbRepository
{
public ProductRepository(IDbContext dbContext) : base(dbContext) { }
public Product Read(int id)
{
return Connection.QuerySingleOrDefault<Product>("select * from dbo.Product where Id = @id", new { id }, transaction: Transaction);
}
}
- Register the repository with your IoC container (.NET Core shown below):
//startup.cs
public void ConfigureServices(IServiceCollection services)
{
//repositories
services.AddScoped<IProductRepository, ProductRepository>();
}
- With the context and repository registered, you're free to inject this into your controller or service layer.
Note the invocation of
dbContext.Commit()
which must occur after every interaction is complete to ensure proper disposal of the connection. This is true whether the interaction is a read or write.
public class ProductService
{
private readonly IDbContext dbContext;
private readonly IProductRepository productRepository;
public ProductService (
IDbContext dbContext,
IProductRepository productRepository)
{
this.dbContext = dbContext;
this.productRepository = productRepository;
}
public Product Read(int id)
{
var product = productRepository.Read(id);
dbContext.Commit(); // You MUST call commit after all interactions
return product;
}
}
Built with ♥ by Pim Brouwers in Toronto, ON.
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 | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. 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.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on LunchPail:
Package | Downloads |
---|---|
LunchPail.Repository
A .NET Standard compliant abstract repository for use with LunchPail. |
GitHub repositories
This package is not used by any popular GitHub repositories.