开发者

Basic question about OOP Class Structure with LINQ

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?


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜