0

My application has the following classes:

public class Widget {        
    public virtual int Id { get; set; }
    public virtual WidgetType Type { get; set; }
    public virtual string Parameters { get; set; }
}

public class WidgetType {        
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string ParametersAssembly { get; set; }
    public virtual string ParametersClass { get; set; }
}

Now if i'd like to update the Parameters for a particular widget i would say something like:

// Get the widget
var widget = GetWidget(1);

// Create an instance of the type parameters class
var parameters = Activator.CreateInstance(Assembly.LoadFrom(Server.MapPath("~/bin/"
    + widget.Type.ParametersAssembly + ".dll")).GetType(widget.Type.ParametersClass));

... Code here to update the parameters

widget.Parameters = new XmlSerializer(parameters.GetType()).Serialize(parameters);

I have to do the reverse when i wish to get the parameters. You can imagine this becomes quite tedious. I was wondering if it was possibly to automatically do this?

I've been looking at the IUserType interface. I found an article which is kind of similar. However my problem is a little more complicated as my type changes based on the type of the widget.

I'd appreciate it if someone could let me know if this is possible and possibly how it could be achieved. Thanks

nfplee
  • 7,643
  • 12
  • 63
  • 124

1 Answers1

1

an easy way

public class Widget
{
    public virtual int Id { get; set; }
    public virtual WidgetType Type { get; set; }

    private string _serializedParameters;
    private virtual string SerializedParameters {
        get
        {
            return new XmlSerializer(Parameters.GetType()).Serialize(Parameters);
        }
        set
        {
            _serializedParameters = value;
            // code to deserialize the Parameters and set to Parameters
            var ptype = Assembly.LoadFrom(Server.MapPath("~/bin/" + widget.Type.ParametersAssembly + ".dll")).GetType(widget.Type.ParametersClass);
            Parameters = Activator.CreateInstance(ptype);
        }
    }
    private object _parameters;
    public virtual object Parameters
    {
        get
        {
            if (_parameters == null)
                _parameters = Activator.CreateInstance(Assembly.LoadFrom(Server.MapPath("~/bin/" + widget.Type.ParametersAssembly + ".dll")).GetType(widget.Type.ParametersClass));
            return _parameters;
        }
        set { _parameters = value; }
    }
}

it can't be in the Parameters property because then you have to get -> alter -> set instead of get -> alter. But you are right that building the parameters object should go in the getter of Parameters because only there we can be sure to have the WidgetType loaded

it is essentially the same as a UserType except that we know that WidgetType is there

Firo
  • 30,626
  • 4
  • 55
  • 94
  • Thanks i can see this working but i'm not sure i fully understand it. Is the Parameters property be one which is mapped to the database? If so then shouldn't it be a string and the the SerializedParameters would return an object? – nfplee Feb 27 '12 at 11:19
  • `SerializedParameters` is the property mapped to db. it contains the conversion which needs other properties to work so its harder to do in a UserType where the other properties may not be initialized. `Parameters` is not mapped at all – Firo Feb 27 '12 at 11:39
  • Still not having much joy. Sorry to be a pain but are you sure the serialization and deserialization code goes in the SerializedParameters property and not the Parameters property? If you could provide an example of getting and setting the property based on my scenario that would be appreciated. Thanks – nfplee Feb 27 '12 at 15:36
  • Thanks for the amendment. It helped alot. – nfplee Feb 28 '12 at 09:18