views:

95

answers:

4

I have this situation:

public class busOrder: IbusOrder
{
    public Order vOrder { get; set; }

    MyDataContext db = new MyDataContext();

    public busOrder(int pOrderId)
    {
         vOrder = db.Orders.SingleOrDefault(p => p.Id == pOrderId);
    }

    public int SaveNew()
    {
        ...
    }
    public int GetStatus()
    {
        ...
    }
}

As you may have noticed, Order is a table in my datacontext and the constructor "fills" vOrder and I can do that very easily with link (one line).

But when I'm making calls to use the objects atributes I have to do it like this:

busOrder openOrder = new busOrder(someId);

then I have to do this to get the columns:

openOrder.vOrder.Date;
openOrder.vOrder.Status;

Is there any way for me to make this values more easily accessible (like openOrder.Date, without having to manually create and set them one by one in the class?

+1  A: 

You can use AutoMapper to copy values of properties between objects.

Jakub Konecki
+1  A: 

You could also use Dynamic Language Runtime.

EDIT: Well if you're using .NET 4.0 you could implement something like this:

class Program
{
    static void Main(string[] args)
    {
        dynamic order = new BusOrder();
        Console.WriteLine(order.Test);
        Console.ReadLine();
    }
}

class BusOrder : DynamicObject
{
    private Order _order = new Order();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = _order.GetType().GetProperty(binder.Name).GetValue(_order, null);

        return true;
    }
}
AS-CII
Elaborate, preferably with example code.
Austin Salonen
A: 

There is no easy way that I know of. When I've had to do this, I usually just write out the first property by hand and then copy and paste up a storm.

public int Status
{
   get { return this.vOrders.Status; }
   set { this.vOrders.Status = value; }
}
Russell McClure
A: 

Do this:

public class busOrder: IbusOrder, Order
{
    public Order vOrder { set {base = value;} }

    MyDataContext db = new MyDataContext();

    public busOrder(int pOrderId)
    {
         vOrder = db.Orders.SingleOrDefault(p => p.Id == pOrderId);
    }
    ...
}

for decreasing overheads use POCO Entity Generator instead of EF itself.

SaeedAlg
Making the class implement Order seems to be the best solution. I get this error, however: "User of keyword base is not valid in this context"
Vitor Reis