tags:

views:

55

answers:

2

As the title suggests I have two models Products And Orders which are actually two table in my database and I have used a LINQ to SQL class to create there models. Now I wan to create a model named "OrderDetails" which will have properties from both the model like product name and id from product and Order number from orders something like this. An then I want to create a view from this custom model and from which I want to add "CRUD" operation. What should be my approach. And in many scenarios I may have to use data from more than 4 models. Please help. I'm wondering helplessly and I have only two days experience in ASP.NET MVC.

+1  A: 

I'd go this route:

namespace Models
{
    public class OrderDetails
    {
        private Products _products;
        private Order _order;

        public OrderDetails(Order order, Products products)
        {
            _order = order;
            _products = products;
        }

        // now expose whatever fields or logic you need.
        // for instance:

        public Customer OrderCustomer()
        {
            return order.Customer();
        }
    }
}

However, before you go around making all of these new models, are you sure you need them? Can you simply use two instances, either passed around separately or in a Tuple? You can get some useful abstraction here, but if you don't need it, keep it simple.

kevingessner
Thanks a lot but is it absolutely necessary to overload the constructor with the two model objects? Can i directly use object of the data context class of the LINQtoSQL class?
Soham Dasgupta
No, it's not. If you declare `_products` and `_orders` as public properties (see http://msdn.microsoft.com/en-us/library/x9fsa0sw(VS.80).aspx), you can use the [terse initializer syntax](http://blah.winsmarts.com/2006/05/21/demystifying-c-30--part-5-object-and-collection-initializers.aspx).
kevingessner
+1  A: 

Linq to SQL does not support custom mapping to the extent that you're looking to do. What you'll want to look at is a full ORM. I highly recommend nHibernate, but Entity Framework, if you must, will do what you want.

joshperry