简体   繁体   English

NHibernate-IUserType用于更改类型?

[英]NHibernate - IUserType For Changing Type?

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. 我一直在看IUserType接口。 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 谢谢

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 . 它不能在Parameters属性中,因为那样您就必须get -> alter -> set而不是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 但是您没错,构建parameters对象应该放在Parameters的getter中,因为只有在那儿,我们才能确保已加载WidgetType

it is essentially the same as a UserType except that we know that WidgetType is there 它与UserType基本相同,只是我们知道WidgetType在那

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM