Workaround or alternative to static methods on the interface

I perform some naive searches in my application, and the search will be performed on several different types of objects (Customer, Appointment, Activity, etc.). I am trying to create an interface that will have types that are searchable. I would like to do something like this:

public interface ISearchable
{
    // Contains the 'at a glance' info from this object 
    // to show in the search results UI
    string SearchDisplay { get; }

    // Constructs the various ORM Criteria objects for searching the through 
    // the numerous fields on the object, excluding ones we don't want values 
    // from then calls that against the ORM and returns the results
    static IEnumerable<ISearchable> Search(string searchFor);
}

I already have a concrete implementation of this on one of the objects of my domain model, but I would like to extend it to others.

The problem is obvious: you cannot have static methods on the interface. Is there another prescribed method for doing what I'm looking for, or is there a workaround?

+3
source share
3 answers

, . , , , :

public interface ISearchDisplayable
{
    // Contains the 'at a glance' info from this object 
    // to show in the search results UI
    string SearchDisplay { get; }
}

public interface ISearchProvider
{
    // Constructs the various ORM Criteria objects for searching the through 
    // the numerous fields on the object, excluding ones we don't want values 
    // from then calls that against the ORM and returns the results
    IEnumerable<ISearchDisplayable> Search(string searchFor);
}

ISearchProvider - , , ISearchDisplayable , .

+3

#, Java, , , , < href= "http://en.wikipedia.org/wiki/Singleton_pattern" rel= "nofollow noreferrer" > singleton.

0

, , ISearchable. Search(); ISearchable .

public class Searcher<T> where T : ISearchable
{
    IEnumerable<T> Search(string searchFor);
}
0

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


All Articles