Microservice Storage - Extension class
Overview
To make interactions with mongo storage feature easier and more affordable we've introduced MongoCrudExtensions class that contains following methods:
- Get
- Create
- Update
- Delete
Example
In the example, there is a TestDocument storage document as described below.
namespace Beamable.Server
{
public class TestDocument : StorageDocument
{
public int IntValue;
public string StringValue;
}
}The example targets the TestStorage StorageObject class.
using Beamable.Common;
using MongoDB.Driver;
namespace Beamable.Server
{
[StorageObject("TestStorage")]
public class TestStorage : MongoStorageObject
{
}
}Then, in a Microservice, the TestDocument can be created, read, updated, and deleted as shown below.
using Beamable.Mongo;
using Beamable.Server;
using MongoDB.Driver;
using System.Collections.Generic;
namespace Beamable.Microservices
{
[Microservice("TestMicroservice")]
public class TestMicroservice : Microservice
{
[ClientCallable]
public async void Get(string id)
{
TestDocument document = await Storage.Get<TestStorage, TestDocument>(id);
}
[ClientCallable]
public async void Create(int intValue, string stringValue)
{
await Storage.Create<TestStorage, TestDocument>(new TestDocument
{
IntValue = intValue, StringValue = stringValue,
});
}
[ClientCallable]
public async void Update(string id, int intValue, string stringValue)
{
await Storage.Update<TestStorage, TestDocument>(id, new TestDocument
{
IntValue = intValue,
StringValue = stringValue
});
}
[ClientCallable]
public async void Delete(string id)
{
await Storage.Delete<TestStorage, TestDocument>(id);
}
}
}
The Collection Name is the Class NameWhen you use these helper methods, the Mongo Collection for the
TestDocumentclass will be called"TestDocument".
Updated 2 months ago
