Suggestions for implementing a simple search for "business objects" in a .NET WinForms application?

What is the best way to provide easy search capabilities for "business objects" in a .NET WinForms application?

By "simple search" I mean something like the search box "Vista + Windows 7", which is located in the upper right corner of almost every window that looks for the contents of this window (nothing unusual, maybe there is no "advanced") to look for or to keep it simple )

By “business objects,” I mean classes based objects for things like “clients” and “addresses” (basically, your simple contact information).

I considered a “simple” custom search, perhaps when my classes implement the “Contains” function, so that I can ask every object if it “contains” any of the user's search conditions (and then in some form of simple rank based on how many words were matched).

I also looked at Lucene.NET, but it seems to me that it is too complicated for my needs - I need to create an index (and update and save it).

Suggestions? Ideas?

+1
source share
4 answers

I don’t know about the “best” way (I would work at Google if I did). However, given that I did something similar in a demonstration with proof of concept / client a few months ago, this did the trick. Please note that I was able to quite effectively contain the problem area, especially by the size of the data set with the ability to directly search, so performance was not a problem.

I created a FilterableListView UserControl . I used the ListView in drill-down mode, I dropped the TextBox directly above it and used the middleware interface to give it a CueText (something like "Filter" or "Search"). Then I updated the contents of the ListView from the background thread (using the equivalent of my SafeInvoke implementation) if the delay is 0.5 seconds since the last TextChanged event from the filter window.

I made a simple, case-insensitive substring match with the contents of the specified field in the ListView , it was fast, simple and efficient. I found Linq to Objects very useful.

A few things that I would do better for a more production-ready implementation:

  • Use the double-click speed to calculate the appropriate delay before performing a search.
  • Provide a callback mechanism to perform the search instead of creating it in the control. Perhaps something like an IFilterable interface?
+2
source

I would use a simple interface for each class, which simply returns a list of search terms that describe the instance. Then you can get all your objects, set search terms for them and rank them based on custom search terms.

This is close to your idea for the Contains interface, but preserves more logic from business classes. You can even explore objects using reflection, and you can simply add some user attributes to the properties to give the search engine some hints about which properties to include in the search.

But keep in mind that you will have to evaluate each search for all of your properties. It will be very slow for about a hundred or so objects if you use the sophisticated ranking function. Using reflection will further aggravate the situation.

Another problem to solve is how to efficiently process the search result. If you have quite a few different classes, this may be a task other than a triangle to display the results and navigate the user to a place in the application where he can do something with these objects.

0
source

I think your approach to using the Contains method in each object is very useful.

You can give " weight " to the members of the object so that if the contains method finds a match, enter a kind of " Score " as the return.

Another thing is to take into account whether the correspondence corresponds to a complete match or not ... and assigns more or less significance to this assessment.

If you search across several types of objects ... perhaps you can assign higher values ​​to the main objects (more business-centered objects) to give them a higher priority.

Just thoughts...

0
source

As long as you don't have too many objects, it makes sense to do what you describe by simply looping through each object. You can use regex rather than just Contains to give it a little more flexibility. Contains will give you an object match, not just the string value of the property inside your object. Matching the string is likely to be more useful to you.

One thing you can do is make a method in your objects, which is just a concatenated string of all the strings in that object that you want to look for. Then use the regular expression to search for the value obtained from this method. It will not be easy the best solution, but it would be easy and fast. As I said, as long as you have too many objects, everything will be fine.

0
source

All Articles