none
How to pass an Entity Class as a parameter RRS feed

  • Question

  • hi , I have an entity called GLASS and another one called GLASS_VIEW in my .edmx file , basically they are same in the structure but they are refering to different tables back in the database, so I wanted to use the same methods which were operating on GLASS entity to work on GLASS_VIEW entity too , I thought I may pass the Entity as a parameter but I'm having hard time to implement this code

    my idea was , and since I'm calling these methods from different project, from where I can't reference the entities , that I pass the entity name as a String and then somehow convert it back to the convinient entity , is it possible ? I appreciate any comment/help


    Musaab Hashim
    • Changed type Mr.me Friday, October 7, 2011 9:44 PM
    Friday, October 7, 2011 6:06 PM

Answers

  • I solved it , not exactly as what I wanted but at least it works fine

     

     public Glass ShowGlasses(decimal? carID, String glassType,String TableName) 
            {
              
                
                using (GlassStoreDAL.Entities glassContext = new Entities())
                {
                    Glass d1 = new Glass();
                    if (TableName == "Glass")
    //here I do the table 
                {
                    ObjectQuery<GLASS> obj=new ObjectQuery<GLASS>("GLASSes",glassContext);
                         
                    var result = from d in obj
                                 select d;
                    if (result.Count() == 1)
                    {
                        foreach (GLASS d in result)
                        {
    
                            d1.id = d.ID;
                            d1.type = d.TYPE;
                            d1.car_id = (Decimal)d.CAR_ID;
                            d1.amount = (int)d.AMOUNT;
                            d1.min_price = (Decimal)d.MIN_PRICE;
    
                        }
                    }
                    else
                    {
                        if (result.Count() > 1)
                        {
                            throw new ApplicationException("Application Exception");
                        }
                    }
                   
                }
                else
                {
                    if (TableName == "view")
    //here I do the view
                    {
                      ObjectQuery<GLASS2_GLASS_VIEW> obj=new ObjectQuery<GLASS2_GLASS_VIEW>("GLASS2_GLASS_VIEW",glassContext);
                        
                    var result = from d in obj
                                 select d;
                    if (result.Count() == 1)
                    {
                        foreach (GLASS2_GLASS_VIEW d in result)
                        {
    
                            d1.id = d.ID;
                            d1.type = d.TYPE;
                            d1.car_id = (Decimal)d.CAR_ID;
                            d1.amount = (int)d.AMOUNT;
                            d1.min_price = (Decimal)d.MIN_PRICE;
    
                        }
                    }
                    else
                    {
                        if (result.Count() > 1)
                        {
                            throw new ApplicationException("Application Exception");
                        }
                    }
                    
                }
                    }
                    return d1;
                }
                   
            }  
    

     

    I will change it to switch case if a third entity should be added

     

     


    Musaab Hashim
    • Marked as answer by Mr.me Saturday, October 8, 2011 9:07 AM
    Saturday, October 8, 2011 9:07 AM

All replies

  • On 10/7/2011 2:06 PM, Mr.me wrote:
    > hi , I have an entity called GLASS and another one called GLASS_VIEW in
    > my .edmx file , basically they are same in the structure but they are
    > refering to different tables back in the database, so I wanted to use
    > the same methods which were operating on GLASS entity to work on
    > GLASS_VIEW entity too , I thought I may pass the Entity as a parameter
    > but I'm having hard time to implement this code
    >
    > my idea was , and since I'm calling these methods from different
    > project, from where I can't reference the entities , that I pass the
    > entity name as a String and then somehow convert it back to the
    > convinient entity , is it possible ? I appreciate any comment/help
    >
     
    The simplest thing to do is create a Data Transfer Object and map data
    out of the entity to the DTO and pass the DTO around and map dto back to
    entity.
     
    public class DTOGlassOrGlassView
    {
         public string ClassType {get; set;)
         public int ID {get; set;}
         public string GlassName {get; set
    }
     You can create a classlib project with the class above in it so that you
    can set reference to it from your projects.
     
    On the EF project side......
     
    var dto = new DTOGlassOrGlassView();
     
    You map the entity properties to the dto properties, and you return
    DTOGlassOrGlassView. You also set ClassType = "GLASS" or "GLASS_VIEW"
    //so that you know the type.
     On the other side you pass DTOGlassOrGlassView back to the EF side.
     
    if (DTOGlassOrGlassView.ClassType == "GLASS")
    {
       var glass = new Glass();
      //map dto properties back to Glass
    }
    else
    {
      var glassv = new GLASS_VIEW();
      //map dto properties back to Glass_View
    }
     
    Friday, October 7, 2011 6:30 PM
  • Hi

    a question.. if the entities have the same structure and are used by the same domain logic, why are they stored in different tables and are not handled as one entity? or at least why dont they inherit from a base table.. If last way, you could probably use polyphormism to give the base type in your domain logic to do the work with the entites.

    Greets

    Holger

    Friday, October 7, 2011 6:58 PM
  • <div><pre lang="x-c#">Hi and thanks for the reply , your answer looks so fine but however I can't make to work with my code , I will post my code to make a more clear picture<div>the decision making  of what table to work with is done on the presentation side because the user select a value from a comboBox, then call a method on the BLL which is look like </div>
    
    //this is on BLL, the Glass class is a BLL class while GLASS is my EF class
    
    public Glass ShowGlasses(decimal? carID, String glassType)
            {
    
                using (Entities glassContext = new Entities())
                {
                    Glass d1 = new Glass();
                    var result = from d in glassContext.GLASSes
                                 where d.CAR_ID == carID
                                 & d.TYPE == glassType
                                 select d;
                    if (result.Count() == 1)
                    {
                        foreach (GLASS d in result)
                        {
    
                            d1.id = d.ID;
                            d1.type = d.TYPE;
                            d1.car_id = (Decimal)d.CAR_ID;
                            d1.amount = (int)d.AMOUNT;
                            d1.min_price = (Decimal)d.MIN_PRICE;
    
                        }
                    }
          return d1;
               }
            }
    //this is the old method which is dealing with just GLASS 
    

    I need to modify this method so that I can do the other entity as well
    Thanks in advance
     

    Musaab Hashim
    Friday, October 7, 2011 9:27 PM
  • Hi , thanks for the comment , I have multible users each one has it's own Oracle schema , the schemas objects are the same , so one user may want to look to another user's table or to look at it's own table , so I made views to every table on the other users schemas that one user may want to look at, and now I just need to switch between the entities , the user's table A  and the user's views of other users A tables , I hope I explained well the scenario.

     

    Thanks


    Musaab Hashim
    Friday, October 7, 2011 9:37 PM
  • I solved it , not exactly as what I wanted but at least it works fine

     

     public Glass ShowGlasses(decimal? carID, String glassType,String TableName) 
            {
              
                
                using (GlassStoreDAL.Entities glassContext = new Entities())
                {
                    Glass d1 = new Glass();
                    if (TableName == "Glass")
    //here I do the table 
                {
                    ObjectQuery<GLASS> obj=new ObjectQuery<GLASS>("GLASSes",glassContext);
                         
                    var result = from d in obj
                                 select d;
                    if (result.Count() == 1)
                    {
                        foreach (GLASS d in result)
                        {
    
                            d1.id = d.ID;
                            d1.type = d.TYPE;
                            d1.car_id = (Decimal)d.CAR_ID;
                            d1.amount = (int)d.AMOUNT;
                            d1.min_price = (Decimal)d.MIN_PRICE;
    
                        }
                    }
                    else
                    {
                        if (result.Count() > 1)
                        {
                            throw new ApplicationException("Application Exception");
                        }
                    }
                   
                }
                else
                {
                    if (TableName == "view")
    //here I do the view
                    {
                      ObjectQuery<GLASS2_GLASS_VIEW> obj=new ObjectQuery<GLASS2_GLASS_VIEW>("GLASS2_GLASS_VIEW",glassContext);
                        
                    var result = from d in obj
                                 select d;
                    if (result.Count() == 1)
                    {
                        foreach (GLASS2_GLASS_VIEW d in result)
                        {
    
                            d1.id = d.ID;
                            d1.type = d.TYPE;
                            d1.car_id = (Decimal)d.CAR_ID;
                            d1.amount = (int)d.AMOUNT;
                            d1.min_price = (Decimal)d.MIN_PRICE;
    
                        }
                    }
                    else
                    {
                        if (result.Count() > 1)
                        {
                            throw new ApplicationException("Application Exception");
                        }
                    }
                    
                }
                    }
                    return d1;
                }
                   
            }  
    

     

    I will change it to switch case if a third entity should be added

     

     


    Musaab Hashim
    • Marked as answer by Mr.me Saturday, October 8, 2011 9:07 AM
    Saturday, October 8, 2011 9:07 AM