Forum General

How to persist a class in Application resourses

Hi there

I need to find an application-level storage for my client business class.

Where to keep the instance? In Resources? If so, could you give me a code example?



  • CodeMonkeyCodeMonkey GBMember ✭✭✭
    edited October 2013

    You could serialize you're objects and store them in a text file or some sort of shared resources.

            public static T DeserializeObjectToString<T>(string stringToDeserialize) 
                where T : class {
                // return reference.
                T ret = null;
                // the xml serialiser to serialise to the generic type set.
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                // using a string reader deserialise the string into an object.
                using (TextReader reader = new StringReader(stringToDeserialize)) {
                    // using the xml serialiser, deserialize the string to the type specified.
                    ret = serializer.Deserialize(reader) as T;
                // return the object created from a string.
                return ret;
            /// <summary>
            /// Serializes an object to a string
            /// </summary>
            /// <returns>string representation of the object</returns>
            /// <param name="objectToSerialize">Object to serialize.</param>
            public static string SerializeObjectToString(object objectToSerialize) {
                // return reference.
                string ret = null;
                // used to serialise the object into xml.
                XmlSerializer xmlSerializer = new XmlSerializer (objectToSerialize.GetType ());
                // used to contain the string xml 
                using (StringWriter textWriter = new StringWriter ()) {
                    // write the xml serialisation into the string writer.
                    xmlSerializer.Serialize (textWriter, objectToSerialize);
                    // set the return value to the text writer to string value.
                    ret = textWriter.ToString ();
                // returns the string representation for the object.
                return ret;
  • NikolayRenziglovNikolayRenziglov CAMember ✭✭

    Thanks, CodeMonkey. But I need an instance of the class available for the whole application: with its properties and methods.

    Let's say, the WCF Manager singleton. Once initialized with binding, endpoint, etc it can be used all over the place.

    In Silverlight app I can add it into App.Current.Resources. How about this environment? Is it possible?

  • KevinMullinsKevinMullins USMember, Beta ✭✭

    I usually create an object I call ApplicationState and pass it to ever other object I create throughout the lifecycle of the app. I then use it as the central communication portal between objects, passing app states, etc.

    Hope that helps!

  • NikolayRenziglovNikolayRenziglov CAMember ✭✭

    Hi KevinMullins.

    Smart solution, the only vague thing for me is where the app can store the instance of your ApplicationState class? Is it app level? Actually, I need the highest, the app, level.


Sign In or Register to comment.