C # Factory Sample

I am creating a search application that has indexed several different data sources. When a query is run against a search engine index, each search result indicates which data source it came from. I built a factory template that I used to display a different template for each type of search result, but I realized that this template will become more difficult to manage, as more and more data sources are indexed by the search engine (i.e. a new code template should be created for each new data source).

I created the following structure for my factory based on an article by Granville Barnett at DotNetSlackers.com

factory pattern http://img11.imageshack.us/img11/8382/factoryi.jpg

To simplify the work with this search application, I decided to create a set of database tables that can be used to determine the individual types of templates that the factory template could use to determine which template to build. I decided that I would need to find a search table that will be used to indicate the type of template to build based on the data source of the search result. Then I will need a table (s) to indicate which fields will be displayed for this type of template. I will also need a table (or additional columns in the template table) that will be used to determine how this field is displayed (for example, hyperlinks, tags, CssClass, etc.).

Does anyone have examples of such a pattern? Please let me know. Thanks, -Robert

+5
source share
1 answer

I would suggest that this proposed solution be no less convenient than just matching a data source with a code template, as it is now. In fact, I would even say that you will lose flexibility by pushing the template diagram and image information into the database, which will complicate your application.

For example, suppose you have these data sources with attributes (if I understand this correctly):

Document { Author, DateModified }
Picture { Size, Caption, Image }
Song { Artist, Length, AlbumCover }

You can then get one of these data sources in the search results. Each item is rendered differently (an image can be displayed with a preview image attached to the left, or Song can display an album cover, etc.)

. , HTML, , , . , , , , . , , , .

, , , , . , , , , .

, . , , , .

, / , factory . CSS . , XML . , , CSS .

:

, switch:

switch (resultType)
{
    case (ResultType.Song):
      factory = new SongResultFactory();
      template = factory.BuildResult();
      break;
    // ...

. - , switch, , :

IDictionary<ResultType, ResultFactory> TemplateMap;
mapping = new Dictionary<ResultType, ResultFactory>();
mapping.Add(ResultType.Song, new SongResultFactory());
// ... for all mappings.

switch :

template = TemplateMap[resultType].CreateTemplate();

, - - , switch, IDictionary, .

XML , :

<TemplateMap>
    <Mapping ResultType="Song" ResultFactoryType="SongResultFactory" />
    <!-- ... -->
</TemplateMap>

et. . IDictionary. - , XML , .

+4

All Articles