I'm working on a project that needs to replace concrete class with Interface. Below is the concrete class and its inheritance class
public class CarRepository
{
private string _model;
private string _colour;
public CarRepository(string model, string colour) {
_model = model;
_colour = colour
}
public string Get(GUID id)
{
return id.ToString();
}
protected virtual void DoSomething(ref ISpecial special) {
special = special.AddFields(Rego, ModelNumber)
}
}
public class BigCarRepository : CarRepository
{
public class BigCarRepository(string model, string colour)
: base(model, colour)
protected override void DoSomething(ref ISpecial special) {
special = special.AddFields(SpecialOffer, OptionPack)
}
}
I want to replace the inheritance of a concrete class for BigCarRepository
from CarRepository
with an interface (i.e. ICarRepository
). Thus I created an interface and replacing the concrete class but couldn't find a way to address the override method. I have created the interface below and modified my concrete classes and the inheritance but stuck at the override method.
public interface ICarRepository
{
string Get(string id);
}
public class CarRepository : ICarRepository
{
private string _model;
private string _colour;
public CarRepository(string model, string colour) {
_model = model;
_colour = colour
}
public string Get(GUID id)
{
return id.ToString();
}
protected virtual void DoSomething(ref ISpecial special) {
special = special.AddFields(Rego, ModelNumber)
}
}
public class BigCarRepository : ICarRepository
{
public ICarRepository _carRepository { get; set; }
public BigCarRepository(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public string Get (string id)
{
return _carRepository.Get(id);
}
**protected override void DoSomething(ref ISpecial special) {
special = special.AddFields(SpecialOffer, OptionPack)
}**
}
Update:
If you need to make an abstraction of CarRepository. You did well by extracting the interface. You also need to add all methods that you want your derived classes to implement.
public interface ICarRepository
{
string Get(string id);
void DoSomething(ref ISpecial special);
}
public class BigCarRepository : ICarRepository
{
public ICarRepository _carRepository { get; set; }
public BigCarRepository(ICarRepository carRepository)
{
_carRepository = carRepository;
}
public string Get (string id)
{
return _carRepository.Get(id);
}
public void DoSomething(ref ISpecial special) {
special = special.AddFields(SpecialOffer, OptionPack);
}
}
If this is what you want, you're fine.
In my previous answer, i suggested that you might need to reuse some logic of base class CarRepository , but still wanted to force BigCarRepository to implement his own logic of DoSomething. This could be usefull if you have the same code being reused in derived classes.
public abstract class CarRepository : ICarRepository
{
private string _model;
private string _colour;
protected CarRepository(string model, string colour) {
_model = model;
_colour = colour
}
public string Get(GUID id)
{
return id.ToString();
}
public absctract void DoSomething(ref ISpecial special); // Force all derived classes to implement this method
protected virtual void DoSomethingBase(ref ISpecial special) {
special = special.AddFields(Rego, ModelNumber)
}
}
public class BigCarRepository : CarRepository
{
public ICarRepository _carRepository { get; set; }
public BigCarRepository(ICarRepository carRepository) : base (pass_the_model, pass_the_colour)
{
_carRepository = carRepository;
}
public override void DoSomething(ref ISpecial special) {
DoSomethingBase(ref special); // if you need some base class logic
special = special.AddFields(SpecialOffer, OptionPack);
}
}