1
I am beginning in the use of simpleject for dependency injection.
I created the Bootstrapper class to register containers:
public class BootStrapper
{
public static void RegisterServices(Container container)
{
container.Register<IRepository, Repository>(Lifestyle.Singleton);
container.Verify();
}
}
I created the Simpleinjectorinitializer class to start simpleinject settings:
public class SimpleInjectorInitializer
{
public static void Initialize()
{
var container = new Container();
InitializeContainer(container);
container.Verify();
}
private static void InitializeContainer(Container container)
{
BootStrapper.RegisterServices(container);
}
}
At class startup I call :
SimpleInjectorInitializer.Initialize();
My variable is stated as
private readonly IRepository _Repository;
When I will execute the command :
Console.WriteLine("Teste" + _repository.SelecionarRegistroPorCommando("123"));
Compiler informs that it has no object instance.
class Program
{
static void Main(string[] args)
{
var test = new TesteIoC();
}
}
public class TesteIoC
{
private readonly IRepository _Repository;
public TesteIoC()
{
SimpleInjectorInitializer.Initialize();
Console.WriteLine("Teste" + _repository.SelecionarRegistroPorCommando("123"));
}
}
You’re testing it where, in a
Console Application
?– novic
Yes I am using a console application to test. This code I posted is from Class Library and in the application console I am instilling the class library.
– b3r3ch1t
Enter the complete code of your
Console Application
.– novic
Virgilio Novic I edited the question and put the Console Application class
– b3r3ch1t