Ask a questionAsk a question
 

AnswerExposing Business Objects

  • Friday, October 30, 2009 9:51 PMOK24 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I want to pass a BO that is a collection of different other BO's into my WS, i am using C# to create my WS how do i expose my BO's to Clients?

Answers

  • Monday, November 02, 2009 10:05 AMNarayanan Dayalan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You need to serialize the BO objects to String and then u can get the string in Windows forms or ASP.NET web forms by de-serializing back to object...


    Code to Serilize
    ------------------
            public string XMLSerialize(object pObject)
            {
                try
                {
                    XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
                    using (StringWriter stream = new StringWriter())
                    {
                        objXMLSerilize.Serialize(stream, pObject); stream.Flush();
                        return stream.ToString();
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw ex;
                }
            }

    Code to De-Serialize
    ------------------------
            public object XMLDeSerialize(object pObject, string strXMLString)
            {
                try
                {
                    XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
                    using (StringReader stream = new StringReader(strXMLString))
                    {
                        return objXMLSerilize.Deserialize(stream);
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw new InvalidOperationException("Failed to create object from xml string", ex);
                }
            }


    How to Serialize using the above code
    --------------------------------------------
    fg_sXMLString = XMLSerialize(objFOTrnCodeMstList);


    How to De-Serialize using the above code
    --------------------------------------------
    objFOTrnCodeMstList = (List<FOTrnCodeMst>)XMLDeSerialize(objFOTrnCodeMstList, fg_sXMLString);

    in the above code objFOTrnCodeMstList is my BO object, and fg_sXMLString is the serialized string



    sounds simple.... just try it out :-)



    Narayanan Dayalan - Zeetaa Business Solutions

All Replies

  • Sunday, November 01, 2009 4:48 AMtechnocrat_aspire Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    You will have to convert the custom collection to a list or array so that they can be serialized. Look at this example


    It uses generics to return a list of custom Business Objects.

  • Monday, November 02, 2009 10:05 AMNarayanan Dayalan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    You need to serialize the BO objects to String and then u can get the string in Windows forms or ASP.NET web forms by de-serializing back to object...


    Code to Serilize
    ------------------
            public string XMLSerialize(object pObject)
            {
                try
                {
                    XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
                    using (StringWriter stream = new StringWriter())
                    {
                        objXMLSerilize.Serialize(stream, pObject); stream.Flush();
                        return stream.ToString();
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw ex;
                }
            }

    Code to De-Serialize
    ------------------------
            public object XMLDeSerialize(object pObject, string strXMLString)
            {
                try
                {
                    XmlSerializer objXMLSerilize = new XmlSerializer(pObject.GetType());
                    using (StringReader stream = new StringReader(strXMLString))
                    {
                        return objXMLSerilize.Deserialize(stream);
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw new InvalidOperationException("Failed to create object from xml string", ex);
                }
            }


    How to Serialize using the above code
    --------------------------------------------
    fg_sXMLString = XMLSerialize(objFOTrnCodeMstList);


    How to De-Serialize using the above code
    --------------------------------------------
    objFOTrnCodeMstList = (List<FOTrnCodeMst>)XMLDeSerialize(objFOTrnCodeMstList, fg_sXMLString);

    in the above code objFOTrnCodeMstList is my BO object, and fg_sXMLString is the serialized string



    sounds simple.... just try it out :-)



    Narayanan Dayalan - Zeetaa Business Solutions