1

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) 
{

}
halfer
  • 19,824
  • 17
  • 99
  • 186
ChinnaR
  • 797
  • 3
  • 9
  • 24
  • 1
    It's C# you can have only one base class, so other parenets need to be interfaces. – Lana Jun 26 '20 at 16:42
  • 1
    I think right way is using Custom as field of NewClass – Lana Jun 26 '20 at 16:43
  • 1
    Are you intending for values of this `NewClass` to be able to be substituted for existing code that expects `Customer` and `PageRequest` values? If not, you shouldn't use inheritance; you should instead create a `NewClass` with a `Customer` property and a `Page Request` property. (This is called "preferring composition over inheritance", if you want to research more.) – Joe Sewell Jun 26 '20 at 16:54
  • yes, I want it to be substituted for existing code – ChinnaR Jun 26 '20 at 17:05
  • "inheriting from sealed" <-- cannot be done. Period. – Lasse V. Karlsen Jun 26 '20 at 22:04
  • what is the best way to create a new class which can have both the class properties? I am ok to remove the sealed if needed be, any refactoring help is really appreciated – ChinnaR Jun 26 '20 at 22:08

1 Answers1

2

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

  • I want to use this class as a get request (http://someendpoint&firstname=a&currentpage=1&perpage=10), I am wondering how can user provide the input parameters as part of query string – ChinnaR Jun 26 '20 at 20:08
  • when api endpoint with the new class is called, it has to be htttp://someendpoint&Customer.Firstname=a&Customer.LastName=b&currentpage=1&perpage=10 , consumers may not be ok with that – ChinnaR Jun 27 '20 at 00:37
  • Is there anyway I can call the endpoint as http://someendpoint&firstname=a&currentpage=1&perpage=10 – ChinnaR Jun 27 '20 at 09:36
  • I don't know. I'm not involved in such things yet. You should create another question on this point. –  Jun 27 '20 at 11:15