Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fluent API (v2) #727

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Add Fluent API (v2) #727

wants to merge 19 commits into from

Conversation

roygoode
Copy link

@roygoode roygoode commented Jul 2, 2018

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 added 19 commits July 2, 2018 10:05
Merge in latest sqlite-net enhancements
This is necessary to allow for ColumnMapping and TableMapping to have derived classes in the next step of Fluent API development
Implements the original functionality of TableMapping.Column for building column definitions from classes decorated with attributes, to allow the functionality to be kept separate from the new Fluent API
Implements the original functionality of TableMapping for building table definitions from classes decorated with attributes, to allow the functionality to be kept separate from the new Fluent API
This provides a common API for indices created by the original attribute-based implementation and the new Fluent API implementation so they can be used interchangeably
This will be used by the Fluent API version(s) of the CreateTable method(s) to add or replace mappings created without using attributes
This will be used for parsing properties from expressions in the Fluent API
Allows building table mappings using a Fluent API
Call this method to create a new instance of TableMappingBuilder<T> to use the Fluent API to build table mappings
This will be used internally to create tables in the database based on given table mappings
CreateTable(), CreateTables(), CreateTableAsync(), CreateTablesAsync()
Used for creating tables based on the provided table mapping, generated from TableMappingBuilder
So you can use the new CreateTable(TableMapping…) methods with the original attribute-based implementation of table mappings if you really want to
Because this method is useful for supplying table mappings for query results, if you really need to specify column names without decorating those classes with attributes
Useful for querying the database with strongly-typed results for those that use the Fluent API to create table mappings
This implementation was missing from the Fluent API but was available via TableAttribute
TableQuery constructor that accepts TableMapping
Based on previous CreateTable tests
Between Fluent API and Attribute API
(Fixes bug that failed new unit tests for Fluent API)
@UriHendler
Copy link

This looks great.

@praeclarum is there any chance of this being merged?

@zzyzy
Copy link

zzyzy commented May 23, 2019

Any updates for this?

@jacksonveroneze
Copy link

Awaiting for this feature.

@vividos
Copy link

vividos commented Dec 12, 2019

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

@michaldobrodenka
Copy link

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

@MaxFromDoqpal
Copy link
Contributor

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

@v-kravets v-kravets mentioned this pull request Jul 1, 2023
bengavin added a commit to CoreBTS/sqlite-net that referenced this pull request Oct 19, 2023
@lancecontreras
Copy link

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.

@michaldobrodenka
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants