📜 ⬆️ ⬇️

Introduction to Sterling NoSQL OODB

image
Sterling NoSQL OODB is a lightweight object NoSQL database designed for use in .NET 4.0, Silverlight 4/5, and Windows Phone 7 projects.

Opportunities


Usage example

Suppose we need to store a database by species. Here we describe such entities:
enum FeedingType {
Herbivore,
Carnivore,
Omnivore
}

class Feeding {
public FeedingType Type { get ; set ; }
public TimeSpan DefaultInterval { get ; set ; }
}

class Animal {
public Guid Key { get ; set ; }
public string Name { get ; set ; }
public TimeSpan LifeExpectancy { get ; set ; }
public Feeding Supply { get ; set ; }
}

* This source code was highlighted with Source Code Highlighter .

Accordingly, we will store in the database objects like Animal.
Now we need to describe the structure of our database:
class BiologyDatabaseInstance: BaseDatabaseInstance {
public const string INDEX_ANIMAL_NAME = "idx_animal_name" ;

public override string Name { get { return "BiologyDb" ; } }

protected override List <ITableDefinition> RegisterTables () {
// - .
return new List <ITableDefinition> {
CreateTableDefinition<Animal, Guid > (animal => animal.Key)
.WithIndex<Animal, string , Guid > (INDEX_ANIMAL_NAME, animal => animal.Name)
};
}
}


* This source code was highlighted with Source Code Highlighter .

In the database there will be one table with a key like Guid and an index for the name of the species.
Now we need to actually activate the database:
var engine = new SterlingEngine ();
engine.Activate();
var databaseInstance = engine.SterlingDatabase.RegisterDatabase<BiologyDatabaseInstance> (
new FileSystemDriver ( "C:/Temp/Animals/" ));

* This source code was highlighted with Source Code Highlighter .

That's all. Can be used. All work is done through a copy, which was provided to us during registration. For example:
var catId = databaseInstance.Save ( new Animal {
Key = Guid .NewGuid (),
LifeExpectancy = TimeSpan .FromDays (365 * 15),
Name = "Cat" ,
Supply = new Feeding {
Type = FeedingType.Carnivore,
DefaultInterval = TimeSpan .FromHours (12)
}
});

var cat = databaseInstance.Load<Animal> (catId);

var orderedAnimals = databaseInstance
.Query<Animal, string , Guid > (BiologyDatabaseInstance.INDEX_ANIMAL_NAME)
.OrderBy (x => x.Index)
.Select (x => x.LazyValue.Value);


* This source code was highlighted with Source Code Highlighter .

Finish working with the database should be a call to Dispose ().

Sources


')

Source: https://habr.com/ru/post/132114/


All Articles