-
Notifications
You must be signed in to change notification settings - Fork 5
Home
ObjectStore is an Object Relational Mapper, which is responsible for mapping between database and the programming language C#. By doing that, ObjectStore follows these principals:
- The entire configuration is done in code. There is no *.edmx or any other file, no additional model designer, it's all defined in .Net code.
- The objects used as model for the data don’t require any dependency to the library. They are, even though they need to follow certain rules, plain old CLR object(POCOs).
- Objects you get from the ObjectStore are always the same instance for the same entity. There is no such thing as a dbContext or objects created by call. You ask for the "USD" entry in your Currencies table, and you always get the same instance back, no matter how often you ask for it.
Let's imagine you are building an application that is, for example, dealing with orders that consist of items of different products and quantities. While orders might be created every few seconds, products might not be changed for weeks. So if you are querying an order and its items that should contain some of the products properties, you can either include those properties in the item model or load all products for this particular order. Both leads you to transmitting product properties that might not have changed the last million times you queried them from the database. The only way for you to prevent this is to query all products and chache them, and ObjectStore is doing that for you. ObjectStore lets you define whether the data, the model is created for, should be loaded all at first time they've been touched, or if only the data should be loaded that has been requested by a certain query. Also, if you are getting the products name in the order item, and you want it to be readonly and never be updated, you can remove the setter of the property and get a compiler error if you set it accidently instead of an error at runtime.
Create a console application with the following commands:
dotnet new console -n "ObjectStoreDemo"
cd .\ObjectStoreDemo\
Add the latest version of an ObjectStore library nuget package, the SQLite version in this case:
dotnet add package ObjectStore.SQLite
Add using statements to the program.cs file:
using ObjectStore;
using ObjectStore.OrMapping;
using ObjectStore.Sqlite;
Add an Entity class:
[Table("Entity1")]
public abstract class Entity1
{
[Mapping(FieldName="EntityId"), IsPrimaryKey]
public abstract int Id { get; }
[Mapping(FieldName = "TextField")]
public abstract string Text { get; set; }
}
Add the following method to do the setup:
public static void Setup()
{
RelationalObjectStore relationalObjectStore =
new RelationalObjectStore("Data Source=Database.db", DataBaseProvider.Instance)
.Register<Entity1>();
ObjectStoreManager.DefaultObjectStore.RegisterObjectProvider(relationalObjectStore);
relationalObjectStore.InitializeDatabase();
}
this needs to be done once at startup before it is used.
Change the main method to call the Setup method and use the ORMapper:
public static void Main()
{
Setup();
Entity1 entity1 = ObjectStoreManager.DefaultObjectStore.CreateObject<Entity1>();
entity1.Text = "Hello World";
ObjectStoreManager.DefaultObjectStore.GetQueryable<Entity1>().Save();
IQueryable<Entity1> queryable = ObjectStoreManager.DefaultObjectStore.GetQueryable<Entity1>();
}
Try it: .Net 5