Reputation: 2724
Giving the following sample:
class Customer
{
ICustomerRepository repository;
private string name;
public Customer(string name, ICustomerRepository repository)
{
Name = name;
this.repository= repository;
}
public string Name
{
get {return name;}
set
{
if(String.IsNullOrWhiteSpace(value))
throw new ArgumentException();
name = value;
}
}
}
In this scenario i can't do that because i have a parameter on constructor. There is an another approach to do that?
Edit:
Abstraction of my the container:
interface IFactory
{
T Get<T>();
IEnumerable<T> GetAll<T>();
}
Implementation:
class ApplicationFactory : IFactory
{
private readonly IKernel ninjectKernel = ApplicationNinjectKernel.BuildNew();
public T Get<T>()
{
return ninjectKernel.Get<T>();
}
public IEnumerable<T> GetAll<T>()
{
return ninjectKernel.GetAll<T>();
}
}
ApplicationNinjectKernel.BuildNew() creates and return a new StandarKernel() with the binds..
ConstructorArgument sounds bad to me. With this approach i will put some ugly thing on my code and also will miss type checking at project time. :/
Upvotes: 0
Views: 92
Reputation: 4203
If you are trying to use Ninject to resolve using a given customer name, you can do it like this:
var customer = kernel.Get<Customer>(new ConstructorArgument("name", "Bob"));
This also requires that you followed the convention of binding your ICustomerRepository in the Ninject Module and loaded it into the Ninject Kernel. Example:
public override void Load()
{
Bind<ICustomerRepository>().To<CustomerRepository>();
}
Upvotes: 1