Search code examples
c#structnhibernate-mappingcastle-activerecord

C# Mapping struct in ActiveRecord


i am making a little application to help me balance my checkbook. i am using Castle ActiveRecord to map the object properties to the database. now here is the problem. as i am making a money program i made a struct Currency

The struct:

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; }
    }
}

The class i am mapping:

[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; }
    }
}

Now in the ideal situation the Currency object would act like a nested object so the amount and currencyType are two colums in the transaction table. but it is not a nested seeing as i want it to act like the currency struct object.

I have no idea what tag i should give the Currency Amount for it to work, i would really appreciate it if any one could help me solve this problem.

I hope all of this is clear.

Greatings Duncan


Solution

  • What about the following? Or didn't I get the question? Note that I changed the structure to a class because you need virtual members for dynamic proxy generation and you cannot have virtual members on structures. By the way, why don't you use auto-implemented properties?

    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; }
    }