Giter VIP home page Giter VIP logo

entityframework.serializableproperty.fody's Introduction

EntityFramework.SerializableProperty.Fody

This is an add-in for Fody

Sometimes it's needed to have an ability to store complex data in serialized form with Entity Framework, despite of it is not supported out of the box. The most common way to do it is write a backing property mapped directly to a database column which will contain the serialized data (as JSON or XML string - does not really matter). Getter of the property will call a serializer and perform necessary serialization logic, and setter will do deserialize operation. Threre are two main disadvantagies of such approach: the backing property should be public (in some cases it's undesirable) and necessity to write the explicit property's logic, that's lead to boilerplate coding.

The EntityFramework.SerializableProperty.Fody library solves the problem in a simple and elegant way. With usage of a technique named IL weaving it adds private backing property with required getter and setter logic and maps database column to it.

The nuget package NuGet Status

https://nuget.org/packages/EntityFramework.SerializableProperty.Fody/

PM> Install-Package EntityFramework.SerializableProperty.Fody

How to use

First, mark the complex property with SerializableProperty attribute. It tells to the Fody weaver that the backing property should be generated:

public class PurchaseOrder
{
    ...
    [SerializableProperty(typeof(SimpleJsonSerializer))]
    public List<Product> Products { get; set; }
    ...
}

Single argument of the attribute is a type of a serializer to be used to serialize/deserialize the complex property. Your serializer should implement ISerializer interface with two methods:

public interface ISerializer
{
    string Serialize(object obj);
    T Deserialize<T>(string str);
}

The last step is to configure the Entity Framework model type with calling Serialized extension method:

modelBuilder.Entity<PurchaseOrder>()
    .ToTable("Orders")
    .Serialized(t => t.Products)
    .HasKey(t => t.Id);

By default, mapped database column name will equal to the property name.
Optionally you can specify another column name by passing second argument to the extension method:

    .Serialized(t => t.Products, "DifferentColumnName")

What gets compiled

public class PurchaseOrder
{
    ...
    public List<Product> Products { get; set; }
    ...
    [CompilerGenerated]
    private string Products_serialized
    {
        get
        {
            return ((ISerializer)new SimpleJsonSerializer()).Serialize(this.Products);
        }
        set
        {
            this.Products = ((ISerializer)new SimpleJsonSerializer()).Deserialize<List<Product>>(value);
        }
    }
    ...
}

License

Licensed under MIT license.

Icon

Ribbon designed by Alexander Skowalsky from the Noun Project.

entityframework.serializableproperty.fody's People

Contributors

alexanderkrutov avatar

Stargazers

 avatar

Watchers

 avatar  avatar

entityframework.serializableproperty.fody's Issues

Incompatbile with fody 2

Hey guys,

this fody add-in is not compatible with fody version 2.0 and higher.

Can you please fix it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.