-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Added Fluent API #533
base: master
Are you sure you want to change the base?
Added Fluent API #533
Conversation
For providing standard behaviour of creating column mappings from Attributes
For providing standard behaviour of creating table mappings from Attributes
For parsing properties from expressions in a Fluent API
For fluently building table mappings
I was looking forward to this! Removing decoration from the classes and moving the sqlite dependency to where it is being used will be awesome 👍 |
I made a change in #599 that is similar. That caches and allows custom building of TableMappings. I wonder if there is a way to combine the ideas. See the The reason cached it was because I saw a lot of memory pressure, since most queries require the |
Hello! I really love this but would like to change a few things too. I think the builder should make So the syntax would be something like: var mapping = 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 (mapping); |
Great work! |
src/SQLite.cs
Outdated
/// Adds a mapping to be used for indexes and naming for the given type. | ||
/// </summary> | ||
/// <param name="tableMapping">The table mapping.</param> | ||
internal void AddMapping(TableMapping tableMapping) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this. Instead, we'll have the builder create a mapping and have CreateTable accept it.
src/SQLite.cs
Outdated
} | ||
} | ||
|
||
public static class TableMappingFluentExtensions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should not be public. I don't like introducing APIs unrelated to this library.
src/SQLite.cs
Outdated
|
||
public class TableMappingBuilder<TEntity> | ||
{ | ||
SQLiteConnection _sqlite; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not want a pointer back to the connection. I'm trying to keep Mappings separate from connections.
src/SQLite.cs
Outdated
/// <returns> | ||
/// The number of entries added to the database schema. | ||
/// </returns> | ||
public int CreateTable(CreateFlags createFlags = CreateFlags.None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to remove this and instead introduce a CreateTable(TableMapping map)
method that registers the mapping if needed but then hands off to the normal code.
Tracking issue is #422 |
@praeclarum @roygoode what would it take to get this going? I started messing around with Roy's code based on your comments but this PR is from April has quite a bit of drift at this point. |
Looking forward to this feature |
Apologies to all in the delay in getting these updates in. I’ve spent pretty much the last 12 months working on non-.NET projects, but I’m back on Xamarin now for a new project. I will re-implement the Fluent API with the latest version and as per @praeclarum’s great suggestions. |
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)
# Conflicts: # src/SQLite.cs
As promised, I've updated to the latest source, and re-applied the new Fluent API changes, and also updated according to @praeclarum's reviews. Here is an update to the 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have implemented all of the changes as requested in the latest commits
I have created a fresh pull request #727 with a cleaner branch as my master was tracking the original master from circa April 2017 and has created diffs that look like a mess. The new pull request should be much easier to merge. |
this will be very helpful if this will be merged in the soonest possible time |
What is status on this pull request? This is a must have feature if you want to map third-party models. |
Is there any status update available? |
@Mrsevic I have merged and fixed current official and fluent api pull requests for testing, it seems ok. I'll try to use it in production. But it's only in my fork (I'm not sure now if all the fixes are pushed to github) |
Trying to build out of @roygoode's changes. Anyone almost close to making it work? |
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:
Based on the following example plain model classes...
Note: CreateTable() must be called to trigger the creation of the mapping and resulting table.
The additional code recycles existing functionality for creating table mappings from attributes.