下面您会看到我的GraphOperations类(使用Neo4jClient
编写在C#
中)执行基本的Neo4j
图操作。方法GraphGetConnection()
连接到Neo4j并返回clientConnection
和我的CreateNode()
方法创建一个节点并返回其节点引用。优化GraphClient连接?
现在在这种方法中,你会看到我去GraphOperations graphOp = new GraphOperations();
,然后clientConnection= graphOp.GraphConnection();
。
- 这是正确的方法吗?
- 每次我想要执行操作时,我都会调用连接吗?
- 如何优化以下代码?我想为每个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;
}
}
感谢您的答复,非常感谢,真的有助于流水线我的代码。你提到过什么异常? –
如果您尝试在没有Neo4J服务器可联系的情况下运行您的代码,那么一个很好的示例就是AggregateException(包装HttpRequestException的内部异常)。这就是Neo4jClient在构建完成后调用“连接”方法的原因。但是,你可以按照相同的方法,或者只是在构造函数中抛出异常... –
我以前见过这个例子,请你提供一个例子来说明你将如何处理这个异常? –