I want to ask for an advise. I’m studying C# and I’m at all times attempting to scale back code duplication and I’m struggling to seek out “greatest follow” in C#.
I’m attempting to make use of Default interface strategies to implement as a lot reusable code as attainable, which works tremendous for easy examples.
I’ve a difficulty if class is derived from generic interfaces with a number of sorts e.g. IComplex<T1,T2,T3>
. Casting object to Generic Interface with many varieties makes code unreadable. My class is derived from extra such complicated Interfaces. Confer with the instance under. Calling a technique Foo() and Foo2() is unreadable:
(complicated as IComplex<int, string, decimal>).Foo();
Utilizing a brand new line of code for casting is an possibility, however I would like to simply use ‘complicated.Foo()’ with out the necessity to duplicate the code already written in `interface IComplex’.
Instance of Interface and sophistication
interface IComplex<T1,T2,T3>
{
T1 Property1 { get; set; }
T2 Property2 { get; set; }
T3 Property3 { get; set; }
void Foo()
{
// Default technique
// Do some sophisticated stuff with Property1-3
}
}
interface IOtherInterface<T1,T2,T3>
{
void Foo2()
{
// Default technique
// Do some sophisticated stuff
}
}
public class Advanced<T1, T2, T3> : IComplex<T1, T2, T3>, IOtherInterface<T1, T2, T3>
{
public T1 Property1 { get; set; }
public T2 Property2 { get; set; }
public T3 Property3 { get; set; }
}
“Unreadable” code
public void ComplexExample()
{
Advanced<int, string, decimal> complicated = new Advanced<int, string, decimal>();
(complicated as IComplex<int, string, decimal>).Foo(); // <<<< This isn't simply Readable !!!
(complicated as IOtherInterface<int, string, decimal>).Foo2(); // <<<< This isn't simply both Readable !!!
}
Desired habits
I want to name a technique instantly like: complicated.Foo();
with out the necessity to replicate the Foo code.
public void DesiredBehaviour()
{
Advanced<int, string, decimal> complicated = new Advanced<int, string, decimal>();
complicated.Foo(); // This might be good, however it's is compile error
complicated.Foo2(); // This might be good, however it's is compile error
}
Is there any technique to reuse the IComplex Foo() technique within the override class Foo() technique ? I’ve tried to make use of static extension strategies, however I’m wondering if one thing cleaner exists. It appears not proper.
I’m conscious of following methods to maximise code reuse:
- Inheritance
- Static Extension strategies
- Default interface strategies
- IoC Frameworks ( I’m not aware of IoC but)
Thanks for sharing your methods.
This query was initially posted right here https://stackoverflow.com/questions/61822369/c-sharp-code-reuse-default-interface-methods-extensions, however I used to be requested to maneuver it to CodeReview.
Edit:
I do know that if just one Interface is used, I can use casting, however it isn’t helpful if I must entry strategies from a number of interfaces.
Thank to Johnathan Barclay for sharing this system:
A method could be to kind the variable because the interface:
public void DesiredBehaviour()
{
IComplex<int, string, decimal> complicated = new Advanced<int, string, decimal>();
complicated.Foo();
}
Or perhaps a manufacturing facility technique if this can be a frequent requirement:
class Advanced<T1, T2, T3> : IComplex<T1, T2, T3>
{
non-public Advanced() { }
static IComplex<T1, T2, T3> Create() => new Advanced<T1, T2, T3>();
}
Then you definately chilly write:
var complicated = Advanced<int, string, decimal>.Create();
complicated.Foo();