简体   繁体   中英

Handling Multiple Data Dependencies in Controllers

I am looking for a proper way to handle data dependencies from where, for example if a purchase(s) is made the loading order should be created based on the purchase(s). Code for Purchases:

public partial class Purchase
 {
   public int PurchaseId { get; set; }
   public int Quantity { get; set; }
   public decimal? Amount { get; set; }
   public decimal? Payment { get; set; }
   public DateTime Date { get; set; }
   public bool IsPaid { get; set; }
   public int CustomerId { get; set; }
   public int? SalesmanId { get; set; }
   public int ProductId { get; set; }
   public string User { get; set; }
 }

and my loading order:

public partial class LoadingOrder
 {
   public int Id {get; set; }
   public int? SalesmanId { get; set; }
   public int ProductId { get; set; }
   public int LoadedQuantity { get; set; }
   public int? ReturnedQuantity { get; set; }
   public DateTime LoadingDay { get; set; }
   public string User { get; set; }
 }

they have related data I use the purchase data to fill the loadingOrder then I save it; all of this in the PurchaseController in the POST Method , clearly I am violating the SOLID Principles . My question arises from this. is there a better way (pattern)?

I don`t like to keep any logic or operation inside the controller. For me the controller is responsible to send data tp the view and receive data from the view.

I`d create a class responsible for manipulating the Purchase and another class responsible for manipulating the LoadingOrder. Your controller can use these classes. In this case you remove the responsibility of the controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM