2013-06-19 95 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

我以前見過這個例子,請你提供一個例子來說明你將如何處理這個異常? –