1

I am currently just exposing the properties through a generic interface e.g.

public interface IBaseClass
{
    int ID { get; set; }
}

internal class MyBaseClass : IBaseClass
{
    public MyBaseClass() { }
    public int ID { get; set; }
}

public class MyExposedClass : IBaseClass
{
    private MyBaseClass _base = new MyBaseClass();

    public int ID
    {
        get { return _base.ID; }
        set { _base.ID = value; }
    }
}

Then in my main application I can do:

IBaseClass c = new MyExposedClass();
c.ID = 12345;

But can't do:

MyBaseClass b = new MyBaseClass();

This is my desired behaviour.

However, I was just wondering if this is the correct approach? Or if there was a better way?

James
  • 80,725
  • 18
  • 167
  • 237

7 Answers7

6

If you only want to prevent instantiation you could make MyBaseClass abstract (make it's constructor protected as well - it is a good design) and have MyExposedClass derive from it. If you want to completely hide the type your approach seems fine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Darn, too late - +1 for speed! – David Neale Apr 30 '10 at 08:47
  • @Darin: Ideally I would like to keep the type hidden completely, although I never actually considered an abstract approach. Would save me having to do all the setters/getters everytime I want to mimic derivation from the class. +1! – James Apr 30 '10 at 08:48
1

This look fine to me. Making small interfaces makes it easier to write decoupled code.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
1

I don't know if this will help, but you can make your base class protected internal. This would mean that any internal class has access to it as if it were public, or any class (from within and without the assembly) can subclass the base class. It won't prevent people from implementing their own sub class though.

Alternatively, exposing through an Interface would be the best way I'd think.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
1

For this you can opt for explicit implementation like this:

public interface IBaseClass
{
    int ID { get; set; }
}

internal class MyBaseClass : IBaseClass
{
    public MyBaseClass() { }
    public int IBaseClass.ID { get; set; }
}

public class MyExposedClass : IBaseClass
{
    private MyBaseClass _base = new MyBaseClass();

    public int IBaseClass.ID
    {
        get { return _base.ID; }
        set { _base.ID = value; }
    }
}

You can refer to a similar post C# Interfaces. Implicit implementation versus Explicit implementation

Community
  • 1
  • 1
Kangkan
  • 15,267
  • 10
  • 70
  • 113
  • @Kangkan: In my case I would like to still have access to the properties even if I create an instance of `MyExposedClass` itself. So implicit would be fine for me +1 for the suggestion though (as I can see from the example I am using explicit) – James Apr 30 '10 at 08:54
0

Make your base class abstract.

David Neale
  • 16,498
  • 6
  • 59
  • 85
0

You could expose the interface as public, implement an internal sealed implementation of that class, and use a factory approach to build instances of the desired interface. That way the client will never know when you change your implementation, or if you have multiple implementations of the same base interface plugged in the factory. You could also eliminate the set accessors in the interface and put them in the internal implementation to only expose the properties to the outside world. That way the exterior code has to make less assumptions over your implementation and you are better isolated. Please correct me if I'm having a poor/bad image of this approach.

Edit: The factory would be public and you'd need some sort of "transfer object" to pass data to the factory. That transfer object implementation would be public, together with it's interface.

DaeMoohn
  • 1,087
  • 13
  • 27
0

Your example seems to include a poor example of taking advantage of inheritence. Since you included a single property it and couldnt come up with a better example i am guessing that its real. I would suggest in this case forget the base class and stick the property on the derived.

mP.
  • 18,002
  • 10
  • 71
  • 105
  • @mP: The example is exactly that....an example! Its the design that matters for the question. This is why I asked it, I wanted to know if there was a way of having a base type that you can derive from but keep that particular type hidden. From what I have seen my approach is basically the only way of doing that (e.g. exposing it through an interface). – James Apr 30 '10 at 09:11