Domanda

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?

È stato utile?

Soluzione

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())?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top