Msam85 0 Newbie Poster

Hi all, I'm new with asp.net and I'm currently starting a project with MVC 2.
As far as I know the models folder is dedicated to store the .cs of our entities and in the controllers folder you receive the petitions from views, create new instances of the entities and call their actions with some data.

What I want to know is if this is the "right" method to do de work in therms of good design software, usability, scalability and minimizing the dependency of the different layers within the application.
I'm using Entity Framework, link2sql and automapper

For example, consideer this scenario:

/models
-OrderModel.cs
/controllers
-OrderController.cs
/views
/Create
-CreateOrder.aspx
/Index
-ListOrderView.aspx

Imagine I want to create a new order, so I fill a form and send the data to OrderController.cs

public ActionResult Create(FormCollection collection){
   var order=new OrderModel();
   order.NumberUnits=collection["numberUnits"];
   order.Provider=collection["selectedProvider"];

   .......

   order.createNewOrder(order);
}

Inside my OrderModel.cs:

public class PedidoModels
    {
        DataEntities data = new DataEntities ();
        public int Id { get; set; }
        public int IdProvider{ get; set; }
        .......     //Some others properties

        public int insertOrder(OrderModel order)
        {
            //This is automapper code, the idea is that this 2 lines
            // do the mapping from my orderModel entity 
            //to my Order entity (of Entity Framework)

            Mapper.CreateMap<OrderModel , Orders>();
            var p = Mapper.Map<OrderModel , Orders>(order);
            data.AddToPedidos(order);
            data.SaveChanges();             
        }        

And the method to list all the orders:

  public List<OrderModels> ListOrdersModels()
  {
      var orders = from item in data.Orders
                   select item;

      var listOrders = new List<OrdersModels>();

      Mapper.CreateMap<Orders, OrdersModels>(); //Again this is for mapping
      foreach(var x in orders){

                var p = Mapper.Map<Orders, OrdersModels>(x);
      
                listOrders .Add(p);         
      } 
          
      //Finally when we got the complete list of the orders, we return it to the controller
      
      return listOrders;
}

In the code I have written, the models provides the business logic AND the CRUD methods (with linq2sql sentences).

Is this a good way to get a good design at the end? Any improvement (maybe in the crud methods) to reduce dependecy?
Thanks in advance!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.