mardi 22 janvier 2019

Best Design For Constantly Changing Property Within A Singleton Class With IoC usage?

I have the following helper class which has public property _variableHandler. The property is public as I initially had visions of setting the property from the calling code before involving methods within the XAMLHelper class, but now doubt this is a smart approach. Mostly because I will need to call the class fairly often and not always update the value of _variableHandler, meaning things will get messy.

public class XAMLHelper
{
    public IVariableTypeHandler _variableHandler;

    public XAMLHelper()
    {
    }
}

I also have a factory which is used to provide the desired concrete instance of a VariableTypeHandler.

I am also using an IoC container (Unity) to provide a single instance of the XAMLHelper class, as shown below.

container.RegisterInstance<XAMLHelper>(new XAMLHelper());

Ideally, I would like to keep this single instance but simply update the value of _variableHandler when specified using like the code below.

container.Resolve(new PropertyInjection(VariableHandlerFactory.GetInstance("Str")));

I have tried adding snippets into container registration, such as a

new InjectionProperty()

But this doesn't seem to update the _variableHandler property once it has been instantiated the first time. Am I missing something important here? Or trying to do something that is not possible with an IoC container?

Aucun commentaire:

Enregistrer un commentaire