Entity Framework 4 – Immutable Value Objects

Ok, the title is not quire accurate, I’m not aware of any way to accomplish truly immutable types for Entity Framework.

However, this is a quite nice attempt IMO:

    public class Address
    {
        //Private setters to avoid external changes
        public string StreetName { get;private set; }
        public string City { get; private set; }
        public string ZipCode { get; private set; }

        //Provide a default ctor for EF4
        [Obsolete("For EF4 Only",true)]
        public Address() {}

        //Force values to be set via ctor.
        public Address(string streetName, string city, string zipCode)
        {
            StreetName = streetName;
            City = city;
            ZipCode = zipCode;
        }

        ...equality overrides and such...
    }

This works very well with Entity Framework 4 and I think it is a fair compromise.

//Roger


One Response to Entity Framework 4 – Immutable Value Objects

  1. Humberto says:

    Hello,

    How can you map value objects in EF4 ?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>