This answer shows how to resolve instances using a factory interfaces without parameters.
I am using the following code
public interface ISimpleBarFactory
{
Bar CreateBar(int value);
}
public sealed class SimpleBarFactory : ISimpleBarFactory
{
private readonly Container _container;
public SimpleBarFactory (Container container)
{
_container = container;
}
public Bar CreateBar(int value)
{
_container.Register(() => new Bar(vlue));
return _container.GetInstance<Bar>();
}
}
to resolve instances which have constructor parameters.
However, I get the following exception when using the factory to instantiate the service class:
The container can't be changed after the first call to GetInstance, GetAllInstances and Verify.
Which is the right way of to resolve instances using factory interfaces with parameters?
Update
The following is my code. I am migrating the code from Ninject.
public interface IFormsUIFactory
{
AccountUI CreateAccountUI(Account account);
}
public class FormsUIFactory
{
private readonly IFormsUIFactory _uiFactory;
public FormsUIFactory(IFormsUIFactory uiFactory)
{
_uiFactory = uiFactory;
}
public void CreateAccountUI(Account account)
{
_uiFactory.CreateAccountUI(account);
}
}
UI class to be injected
public partial class AccountUI : Form
{
private readonly IAccountMaintenanceProcessor _processor;
private readonly Account _account;
public AccountUI(IAccountMaintenanceProcessor accountProcessor, Account account)
{
_processor = accountProcessor;
_account = account;
}
}
Instantiating code:
var account = new Account();
// Populate values for the account
var frm = _uiFactory.CreateAccountUI(account);