FutureStateMobile / AppCore.Data

A Portable, cross-platform, light weight, opinionated ORM designed to work across multiple Databases
9 stars 5 forks source link

Add support for custom types #7

Closed ChaseFlorell closed 10 years ago

ChaseFlorell commented 10 years ago

We have a separate application where we have a custom LatLong type. We need to be able to add custom types without recompiling AppCore.Data.

ChaseFlorell commented 10 years ago

This was completed in SHA: 5b51f282d0fe060e20c430011cb211bcfd2b54ca

Usage:

First create a custom type interface in your shared PCL.

public interface ICustomDialect
{
    string LatLong { get; }
}

Then implement it in each of your platform specific code.

// This is in a Server project
public class CustomDialect : ICustomDialect
{
    public string LatLong { get { return "decimal(9, 6)"; } }
}

And register it

container.Register<ICustomDialect, CustomDialect>();

From there, you can use it in your migrations.

public class Migration001: Migration {
    private readonly ICustomDialect _customDialect;
    public Migration001(ICustomDialect customDialect) {
        _customDialect = customDialect;
    }
    public override void Migrate() {
        var locationTable = database.AddTable("Locations");
        timeEventTable.AddColumn("Latitude", typeof (LatLong)).AsCustomType(_customDialect.LatLong); // here's where you add support
        timeEventTable.AddColumn("Longitude", typeof (LatLong)).AsCustomType(_customDialect.LatLong); // and here also
        DbProvider.ExecuteNonQuery(migration.GenerateDDL(database));
    }
}

Check the Test if you're stumped.