Skip to content

Nolan-Ramsden/SimplySqlSchema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimplySqlSchema

Some crappy little abstractions over SQL, for creating, migrating and querying tables.

Available On Nuget

Useful Features

Example Usage

Introductury Usage

The example accomplishes the following tasks in order:

  1. Determine the table schema from a POCO.
  2. Apply that schema to the DB.
  3. Do a Insert and Get on the table.
class TestObject
{
  [Key]
  public int Key { get; set; }

  [MaxLength(100)]
  public string Value { get; set; }
}

public async Task Demo()
{
  // 1. Determine the table schema from the POCO
  IObjectSchemaExtractor extractor = new DataAnnotationsSchemaExtractor();
  ObjectSchema schema = extractor.GetObjectSchema(typeof(TestObject), "TestTable");

  // 2. Apply the schema to the DB
  IDbConnection connection = new SQLiteConnection($"Data Source=Test.db;Version=3;");
  ISchemaManager manager = new SQLiteSchemaManager();
  await manager.CreateObject(connection, schema);

  // 3. Insert and Get
  ISchemaQuerier querier = new SQLiteQuerier();
  await querier.Insert(connection, schema, obj: new TestObject()
  {
      Key = 10,
      Value = "Hey there"
  });
  TestObject fetched = await querier.Get(connection, schema, keyedObject: new TestObject()
  {
      Key = 10
  });
}

Registering with .NET core

The following example adds all the required interfaces to the .NET core dependency container.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSimplySqlSchema();
}

Backend-Agnostic Example

The ISchemaManagerDelegator abstracts away the backend providers, so that backends can be determined at runtime, or multiple different backends can be used within the same project. This example gets a schema from the object, and safely migrates it, assuming the POCO was different last time the code was run (but works if nothing has changed as well).

// This method assumes dependency injection for simplicity
public async Task Demo(ISchemaManagerDelegator delegator, ISchemaMigrator migrator, IDbConnection connection)
{
    var target = delegator.GetObjectSchemaExtractor().GetObjectSchema(typeof(TestObject));
    var manager = delegator.GetSchemaManager(BackendType.MySql);

    await migrator.PlanAndExecute(
      connection: connection,
      targetManager: manager,
      targetSchema: target,
      options: new MigrationOptions()
      {
        AllowDataloss = false,
        ForceDropRecreate = false
      }
    );
}

Built With

  • Dapper - For querying and parameterization

About

Simple interface for creating/updating SQL schemas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages