|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWe all know that when writing software, re-use is of importance to get the most out of the time we have. When we apply this to object collections, we should be aiming to write collection agnostic code that can be used over and over again. For example, when we use a collection of say User objects, we should be able to give it the same operation as we give a collection of Car objects. The operation that we pass to the collection shouldn't care if the collection has cars, users or elephants. It just knows how to perform its operation. BackgroundI stumbled on a great article recently on sorting object collections using reflection. The Reflective Filter FeaturesThe features of the
Using the codeTo use the Example: We have a collection of names and IDs and want to filter it so we only get back names that are equal to Phil or Richard. // In the reflective filter, we pass it (at the least)
// the typeof object that is contained in the collection we are filtering
// We also pass in the type of collection we arefiltering (and expect back)
ReflectiveFilter rFilter = new
ReflectiveFilter(typeof(CompanyName.DomainObjects.ConstrainedType.NameIdReference),
typeof(CompanyName.DomainObjects.ConstrainedType.NameIdReferenceCollection),
FilterClause.Equals, FilterOperand.Or);
// add filters
// as above we have used the OR operator and the Equals clause
// this means we will get all records where
// the name=Richard OR name=Phil
rFilter.AddFilter("Name", "Richard");
rFilter.AddFilter("Name", "Phil");
ACollectionType filteredCollection = new NameIdReferenceCollection();
filteredCollection = nameIdReferenceCollection.Filter(rFilter);
As you can see from the example, we pass in up to four parameters.
The The The default constructor accepts just two parameters:
ConclusionThe
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||