Lightmap for .Net
I've been working on a new open source project called Lightmap that provides developers with a way to model their databases, in a database agnostic manor. This is done by creating migration objects that define what the table layout (along with other database objects) would look like. Lightmap supports several different ways of doing this - the following example shows just one. We'll use a couple different C# model classes to represent tables, related to each other.
Migrations
We'll use the following two classes as our table definitions.
public class User
{
public int UserId { get; set; }
public string Email { get; set; }
public int LockoutEnabled { get; set; }
public string PasswordHash { get; set; }
public string UserName { get; set; }
}
public class UserLogins
{
public string LoginProvider { get; set; }
public int ProviderId { get; set; }
public string ProviderDisplayName { get; set; }
public string UserId { get; set; }
}
Next we'll create our database model with an initial migration object.
public class InitialMigration : IMigration
{
public InitialMigration(IDataModel dataModel) => this.DataModel = dataModel;
public IDataModel DataModel { get; }
public void Apply()
{
this.DataModel.AddTable<User>()
.AlterColumn(table => table.UserId).IsPrimaryKey();
this.DataModel.AddTable<UserLogin>()
.AlterColumn(table => table.ProviderId).IsPrimaryKey()
.WithForeignKey<User>(
(userTable, loginTable) => userTable.UserId == loginTable.UserId);
}
public void Revert() { } // Revert support not implemented yet.
}
The API is fairly straight forward, and has hook points for database provider specific implementations to expose additional constraints and database features via extension methods.
Wiring it up
To get it wired up in your application, you need to create an instance of the IDataModel
implementation, IDatabaseManager
implementation for the database of your choice, along with an instance of an IDatabaseMigrator
that works with your database.
Currently only Sqlite has working implementations. The Lightmap API and base feature set is still being developed; until it's stabilized, I won't be adding other databases myself. Sql Server and MySql are the next two I plan on supporting as soon as the API churn quiets down.
The wire up for Lightmap will be simplified in the future as well, for apps that use ASP.Net Core. For now, you would wire it all up manually like this:
IDataModel dataModel = new DataModel();
IDatabaseManager databaseManager = new SqliteDatabaseManager("MyDb", "Data Source=MyDb.sqlite");
IMigration migration = new InitialMigration(dataModel);
IDatabaseMigrator migrator = new SqliteMigrator(migration);
migrator.Apply(databaseManager);
Ideally, all you would have to do is use the ASP.Net Core middleware to wire this up. I'm thinking of something along the lines of:
appBuilder.UseLightmap(config => config.WithSqlite());
There will be additional things on the configuration callback instance that you can use to setup various options, like default schema names.
Wrapping up
The project is still under active development and isn't ready for a release yet. If anyone would like to take it for a spin, they can clone it from GitHub, compile and add it to their projects. It currently only runs on NetStandard 1.4. The target framework will probably change to NetStandard 2.0 when that ships and I'll also work towards supporting Net46 as well so that the library can run on Full Framework, Mono and Core.
I'm not to far out from having an initial beta shipped on NuGet.