Dependency Injection
.AddAvalancheAccessor() extension method (Avalanche.Accessor.DependencyInjection.dll) adds support for accessor service requests.
// Create Dependency Injection service
IServiceProvider service =
new ServiceCollection()
.AddAvalancheService()
.AddHandlers(NetAccessorHandlers.Instance)
.AddCachePolicy(CachePolicies.Default)
.AddAvalancheAccessor()
.BuildServiceProvider();
IServiceProvider.GetService(IRecordAccessor<Record>) is forwarded as request to service stack as RecordAccessorRequest, which is handled by service stack. Default handlers use type builder to construct record and field accessors.
// Request accessor service
IRecordAccessor<MyRecord> recordAccessor =
service.GetRequiredService<IRecordAccessor<MyRecord>>();
// Create record
MyRecord myRecord = recordAccessor.DefaultConstructor.Read(default);
// Write "Name" field
recordAccessor.Fields[1].Writer.WriteAs<string, MyRecord>("ABC", myRecord);
IListAccessor<List> is forwarded to service stack as ListAccessorRequest.
// Request accessor service
IListAccessor<int[]> listAccessor =
service.GetRequiredService<IListAccessor<int[]>>();
// Use accessor
int[] array = listAccessor.DefaultConstructor.Read(default);
IListAccessor<List, Element> also is forwarded as ListAccessorRequest.
// Request accessor service
IListAccessor<int[], int> listAccessor2 =
service.GetRequiredService<IListAccessor<int[], int>>();
And IMapAccessor<Map> is forwarded as MapAccessorRequest.
// Request accessor service
IMapAccessor<Dictionary<string, int>> mapAccessor =
service.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
// Use accessor
Dictionary<string, int> map = mapAccessor.DefaultConstructor.Read(default);
IMapAccessor<Map, Key, Value> is also forwarded as MapAccessorRequest.
// Request accessor service
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor2 =
service.GetRequiredService<IMapAccessor<Dictionary<string, int>, string, int>>();
Full Example
Full example
using System;
using System.Collections.Generic;
using Avalanche.Accessor;
using Avalanche.Service;
using Avalanche.Writer;
using Microsoft.Extensions.DependencyInjection;
public class dependencyinjection
{
public static void Run()
{
{
// <01>
// Create Dependency Injection service
IServiceProvider service =
new ServiceCollection()
.AddAvalancheService()
.AddHandlers(NetAccessorHandlers.Instance)
.AddCachePolicy(CachePolicies.Default)
.AddAvalancheAccessor()
.BuildServiceProvider();
// </01>
// <02>
// Request accessor service
IRecordAccessor<MyRecord> recordAccessor =
service.GetRequiredService<IRecordAccessor<MyRecord>>();
// Create record
MyRecord myRecord = recordAccessor.DefaultConstructor.Read(default);
// Write "Name" field
recordAccessor.Fields[1].Writer.WriteAs<string, MyRecord>("ABC", myRecord);
// </02>
// <03>
// Request accessor service
IListAccessor<int[]> listAccessor =
service.GetRequiredService<IListAccessor<int[]>>();
// Use accessor
int[] array = listAccessor.DefaultConstructor.Read(default);
// </03>
// <04>
// Request accessor service
IListAccessor<int[], int> listAccessor2 =
service.GetRequiredService<IListAccessor<int[], int>>();
// </04>
// Use accessor
int[] array2 = listAccessor2.DefaultConstructor.Read(default);
// <05>
// Request accessor service
IMapAccessor<Dictionary<string, int>> mapAccessor =
service.GetRequiredService<IMapAccessor<Dictionary<string, int>>>();
// Use accessor
Dictionary<string, int> map = mapAccessor.DefaultConstructor.Read(default);
// </05>
// <06>
// Request accessor service
IMapAccessor<Dictionary<string, int>, string, int> mapAccessor2 =
service.GetRequiredService<IMapAccessor<Dictionary<string, int>, string, int>>();
// </06>
// Use accessor
Dictionary<string, int> map2 = mapAccessor2.DefaultConstructor.Read(default);
}
}
// <00>
public class MyRecord
{
public int Id = 0;
public string Name = null!;
}
// <00>
}