First List of Entity Framework Code Mapping of Properties

entity-framework mapping model

Question

I'm starting off by working with Entity Framework code. I have an easy model.

public class Variable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public IList<string> TextOptions
    {
        get;
        set;
    }
}

I had problems with the property.TextOptions sorting outList<String> .

This doesn't map when I attempt to implement it in Entity Framework.

On this site (stackoverflow), I discovered a remedy for my problem. I essentially rewrote my class such that it now accepts the list and converts it into a delimited string that is saved instead:

public class Variable : IVariable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public virtual IList<string> TextOptions
    {
        get
        {

            return _TextOptions;

        }
        set
        {
            _TextOptions = value;
        }
    }

    private IList<string> _TextOptions;

    public string TextOptionsSerialized
    {
        get
        {
            return String.Join(";", _TextOptions);
        }
        set
        {
            _TextOptions = value.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }
}

The code is effective. The issue I have with it is that, in my opinion, it breaches the principle of separation of concerns. My model class shouldn't need to worry about serializing a list of strings so that Entity framework can persist it, in my opinion.

While using ASP.Net MVC, I encountered a similar problem. I received a client post that would be mapped to a model. Compared to the post, there were various problems with the model's organizational structure. I was able to manage the conversion in MVC by creating a Custom Model Binder that was both safe and reusable.

Is there ANY way to do this using Entity Framework that is as streamlined as using Custom Model Binders for MVC?

1
23
11/29/2011 9:08:17 PM

Accepted Answer

No type converters or unique type mappers are available in EF as an alternative to model binders from MVC. To induce persistence, you must always utilize some kind of hack. Maps are another similar method.TextOptions is a grouping of connected items. Although it will complicate your model and working with it, it will improve your separation of concerns.Variable .

public class Variable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public IList<TextOption> TextOptions
    {
        get;
        set;
    }
}

public class TextOption
{
    public int Id { get; set; }
    public string Text { get; set; }
}
26
11/30/2011 11:19:45 AM

Popular Answer

Utilizing JSON.NET for the serialization would be a third choice.

Using the three alternatives given in this thread (String split serialization, JSON.NET, and adding an entity), I conducted some performance experiments for write situations and discovered that JSON.NET produces the best results.

Initial write results for 200 equal entities (perform the test on your own by seeing the source code here.) are as follows:

  • 896 milliseconds were needed to write entities using the string serializer.
  • 516 milliseconds for writing entities using the json serializer.
  • 706 milliseconds were needed to write entities utilizing multiple entities.

JSON serializer example:

public class VariableJson
{
    public string Name { get; set; }

    public int Id { get; set; }

    public virtual IList<string> TextOptions
    {
        get
        {

            return _TextOptions;

        }
        set
        {
            _TextOptions = value;
        }
    }

    private IList<string> _TextOptions;

    public string TextOptionsSerialized
    {
        get
        {
            return JsonConvert.SerializeObject(_TextOptions);
        }
        set
        {
            _TextOptions = JsonConvert.DeserializeObject<IList<string>>(value);                    
        }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow