开发者

How to use a class as the base, but hide the class type publically?

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?


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.


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


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.


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


Make your base class abstract.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜