views:

53

answers:

4

Hi,

I have a class which stores data collected by the asp.net webserver. It contains several properties like:

private string _actionName;
public string ActionName
    {
        get
        {
            if (_actionName == null)
                _actionName = Request.Params["action_name"] != null
                                  ? Server.UrlDecode(Request.Params["action_name"])
                                  : "";

            return _actionName;
        }
    }

This class is serialized to a file. Basically the data collected by webserver is written to files. Later these files are read and deserialized and the properties data needs to be imported to database offline.

My question is whether the above code will serialize the properties correctly extracting data from query string and later when the data is deserialized the properties are correctly populated and returned?

I am using binary serialization.

Thanks in advance.

A: 

Depending on the context of the serialization, the property ActionName will have a value or not when serializing and therefore will retrieve it or not after deserialization.

But you should not hope for Request.Params["action_name"] to get its value back if the source of the data was in there at the beginning of the process.

Yoann
A: 

If the instance gets serialized before the ActionName property gets read at least once, it will not serialize properly. I would propose these changes, but even in this case you must be absolutelly sure that the serialization is occuring on the request thread (ie. so you have access to Request and Server).

[NonSerialized]
private string _actionName; 
public string ActionName 
    { 
        get 
        { 
            if (_actionName == null) 
                _actionName = Request.Params["action_name"] != null 
                                  ? Server.UrlDecode(Request.Params["action_name"]) 
                                  : ""; 

            return _actionName; 
        } 
        set
        {
            _actionName= value;
         }
    }
Radu094
A: 

This will work with Binary serialization if _actionName has a value. Otherwise after deserialization if would look at the Request or Server objects to fetch the value again which might not be expected.

Bharath K
A: 

I think that others has already advised you about caveats - so for serialization to work, you must a) Invoke each of property getter before serialization b) After deserialization, must modify each property field to some not null default value (e.g. String.Empty).

Now if option b is feasible then

  1. If you have only one such class the implement ISerializable and satisfy conditions a & b during serialization.
  2. If you have many such classes then relatively easy solution could be to have a generic serialization surrogate for your types that will probably use reflection (to do property get and field set) and ensure that conditions a and b gets satisfied during serialization.

If b is not feasible then I am afraid that you don't have any other choice but to update property definitions.

VinayC