c# - 确定 Neo4j 驱动程序和 session 对象的范围

标签 c# .net dependency-injection neo4j

我目前正处于 Neo4j 学习曲线的起点,因此我正在对依赖注入(inject)的范围设定做出很多假设(目前正在 Razor Pages 应用程序中进行试验)。

假设 1:Driver 是一个轻量级协议(protocol)包装器,在 Singleton 范围内是安全的。

假设 2:将 Session 的范围限定为 Web 请求生命周期可能会有用。例如,我可能想在某些中间件中添加一个事务。

下面是一些 DI 设置示例:

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddSingleton(_ => GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "hubdb")))
        .AddScoped(_ => _.GetRequiredService<IDriver>().Session())
        .AddRazorPages();
}

还有一个简单的业务对象,它将被连接并注入(inject)到PageModel类中:

public class SignupVendor
{
    private readonly ISession session;

    public SignupVendor(ISession session)
    {
        this.session = session;
    }

    public async Task RunAsync(string name)
    {
        using var transaction = await session.BeginTransactionAsync();

        try
        {
            var result = await transaction.RunAsync(
                "CREATE (v:Vendor {name:$name}) RETURN v", 
                new { name });

            await transaction.CommitAsync();
        }
        catch
        {
            await transaction.RollbackAsync();
            throw;
        }
    }
}

你能证实或评论我的假设吗?

最佳答案

引用Neo4j 1.7 Driver Manual

Assumption 1: Driver is a lightweight protocol wrapper that would be safe in Singleton scope.

简短回答:

来自文档

The driver object

A Neo4j client application will require a driver instance in order to provide access to the database. This driver should be made available to all parts of the application that need to interact with Neo4j. In languages where thread safety is an issue, the driver can be considered thread-safe.

A note on lifecycle

Applications will typically construct a driver instance on startup and destroy it on exit. Destroying a driver instance will immediately shut down any connections previously opened via that driver; for drivers that contain a connection pool, the entire pool will be shut down.

强调我的。


Assumption 2: It might be useful to scope Session to the web request lifetime. For instance, I might want to add to a transaction in some middle-ware.

简短回答:

来自文档

Sessions

A session is a container for a sequence of transactions. Sessions borrow connections from a pool as required and so should be considered lightweight and disposable. In languages where thread safety is an issue, a session should not be considered thread-safe. In languages that support them, sessions are usually scoped within a context block. This ensures that they are properly closed and that any underlying connections are released and not leaked.

强调我的

关于c# - 确定 Neo4j 驱动程序和 session 对象的范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58880721/

相关文章:

c# - DataGridView 不显示数据库中带有空图像的所有值

c# - 使用锁是否比使用本地(单个应用程序)信号量具有更好的性能?

c# - 使用依赖注入(inject)注入(inject)多个实现

c# - 与巨大的数据流异步

java - Dagger:多次注入(inject)的类中没有可注入(inject)成员

c# - 如何在 C# 中将字符串转换为 "Null Terminated Byte Array"?

.net - LINQ : Generics with IQueryable

c# - 动态生成一个 Action

dependency-injection - 你可以在 Angular Dart 的构造函数中混合常规参数和注入(inject)参数吗?

php - Symfony2 Lazy Services 何时使用?