Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to nested interfaces (not possible in C#)

Tags:

I'm using interfaces in this case mostly as a handle to an immutable instance of an object. The problem is that nested interfaces in C# are not allowed. Here is the code:

public interface ICountry {     ICountryInfo Info { get; }      // Nested interface results in error message:     // Error    13  'ICountryInfo': interfaces cannot declare types     public interface ICountryInfo     {         int Population { get; }         string Note { get; }     } }   public class Country : ICountry {     CountryInfo Info { get; set; }      public class CountryInfo : ICountry.ICountryInfo     {         int Population { get; set; }         string Note { get; set; }         .....     }     ..... } 

I'm looking for an alternative, anybody would have a solution?

like image 539
ericdes Avatar asked Feb 18 '10 08:02

ericdes


People also ask

Is nested interface possible?

We can declare interfaces as member of a class or another interface. Such an interface is called as member interface or nested interface. Interfaces (or classes) can have only public and default access specifiers when declared outside any other class (Refer this for details).

Can we have nested interface in C#?

The problem is that nested interfaces in C# are not allowed.

CAN interfaces have nested classes?

Yes, you can define a class inside an interface.

Why nested interfaces are static?

An inner interface has to be static in order to be accessed. The interface isn't associated with instances of the class, but with the class itself, so it would be accessed with Foo. Bar , like so: public class Baz implements Foo.


2 Answers

VB.NET allows this. So, you can create a VB.NET assembly only with the interface definitions that you need:

Public Interface ICountry   ReadOnly Property Info() As ICountryInfo        Public Interface ICountryInfo     ReadOnly Property Population() As Integer     ReadOnly Property Note() As String   End Interface End Interface 

As for the implementation, C# does not support covariant return types, so you must declare your class like this:

public class Country : ICountry {   // this property cannot be declared as CountryInfo   public ICountry.ICountryInfo Info { get; set; }      public class CountryInfo : ICountry.ICountryInfo {     public string Note { get; set; }     public int Population { get; set; }   } } 
like image 92
Jordão Avatar answered Sep 29 '22 22:09

Jordão


If the end goal is to use this with dependency injection, what's wrong with injecting them into each other instead of nesting?

public interface ICountry {     ICountryInfo Info { get; } }  public interface ICountryInfo {     int Population { get; set; }     string Note { get; set; } } 

and implement as:

public class Country : ICountry {     private readonly ICountryInfo _countryInfo;      public Country(ICountryInfo countryInfo)     {         _countryInfo = countryInfo;     }      public ICountryInfo Info     {         get { return _countryInfo; }     } }  public class CountryInfo : ICountryInfo {     public int Population { get; set; }     public string Note { get; set;} } 

Then once you set up your bindings for ICountry & ICountryInfo, CountryInfo will inject into Country whenever Country is injected.

You could then restrict the binding, if you wanted, to only inject CountryInfo into Country and nowhere else. Example in Ninject:

Bind<ICountry>().To<Country>(); Bind<ICountryInfo>().To<CountryInfo>().WhenInjectedInto<Country>(); 
like image 40
friggle Avatar answered Sep 29 '22 20:09

friggle