Closed zlittell closed 4 years ago
More info:
When I initially made this I was using an instance registration command that looked like this:
this.container.RegisterInstance(typeof(DeviceHelper), typeof(DeviceHelper).ToString(), typeof(DeviceHelper));
Which is definitely why it wasn't breaking in the constructor.
I have now switched to trying to do this
this.container.RegisterInstance(typeof(DeviceHelper), typeof(DeviceHelper).ToString(), new DeviceHelper());
But my problem is that I inject dependencies into the constructor of DeviceHelper. It complains that I am not providing the parameters required.
public DeviceHelper(IEventAggregator aggregator, DeviceModel model)
EDIT:
This is how I got it to work
this.container.RegisterInstance(typeof(DeviceHelper), typeof(DeviceHelper).ToString(), new DeviceHelper(this.container.GetInstance<IEventAggregator>(), this.container.GetInstance<Models.DeviceModel>()));
I have seen this mentioned as "Antipattern" but normally refers to manually getting instances in other code not the bootstrapper config. My final question, is this the best way to do this or is there something more preferred?
Sounds like you want to use Singleton
.
I can see how a singleton would work better because I only ever want one, but the singleton isn't created until called. What would be the preferred method of causing the singleton instance to be created in the bootstrapper so that it runs in the background of the app constantly.
I would probably just resolve the singleton from the container at startup to ensure its creation.
So I set this up when I got home. Just added a section to the configure function that calls this.container.GetInstance
Feel dumb after I have it all working, but thank you for all the explanations.
I am not really a high level object oriented programmer, so I have been struggling through an app. It has been fun to learn though, and it has been a lot of learning.
I am at a point where I want to create a helper class that would always exist, this would capture events from the aggregator and parse them into a model.
My problem is that I registered the instance and it just never instantiates it. I can put a breakpoint in the constructor of the helper and it never breaks. If I register the helper as a singleton and then pass it in the constructor of my main view model it works fine because it seems something is requesting it from the container and then the container creates it finally. I thought registering instances provided this functionality over singletons, but I must be mistaken. Is this the correct way to handle this or is there some better way? My main view model never touches the helper, it is just supposed to sit in the background and catch events, so passing it in the constructor seems silly.
-Zack-