locked
Inherit from two partial class RRS feed

  • Question

  • User-1250924062 posted

    Hi all,

    I have edmx file with two entity (Master and Detail).

    Master has 5 fields, Detail has 10 fields.

    I have also two partial class named Master and Detail with my personal business method.

    Now I want to create a class that groups all 15 properties.
    I know that with c# I can only inherit from a one class.
    So, I have to use interfaces.

    What is the best method for doing this?
    Can anyone explain it to me?

    tks a lot

    D.

    Sunday, March 6, 2016 9:05 AM

Answers

  • User-986267747 posted

    Hi dupin,

    if you already have the properties in master and in detail, why create a new class?

    The client receives data that maps directly to your database tables. However, that’s not always a good idea. Sometimes you want to change the shape of the data that you send to client. For example, you might want to:

    • Remove circular references (see previous section).
    • Hide particular properties that clients are not supposed to view.
    • Omit some properties in order to reduce payload size.
    • Flatten object graphs that contain nested objects, to make them more convenient for clients.
    • Avoid “over-posting” vulnerabilities.
    • Decouple your service layer from your database layer.

     

    I only need a container where to write the data of the select.

    If you don’t want to create a class, just use an anonymous class. like this:

    var query = from m in db.Master
                    join d in db.Detail
                    on m.MasterId = d.MasterId
                    select new
                    {
                        DetailId = d.DetailId,
                        MasterId = m.MasterId,
                        Title = m.Title,
                        AuthorName= m.AuthorName
    …
                    };
    

    Best regards,

    Klein Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 8:41 AM

All replies

  • User-986267747 posted

    Hi dupin,

    According to your description, I assume that you want to fill the class with two classes’ data when you handle a select method. You don’t need to create a class which inherits two classes. You could Create a Data Transfer Object that contains all of 15 properties. And then you could use the LINQ Select statement to retrieve the data from entities and put it into DTO.

    public class Master
    
        {
    
            public int MasterId { get; set; }
    
            public string Title { get; set; }
    
            public string AuthorName { get; set; }
    
    …
    
        }
    
     
    
        public class Detail
    
        {
    
            public int DetailId { get; set; }
    
            public int MasterId { get; set; }
    
            public string other { get; set; }
    
    …
    
        }
    
     
    
        public class MasterDto
    
        {
    
            public int DetailId { get; set; }
    
            public int MasterId { get; set; }
    
            public string Title { get; set; }
    
            public string AuthorName { get; set; }
    
            public string other { get; set; }
    
    …
    
    }
    
     

    then you could retrieve the data like this:

    var query = from m in db.Master
    
                    join d in db.Detail
    
                    on m.MasterId = d.MasterId
    
                    select new MasterDto()
    
                    {
    
                        DetailId = d.DetailId,
    
                        MasterId = m.MasterId,
    
                        Title = m.Title,
    
                        AuthorName= m.AuthorName
    
                    };
    

    For more information about DTOs, please refer to:

    http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5

    Best regards,

    Klein Zhang

    Monday, March 7, 2016 3:26 AM
  • User-1250924062 posted

    Hi Klein,

    if you already have the properties in master and in detail, why create a new class?
    The join is correct. I only need a container where to write the data of the select.

    thks

    D.

    Monday, March 7, 2016 7:50 AM
  • User-986267747 posted

    Hi dupin,

    if you already have the properties in master and in detail, why create a new class?

    The client receives data that maps directly to your database tables. However, that’s not always a good idea. Sometimes you want to change the shape of the data that you send to client. For example, you might want to:

    • Remove circular references (see previous section).
    • Hide particular properties that clients are not supposed to view.
    • Omit some properties in order to reduce payload size.
    • Flatten object graphs that contain nested objects, to make them more convenient for clients.
    • Avoid “over-posting” vulnerabilities.
    • Decouple your service layer from your database layer.

     

    I only need a container where to write the data of the select.

    If you don’t want to create a class, just use an anonymous class. like this:

    var query = from m in db.Master
                    join d in db.Detail
                    on m.MasterId = d.MasterId
                    select new
                    {
                        DetailId = d.DetailId,
                        MasterId = m.MasterId,
                        Title = m.Title,
                        AuthorName= m.AuthorName
    …
                    };
    

    Best regards,

    Klein Zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 8, 2016 8:41 AM