Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Interface too rigid

I'm working with some legacy code and have this existing interface:

public interface IProcess
{
    IDTO Process(IDTO request);
}

There is then a factory with a huge switch statement returning objects (17 of them) that implement the interface. The created objects then call process like:

return xProcess.Process(requestDTO);

My problem is that I now need to change a few of these to pass in a second parameter like:

xProcess.Process(requestDTO, Id);

I'm looking for an elegant way to achieve this.

I have tried making the interface an abstract class like:

public abstract class IProcess
{

    public virtual IDTO Process(IDTO request)
    {
       return null;
    }

    public virtual IDTO Process(IDTO request, int Id)
    {
        return null;
    }
}

This allows me to override the Process method in the created classes but I have a problem that IProcess is inherited by another interface, which is now obviously broken.

The legacy platform is huge and the test coverage is not great so I'm looking for a nice elegant solution with the minimum impact to the rest of the code base. Perhaps, there is a pattern I'm overlooking?

Can anyone help please? (examples would be great)

Thanks

David

like image 313
davy Avatar asked Dec 31 '25 00:12

davy


1 Answers

Can you not create a new interface that inherits from the existing one, like

public interface IProcess2 : IProcess
{
    IDTO Process(IDTO request, int Id);
}

Then change your classes to inherit from the new one.

This should stop any users of the existing IProcess from breaking.

like image 141
Justin Harvey Avatar answered Jan 01 '26 12:01

Justin Harvey