Skip to content

About

"Extendable" (.NET CORE 2.0) is an open source project allows you to extend the types in your projects by introducing new dynamic properties with literally no effort.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

26 Commits

Folders and files

Repository files navigation

Extendable

"Extendable" is an open source .NET Core library that lets you add dynamic fields to your objects with almost no effort.

Features

  • 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.

Getting Started

This section walks you through installing the library and integrating it into an existing project, step by step.

Prerequisites

This library targets .NET Core 2.0. Contributions to support more frameworks are welcome.

Installing

Available as a NuGet package, updated after each successful build (continuous integration).

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

PM> Install-Package Extendable

Integrate with your project

  • First, implement the IExtendable interface 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.

Configuration

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

Library API and how to use it

  • 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");

Build Server

Build status

Dependencies

  • xUnit - Unit Testing Framework

Contributing

Contributions are welcome. You can reach me at me@aldenmenzalji.com.

Versioning

We use SemVer for versioning.

Author

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

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

"Extendable" (.NET CORE 2.0) is an open source project allows you to extend the types in your projects by introducing new dynamic properties with literally no effort.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages