"Extendable" is an open source .NET Core library that lets you add dynamic fields to your objects with almost no effort.
-
Add new fields to your types without changing the underlying database table.
-
Simple to use:
obj.SetAttribute("LastName", "Menz"); string lastName = obj.GetAttribute<string>("LastName") -
Multi-language support for your fields.
obj.SetAttribute("LastName", "Menz", "ar"); string arLastName = obj.GetAttribute<string>("LastName", "ar") -
Works with any data store (SQL Server, Oracle, etc.), since you implement the field provider.
-
Built-in caching layer for better performance.
This section walks you through installing the library and integrating it into an existing project, step by step.
This library targets .NET Core 2.0. Contributions to support more frameworks are welcome.
Available as a NuGet package, updated after each successful build (continuous integration).
Package: https://www.nuget.org/packages/Extendable
PM> Install-Package Extendable
- First, implement the
IExtendableinterface in the types you want to extend with dynamic fields.
public class User : IExtendable
{
public int Id { get; set; }
public string Name { get; set; }
}
- Then, create your own field provider to store field values in your permanent store (SQL Server, Oracle, files, XML, etc.). You only need to override these 3 methods; everything else works as is.
Example provider using Entity Framework:
public class ApplicationDbContext : DbContext
{
// ...
public DbSet Fields { set; get; }
// ...
}
public class EntityFrameworkFieldProvider : BaseFieldProvider
{
public override void UpdateFieldInDb(Field field)
{
using (var context = new ApplicationDbContext())
{
var entity = context.Fields.Find(field.Id);
entity.LastUpdatedUtc = field.LastUpdatedUtc;
entity.FieldValue = field.FieldValue;
context.SaveChanges();
}
}
public override Field GetFieldFromDb(string holderType, string holderId, string fieldName, string language = "en")
{
using (var context = new ApplicationDbContext())
{
return context.Fields.SingleOrDefault(f => f.HolderType == holderType
&& f.HolderId == holderId
&& f.FieldName == fieldName
&& f.Language == language);
}
}
public override void AddFieldValueToDb(Field field)
{
using (var context = new ApplicationDbContext())
{
context.Fields.Add(field);
context.SaveChanges();
}
}
}
Another example, used for testing: InMemoryFieldProvider.
Configure the library at application startup:
Extendable.Configuration.FieldProvider = new EntityFrameworkFieldProvider(); // your field provider, mandatory. Extendable.Configuration.CacheEnabled = true; // true by default Extendable.Configuration.CacheSizeLimit = 1 * 1024 * 1024; // 1 MB by default
- Set and read dynamic fields on your objects:
//Set Attributes
user.SetAttribute("LastName", "Menz");
user.SetAttribute("LastName", "الكنية", "ar");
user.SetAttribute("LastName", "soyadı", "tr");
user.SetAttribute("Age", 28);
//Get Attributes
string enLastName = user.GetAttribute<string>("LastName");
string arLastName = user.GetAttribute("LastName", "ar");
string trLastName = user.GetAttribute("LastName", "tr");
int age = user.GetAttribute<int>("Age");
- xUnit - Unit Testing Framework
Contributions are welcome. You can reach me at me@aldenmenzalji.com.
We use SemVer for versioning.
- Alden - https://github.com/alden-m
See also the list of contributors who participated in this project.
This project is licensed under the MIT License. See the LICENSE file for details.