sqlite-net
sqlite-net copied to clipboard
Add Fluent API (v2)
The changes made in this pull request allow tables to be created using a Fluent API with attribute-free objects (aka POCOs). This allows for cleaner separation of layers and aids dependency injection by not requiring references to sqlite-net to decorate model classes with attributes.
Example usage:
var entity1 = TableMapping.Build<SampleEntity>()
.TableName("ExampleTable")
.PrimaryKey(x => x.Key)
.ToMapping();
var entity2 = TableMapping.Build<SampleEntity2>()
.TableName("Users")
.PrimaryKey(x => x.Id)
.AutoIncrement(x => x.Id)
.ColumnName(x => x.PasswordHash, "Password")
.Unique(x => x.Username)
.ToMapping();
var entity3 = TableMapping.Build<SampleEntity3>()
.TableName("Contacts")
.Index("ix_full", x => x.Name, x => x.Email, x => x.Telephone)
.PrimaryKey(x => x.Id, true)
.MaxLength(x => x.Email, 255)
.NotNull(x => x.Email)
.Ignore(x => x.SecretMessage)
.ToMapping();
connection.CreateTable(entity1);
connection.CreateTable(entity2);
connection.CreateTable(entity3);
// OR
connection.CreateTables(CreateFlags.None, entity1, entity2, entity3);
Based on the following example plain model classes...
public class SampleEntity
{
public string Key { get; set; }
public string Value { get; set; }
}
public class SampleEntity2
{
public int Id { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
}
public class SampleEntity3
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
public string SecretMessage { get; set; }
}
The additional code recycles existing functionality for creating table mappings from attributes.
This looks great.
@praeclarum is there any chance of this being merged?
Any updates for this?
Awaiting for this feature.
@praeclarum I'm also in need to use model classes from different assemblies to store then in tables, so the feature in this PR looks as it works be the solution. How can I help in integrating this PR to sqlite-net? Or is there any other way to specify TableMappings for arbitrary types? Thanks!
I've made a fork of current repo and merged manually changes from old RoyGoode(thx!) code.
I will play with it and maybe use it.
https://github.com/michaldobrodenka/sqlite-net
@praeclarum SQLite-Net is a really useful repo and I would love to see the Fluent API feature being implemented! This would massively improve the workflow with shared data models, e.g. between backend and mobile applications when its very likely to use SQLite on the mobile side.
This seems to be very old, most of the changes are not compatible with the latest. I'll try to build out of @RoyGoode's code and see if I can at least make it work with the latest master.
This seems to be very old, most of the changes are not compatible with the latest. I'll try to build out of @RoyGoode's code and see if I can at least make it work with the latest master.
I don't remember exactly, but I think there were some problems in @RoyGoode PR. I took his code 4 years ago and fixed them, check my fork. I'm using his fluent api with fixes for 4 years already.