Table of Contents

Wisp.ResourceManagement Net-Version-Badge LiteDB-badge License-Badge

This is a template heavy implementation of service classes related to resource handling and database management. LiteDB NoSQL, embedded database is used in this project. All collection CRUD operations are handled automatically by this library, thus making it a good starting point for LiteDB based development.

Installation

Simply add the provided NuGet package to your project.

The LiteDBBase class

This class is meant as a base class for your own LiteDB service. It provides out of the box functionality to connect to a database, file handling, and collection CRUD operations.

Example usage

To create an instance of the database, (without inheriting it).

var db = new LiteDBBase(logger); //Create a new instance
db.Connect(path: "c:/.../Myfile.db", password: "1234"); //connect to a database
Debug.Assert(db.IsConnected); //check if DB is connected

Now assume we have the following model.

class MyModel : IDBCollectionItem<Guid>{
    private Guid _ID;
    
    [BsonId]
    public Guid ID { 
        get => _ID;
        set => _ID = value;
    }

    [BsonIgnore]
    public BsonValue IDBsonValue {
        get => _ID;
        set => _ID = value;
    }

    public string TestString;
    public double TestNumber;
}

We should inherit IDBCollectionItem interface in our models if we want to use the automatic collection CRUD operations.

Note: A convenience class to inherit from exists, if Guid is the BsonId type: GuidCollection.

CRUD operations are automatically registered by the LiteDBBase class.

string myCollectionName = "MyModelCollection";

MyModel model = new MyModel();

db.Insert(myCollectionName, new MyModel()); //add a new item
db.Delete(myCollectionName, model.ID); //delete the item

Furthermore, uploading/downloading abstract files to and from the database is also handled. Both stream and filesystem filepaths are supported.

db.UploadFile(fileid: "someid", filepath: ".../myfile.png"); //upload file to database
var metadata = FindFile("someid"); //download the file's metadata from the database
await db.CopyFileToStreamAsync(metadata, SomeStream); //async copy db file to stream.

See the docs for all available methods.

LiteDBBase is set up in a way to throw no errors.

The DBDefaultCollectionManager class

This class handles CRUD operations with the help of LiteDBBase. It maps class types to collection names in the database. It is meant to be used as part of a resource manager.

Example

var colmanager = DBDefaultCollectionManager(db, logger);

//map type to a db collection
colmanager.CollectionNamesByType[typeof(MyModel)] = "MyModelsCollection";

MyModel model = new MyModel();
colmanager.Insert(model); //Inserts model into MyModelsCollection of db

The ManagedObjectRegistry class

Sometimes, there's the need to load files from the filesystem, or the database, such as images or audio files. Such items might need significant resource allocation. It would be a good idea to load those resources just once, and pass a reference to them, around the application when they are needed. Thus avoiding accessing the database or the filesystem needlessly.

The ManagedObjectRegistry should be used in conjunction with a Resource Manager, to manage the lifecycle of a loaded resource. This is achieved in the following way:

  • The resource manager checks if the ManagedObjectRegistry already has the requested resource loaded.
  • If the resource is not registered, the Resource Manager proceeds to load the resource, and register it to the ManagedObjectRegistry.
  • The resource manager returns an IManagedObject, which holds a reference to the resource.
  • Subsequent calls to the manager for the resource, result in an extra IManagedObject.
  • When all IManagedObjects are deallocated, so does the target resource.

Example

//create a new registry for MyModel objects
var registry = new ManagedObjectRegistry<MyModel, Guid>();

//A managed type, holding a model, and using Guid as the ID type
IManagedObject<MyModel, Guid> managed_model = 
    //Add a new previously loaded from the "Filesystem" MyModel resource
    registry.AddNewManagedType(mymodel.ID, mymodel, "Filesystem");

//check if an object with a specific ID is registered
if(registry.ManagedObjectExists(mymodel.ID))
    //then get another managed reference to mymodel
    var anotherManagedObject = registry.GetManagedObject(mymodel.ID);

Documentation

Documentation for this project can be found here.

License

This project is licensed under the MIT license.