sqlite-net icon indicating copy to clipboard operation
sqlite-net copied to clipboard

Add Fluent API (v2)

Open roygoode opened this issue 6 years ago • 8 comments

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.

roygoode avatar Jul 02 '18 14:07 roygoode

This looks great.

@praeclarum is there any chance of this being merged?

UriHendler avatar Apr 03 '19 14:04 UriHendler

Any updates for this?

zzyzy avatar May 23 '19 13:05 zzyzy

Awaiting for this feature.

jacksonveroneze avatar Sep 28 '19 12:09 jacksonveroneze

@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!

vividos avatar Dec 12 '19 14:12 vividos

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

michaldobrodenka avatar Jul 09 '20 15:07 michaldobrodenka

@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.

MaxFromDoqpal avatar Sep 02 '21 11:09 MaxFromDoqpal

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.

lancecontreras avatar Apr 02 '24 23:04 lancecontreras

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.

michaldobrodenka avatar Apr 03 '24 05:04 michaldobrodenka