hardywang
hardywang

Reputation: 5162

Unity Framework 2.0 Type config without config file and constructor parameter

I have a class like:

public class CustomDatabase : Database
{
    public CustomDatabase(string connString, ILogger logger)
    {
    }
}

for example I can use the following config section to config a type and constructor parameters

      <type type="Database" mapTo="UnityExamples.Common.CustomDatabase, UnityExamples.Common">
        <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,  Microsoft.Practices.Unity.Configuration">
          <constructor>
            <param name="connString" parameterType="System.String">
              <value value="connection value..."/>
            </param>
            <param name="logger" parameterType="ILogger">
              <dependency />
            </param>
          </constructor>
        </typeConfig>
      </type>

But how can I convert this section into pure C# code configuration?

Upvotes: 0

Views: 611

Answers (1)

Sebastian Weber
Sebastian Weber

Reputation: 6806

Try the following

container.RegisterType<Database, CustomDatabase>(new InjectionConstructor("someConnectionString", typeof(ILogger)));

Upvotes: 1

Related Questions