Skip to content

Latest commit

 

History

History
173 lines (114 loc) · 5.29 KB

README.md

File metadata and controls

173 lines (114 loc) · 5.29 KB

Extendable

"Extendable" is an open source project under .net core framework, with "Extendable" you can add dynamic fields to your objects with literally no effort.

Features

  • Introduce new fields to your types without changing underlying table in database.

  • You can use it as simple as

    obj.SetAttribute("LastName", "Menz");
    string lastName = obj.GetAttribute<string>("LastName")
    
  • Multi-Languages support for your fields.

    obj.SetAttribute("LastName", "Menz", "ar");
    string arLastName = obj.GetAttribute<string>("LastName", "ar")
    
  • Work with any data provider (SQL Server, Oracle ..etc) as you are the one who implements the field provider.

  • Caching layer for your fields to enhance the performance of your application.

Getting Started

I'll walk you through this library and how to install and integrate to your exising project step by step.

Prerequisites

This library is targeting .net core 2.0 application, feel free to contribute to this repository to target more frameworks.

Installing

This projects is available as NuGet package being updated whenever ther's a successful build (continuous integration).

You need to get this package installed in your project.

Package Link : https://www.nuget.org/packages/Extendable

PM> Install-Package Extendable

Integrate with your project

  • Firstly, Implement IExtendable interface in the types that 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 to your permanent store (SQL Server, Oracle, Files, Xml ..etc) All what you need is to override these 3 methods. All other methods will work fine after your changes.

Here you can find example of Implementing such provider with EntityFramework for instance.

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();
        }
    }
}

You can find another example here InMemoryFieldProvider for testing purposes.

Configuration

At the application startup you need to configure this library properly, as following :

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

Library API and how to use it

  • Now you can set new dynamic fields to your object as simple as
//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");

Build Server

Build status

Dependencies

Contributing

Feel free to contribute to this repository, you can reach me out by the email eng.ngmalden@gmail.com

Versioning

We use SemVer for versioning.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details