Generic Entity?

Posts   
 
    
MikeH
User
Posts: 3
Joined: 21-Oct-2005
# Posted on: 21-Oct-2005 19:05:00   

Hi all!

I am starting a new project that will require XML web services. I am using the adapter scenario and am serializing/deserializing to/from XmlNode objects to get the data across the wire. Here's some example code:


[WebMethod]
public XmlNode GetClientDetails(int clientId)
{
    DataAccessAdapter da = new DataAccessAdapter();

    ClientEntity client = new ClientEntity(clientId);
    da.FetchEntity(client);

    // Get the client's classes
    da.FetchEntityCollection(client.Class, client.GetRelationInfoClass());

    return LLBLGenSupport.EntityToXmlNode(client);
}

[WebMethod]
public XmlNode SaveClient(XmlNode entity)
{
    ClientEntity client = new ClientEntity();
    client.ReadXml(entity);

    DataAccessAdapter da = new DataAccessAdapter();
    da.SaveEntity(client);

    return LLBLGenSupport.EntityToXmlNode(client);
}

The LLBLGenSupport class is a simple helper class that I have running on both the web service and a test client app:


public class LLBLGenSupport
{
    /// <summary>
    /// Converts the given Entity object into an XmlNode object for transport over a web method
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public static XmlNode EntityToXmlNode(EntityBase2 entity)
    {
        XmlDocument doc = new XmlDocument();
        XmlNode node;

        entity.WriteXml(doc, out node);

        return node;
    }
}

All of this code works great. My question is this: is there any way that I can write a "Generic" Save method that will take in any kind of entity and save it properly? The EntityBase2 class is abstract, so I can't instatiate it to be able to execute the ReadXml method for it. I tried writing a "GenericEntity" class that inherits from EntityBase2, but I didn't have much luck. What would be perfect would be a factory method that takes an XmlNode as input and returns an EntityBase2 object that I can then pass to the SaveEntity method (or cast it to the type I need, or whatever).

Is there any method that would allow me to do that?

Thanks!

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 22-Oct-2005 10:06:55   

No that's not possible, as XmlWebservices don't support polymorphism. We found that out 2 days ago in testing polymorphic methods in webservices using object hierarchies. Webmethods have to specify the exact type of the object they expect/return.

Frans Bouma | Lead developer LLBLGen Pro