1

EDITED FOR CLARIFICATION

In the past, I have seen a whole manner of resolutions and fudges. Some really stand out. One particular resolution that I initially thought of as a fudge possibly deserves a category of its own.

It could be considered a hack or a pattern, as a design pattern is just a piece of reusable code. So, it still conforms to the definition: http://en.wikipedia.org/wiki/Software_design_pattern

It possibly already has a name but I have not seen it in the literature. I was wondering if anyone knows another name for what I labelled the "exposer (hack) pattern".

I have included an example of it below. This code is used to expose data that is not available to a data source. Instead, the results are not generated until a data bind method is called. Unfortunately, these results are not stored anywhere in the data source after execution. This is due to an oversight (or bad design) in the third party API. So, the results are passed a repeater to store locally, before returning these out as a populated PageDataCollection:

public static PageDataCollection Search(this SearchDataSource searchDataSource)
{
            PageDataCollection results = new PageDataCollection();

            //This is a dummy repeater whose only purpose is to provide a way of
            //iterating over the search results
            Repeater dummy = new Repeater();

            dummy.ItemDataBound += delegate(Object sender, RepeaterItemEventArgs e)
            {
                if (e != null && e.Item != null && e.Item.DataItem != null)
                {
                    PageData pageData = e.Item.DataItem as PageData;

                    if (pageData != null)
                    {
                        results.Add(pageData);
                    }
                }
            };

            dummy.DataSource = searchDataSource;

            dummy.DataBind();

            return results;
        }

3 Answers 3

5

Given your reference to problematic 3rd party API you can't just get rid of, your workaround looks pretty close to Escape Hatch:

Context: You are in a paradigm (e.g. language, runtime environment, scribble on back of envelope) and you need to express or do some thing which does not fit.

Problem: You are being restricted by your current tool set.

Forces: You may not be able to ditch your current tool for various reasons: technical, legal, political or legacy code...

By the way if you've got a lot of issues of that kind, consider arranging an Anticorruption Layer:

If your application needs to deal with a database or another application whose model is undesirable or inapplicable to the model you want within your own application, use an AnticorruptionLayer to translate to/from that model and yours.

1
  • 2
    +1 I really like this answer - I knew it made sense because it wasn't a fudge (despite that it doesn't seem neat). Now, I have a name for it, I'm happy.
    – Phil C
    Dec 26, 2011 at 12:32
1

I wouldn't want it known as a pattern because it really is not a pattern to recommend in general.

From you comment to Tom Squires' answer I understand that it is to expose stuff in classes of which you are not the author. And you need some way to get at stuff not exposed by the authors of those classes.

I use TProtectedSomeClass = class(TSomeClass) (Delphi) to get access to protected members of a class in libraries I don't control (even if I have the source). It is known as the "protected hack".

What you are doing feels like hacking as well and I think that if you name the pattern it would be good to have "hack" in the name of it. "Hacker" sounds nice and short. :-)

10
  • It's not so much about getting access to protected members in this case. The results are not generated until a data bind is called but these results are not stored back into searchDataSource... Let me amend... Incidientally, a design pattern is just a piece of reusable code. So, it still conforms to the definition: en.wikipedia.org/wiki/Software_design_pattern
    – Phil C
    Dec 26, 2011 at 10:54
  • Yeah, I didn't write it. I added code guards to it.
    – Phil C
    Dec 26, 2011 at 11:02
  • While it may follow the definition, I don't consider it a design pattern in the sense that in general design patterns are considered "desirable" ways of solving problems. This case seems undesirable, and only to be used if other avenues have been exhausted (like getting the authors to offer other ways of doing what you need to do). Also keep in mind the "Tell Don't Ask" principle. It is usually better to tell a class to do something than to find a way to get at its innards. While Reflection (and similar techniques) are very powerful, I am still in two minds about when it should be used. Dec 26, 2011 at 12:48
  • It sounds like your environment is very different to mine. This third party API are not normally responsive to change requests, much less responsive than Microsoft even... I've already covered your other points... What use is reflection if the expected results property does not exist, let alone exposed?
    – Phil C
    Dec 26, 2011 at 12:53
  • @Carnotaurus: I have found third party component and API implementing library vendors generally very responsive to developer requests. If by "third party API" you mean the ones actually specifying the API (the API providers) you are probably right. Reflection can't do anything for things that don't exist. However, it can get at the private members of any class. It makes it very useful for framework and library development, but is a two-edged sword as it defeats encapsulation. Dec 26, 2011 at 13:01
0

I don't know of an applicable anti pattern. I would just says its an arse backward way of coding. If you're writing code like that, you're either using the wrong classes or don't understand the correct implementation of those classes.

Edit from your comments (I didn't get that from the question) it seems you're coding like this to get around the limitations of a 3rd party API. The most appropriate term is a "hack". Hacks should be avoided where possible but clearly documented when they can't be avoided.

9
  • Just read the question again - this style is used when there is nothing in a third party API to expose what results you need to return. So, there is by definition, no neat way of getting back what you want.
    – Phil C
    Dec 26, 2011 at 10:22
  • That's kind of harsh. Correct, and I would definitely upvote if it was a comment, but I don't like it as an answer. Care to expand with at least some hints towards a better implementation?
    – yannis
    Dec 26, 2011 at 10:23
  • I cannot see how the implementation can be vastly improved because such code use is a result of bad design in the third party API. So, any innovation to get back what you want would look like a fudge.
    – Phil C
    Dec 26, 2011 at 10:27
  • @Carnotaurus If you know the quality of your code is not so good, regardless of the reasons, I'm a little puzzled on why you are calling it a pattern. "bad design in the third party API" is a situation where a hack would be appropriate, but patterns are supposed to provide good design solutions and I don't really think the term is applicable in situations like yours. Assuming of course that by pattern you mean design pattern.
    – yannis
    Dec 26, 2011 at 10:35
  • @Yannis I don't like the code any more than you do. It gets the job done in the best way possible (lots of balking for example) considering the flaw in the thirty party API. So how can it be a hack it's the best way to achieve the desired goal?
    – Phil C
    Dec 26, 2011 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.