C # interfaces with the same method name

I don’t know that what I would like to do is simply impossible: or I don’t think about it in the right way.

I am trying to build a repository interface class that takes a generic type and uses it as a basis for returning with most of its methods, i.e.:

public interface IRepository<T> { void Add(T source); T Find(int id); } 

This will then be inherited by the actual repository class, for example:

 public class TestClientRepository : IRepository<ClientEmailAddress>, IRepository<ClientAccount> { } 

The idea is that in ClientRepository, for example, I want to perform operations with several different types of objects (ClientAccount, ClientEmailAddress, etc.); but basically all types of operations are needed.

When I try to use TestClientRepository (after an explicit implementation of the interface), I do not see several search and add methods.

Can anyone help? Thanks.

+6
source share
4 answers

Of course, all you have to do is use it as an appropriate interface:

 TestClientRepository repo = new TestClientRepository(); IRepository<ClientEmailAddress> addrRepo = repo; ClientEmailAddress address = addrRepo.Find(10); IRepository<ClientAccount> accountRepo = repo; ClientAccount accoutn = accountRepo.Find(5); 

Basically, explicitly implemented interface methods can only be called by an expression of an interface type, and not by a specific type that implements the interface.

+5
source

You said:

(after implementing the interfaces explicitly)

When you implement an interface explicitly, the only way to "see" these methods is to bind the object to an explicitly implemented type. Therefore, if you want to use it as an IRepository<ClientEmailAddress> , you will need to use it as such. Using it as a TestClientRepository will not allow you to see any explicitly implemented methods.

+2
source

Since the common parameters in legacy interfaces are different, you really don't need an explicit interface implementation for Add .

Unfortunately, the general parameter does not affect the Find signature, but you can still choose one of two Find for the "default". For instance:

 interface IRepository<T> { void Add(T source); T Find(int id); } class ClientEmailAddress { } class ClientAccount { } class TestClientRepository : IRepository<ClientEmailAddress>, IRepository<ClientAccount> { public void Add(ClientEmailAddress source) { throw new NotImplementedException(); } public void Add(ClientAccount source) { throw new NotImplementedException(); } public ClientAccount Find(int id) { throw new NotImplementedException(); } ClientEmailAddress IRepository<ClientEmailAddress>.Find(int id) { throw new NotImplementedException(); } } // ... var x = new TestClientRepository(); x.Find(0); // Calls IRepository<ClientAccount>.Find. ((IRepository<ClientAccount>)x).Find(0); // Same as above. ((IRepository<ClientEmailAddress>)x).Find(0); // Calls IRepository<ClientEmailAddress>.Find. 
+2
source

When I explicitly implemented an interface for one of the interfaces, I could not use the var keyword

 var tcr = new TestClientRepository(); tcr. -- nothing there. 

When I specify the type, this works as expected.

 IRepository<ClientAccount> ca = new TestClientRepository(); ca.Add(new ClientAccount { AccountName = "test2" }); IRepository<ClientEmailAddress> cea = new TestClientRepository(); cea.Add(new ClientEmailAddress { Email = " test2@test.com " }); 
0
source

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


All Articles