DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. How to Build Your First .NET Graph Database

How to Build Your First .NET Graph Database

Damaris Coll user avatar by
Damaris Coll
·
Mar. 13, 13 · Interview
Like (0)
Save
Tweet
Share
13.39K Views

Join the DZone community and get the full member experience.

Join For Free

There has been a lot of talking about NoSQL lately, and how this "new" databases can solve problems of Big Data. NoSQL is a movement that promotes alternatives to the relational model in order to provide higher scalability and availability using simple and lightweight systems of storage and retrieval.

As part of the NoSQL movement, Graph Databases in particular are especially suited for applications that need to analyze relationships in a network of data assuring high scalability. They are also suited to deal with ad-hoc data or including different type of data as they are less rigid with the schema. 

Among the most well-known databases there are solutions specially for .Net developers, for instance DEX graph database offers full support for their native C# API with direct and fast access to the graph core, popular Neo4j offers third-party .NET client via REST API, Microsoft research Trinity also supports C# language (although it is more a key-value solution) and CloudGraph seems to focus also on .Net with a beta-release.

So, for all .Net developers that are interested into using graphs for their applications, this article will give them a first hint of how to construct a first graph in C# using Visual Studio. For the example we are going to use DEX's native .Net API for a simple first approach.

  1. Download your API (in this case DEX API can be downloaded from here )
  2. Create the C# console application
  3. Replace the Program.cs with the sample below.  This is a typical HelloWorld example where the developer will create a graph database with two nodes (of the same type) and an edge between them. Nodes and edges are the main units of a graph (graph theory ), nodes can be seen as a row of a relational table whereas edges could be seen as the foreign key relating two rows from different tables. The example also includes a neighbors operation, in order to see how information is retrieved from a graph database. Following the relational simile, this retrieval from the graph would be like a select operation from the rows of the two tables joined by the foreign key.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using com.sparsity.dex.gdb;
    
    namespace HelloWorld
    {
        class Program
        {
            static void Main(string[] args)
            {
                //
                // Create a sample database
                //
                DexConfig cfg = new DexConfig();
                Dex dex = new Dex(cfg);
                Database db = dex.Create("HelloWorld.dex", "HelloWorld");
                Session sess = db.NewSession();
                Graph g = sess.GetGraph();
    
                // Add a node type with two attributes
                int nodeType = g.NewNodeType("TheNodeType");
                int idAttribute = g.NewAttribute(nodeType, "id", DataType.Long, AttributeKind.Unique);
                int nameAttribute = g.NewAttribute(nodeType, "name", DataType.String, AttributeKind.Indexed);
    
                // Add a directed edge type with an attribute
                int edgeType = g.NewEdgeType("TheEdgeType", true, false);
    
                // Add a node
                long hellow = g.NewNode(nodeType);
                Value value = new Value();
                g.SetAttribute(hellow, idAttribute, value.SetLong(1));
                g.SetAttribute(hellow, nameAttribute, value.SetString("Hellow"));
    
                // Add another node
                long world = g.NewNode(nodeType);
                g.SetAttribute(world, idAttribute, value.SetLong(2));
                g.SetAttribute(world, nameAttribute, value.SetString("World"));
    
                // Add an edge
                long theEdge = g.NewEdge(edgeType, hellow, world);
    
                // Get the neighbors of the first node using the edges of "TheEdgeType" type
                Objects neighbors = g.Neighbors(hellow, edgeType, EdgesDirection.Outgoing);
    
                // Say hello to the neighbors
                ObjectsIterator it = neighbors.Iterator();
                while (it.HasNext())
                {
                    long neighborOid = it.Next();
                    g.GetAttribute(neighborOid, edgeType, value);
                    System.Console.WriteLine("Hello " + value.GetString());
                }
                // The ObjectsIterator must be closed
                it.Close();
                // The Objects must be closed
                neighbors.Close();
    
                // Close the database
                sess.Close();
                db.Close();
                dex.Close();
            }
        }
    }
  4. Add a reference in the project (Menu Project -> AddReference) to the library "dexnet.dll".
  5. Put the other dlls in a directory where the application can found it. The easier way is to put them where the application exe is created. Or set the working directory to the directory where you have the dlls. But you could also put them with the windows dlls.
  6. Here it is your first .Net graph!
  7. What next? populate your graph database with more nodes and edges (play with different types of nodes and edges!) and also do not forget to use popular graph algorithms (take a look at this dzone article here or this more advanced here )


Relational database Database Graph (Unix) Build (game engine)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java REST API Frameworks
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • GitLab vs Jenkins: Which Is the Best CI/CD Tool?
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: