1

I'm using reflection as a way of implementing a factory pattern:

        Type type = GetProviderType(vendor);
        ConstructorInfo constructorInfo = type.GetConstructor(new Type[] { typeof(NameValueCollection) });
        ExternalSourceProvider vendorSourceProvider = (ExternalSourceProvider)constructorInfo.Invoke(new Object[] { requestData });

I can ensure that a member of the fabricated object is invoked by adding this code:

        _externalSiteId = vendorSourceProvider.ExternalId;
        _app = vendorSourceProvider.App;

This is smelly - I shouldn't need to 'touch' the member to ensure it's invoked. Any ideas on how to ensure invocation of the member? The reason I want to invoke these members is that I need to 'bootstrap' the fabricated 'vendorSourceProvider' object (and touching the member allows me to do this in the {get} of the member). Any ideas on how to achieve what I'm trying to do without needlessly 'touching' the member?

3
  • 4
    What's smelly is that you want to invoke a getter without using the result. Commented Nov 14, 2016 at 4:04
  • Questions asking for assistance in explaining, writing or debugging code are off-topic here. Please ask this on Stack Overflow. Voting to close.
    – David Arno
    Commented Nov 14, 2016 at 8:54
  • 1
    You're using reflection to invoke a constructor that could otherwise be invoked with new. You already have a lot of smell; what significant difference will "touching your member" make? [insert Saturday Night Live joke here] Commented Nov 14, 2016 at 16:16

1 Answer 1

3

You seem to be overcomplicating your overcomplifications.

If you want to bootstrap an object into a specific state, then give it a method that accomplishes this.

What you're doing is akin to using magic and quantum mechanics to sort a deck of cards. Why not simply sort the deck of cards (deck.sort() or deck.prepare())?

Your Answer

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

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