0

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.

SamJackSon
  • 1,071
  • 14
  • 19

2 Answers2

1

I wonder if what you are looking for is Object Composition, such that some objects are composed of smaller objects with specific functionality - see http://en.wikipedia.org/wiki/Object_composition

Then using Dependency Injection makes this simple. Which .NET Dependency Injection frameworks are worth looking into?

More reading

Community
  • 1
  • 1
BigJump
  • 15,561
  • 3
  • 31
  • 29
0

If you do not need all the functionalities of LibraryClass, you can just create a wrapper of it. Encapsulate it in a class and expose only what you want from it.

public class LibraryClassWrapper
{
   private LibraryClass _libraryClass = new LibraryClass();
   public void MyFoo()
   {
      // do something... then call a method in the LibraryClass (if required)
      _libraryClass.Foo();
   }
}

You can then separate other functionalites in other interfaces and inherit your MyClass object from them.

public interface IAuth {...}
public interface IPaging {...}
public class MyClass : LibraryClassWrapper, IPaging, IAuth {...}

Remember, you cannot inherit from a concrete class in an interface. C# only supports multiple interface inheritance. Classes can only inherit from one concrete class and multiple interfaces.

Community
  • 1
  • 1
Alex R.
  • 4,664
  • 4
  • 30
  • 40
  • In this case, where would I write the definition of IPaging & IAuth? In MyClas? I am doing that now which I want to separate. – SamJackSon Jul 07 '11 at 06:07
  • You can put the _definitions_ somewhere else. Are you referring to the _implementations_? – Alex R. Jul 07 '11 at 06:38