【问题标题】:C# Mapping struct in ActiveRecordActiveRecord 中的 C# 映射结构
【发布时间】:2010-02-01 19:21:33
【问题描述】:

我正在做一个小应用程序来帮助我平衡我的支票簿。 我正在使用 Castle ActiveRecord 将对象属性映射到数据库。现在这是问题所在。当我在做一个赚钱程序时,我做了一个结构货币

结构:

public struct Currency
{
    private long amount;
    private CurrencyType currencyType;

    public long Amount
    {
        get { return this.amount; }
        set { this.amount = value; }
    }

    public CurrencyType CurrencyType
    {
        get { return this.currencyType; }
        set { this.currencyType = value; }
    }
}

我正在映射的类:

[ActiveRecord("[Transaction]")]
public class Transaction: HasOwnerModelBase
{
    private Currency amount;
    private Category category;

    [BelongsTo]
    public virtual Category Category
    {
        get { return this.category; }
        set { this.category = value; }
    }

    public virtual Currency Amount
    {
        get { return this.amount; }
        set { this.amount = value; }
    }
}

现在,在理想情况下,Currency 对象的行为类似于嵌套对象,因此 amount 和 currencyType 是交易表中的两个列。 但它不是嵌套视图,因为我希望它像货币结构对象一样。

我不知道我应该给货币金额添加什么标签才能让它工作,如果有人能帮助我解决这个问题,我将不胜感激。

我希望这一切都清楚。

伟大的邓肯

【问题讨论】:

    标签: c# struct nhibernate-mapping castle-activerecord


    【解决方案1】:

    接下来呢?还是我没有得到问题?请注意,我将结构更改为类,因为您需要虚拟成员来生成动态代理,并且结构上不能有虚拟成员。顺便问一下,你为什么不使用自动实现的属性?

    public class Currency
    {
        [Property]
        public virtual Int64 Amount { get; set; }   
    
        [Property]
        public virtual CurrencyType CurrencyType { get; set; }
    }
    
    [ActiveRecord("[Transaction]")]
    public class Transaction: HasOwnerModelBase
    {
        [BelongsTo]
        public virtual Category Category { get; set; }
    
        [Nested]  
        public virtual Currency Amount { get; set; }
    }
    

    【讨论】:

    • 嗯,虚拟的东西我知道我不好,但对于这个例子来说并不重要。自动属性的事情是,在工作中我们仍然必须使用 vs2005,所以还不习惯;)嵌套确实是一个好主意,但我已经很难做到这一点。我现在无法真正测试它,但我很确定当我从数据库中提取交易时获取的对象不是货币对象。如果是这样的话。我真的很笨
    • 它应该是 Currency 类的实例或从 Currency 派生的动态代理的实例,具体取决于您是否使用透明延迟加载。
    • 我会在明天可以正确测试时尝试一下。将更新thanx以获得帮助!
    • +1 嵌套是要走的路。顺便说一句,我认为组件没有被代理。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2011-02-09
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    相关资源
    最近更新 更多