Resolving Strings in Unity Application Block

Topics: Enterprise Library Core, General discussion
May 11, 2010 at 7:55 PM
Edited May 11, 2010 at 8:01 PM

I can't get my application to correctly resolve string parameter values in a constructor, when using the unity application block.

 

My configuration looks like:

 

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="Accounts.Business.Repository"/>
    <namespace name="Accounts.Business.Repository.SqlServer"/>
    <container>
      <register type="IAccountsRepository" mapTo="AccountsRepository"></register>
      <register type="AccountsRepository" name="AccountsRepository">
        <constructor>
          <param name="connectionStringKey" value="SetupDb">
          </param>
        </constructor>
      </register>
    </container>
  </unity>

 

The constructor for AccountsRepository looks like:

 

public class AccountsRepository : IAccountsRepository
    {
        private string _ConnectionStringKey;

        public AccountsRepository(string connectionStringKey)
        {
            this._ConnectionStringKey = connectionStringKey;
        }

        // rest of class
    }

 

My classes are resolved in a UnityControllerFactory I built:

public class UnityControllerFactory : DefaultControllerFactory
    {
        private IUnityContainer _Container;
        
        public UnityControllerFactory()
        {
            // Instantiate a container, taking configuration from unity.config
            _Container = new UnityContainer().LoadConfiguration();
        }

        protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
        {
            IController controller;

            if (controllerType == null)
            {
                controller = null;
                //throw new HttpException(404, string.Format("The controller for path '{0}' could not be found or it does not implement IController.", requestContext.HttpContext.Request.Path));
            }
            else
            {
                controller = _Container.Resolve(controllerType) as IController;
            }

            return controller;
        }
    }

 

 

Whenever IAccountsRepository attempts to get resolved, I get the error:

The type String cannot be constructed. You must configure the container to supply this value.

How can I get this to work?

May 12, 2010 at 1:28 AM
Edited May 12, 2010 at 1:30 AM

The reason is because you configured the constructor injection for the AccountsRepository type but you are resolving for an IAccountsRepository.  You should put the <constructor> element under the <register> element for the IAccountsRepository type and remove the mapping of AccountsRepository to AccountsRepository. 

This is actually a Unity concern.  For such questions, please post it in the Unity forum - http://unity.codeplex.com/Thread/List.aspx.  

 

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@Avanade.com

May 12, 2010 at 2:08 AM
Edited May 12, 2010 at 2:08 AM

That worked.   My configuration looks like:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="Accounts.Business.Repository"/>
    <namespace name="Accounts.Business.Repository.SqlServer"/>
    <container>
      <register type="IAccountsRepository" mapTo="AccountsRepository">
        <constructor>
          <param name="connectionStringKey" value="SetupDb">
          </param>
        </constructor>        
      </register>
    </container>
  </unity>

and it fixed it.  Thanks for the pointer; I misunderstood how this works but now it makes sense.