I’ve got a simple object into which I’m trying to inject properties. I’m unclear why this doesn’t seem to be working. My properties end up being null after I call resolve.
This is my object:
public interface IBasePage
{
[Dependency]
string Title { get; set; }
[Dependency]
string BaseUrl { get; set; }
}
This is what’s in my unity config file:
<register type="IWebActionDriver" mapTo="ConcreteWebDriver"/>
<register type="IBasePage" name="LoginPage" mapTo="LoginPage">
<property name="Title" value="Login Page Title"/>
<property name="BaseUrl" value="http://dev1.company.com"/>
</register>
In the startup method for my unit tests, here’s what I’m trying to do:
_container = new UnityContainer();
UnityConfigurationSection configSection =
(UnityConfigurationSection) ConfigurationManager.GetSection("unity");
configSection.Configure(_container, "testContainer");
_page = _container.Resolve<LoginPage>();
My concrete class looks like this:
[InjectionConstructor]
public LoginPage(IWebActionDriver driver)
{
_driver = driver;
_driver.Initialize();
this.CurrentPage = _driver;
}
Lo and Behold, the properties for Title and BaseUrl are null. Is it okay to use both an constructor injection and set properties on the object? Am I doing something else odd here?
The LoginPage is registered as a named registration (which makes sense because you can obviously have more than one IBasePage concrete type). So you should be able to achieve the behavior you want by resolving IBasePage by the name:
_page = _container.Resolve<IBasePage>("LoginPage");