Abstract id attribute in C # or Java

I am developing an API. Here are some examples from the interface:

Entry findEntry(int processId); Entry findEntry(int processId, Filter filter); 

where processId refers to some unique identifying information. However, I do not know what type processId .

How to abstract from an element like id of something?

The best I could come up with was to create a dummy interface:

 Entry findEntry(ProcessId id); Entry findEntry(ProcessId, Filter filter); 

However, I am worried that with the above approach, I can get the API client to run at a very high level of abstraction. For example, equality of the process identifier will no longer work (whereas if they used int-it).

Clarification: I was unable to clarify that I am writing only interfaces (contracts) that will be implemented later, possibly by another team. That is why I cannot force certain things, such as the equals method.

+1
source share
1 answer

Generic is your friend here

in the method itself, if necessary

 Entry findEntry<TKey>(TKey processId); 

or maybe class

 public class EntryFinder<TKey> { public Entry findEntry(TKey processId) { // Implementation } } 

Change If you define an interface, you can also define it there and leave it to the interface implementers to figure out what type they want to use to identify the record.

 public interface IEntryFinder<TKey> { Entry findEntry(TKey processId); } 

Using:

 // Foo are looked up by integer public class FooEntryFinder : IEntryFinder<int> { public Entry findEntry(int processId) { // Implementation } } // Baa are looked up by string public class BaaEntryFinder : IEntryFinder<string> { public Entry findEntry(string processId) { // Implementation } } 
+6
source

All Articles