How to hide the implementation

Suppose I have a class library where any inner classes have access to the following interface:

interface myInterface { string myProperty { get; set; } // notice setter. } 

But if someone adds this class library to their project, they will get the following interface:

 public interface myInterface { string myProperty { get; } } 

What is the most efficient and acceptable way to do this? Does one interface implement another?

+6
source share
3 answers

Make your public interface only the recipient:

 public interface myInterface { string myProperty { get; } } 

And then output another internal interface from it that has a setter:

 internal interface myInternalInterface : myInterface { new string myProperty { get; set; } } 

You can implement the internal interface:

 class myImplementation : myInternalInterface { public string myProperty{get; set;} } 

If you need to call setter, you can direct your instance to internal intuition and call it. However, this approach is a little constructive odor, so use it sparingly.

+2
source

You can have an internal interface extending the open interface, for example:

 public interface MyInternalInterface: MyPublicInterface { string MyProperty { set; } } public interface MyPublicInterface { string MyProperty { get; } } internal class A: MyInternalInterface { public string MyProperty { get; set; } } public class Foo { private A _a = new A(); internal MyInternalInterface GetInternalA() { return _a; } public MyPublicInterface GetA() { return _a; } } 

This way you do not need any throws or anything else.

+1
source

I thought the @adrianbanks answer could be improved on mine, but I donโ€™t think it really (despite being excellent) - because you have no guarantee that the public interface instance passed to you also implements the internal one - This also applies to this decision. It also exists that it only works if the type of implementation is internal - this is not good if you want to provide public types as standard implementations of an interface or as the basis for a hierarchy.

This is what I use. Given:

 interface myInterface { string myProperty { get; set; } } public interface myPublicInterface { string myProperty { get; } } 

At first you cannot make myPublicInterface inherit myInterface , because the compiler will moan about inconsistent availability. That way, you can explicitly implement the internal using the property support tool, and then implement the public one implicitly:

 public class MyClass : myInterface, myPublicInterface { private string _myProperty; string myInterface.myProperty { get { return _myProperty; } set { _myProperty = value; } } public string myProperty { get { return _myProperty; } } } 

Note. In some cases, the getter may not be suitable for a private third-party user, but it may be some kind of logic that calculates values โ€‹โ€‹from other properties. In this case - to keep it DRY - you can put the logic in a public getter and leech, which for an explicit getter:

 string myInterface.myProperty { get { return MyProperty; } set { /*whatever logic you need to set the value*/ } } public string myProperty { get { /*whatever complex logic is used to get the value*/ } } 

You can do it differently, but you have to make the terrible look of the built-in interface:

 string myInterface.myProperty { get { /*whatever complex logic is used to get the value*/ } set { /*whatever logic you need to set the value*/ } } public string myProperty { get { return ((myInterface)this).myProperty; } } 

What you should try to avoid when possible.

0
source

All Articles