0

I am currently writing a DLL file, which uses some inheritances. Now I am having trouble with exposing my classes, because all the base classes are exposed too.

For instance:

public Class TestBase // Base class that gets exposed
{
}
public Class TestFunctions : TestBase // The class that I want to expose gets exposed
{
}

Problem of internal or other modifiers (like protected):

internal Class TestBase // Base class that won't get exposed
{
}
internal Class TestFunctions : TestBase // The class that I want to expose won't get exposed either
{
}

I want to expose TestFunctions to the user of the DLL file, but I dont want to expose the TestBase, because the base class is only used internally. Exposing the base class is redundant for the user of the DLL, since all that he needs is contained inside one function class. How do I achieve what I need? I heared interfaces can help me out, but I cant figure out what exactly I need to do, since the user cannot instantiate a instance.

Anon
  • 134
  • 1
  • 9
  • Why do you NOT want to expose the base class? That answer might help others understand if interfaces and/or abstract classes might help. – Nisarg Shah Aug 27 '18 at 07:32
  • @NisargShah I edited my question – Anon Aug 27 '18 at 07:34
  • You encapsulate all members that you need with help of `private` (available only from the same class), `internal` (exposed to all members in the same assembly) or `protected` (exposed to derived types only) modifiers – Fabjan Aug 27 '18 at 07:35
  • @Fabjan no, that would not work. I am using inheritance, that means that all base classes have to have the same modifier as the class I want to expose. If I use `internal´ or 'protected' for the base, the class I want to expose is hidden too. – Anon Aug 27 '18 at 07:38
  • @Anon Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Fabjan Aug 27 '18 at 07:40
  • @Fabjan I tried to fix my example im my question. Please tell me if that helped.. – Anon Aug 27 '18 at 07:46

1 Answers1

1

You can use a factory method and an interface:

For example:

//your classes: internal
internal class TestBase // Base class that I dont want to expose
{

}

//note: added interface
//note2: this class is not exposed
internal class TestFunctions : TestBase, IYourTestClass // The class that I want to expose
{

}

//an interface to communicate with the outside world:
public interface IYourTestClass
{
    //bool Test();  some functions and properties
}

//and a public factory method (this is the most simple version)
public static class TestClassesFactory
{
    public static IYourTestClass GetTestClass()
    {
        return new TestFunctions();
    }
}

So in your caller's application now both classes aren't exposed. Instead you can use the factory to request a new one:

public void Main()
{
    IYourTestClass jeuh = TestClassesFactory.GetTestClass();
}
Stefan
  • 17,448
  • 11
  • 60
  • 79
  • That looks promising, I will give it a try. Thanks a lot! Would this be the so called "factory pattern" then? – Anon Aug 27 '18 at 07:39
  • an additional note: this is the most simple factory type: normally there are more `IYourTestClass`-related types and the factory can actually choose which one to return. – Stefan Aug 27 '18 at 07:43
  • for example: A method that says: Give me the instance of the object that you need to get returned ? – Anon Aug 27 '18 at 07:48
  • @Anon: I was mistaken: difference between "factory", "factory method" and "abstract factory" can be found here https://stackoverflow.com/questions/13029261/design-patterns-factory-vs-factory-method-vs-abstract-factory#13030163. Nevertheless, it should work. And yes, that's what the factory does. Although, in practice, it's more like: give me an instance of an object with this interface. You can hide 1) implementation details, and 2) creation parameters. Commonly used with inversion of control, for example in .net Core, where you request a service from the framework. – Stefan Aug 27 '18 at 07:51
  • Code project article: https://www.codeproject.com/articles/716413/factory-method-pattern-vs-abstract-factory-pattern – Stefan Aug 27 '18 at 07:52
  • Thanks a lot for the afford @Stefan. Guess I gotta learn a lot now :D Either way, your answer is very much appreciated! – Anon Aug 27 '18 at 07:53