views:

128

answers:

3

Hi,

I have an entity class Customer which as the property Address that is an object of the Address class and that has few properties. It looks as follows:

public partial class Customer
{
    public virtual int ID { get; set; }

    public virtual string Symbol { get; set; }

    public virtual string Name { get; set; }

    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }

    public virtual string NIP { get; set; }

    public virtual Address Address { get; set; }
}

public partial class Address
{
    public virtual int ID { get; set; }

    public virtual string Descriptive { get; set; }

    public virtual string Street { get; set; }

    public virtual string City { get; set; }

    public virtual string PostCode { get; set; }

    public virtual string Country { get; set; }
}

There's no relation between them in the sanse of database or entities. The Address is just an object contained in the Customer class and they are produced by an NHibernate framework. In fact the <component /> mapping element is being used for that:

<class name="Customer" table="`CRM_CUSTOMER`">
    <id name="ID">
        <generator class="native" />
    </id>
    <property name="Symbol" unique="true" />
    <property name="Name" />
    <property name="FirstName" />
    <property name="LastName" />
    <property name="NIP" />
    <component name="Address" class="Address">
        <property name="Descriptive" />
        <property name="Street" />
        <property name="City" />
        <property name="PostCode" />
        <property name="Country" />
    </component>
</class>

However, the client generated code does not see an Address class/property at all. Nothing helps. Have read tons of articles and nothing. So if anyone could help, than it would be really really appreciated :)

I am using VS 2010 Proffesional.

TIA

Roland

A: 

Try and decorate the Customer class address property with the [Include] attribute. This should prompt RIA to return the Address property.

If this doesn't work I'd see if the server-side domain service method has the address property populated correctly and if the problem is in passing the data to your Silverlight client.

You could then try and treat Address as a RIA domain entity and build a relationship between customer and address. The include approach should then produce results.

Hope this helps.

Rus
A: 

Hello,

[Sorry for the delay... I'm back here]

The [Include] attribute does not help, unfortunatelly. It is required to add an [association] accompanying attribute. But this implies what I wanted to avoid - enforced additional fields and implementations to the domain objects that were intented to be simple and without such unnecessarities and which were intented for ORM mapping (NHibernate).

The solution is quite invasive and introduced all the stuff required for an associations with respect to the entities - IDs. This also has had its impact on the mapping as I needed to put the <parent /> element inside the <component />. Just to get an ID from the parent - have had no better idea on how to get it...

Finally, the domain classes became as follows:

[MetadataType(typeof(CustomerMetadata))]
public partial class Customer
{
    [DataContract]
    internal class CustomerMetadata
    {
        [Key]
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Symbol { get; set; }

        [DataMember]
        public string Name { get; set; }

        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }

        [DataMember]
        public string NIP { get; set; }

        [Include]
        [Association("Customer_Address", "ID", "CustomerID", IsForeignKey = true)]
        [DataMember]
        public Address Address { get; set; }
    }
}

...and

public partial class Customer
{
    public virtual int ID { get; set; }

    public virtual string Symbol { get; set; }

    public virtual string Name { get; set; }

    public virtual string FirstName { get; set; }

    public virtual string LastName { get; set; }

    public virtual string NIP { get; set; }

    public virtual Address Address { get; set; }
}

To make it work, the mapping has been changed to the following:

<class name="Customer" table="`CRM_CUSTOMER`">
    <id name="ID">
        <generator class="native" />
    </id>
    <property name="Symbol" unique="true" />
    <property name="Name" />
    <property name="FirstName" />
    <property name="LastName" />
    <property name="NIP" />
    <component name="Address" class="Address">
        <parent name="Customer" />
        <property name="Descriptive" />
        <property name="Street" />
        <property name="City" />
        <property name="PostCode" />
        <property name="Country" />
    </component>
</class>

The <parent /> element is new there. I am not only sure whether an optional attribute to make the association a foreign key association was correct...

I was also surprising to me that I don't neeed any domain service for an address itself as the generated code contains it and all other things which I expected to occure at the very beginning of the story with the RIA services...

The solution became really invasive and as such not the perfect for me but for now at least satisfying. For now it works. I only wonder wat I shall do with many-to-many association in NH?

Hope this helps someone. Cheers.

Roland

Roland
A: 

One more thing... I forgot about the Address class. Here it is:

[MetadataType(typeof(AddressMetadata))]
public partial class Address
{
    public virtual int CustomerID
    {
        get { return Customer.ID; }
    }

    [DataContract]
    internal class AddressMetadata
    {
        [Key]
        [DataMember]
        public int CustomerID { get; set; }

        [DataMember]
        public Customer Customer { get; set; }

        [DataMember]
        public string Descriptive { get; set; }

        [DataMember]
        public string Street { get; set; }

        [DataMember]
        public string City { get; set; }

        [DataMember]
        public string PostCode { get; set; }

        [DataMember]
        public string Country { get; set; }
    }

...and:

public partial class Address
{
    public virtual Customer Customer { get; set; }

    public virtual string Descriptive { get; set; }

    public virtual string Street { get; set; }

    public virtual string City { get; set; }

    public virtual string PostCode { get; set; }

    public virtual string Country { get; set; }
}
Roland