0

下面您将看到我的 GraphOperations 类(用C#using编写Neo4jClient),它执行基本的Neo4j图形操作。该GraphGetConnection()方法连接到 Neo4j 并返回clientConnection,我的CreateNode()方法创建了一个节点并返回其节点引用。

现在在那个方法中,你会看到我要去GraphOperations graphOp = new GraphOperations();然后clientConnection= graphOp.GraphConnection();

  1. 这是正确的方法吗?
  2. 每次我想执行操作时都调用连接吗?
  3. 如何优化下面的代码?我想为每个 CRUD 操作创建一个方法,并希望找到执行此操作的最佳方法。

我希望这个问题足够清楚?

using Neo4jClient;

public class GraphOperations
{
    GraphClient clientConnection;

    public GraphClient GraphGetConnection()
    {
        clientConnection = new GraphClient(new Uri("http://localhost:7474/db/data"));
        clientConnection.Connect();

        return clientConnection;
    }

    public long GraphCreateNode(string type, string name, string customerName, string documentReference, int newVersionNumber)
    {

        Guid nodeGuid = Guid.NewGuid();
        System.DateTime dateTime = System.DateTime.Now;
        string timeStamp = String.Format("{0:dd MMMM yyyy HH:mm:ss}", dateTime);

        GraphOperations graphOp = new GraphOperations();
        clientConnection = graphOp.GraphGetConnection();

        var createNode = clientConnection.Create(new VersionNode()
        {
            GUID = nodeGuid.ToString(),
            Name = name,
            Type = type,
            DocumentReference = documentReference,
            DateTimeCreated = timeStamp,
            Version = newVersionNumber
        });

        return createNode.Id;
    }
}
4

1 回答 1

2

好吧,您已经将 存储GraphClientGraphOperations类中,并且由于您的GraphCreateNode方法不是静态的,您可以只使用该字段,因此您的GraphCreateNode方法变为:

public long GraphCreateNode(string type, string name, string customerName, string documentReference, int newVersionNumber)
{
    /* CODE */
    this.GraphGetConnection(); //Don't care or need the return from GraphGetConnection
    //From then on just:
    clientConnection.DOSTUFF ....
    /* CODE */

就个人而言,我会改变一些事情来让自己的生活更轻松一些:

public class GraphOperations 
{
    private GraphClient clientConnection;
    private void InitializeGraphClient()
    {
        if(this.clientConnection != null)
            return;

        this.clientConnection = new GraphClient(new Uri("http://localhost:7474/db/data"));
        this.clientConnection.Connect();
    }

    public NodeReference CreateNode(/*parameters*/) 
    {
        InitializeGraphClient();

        Guid nodeGuid = Guid.NewGuid();
        System.DateTime dateTime = System.DateTime.Now;
        string timeStamp = String.Format("{0:dd MMMM yyyy HH:mm:ss}", dateTime);

        var createNode = this.clientConnection.Create(
            new VersionNode()
                    {
                        GUID = nodeGuid.ToString(),
                        Name = name,
                        Type = type,
                        DocumentReference = documentReference,
                        DateTimeCreated = timeStamp,
                        Version = newVersionNumber
                    });

        return createNode.Id;
    }
}

在每种方法(CRUD-wise)中,您都会调用InitializeGraphClient,这将确保连接存在。另一种方法(这可能更可取)是将初始化粘贴到构造函数中GraphOperations

public class GraphOperations 
{
    private readonly GraphClient clientConnection;
    public GraphOperations()
    {
        this.clientConnection = new GraphClient(new Uri("http://localhost:7474/db/data"));
        this.clientConnection.Connect();
    }

    public NodeReference CreateNode(/*parameters*/) 
    {
        Guid nodeGuid = Guid.NewGuid();
        System.DateTime dateTime = System.DateTime.Now;
        string timeStamp = String.Format("{0:dd MMMM yyyy HH:mm:ss}", dateTime);

        var createNode = this.clientConnection.Create(
            new VersionNode()
                    {
                        GUID = nodeGuid.ToString(),
                        Name = name,
                        Type = type,
                        DocumentReference = documentReference,
                        DateTimeCreated = timeStamp,
                        Version = newVersionNumber
                    });

        return createNode.Id;
    }
}

并且使用它您应该始终知道该GraphClient实例将在那里,这意味着您的 CRUD 方法可以专注于执行 CRUD 而不是初始化GraphClient. 这有Exception可能从构造函数中抛出一个,但至于这是否是一件坏事是个人喜好。

于 2013-06-20T11:40:55.320 回答