Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,19 @@ var repository = new SqlRepository(() => new SqlConnection(connectionString));
implements list/get/exists/add/update/upsert/delete against a `Table<T>`.

For a quick start, declaring a dedicated entity class is not necessary – the simplest form
is to instantiate `Entity<T>` directly:
is to instantiate `Entity<T>` directly, passing the table name as a plain string:

```csharp
using Simpleverse.Repository.Db;
using Simpleverse.Repository.Db.Entity;

var entity = new Entity<Identity>(repository, "I");
```

Internally this constructs a `Table<Identity>` for you. The more complicated
`(repository, Table<TModel>)` overload is still available and useful for advanced cases –
e.g. when you need to control schema qualification, quoting, or reuse a `Table<T>` instance
that already exists elsewhere:

```csharp
using Simpleverse.Repository.Db;
Expand Down
26 changes: 26 additions & 0 deletions src/Simpleverse.Repository.Db.Test/SqlServer/Entity/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ public async Task ListAsync_WhenFilteredByNullableGuidListContainingNull_SkipsNu
Assert.Equal(recordWithGuid.Guid, returned[0].Guid);
}
}

[Fact]
public void Constructor_WhenGivenTableNameString_CreatesEquivalentTableToTableConstructorOverload()
{
// arrange & act
var entityFromTable = new IdentityEntity(_sqlRepository);
var entityFromTableName = new IdentityEntityByTableName(_sqlRepository);

// assert
Assert.Equal(entityFromTable.TableName, entityFromTableName.TableName);
}
}

public class IdentityEntity : Entity<Identity, IdentityQueryFilter, DbQueryOptions>
Expand All @@ -354,6 +365,8 @@ public IdentityEntity(DatabaseFixture fixture)

public IdentityEntity(SqlRepository sqlRepository) : base(sqlRepository, new Table<Identity>("I")) { }

public string TableName => Source.TableName;

protected override void SelectQuery(QueryBuilder<Identity> builder, IdentityQueryFilter filter, DbQueryOptions options)
{
var explicitKey = new Table<ExplicitKey>("EK");
Expand All @@ -376,6 +389,19 @@ protected override void Filter(QueryBuilder<Identity> builder, IdentityQueryFilt
}
}

public class IdentityEntityByTableName : Entity<Identity, IdentityQueryFilter, DbQueryOptions>
{
public IdentityEntityByTableName(SqlRepository sqlRepository) : base(sqlRepository, "I") { }

public string TableName => Source.TableName;

protected override void Filter(QueryBuilder<Identity> builder, IdentityQueryFilter filter)
{
builder.Where(x => x.Name, filter.Name);
base.Filter(builder, filter);
}
}

public class IdentityQueryFilter
{
public virtual string Name { get; set; }
Expand Down
20 changes: 20 additions & 0 deletions src/Simpleverse.Repository.Db/Entity/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public Entity(DbRepository repository, Table<TModel> source)
Source = source;
}

public Entity(DbRepository repository, string tableName)
: this(repository, new Table<TModel>(tableName))
{
}

#region IQuery

#region Get
Expand Down Expand Up @@ -670,6 +675,11 @@ public Entity(DbRepository repository, Table<TModel> source)
: base(repository, source)
{
}

public Entity(DbRepository repository, string tableName)
: base(repository, tableName)
{
}
}

public class Entity<T, TOptions>
Expand All @@ -681,6 +691,11 @@ public Entity(DbRepository repository, Table<T> source)
: base(repository, source)
{
}

public Entity(DbRepository repository, string tableName)
: base(repository, tableName)
{
}
}

public class Entity<T>
Expand All @@ -691,5 +706,10 @@ public Entity(DbRepository repository, Table<T> source)
: base(repository, source)
{
}

public Entity(DbRepository repository, string tableName)
: base(repository, tableName)
{
}
}
}
Loading