views:

90

answers:

3

I need to add Formula property/field on SubSonic | SimpleRepository Can someone tell me how to? or is it not possible?

br, No Body

+1  A: 

Why not just do the calculation within the object definition itself?

So

    public class OrderLine
    {
        public int OrderId { get; set; }
        public int Qty { get; set; }
        public decimal ProductPrice { get; set; }
        public decimal LineCost
        {
            get { return Qty * Convert.ToDecimal(LineCost); }
        }
    }
Podge
thanks Podge, I'll try this one. Is this the proper way or just an work around?
No Body
Failed: SqlException: Invalid column name 'LineCost'@Podge, remember I am using SimpleRepository and handcrafted my class/object
No Body
+2  A: 

Just add [SubSonicIgnore] to above LineCost

so

        [SubSonicIgnore]
        public decimal LineCost
        {
            get { return Qty * Convert.ToDecimal(LineCost); }
        }

this is happening as LineCost is being mapped to the database.

Podge
It works!, But Podge, is the the proper way of doing the above requirements or just a work-around?
No Body
I do not see it as a work around, I feel it is the best solution. The reason is that you have one place where the calculation occurs, if you are doing it in SQL/Linq you are combining a data retrieval with a calculation, every time you retrieve a OrderLine you have to include the calculation, forget and you have a nice little feature.
Podge
A: 

I can only see a way by using anoynmous types, and then you will have to convert the type to the orderline (its not very nice)

                var x =from o in repo.All<OrderLine>()    
                    select new   
                    {
                        OrderId = o.OrderId,
                        ProductPrice = o.ProductPrice,
                        Qty = o.Qty,
                        LineCost = o.ProductPrice * o.Qty
                    };


            List<OrderLine> orders = null;
            foreach (var t in x)
            {
                orders.Add(new OrderLine
                {
                    LineCost = t.LineCost,
                    OrderId = t.OrderId,
                    ProductPrice = t.ProductPrice,
                    Qty = t.Qty
                });

            }
Podge