This might turn out to be newbie question, as simple as not possible, but I will give it go anyway. I am coming from JavaScript background, so not sure if C# (and other OOP languages) have anything inbuilt to solve this problem.
I want to add auxiliary functionality to my class using interfaces & impleentation delegated to separate concrete classes so that they can be resused.
In following implementation, I am using "LibraryClass" from external API which I cannot modify. I am writing "MyClass" which uses it by extending it.
public class LibraryClass {...}
public class MyClass : LibraryClass {...}
Now I want to add functionlity such as paging. So what I did, I created new class "Paging" which extends "LibraryClass" & extended "MyClass" from it.
Current Implementation
public class LibraryClass {...}
public class Paging : LibraryClass {
protected int pageNumber = 1;
public void setPaging(TRequest request) {
var httpReq = this.RequestContext.Get<IHttpRequest>();
pageNumber = httpReq.QueryString["page"]; // parsing syntax skipped
}
}
public class MyClass : Paging {
public override object OnGet(Products request) {
setPaging(request);
return new ProductsResponse { Products = GetAll() };
}
private List<Product> GetAll() {
// Use base.pageNumber to pull appropriate data
}
}
Can this be more cleaner way with interfaces or any other C# mechanism?
Proposed Implementation
public class LibraryClass {...}
public interface IPaging : LibraryClass {
void setPaging(TRequest request)
}
public interface Paging : IPaging {
public void setPaging(TRequest request) {
var httpReq = this.RequestContext.Get<IHttpRequest>();
pageNumber = httpReq.QueryString["page"]; // parsing syntax skipped
}
}
public class MyClass : IPaging {...}
I also want to add more "concrete" functionality to MyClass like access & delegate implementation to another class.
public interface IAuth {...}
public class Auth : IAuth {...}
public class MyClass : IPaging, IAuth {...}
The key here is separation of code.
I might have missed some basic OOP class in school, but its never tool late I guess.