views:

26

answers:

1

Hi there,

I'm working on an event-planning application for the contacts in a phone book. Avoiding all the public virtual and protected stuff, my Contact class looks like:

class Contact {
    //...
    Int32 Id { get; private set; } //primary key;
    String Name { get; private set; } 
    //...
}

A customer asked me to handle both his own phone book and my application's one. So I thought to extract an IContact interface from Contact, and to add another class InternalContact (this name sucks, I know), implementing the same interface. The problem is that the customer's db uses an assigned string as a primary key, so Contact''s Id type and InternalContact's Id type will be different. Is it possible to map the Invitation.Contact property using an <any> type mapping, even is the Id types are different?

Thanks in advance, Giulio

A: 

Not sure if this is what you are asking but you could do something like this to create the classes:

interface IContact<T>
{
    T Id { get; }
}

public class Contact : IContact<int>
{
    public int Id { get; private set; }
}

public class InternalContact : IContact<string>
{
    public string Id { get; private set; }
}
spinon
Generic? Uhm... sounds interesting. ;-) But is it possible? What should I write in the mapping file? Any link to any example about this technique? Thanks.
petrux
Yes this is possible. Here is a link to an introduction:http://msdn.microsoft.com/en-us/library/ms379564(VS.80).aspx
spinon
It seems like you didn't get the 'core' of my question: I know what generics are, I don't know how to implement a polymorphic mapping with NHibernate where entities' Id are of different types. Or maybe I'm missing something?
petrux