-
Notifications
You must be signed in to change notification settings - Fork 5
Customization
CherrySeed is built from scratch with one architecture objective - customization and extensibility. This means that it is possible to create your own custom data providers, repositories, primary key ID generators, etc.
Implementing a custom data provider is easy. Create a class which inherits from IDataProvider.
public class MyCustomDataProvider : IDataProvider
{
public List<EntityData> GetEntityData()
{
...
}
}
The method GetEntityData() returns a list of objects of the class EntityData. The class EntityData consists of a property EntityName. This property should match the name of the entity class. For more info see How to have a match.
Property Objects defines test data as Dictionary<string, string>. The key of the dictionary contains the field name and the value of the dictionary contains the corresponding field value.
var entityData = new EntityData
{
EntityName = "MyNamespace.MyEntity",
Objects = new List<Dictionary<string, string>>
{
new Dictionary<string, string>
{
{ "Id", "1" },
{ "Name", "Michael" },
{ "Date", "2016-01-01" },
{ "IsEnabled", "true" }
}
}
};
To set your self-created data provider globally see Setting Global Data Provider.
Create a new class which inherits from the interface IRepository.
public class MyCustomRepository : IRepository
{
public void SaveEntity(object obj)
{ ... }
public void RemoveEntities(Type type)
{ ... }
public object LoadEntity(Type type, object id)
{ ... }
}
The method SaveEntity(object obj) creates a new entry of a specific entity in the database. The method RemoveEntities(Type type) removes all entries from a specific entity in the database. The parameter type is the entity class. The method LoadEntity(Type type, object id) loads a specific entry of a specific entity from the database. The parameter type is the entity class and parameter id is the primary key ID.
To set your self-created repository globally see Setting Global Repository or to set it entity-specific see Setting Entity Specific Repository.
Create a new class which inherits from IPrimaryKeyIdGenerator.
public class MyCustomIdGenerator : IPrimaryKeyIdGenerator
{
public object Generate()
{ ... }
}
The method Generate() should return an unique identifier. It depends on the implementation which datatype is returned. To set your self-created ID generator globally see Setting Global ID Generator or to set it entity-specific see Setting Entity Specific ID Generator.
To create a custom type transformation you have to create a class which inherits from ITypeTransformation. The method Transform(Type type, string str) transforms NOT nullable values. On the other hand, the method TransformNullable(Type type, string str) is responsible for transforming nullable values.
public class CustomTypeTransformation: ITypeTransformation
{
public object Transform(Type type, string str)
{
// do here some magic transformations
}
public object TransformNullable(Type type, string str)
{
// do here some magic transformations
}
}
Finally, you have to register this custom type transformation with the method AddTypeTransformation(Type type, ITypeTransformation transformation). With this registration CherrySeed knows this custom type transformation and uses it automatically if the destination type is the registered type.
var config = new CherrySeedConfiguration(cfg =>
{
cfg.AddTypeTransformation(typeof(customType), new CustomTypeTransformation());
...
});
It is also possible to override built-in type transformations. For example, to override the type transformation of type string you have to configure CherrySeed in the following way:
var config = new CherrySeedConfiguration(cfg =>
{
cfg.AddTypeTransformation(typeof(string), new MySpecialStringTransformation());
...
});