优化GraphClient连接?

时间:2013-06-19 07:35:11

标签: neo4j neo4jclient

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

现在,在该方法中,您会看到我正在GraphOperations graphOp = new GraphOperations();然后clientConnection= graphOp.GraphConnection();

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

    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;
        }
    }
    

1 个答案:

答案 0 :(得分:2)

好吧,您已将GraphClient存储在GraphOperations课程中,并且由于您的GraphCreateNode方法不是静态的,您可以使用该字段,因此{{1}方法就像:

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 */

在每种方法(CRUD方式)中,您将调用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; } } ,这将确保连接在那里。另一种方式(这可能是更可取的)是将初始化强加到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; } } 实例将在那里,这意味着你的CRUD方法可以专注于做CRUD而不是初始化GraphClient。这样就有可能从构造函数中抛出GraphClient,但关于这是否是一件坏事是个人偏好。