Twino.Extensions.Data

Entity Framework Core Data Context extension for Twino IOC


Keywords
context, data, entity, factory, framework, ioc, service, twino, extensions
License
GPL-3.0
Install
Install-Package Twino.Extensions.Data -Version 4.1.0

Documentation

Twino Extensions

NuGet NuGet NuGet

Twino Extensions project has useful extensions for twino projects.

Twino Extensions Consumer Factory

Consumer Factory is an extension for connecting Twino MQ easily. It creates a TmqStickyConnector and registers it to service provider. You can get that connector with ITwinoBus service type.

services.UseTwinoBus(cfg => cfg.AddHost("tmq://127.0.0.1:22200")
                               .AddTransientConsumers(typeof(Program)));

NOTE: The usage above is for Twino IOC. If you're using Twino.Extensions.Bus with Microsoft Dependency Injection, you need to use both AddTwinoBus and UseTwinoBus

Twino Extensions Http

HttpClient pool factory. You can use one pool as default or multiple pools with keys.

Basic Usage

IServiceContainer container = new ServiceContainer();

//pool size 32
//configuration action is executed before each factory.Create() usage
container.AddHttpClient(32, httpClient =>
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Token...");
});

//IHttpClientFactory is injectable
IHttpClientFactory factory = container.Get<IHttpClientFactory>();
HttpClient client = factory.Create();
//use client here

Usage With Keys

IServiceContainer container = new ServiceContainer();

//for service a
//pool size 32
//configuration action is executed before each factory.Create() usage
container.AddHttpClient("service-a", 16, httpClient =>
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Token-A...");
});

//for service b
//pool size 32
//configuration action is executed before each factory.Create() usage
container.AddHttpClient("service-b", 8, httpClient =>
{
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Token-B...");
});

//IHttpClientFactory is injectable
IHttpClientFactory factory = container.Get<IHttpClientFactory>();

//get for service-a
HttpClient clientA = factory.Create("service-a");

//get for service-b
HttpClient clientB = factory.Create("service-b");
//use client here

Twino Extensions Data

Data Context pool for DbContext objects. Implementation methods starts with AddData (you can still use AddDb methods of Microsoft Dependency Injection implementation). Here is a quick example:

IServiceContainer container = new ServiceContainer();

//container.AddDataContextTransient
//container.AddDataContextTransientPool
container.AddDataContextScoped<MyContext>(o => o.UseNpgsql("connection-string"));
container.AddDataContextScopedPool<MyContext>(o => o.UseNpgsql("connection-string"),
                                              p =>
                                              {
                                                  p.PoolMinSize = 20;
                                                  p.PoolMaxSize = 120;
                                                  p.MaximumLockDuration = TimeSpan.FromSeconds(60);
                                                  p.IdleTimeout = TimeSpan.FromSeconds(90);
                                                  p.WaitAvailableDuration = TimeSpan.FromMilliseconds(1500);
                                                  p.ExceedLimitWhenWaitTimeout = false;
                                              });

//create a scope for using Scoped registrations
IContainerScope scope = container.CreateScope();

//get db context from pool
MyContext context = container.Get<MyContext>(scope);

Thanks

Thanks to JetBrains for open source license to use on this project.

jetbrains