Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello

how i can updating total of master when each one detail have been inserted /updated/deleted, and not all details of master?

if i used repository for master and repository of details, and if these repositories implements IDisposable, how to use the tow repositories with each their save methode to updating master total with unitprice of this detail in class BLL?

What I have tried:

i have created solution in visual studio 2012 and this solution is layred in 4 project :

1. project presentation layer (asp.net mvc)

2. business entities layer

C#
public class Master
{
    public int Id {get; set;}
    public decimal TotalPrice {get; set;}
   //the rest of properties
    public private ICollection<Detail> Details {get; set;}
}

public class Detail
{
   public int Id {get; set;}
   public decimal UnitePrice {get; set;}
   //the rest of properties
   public int MasterId {get; set;}
   public private Master Master {get; set;}
}


3. data access layer (ado.net data model entity framework + repositories)

C#
public class MasterRepository : IMasterRepository{
//code of class to implemente GetAll + CRUD for master
}


public class DetailRepository : IDetailRepository{

    EFContext context = new EFContext();

    //Get the details for one master

    public IEnumerableDetail GetAllDetailsByMasterId(int masterId)
    {
       var query = context.Details.Where(d=>d.MasterId == masterId)
    }

   //the rest of code to implemente CRUD of details

}

4. but for business logic layer in classes Bll i try to calculate the total of the master by sum of unit prices for the details

C#
public class MasterDetailsBll
{

    public decimal GetTotal(){

   //call the methode GetAllDetailsByMasterId of DetailRepository thet return 
    //enumerebal<detail> and then call the extention methode Sum for calculate 
    // the sum of unite prices of detail
  
       using (var repository = new DetailRepository())
       {       
          var total = reopsitory.GetAllDetailsByMasterId(masterId).Sum(d=>d.UnitePrice);
       }

      //call the CRUD Mehodes of repositoryMaster and CRUD of the repositoryDetails
    }
}


I would be grateful if you helped me

goodby soon
Posted
Updated 8-Oct-17 4:11am
v2
Comments
GKP1992 9-Oct-17 2:02am    
From what I understand from your question, you want to update your master records with the sum of the Unit prices in the details records based on the MasterId. Is that what you want to ask?
David2509 9-Oct-17 13:11pm    
Yes that what i want to ask
Maciej Los 13-Oct-17 14:37pm    
Why? You don't have to update master table. All you need to do is to create method which return current repository.
David2509 15-Oct-17 14:01pm    
i don't inderstand can you give me an example

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900