Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity constructor with default params

Problem with injection of constructor with default params.

For example

container.RegisterType<IAddressImportRepository, AddressImportRepository>(
    new PerRequestLifetimeManager());

public AddressImportRepository(
    FairFlexxDbContext context, bool isUseSecurePredicate = true)
    : base(context, isUseSecurePredicate)
{
}

Error:

[InvalidOperationException: The type Boolean does not have an accessible constructor.]
Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.ThrowForNullExistingObject(IBuilderContext context) +239 lambda_method(Closure , IBuilderContext ) +60
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.b__0(IBuilderContext context) +71
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +167
Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101 lambda_method(Closure , IBuilderContext ) +425
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.b__0(IBuilderContext context) +71
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey) +167
Microsoft.Practices.Unity.ObjectBuilder.NamedTypeDependencyResolverPolicy.Resolve(IBuilderContext context) +101 lambda_method(Closure , IBuilderContext ) +222
Microsoft.Practices.ObjectBuilder2.<>c__DisplayClass1.b__0(IBuilderContext context) +71
Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) +42
Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) +333
Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) +274
Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides) +383

like image 341
zayats Avatar asked Nov 29 '14 16:11

zayats


1 Answers

Unity uses reflection to determine the constructor parameter types. With your constructor, it found parameters FairFlexxDbContext and bool. It tries to construct an instance of each type and fails on the bool because it is a value type. It does not recognize the default value you have supplied to get past this error.

To get this to work, you have a couple options. Either tell the register your IAddressImportRepository with an InjectionConstructor and tell it how to resolve your constructor parameters or move your bool parameter to be a property instead. Since the bool is an optional parameter, I would recommend moving it to be a property.

container.RegisterType<IAddressImportRepository, AddressImportRepository>(
    new PerRequestLifetimeManager(), 
    new InjectionConstructor(new ResolvedParameter<FairFlexxDbContext>(), true));

or

public AddressImportRepository(FairFlexxDbContext context)
    : base(context, true)
{
    IsUseSecurePredicate = true;
}

public bool IsUseSecurePredicate { get; set; }
like image 199
TylerOhlsen Avatar answered Nov 12 '22 10:11

TylerOhlsen