0

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?

2 Answers 2

1

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");

Sign up to request clarification or add additional context in comments.

Comments

0

You need to include the namespace and assembly names in the config file like:

 <namespace name="MyNameSpace" />
 <assembly name="MyAssemblyName" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.