Self service interface

This is what I want to do:

Interface IMyInterface { List<IMyInterface> GetAll(string whatever) } 

so that classes implementing this must have a function that returns a list of its type. Is it possible? I know that - technically - a class that implements this might return a list of other classes that implement it, not necessarily the same class, but I can live with it even if it is not perfect.

I tried this, but I can't get the implementing class to correctly implement the method.

+4
source share
4 answers

This works for me:

 public interface IMyInterface { List<IMyInterface> GetAll(string whatever); } public class Program : IMyInterface { public string Member { get; set; } public List<IMyInterface> GetAll(string whatever) { return new List<IMyInterface>() { new Program() { Member = whatever } }; } static void Main(string[] args) { List<IMyInterface> all = new Program().GetAll("whatever"); Console.WriteLine(all.Count); } } 
+1
source

The implementation of this interface is straightforward:

 public class MyInterfaceImpl : IMyInterface { public List<IMyInterface> GetAll(string whatever) { return new List<IMyInterface> { new MyInterfaceImpl(), this }; } } 

Please note that the signature of the method must be exactly the same, i.e. return type should be List<IMyInterface> , not List<MyInterfaceImpl> .

If you want the type in the list to be the same type as the class that implements the interface, you will have to use generics:

 public interface IMyInterface<T> where T : IMyInterface<T> { List<T> GetAll(string whatever) } public class MyInterfaceImpl : IMyInterface<MyInterfaceImpl> { public List<MyInterfaceImpl> GetAll(string whatever) { return new List<MyInterfaceImpl > { new MyInterfaceImpl(), this }; } } 
+9
source

This is a normal solution. You have an IPerson interface and you want to access each parent. Therefore, it would be wise to have an interface declaration like this:

 interface IPerson { IList<IPerson> GetAllParents(); } 

Now you can get the parents from these parents, and then get the parents ... I hope you have an idea. This design is very flexible because it allows you to simulate deep dynamic structures using simple static models.

The implementation is very simple:

 class Person : IPerson { IList<IPerson> parents; public Person(IList<IPerson> parents) { this.parents = parents; } public IList<IPerson> GetAllParents() { return parents; } } 

In a sense, you need to create some people without parents (some kind of Adam and Eve), and then add children by keeping links to their parents. As you can see, my naive model can handle randomly deep family structures with a very simple interface that is open to the outside.

+2
source

I do not understand why the interface cannot refer to itself - no problem below.

 interface ITest { List<ITest> GetAll(string whatever); } class MyClass : ITest { public List<ITest> GetAll(string whatever) { return new List<ITest>(); } } 
+1
source

Source: https://habr.com/ru/post/1484598/


All Articles