Is there a way for me to extend this Additional class without breaking
the existing code?
In order extend the Additional class
and add properties to it without breaking the existing workings, we could follow couple of approach, one of the way could be Wrapper Class
This approach allows you to keep using the existing third-party class (Customer and Additional) without any modifications to their usage while adding new properties to Additional.
First of all, I would be creating a customer with existing details, then need to Wrap the AdditionalDetails where would would inlcude the new proeprty and finally would build view model to pass the data to the view.
Let's have a look in practice:
Existing Class:
public class Additional
{
public string ExistingProperty { get; set; }
}
public class Customer
{
public string Name { get; set; }
public Additional AdditionalDetails { get; set; }
}
public interface IAdditional
{
string ExistingProperty { get; }
}
Wrapper Class:
public class ExtendedAdditional : IAdditional
{
private readonly Additional _original;
public ExtendedAdditional(Additional original)
{
_original = original ?? throw new ArgumentNullException(nameof(original));
}
public string ExistingProperty => _original.ExistingProperty;
public string NewProperty { get; set; }
}
View Model:
public class CustomerViewModel
{
public Customer Customer { get; set; }
public ExtendedAdditional ExtendedAdditional { get; set; }
}
Controller:
public IActionResult Index()
{
var customer = new Customer
{
Name = "Kiron Test",
AdditionalDetails = new Additional { ExistingProperty = "Existing Value" }
};
var extendedAdditional = new ExtendedAdditional(customer.AdditionalDetails)
{
NewProperty = "New Value"
};
var model = new CustomerViewModel
{
Customer = customer,
ExtendedAdditional = extendedAdditional
};
return View(model);
}
View:
@model CustomerViewModel
<h2>Customer Details</h2>
<p>Name: @Model.Customer.Name</p>
<h3>Additional Details</h3>
<p>Existing Property: @Model.ExtendedAdditional.ExistingProperty</p>
<p>New Property: @Model.ExtendedAdditional.NewProperty</p>
Output:
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/pBVIIiRf.png)
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/jt3CeS6F.png)
Note: In most cases, using a wrapper class (ExtendedAdditional) is considered more elegant and aligns better with principles of object-oriented design and separation of concerns. It provides a clear boundary between your application's logic and the third-party library's implementation.
Additional
class is not sealed, you can create a derived class that inherits from Additional and add the new properties you need.