下面您将看到我的GraphOperations类(使用C#
编写,使用Neo4jClient
)执行基本的Neo4j
图形操作。 GraphGetConnection()
方法连接到Neo4j并返回clientConnection
,我的CreateNode()
方法创建了一个节点并返回其节点引用。
现在,在该方法中,您会看到我正在GraphOperations graphOp = new GraphOperations();
然后clientConnection= graphOp.GraphConnection();
。
我希望这个问题足够清楚?
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;
}
}
答案 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
,但关于这是否是一件坏事是个人偏好。