Extension - Provide your own IDataSourceDefinition

The DefaultDataSourceDefinition uses reflection to extract the serialized properties from your persisted elements.
Implementing a new IDataSourceDefinition allows to choose an other way to provide/extract the properties.

Business Data

/// <summary>
/// Player class - basic version.
/// </summary>
public class Player
{
    /// <summary>
    /// Gets or sets the identifier of the player.
    /// </summary>
    public int Identifier { get; set; }

    /// <summary>
    /// Gets or sets the name of the player.
    /// </summary>
    public string Name { get; set; }
}

Extension

This implementation of IDataSourceDefinition will be preconfigured.

private class MyDataSourceDefinition : IDataSourceDefinition
{
    private readonly ICollection<IClassDefinition> classes;

    private readonly ICollection<IIndexDefinition> indexes;

    public MyDataSourceDefinition()
    {
        this.classes = new LinkedList<IClassDefinition>();
        this.indexes = new LinkedList<IIndexDefinition>();

        // Add the definition for the player class
        this.Classes.Add(new PlayerClassDefinition());
    }

    public string Name
    {
        get { return "TeamDS"; }
    }

    public ICollection<IClassDefinition> Classes
    {
        get { return this.classes; }
    }

    public ICollection<IIndexDefinition> Indexes
    {
        get { return this.indexes; }
    }
}

... and we provide our own implementation of IClassDefinition targeting the Player class.
The other interfaces part of the definition are the IPropertyDefinition and the IIndexDefinition. Our sample doesn't implements those two interface but you can do so when the default behaviors don't fit your requirements.

private class PlayerClassDefinition : IClassDefinition
{
    private readonly ICollection<IPropertyDefinition> properties;

    public PlayerClassDefinition()
    {
        this.properties = new LinkedList<IPropertyDefinition>();

        // The properties are directly registered
        this.properties.Add(new Api.DefaultPropertyDefinition(
            typeof(int),
            i => ((Player)i).Identifier,
            (i, v) => ((Player)i).Identifier = (int)v));
        this.properties.Add(new Api.DefaultPropertyDefinition(
            typeof(string),
            i => ((Player)i).Name,
            (i, v) => ((Player)i).Name = (string)v));
    }

    public Type Type
    {
        get { return typeof(Player); }
    }

    public Type KeyType
    {
        get { return typeof(int); }
    }

    public ICollection<IPropertyDefinition> Properties
    {
        get { return this.properties; }
    }

    public object RetrieveKey(object value)
    {
        return ((Player)value).Identifier;
    }

    public object CreateInstance(object key)
    {
        return new Player();
    }
}

Integration

The creation of the definition becomes easier and the way to use the library remains the same.

// Create a new data source definition based on a specific initialization process
// by the way, with a such approach the configuration can be outsourced.
IDataSourceDefinition definition = new MyDataSourceDefinition();

// Initialize the data source with this definition
IDataSource ds = Engine.Instance.RegisterDataSource(definition);

// Create business objects
Player player = new Player() { Identifier = 1, Name = "Superman" };
ds.Save(player);
Player loaded = ds.Load<Player>(1);

More About Definition Extension

The definition is based on four interfaces - and each have a default implementation that can be used "as-this" or override.

Interface Default implementation Description of the implementation
IDataSourceDefinition DefaultDataSourceDefinition Provides methods to add new class or index definitions
IClassDefinition Api.DefaultClassDefinition Uses reflection to extract serializable properties from the class
IPropertyDefinition Api.DefaultPropertyDefinition Uses Action/Func to map stored properties to getter and setter
IIndexDefinition Api.DefaultIndexDefinition Uses a predefined Func to map an index with its values

Last edited Mar 31, 2011 at 7:15 PM by Faz, version 3

Comments

No comments yet.