Code First & Value Objects

May 3, 2012 at 6:53 AM

How to implement value objects concept of DDD using naked objects for .net framework.
Say, I have Customer & Supplier classes. Both have addresses. Here Address is a Value Objects.
I have used CustomerAddress & SupplierAddress classes for this purpose. For Example

public class CustomerAddress
{
    [Hidden]
    public virtual int AddressID { get; set; }

    [Hidden]
    public virtual int CustomerID { get; set; }

    [Disabled]
    public virtual Address Address { get; set; }

    [Hidden]
    public virtual Customer Customer{ get; set; }
}

But getting following errors

One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'CustomerAddress' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �CustomerAddresses� is based on type �CustomerAddress� that has no keys defined.

May 3, 2012 at 6:58 AM

I have added {key] attribute as below

public class CustomerAddress
{
    [Hidden]
    [Key]
           public virtual int AddressID { get; set; }

    [Hidden]
    [Key]
        public virtual int CustomerID { get; set; }

    [Disabled]
    public virtual Address Address { get; set; }

    [Hidden]
    public virtual Customer Customer{ get; set; }
}

But still getting following errors

Unable to determine composite primary key ordering for type 'CRM.Model.CustomerAddress'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

 

Editor
May 3, 2012 at 7:55 AM
You should be able to use EF "complex types" as a reasonable approximation for these ... they are understood by the NO MVC framework.

Here's an SO thread that shows complex

If that doesn't work for you (for whatever reason), then a reasonable compromise is to define some serialization format of the user type (eg a JSON doc) and store that in the DB as a simple string. Make this property [Hidden]. Then add a derived property that deserializes and returns the value type as necessary.

HTH
Dan



On 3 May 2012 06:53, mhossain <notifications@codeplex.com> wrote:

From: mhossain

How to implement value objects concept of DDD using naked objects for .net framework.
Say, I have Customer & Supplier classes. Both have addresses. Here Address is a Value Objects.
I have used CustomerAddress & SupplierAddress classes for this purpose. For Example

public class CustomerAddress
{
[Hidden]
public virtual int AddressID { get; set; }

[Hidden]
public virtual int CustomerID { get; set; }

[Disabled]
public virtual Address Address { get; set; }

[Hidden]
public virtual Customer Customer{ get; set; }
}

But getting following errors

One or more validation errors were detected during model generation:

System.Data.Edm.EdmEntityType: : EntityType 'CustomerAddress' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �CustomerAddresses� is based on type �CustomerAddress� that has no keys defined.

Read the full discussion online.

To add a post to this discussion, reply to this email (nakedobjects@discussions.codeplex.com)

To start a new discussion for this project, email nakedobjects@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com


Coordinator
May 3, 2012 at 8:46 AM
Edited May 3, 2012 at 8:49 AM

Unless you are particular about the form of keys you want used in the database tables, I recommend that you write:

public class CustomerAddress
{
    [Hidden]
    public virtual int CustomerAddressID { get; set; }

    [Disabled]
    public virtual Address Address { get; set; }

    [Hidden]
    public virtual Customer Customer{ get; set; } 
}

That way, CustomerAddress will have its own singular key (there is no need to add the [Key] annotation as CustomerAddressID will be picked up by convention).   Also note that I have not included the foreign key properties to Address and Customer.  I find this style of coding cleaner, but both are allowed.  

Please note, though, that the issues you encountered are arising from Entity Framework, not from Naked Objects  -  you would get the same errors doing a Code First project using EF without Naked Objects,