views:

108

answers:

1

Hi all, is it possible inject static property, like I do below, because it does not work for me?

    public static IMerchantModule MerchantModule { get; set; }

    public RequestBaseValidationRules()
    {
        MerchantModule = ObjectFactory.GetInstance<IMerchantModule>();
    }

It works when I inject to non static property.

Any tip welcome. Thanks, X.

Update: MerchantModule is null when it is accessed, see the example below

    public static IBusinessRule<T> Sha1HashChecksum
    {
        get
        {
            return new BusinessRule<T>(
                MethodBase.GetCurrentMethod().Name, "Sha1Hash is not valid",
                request =>
                    {
                        string sharedSecret =
                            MerchantModule.GetSharedSecretForMerchantId(request.MerchantId);
                        string hashCheck = HashHelper.GetSha1Hash(request.StringToHash, sharedSecret);
                        return request.Sha1Hash.Equals(hashCheck);
                    });
        }
    }
A: 

No, there is no problem with storing a value returned from StructureMap into a static property.

In your example, you are setting the MerchantModule property in an instance constructor, but then referencing it from a static property (Sha1HashChecksum). If you haven't yet created an instance of your class (which executes the instance constructor), the MerchantModule property will be null. Either change your code to use instance methods/properties, or set the static MerchantModule property in a static constructor (replace the word "public" with "static" in your constructor declaration).

Joshua Flanagan
Oh I got it. :-) thanks for pointing it out.
Xabatcha