Q:

Repository Service layer Search Method best practice.

Repository layer 
    public async Task<IEnumerable<AbstractDataType>> Search()
    {
        var items = context.Table.ToList().Select(x => x.ToDomain());
        return items;
    }

Service layer
    var search = await eProfileRepository.Search();            
    var item = search.Where(x => x.Id == id).FirstOrDefault();

My question is this.  I want a dynamic search method that i can use in my service layer to search on a table.  Will the above get all of the records from the database before the where clause can be applied?  The reason I have to do this is because I have this extension method that converts the entity type -> domain type.  If i do an IQueryable return and i dont do a .ToList() I get an exception complaining that the extension method can't determine the type or somethign like that.  Anyone know of the negative ramifications of this code?  Thanks.

A:

The best practice is to not have a dynamic search function in your repository. All types of implementations will be leaky abstractions. It's also very difficult to index the tables properly if you allow searches on everything.
You could use the specification pattern to get a more dynamic nature, but still have control over what can be searched.
Or do a proper design where you are aware of what kind of searches the system really should support.
The good thing with explicit find methods in the repository is that you have full control of what the system should support. You can also vary the returned result set. A wide search typically returns less properties per object than a narrow search.

