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

KORM - ideas and suggestions for new features #8

Open
LukasSefcik opened this issue Mar 22, 2019 · 0 comments
Open

KORM - ideas and suggestions for new features #8

LukasSefcik opened this issue Mar 22, 2019 · 0 comments
Assignees
Milestone

Comments

@LukasSefcik
Copy link
Collaborator

LukasSefcik commented Mar 22, 2019

General info

Do not close this issue. This is a "meta issue". It is just for writing down any ideas and suggestions for KORM. Ideas here, once consulted, will be moved to separate issues.

Simpler Delete of entities

Implemented in #40.

Now, if we want to delete an entity, we can use direct SQL statement (ExecuteNonQuery), or DbSet. The disadvantage of ExecuteNonQuery is, that it does not have ane knowledge about entity type, so the SQL must be hand written. Disadvantage of DbSet is that we need to create an instance of the entity, just to have its ID. I'd like to have something which can be used without creating an entity instance:

_database.Delete<EntityType>(123)

We could support delete with condition (instead of just primary key):

_database.Delete<EntityType>(item => item.ParentId == 123);

Things to think about:

  • Where to implement this (IDbSet, IDatabase...). And it should at least support int and long primary keys (generic of <TEntity, TKey>?), but better any keys (what about composite ones?).
  • What kind of expressions to support when deleting with condition and throw some exception if we cannot translate it into SQL.

Value generators for columns

Implemented in #41.

Now we have some support for generated primary keys. But we only support int (no long). It is achieved by Key attribute. It would be better to have something like ValueGenerator attribute, which will generate value for column on INSERT. And it could be used for any column, not just primary key.

Timestamp column

This would be special column (for start marked with some attribute) of DateTime/DateTimeOffset type. Value for this column would be automatically set to current date and time on every INSERT or UPDATE.

Soft delete

It is common to use so-called soft delete instead of directly deleting a database record. The record is not deleted, it is only marked with IsDeleted flag.

It would be nice to support it directly in KORM.

I suggest something like this:

public class DatabaseConfiguration : DatabaseConfigurationBase
{
    public override void OnModelCreating(ModelConfigurationBuilder modelBuilder)
    {
        modelBuilder.Entity<Document>()
            .UseSoftDelete(entity => entity.IsDeleted);
    }
}

After calling dbSet.CommitChanges(); KORM will call update IsDeleted = true.

Global Query Filters

Implemented in #47

Discused in #42.

In many cases, we want to define a global filter to apply to each query. For example: IsDeleted = false, ParentId = 1, UserId = ActiveUser.Id, etc.

It would be great to be able to define it in one place and KORM would automatically add this condition to every query.

public class DatabaseConfiguration : DatabaseConfigurationBase
{
    public override void OnModelCreating(ModelConfigurationBuilder modelBuilder)
    {
        modelBuilder.Entity<Document>()
            .UseSoftDelete(entity => entity.IsDeleted)
            .HasQueryFilter(entity => entity.IsDeleted == false && entity.ParentId == 1);
    }
}

KORM will automatically add a condition ((IsDeleted = 0) AND (ParentId = 1) when calling any query using Query<Document>().

It should be possible to do this in all scenarios except for direct sql calls _database.Query<Document>().Sql("SELECT * FROM DOCUMENT");

Ignoring global filters

If I need to call a query without these conditions, I must explicitly say:

_database.Query<Document>()
    .IgnoreQueryFilters()
    .ToList();

Things to think about

  • We can have multiple classes to retrieve data from the same table. For example: DocumentHeader, DocumentVerySpecialDto, etc. It would be nice to define this filter for all these classes.
  • We need overload with string condition HasQueryFilter(string whereCondition, params object[] args);

Use ConnectionStrings section with named connection strings

Implemented in Kros.KORM.Extensions.Asp repository.

Filed in Kros.KORM.Extensions.Asp repository.

We use our own setup of connection string in setting. We require ConnectionString section with ProviderName and ConnectionString subkeys. We could use default connection strings settings (as Entity Framework does it). The section name is ConnectionStrings and subkeys are name of connection strings. Provider can be integrated in connection string itself. When creating IDatabase, connection string name will be specified. If the name is not specified, default name DefaultConnection will be used.

Use different kind of configuration than attributes

Implemented in #18

Now some configuration is allowed only using attributes (Key, Alias). Something can be changed using custom ModelMapper. Maybe we can think about different kind of configuration instead of attributes.

Use new feature of C# 8.0 - Async Streams for async loading data

Use new C# 8.0 feature Async Streams for async obtaining data by KORM.

IEnumerable<Person> people = await _database.Query<Person>().AsAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants