ChinnaR
ChinnaR

Reputation: 837

How to create a class inheriting from a sealed and a public class at the same time

I have a sealed and public class objects consumed in many classes of the project.

public sealed class Customer {

public int Id{get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string MiddleName {get; set;}
public string Address {get; set;}

}

public class PageRequest
{
     public int CurrentPage {get; set;}
     public int PerPage {get; set;}
     public string SortBy {get; set;}
}

I would need to create a new class object with all the properties combined in both the classes rather than repeating all the properties. The following can bring in the pagerequest properties into the new class, but how can I manage to get the properties of Customer class. I cannot inherit the properties of the Customer as it's a sealed class. Removing sealed from Customer may not be a good idea as it's been used by many areas of application, even then I cannot have both Customer and PageRequest as base classes. I am ok to change the existing Customer class if there is a better approach

public class NewClass : PageRequest
{

}

Is repeating the properties in the NewClass like below a better one?

public class NewClass : PageRequest
    {
        public int Id{get; set;}
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string MiddleName {get; set;}
    public string Address {get; set;}
    }

This new class object will be used as part of new api get request query string where a user has to provide one or many of (firstname, lastname, address) and currentpage, perpage, sortby as input parameters, need a better approach to fit all the parameters with the request. I want to use this class as a get request (http://someendpoint&firstname=a&lastname=b&currentpage=1&perpage=10)

public Task<PaginatedResult<SomeDto>> GetResult([FromQuery]NewClass request) 
{

}

Upvotes: 1

Views: 411

Answers (1)

user12031933
user12031933

Reputation:

There is no multiple inheritence in C# and one class is sealed...

You can use composition like that:

public class NewClass : PageRequest
{
  public Customer Customer { get; private set; }
  public NewClass()
  {
    Customer = new Customer();
  }
}

So the Customer instance is created when a NewClass instance is created and will live until its destruction.

You also can add some wrapper methods if you want, and mark the Customer as private, like:

public class NewClass : PageRequest
{
  private readonly Customer Customer = new Customer();
  public string FirstName
  {
    get { return Customer.FirstName; }
    set { Customer.FirstName = value; }
  }
  public NewClass()
  {
  }
}

In case of two or more sealed classes or if you don't want to use inheritence at all you can do the same thing with all artifacts:

public class NewClass
{
  private readonly Customer Customer = new Customer();
  private readonly PageRequest PageRequest = new PageRequest();
}

Difference between Composition and Aggregation @ C# Cormer

What is the difference between association, aggregation and composition?

C# code for association, aggregation, composition

Upvotes: 2

Related Questions