2013-06-19 70 views
0

下面您会看到我的GraphOperations类(使用Neo4jClient编写在C#中)执行基本的Neo4j图操作。方法GraphGetConnection()连接到Neo4j并返回clientConnection和我的CreateNode()方法创建一个节点并返回其节点引用。优化GraphClient连接?

现在在这种方法中,你会看到我去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; 
    } 
} 

回答

2

好了,你已经在GraphOperations类存储GraphClient,并为您的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明智的),你会打电话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可以从构造函数抛出,但至于这是否是坏事是个人偏好。

+0

感谢您的答复,非常感谢,真的有助于流水线我的代码。你提到过什么异常? –

+1

如果您尝试在没有Neo4J服务器可联系的情况下运行您的代码,那么一个很好的示例就是AggregateException(包装HttpRequestException的内部异常)。这就是Neo4jClient在构建完成后调用“连接”方法的原因。但是,你可以按照相同的方法,或者只是在构造函数中抛出异常... –

+0

我以前见过这个例子,请你提供一个例子来说明你将如何处理这个异常? –