tags:

views:

126

answers:

2

Hi

We have a Price value object, that we want to map to a decimal SQL field via Linq-to-Sql.

We use Linq-to-SQL's attributes directly (that is... we don't use the Linq-to-Sql designer).

I want to write something like this:

[Table]
public class Product: Aggregate
{
    //...
    [Column]
    public string Name {get; set;}

    [Column]
    public Price Price {get; set;}

    [Column]
    public Price? GoldCustomerPrice {get; set;}

    //...
 }

In the sample above Linq-to-sql automatically detects that Name should be mapped to a VarChar. But how do do I tell it to map Price.ExcludingVat to a decimal and GoldCustomerPrice.Value.ExcludingVat to a decimal field allowing null?

This is how I do it right now. Very verbose.

[Table]
public class Product: Aggregate
{
    //...
    [Column]
    public string Name {get; set;}

    [Column(Name = "Price")]
    decimal priceExcludingVat;

    public Price Price
    {
        get { return new Price(priceExcludingVat) }
        set { priceExcludingVat = value.ExcludingVat ; }
    }

    [Column(Name = "GoldCustomerPrice")]
    private decimal? goldCustomerPriceExcludingVat;

    public Price? GoldCustomerPrice 
    {
        get
        {
            if(goldCustomerPriceExcludingVat.HasValue)
                return new Price(goldCustomerPriceExcludingVat.Value)
            else
                return (Price?) null;
        }
        set
        { 
            if (value == null)
                goldCustomerPriceExcludingVat = null;
            else
                goldCustomerPriceExcludingVat = value.Value.ExcludingVat;
        }
    }
    //...
 }

And in case you are wondering the Price value object looks like this:

public struct Price 
{
    public Price(decimal priceExcludingVat) : this()
    {
        ExcludingVat = priceExcludingVat;
        VatRate = Settings.VatRate;
    }

    public decimal ExcludingVat { get; private set; }

    public decimal VatRate{ get; set;}

    public decimal Vat
    {
        get { return ExcludingVat * VatRate; }
    }

    public decimal IncludingVat
    {
        get { return ExcludingVat * Vat; }
    }

    //... a lot of operator overloads
}
A: 

Is this what you're after?

[Column(Storage="Price", DbType="float NOT NULL")]
DavidGouge
The Price object can not be converted to a decimal/float, so if this should work I would have to write something like:[Column(Storage="Price.ExcludingVat", DbType="float NOT NULL")]... which doesn't work.
Thomas Jespersen
Ahh, I see what you mean. Is the price excluding vat stored as a column in the db? Or calculated?
DavidGouge
It its stored in the DB. It's just a normal Value object (DDD), which we would like to map. In NHibernate this is very simple, but I guess it impossible to make Linq to SQL work with value objects.
Thomas Jespersen
Ahh, you might want to have a look at the Entity Framework instead. Esp ver 4.
DavidGouge
A: 

Doesn't it work with operator overloading, where you implement one for decimals?

Daniel