0

I'm new to MVC and am used to using Forms. My question is suppose I have basic models setup for four tables with the following fields;

    Branch
    ------
    Branch_Nu
    Branch_Address

    Orders
   -----
    Order_Nu
    Branch_Nu
    Product_Nu
    Customer_Nu
    totcost

    Products
    ---------
    Product_Nu
    Product
    Price

    Customer
    ----------
    Customer_Nu
    Name
    Address
    City
    St
    Zip

I'm interested in the following scenarios,

I want to see all orders for a branch; 
Branch->orders

I want to see all orders for a customer from a particular branch;
Branch->orders->products
         |-->customer

I want to see all orders for a customer regardless of branch they
purchased from; 
customer->orders->branch

I want to see all branches that sold a particular product;
products->orders->Branch

I want to see which customers bought a particular product;
products->orders->customer

Question is can I use different controllers for the different scenarios using the same basic models that is submitted to different controller methods, or do I need different models for the different scenarios which is then submitted to different controller methods?

If I were using forms I would just have a different select statements and forms for each scenario, in MVC?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Dave
  • 3
  • 5

2 Answers2

0

Question is can I use different controllers for the different scenarios using the same basic models

Yes

do I need different models for the different scenarios which is then submitted to different controller methods

You don't need them, but generally speaking using a model for a different logical function tends (not always) to break the Single Responsibility Principle.

In your instance it appears you are viewing the same data by different views (not logic). An order is an order, for viewing purposes anywhere on your site, generally I use the same model. Placing an order (different logic function) I would most likely have a new model.

Considering you are viewing the same data this is a classic example of the best use of MVC Templates.

If I were using forms I would just have a different select statements and forms for each scenario, in MVC?

I would probably design it like:

I want to see all orders for a branch; 
Branch->orders
public class BranchController() { public ActionResult Orders() {}}

I want to see all orders for a customer from a particular branch;
Branch->orders->products
         |-->customer
public class BranchController() 
{ 
   public ActionResult Orders() {}
   public ActionResult CustomerOrders() {}
   public ActionResult ProductOrders() {}
}

// ETC

A model example:

public class OrderVM
{
  //I would get rid of hungarian notation
  // https://stackoverflow.com/questions/111933/why-shouldnt-i-use-hungarian-notation
  public int OrderId { get; set; }
  public int BranchId { get; set; }
  public int ProductId { get; set; }
  public int CustomerId { get; set; }
}

Then store how the html is rendered in /views/shared/templates/display/OrderVM.cshtml so it can be used throughout the application, allowing overrides per controller/area as well.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

your entity framework models represent the relationships between your classes. In a code first approach, your models will dictate the design of your database. Take the following three classes for example:

public class Cat 
{
  [Key]
  public int Id {get; set;} 

  public string Name {get; set;}

  //Foreign key to animalgroup table
  public int AnimalGroupId {get; set;}

  //navigation property to AnimalGroup
  //allows you to do myCat.AnimalGroup outside of this class to retrieve
  //the associated animal group
  public virtual AnimalGroup AnimalGroup {get; set;}
}

public class Dog
{ 
  [Key]
  public int Id {get; set;}

  public string Name {get; set;}

  public bool IsFluffy {get; set;}

  //Foreign key to animalgroup table
  public int AnimalGroupId {get; set;}

  //navigation property to AnimalGroup
  //allows you to do myDog.AnimalGroup outside of this class to retrieve
  //the associated animal group
  public virtual AnimalGroup AnimalGroup {get; set;}
}

public class AnimalGroup
{
  [Key]
  public int Id {get; set;}

  public string Name {get; set;}     

  public virtual ICollection<Cat> Cats {get; set;}

  public virtual ICollection<Dog> Dogs {get; set;
}

this represents two one-to-many relationships. A AnimalGroup can contain multiple Cats and an AnimalGroup can contain many Dogs. You can write queries to do CRUD operations. A very simplistic example is the following:

//create and save an animal group
AnimalGroup group = new AnimalGroup();
group.Name = "my animal group";
_dbContext.AnimalGroups.Add(group);
_dbContext.SaveChanges();

//create and save a cat associated with the animal group
Cat myCat = new Cat();
cat.Name = "kitty";
cat.AnimalGroupId = group.Id;
_dbContext.Cats.Add(myCat);

_dbContext.SaveChanges();
GregH
  • 5,125
  • 8
  • 55
  • 109