The C# Neo4JClient has GraphClient
where you have to call .Connect()
var client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
In all of the examples that I've seen, it's in a Console app, so they declare GraphClient
in Main()
and re-use it. And the documentation mentions how its Thread Safe and to only have one instance per database and to not call .Connect()
multiple times.
But what about in ASP.NET applications? Should I be sticking it in some static class that is accessed by the pages (or store it in the Application state)? Like this:
public class DbConnection
{
private static GraphClient _client;
public static GraphClient GraphClient
{
get
{
if (_client == null)
{
_client = new GraphClient(new Uri("http://localhost:7474/db/data"));
_client.Connect();
}
return _client;
}
}
}
And then in any ASPX page I could simply:
protected void Page_Load(object sender, EventArgs e)
{
GraphClient client = DbConnection.GraphClient;
//do whatever I need to with client
}
Or am I not understanding that correctly, and that would cause all kinds of problems? Do I need to call .Connect()
in each method (or maybe once per page lifecycle), like so:
private GraphClient _client;
private GraphClient PageGraphClient
{
get { //same as in previous, check if null and load _client with the connection }
}
protected void Page_Load(object sender, EventArgs e)
{
//do whatever I need to with PageGraphClient
}
protected void btnSave_Click(object sender, EventArgs e)
{
//do whatever I need to with PageGraphClient
}
And so on? I guess I'm just getting hung up on the whole Thread Safe thing and "making too many connections to the db" and wanting to make sure I'm not missing an easy/correct way to do this.
(and yes, I know that I shouldn't be calling the database commands from the ASPX pages directly, I'm over-simplifying for the sake of understanding how the GraphClient class works ;)