JsonFileDataBase 1.0.2
dotnet add package JsonFileDataBase --version 1.0.2
NuGet\Install-Package JsonFileDataBase -Version 1.0.2
<PackageReference Include="JsonFileDataBase" Version="1.0.2" />
paket add JsonFileDataBase --version 1.0.2
#r "nuget: JsonFileDataBase, 1.0.2"
// Install JsonFileDataBase as a Cake Addin
#addin nuget:?package=JsonFileDataBase&version=1.0.2
// Install JsonFileDataBase as a Cake Tool
#tool nuget:?package=JsonFileDataBase&version=1.0.2
JsonFileDataBase
version 1.0.2
See Sample Code See GitHub Repository
vNext: support simple relations
The following is a demo fo use of JsonFileDataBase in a C# web project
Create the data model
Your model classes should implement ITable
interface and thus must contain the Id
propoerty.
public class Person : ITable
{
public int Id { get; set; }
//add other propoerties
}
public class Location :ITable
{
public int Id { get; set; }
//add other propoerties
}
Create the Database Context
The main class that coordinates JsonFileDataBase
functionality for a given data model is the database context class. You create this class by deriving from the JsonFileDB.DbContext
class. In your code you specify which entities are included in the data model.
The DBContext constructor requires a string
of the path to the JSON file to be used as database.
public AppDBContext()
:base("jsonFilePath") //can be provided as string literal
{
}
OR alternatively The path can be set in the app setting file and injected from configuration service using dependancy injection
public AppDBContext(IConfiguration configuration)
:base(configuration.GetConnectionString("JSONFilePath"))
{
}
Add the Models in the Database context: regester the classes as tables
public class AppDBContext : DBContext
{
public AppDBContext()
:base("jsonFilePath") //can be provided as string literal
{
}
//declare all table intstances
public Dataset<Person> Persons { get; set; }
public Dataset<Location> Locations { get; set; }
}
This code creates a DbSet property for each entity set. In JsonFileDataBase
terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.
Finally, initialize the Dataset (table) instances in the database; this generates the tables only once and if the don't exist.
public AppDBContext()
:base("")
{
Persons = new Dataset<Person>(_database);
Locations = new Dataset<Location>(_database);
}
The final code should look similar to this:
public class AppDBContext : DBContext
{
public AppDBContext()
:base("jsonFilePath") //can be provided as string literal
{
Persons = new Dataset<Person>(_database);
Locations = new Dataset<Location>(_database);
}
//declare all table intstances
public Dataset<Person> Persons { get; set; }
public Dataset<Location> Locations { get; set; }
}
When the database is created, JsonFileDataBase
creates tables that have names the same as the Model class names all in lowercase.
Register the context with dependency injection
ASP.NET Core implements dependency injection by default. Services (such as the JsonFileDataBase
database context) are registered with dependency injection during application startup. Components that require these services (such as MVC controllers) are provided these services via constructor parameters.
To register AppDBContext
as a service, open Startup.cs
, and add the services.AddSingleton<IDBContext, AppDBContext>();
line to the ConfigureServices method.
public void ConfigureServices(IServiceCollection services
{
services.AddSingleton<IDBContext, AppDBContext>();
services.AddMvc();
}
Register the DBContext in the controler class
public class PersonController : Controller
{
private AppDBContext _db;
public PersonController(IDBContext db)
{
_db = (AppDBContext) db;
}
}
From here the Database CRUD operation can be achieved
Example:
_db.Persons.Add(person); //add person to database
_db.Persons.GetAll(); //get all entities from the person table
_db.Persons.GetAllAsync(); //get all entities from the person table asyncronously
_db.Persons.Find(id); //find a person with a specific id from the person table
_db.Persons.Findasync(id); //find a person with a specific id from the person table asyncronously
_db.Persons.Remove(id); //remos the person with a specified id from the person table
_db.Persons.Update(person); //updates the person
Save changes to database after Add(), Remove(), and Update() function call.
_db.SaveChanges(); //saves changes to database
See Sample Code
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 11.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Includes async methos for Find and getAll operation.