Injection into Console Application with the Simple Injector

stackada picture stackada · Aug 9, 2015 · Viewed 18.6k times · Source

I am using Simple Injector for test purpose but pretty new on OOP. I am trying to create loosely couple classes. Here is the my scenario.

I have User repo and interface like this.

public class UserRepository : IUserRepository
{
    public void Add(Model.User user)
    {
        Console.WriteLine("Name:"+user.Name+"\n"+"SurName:"+user.SurName);
    }

    public void Delete(int id)
    {
        throw new NotImplementedException();
    }
}

public interface IUserRepository
{
    void Add(User user);
    void Delete(int id);
}

My TestInjectedClass Class and interface are something like this which I am planning to use in Program Main.

public class TestInjectedClass : ITestInjectedClass
{
    private readonly IUserRepository _userRepository;
    public TestInjectedClass(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public void UserRepoRun()
    {
        var user = new User() {Id = 1,Name = "ada",SurName = "stack"};
        _userRepository.Add(user);
    }
}
public interface ITestInjectedClass
{
    void UserRepoRun();
}

And My console program looks like this:

class Program 
{
    static ITestInjectedClass _testInjectedClass;
    private static IUserRepository _userRepository;
    static void Main(string[] args)
    { 
        _testInjectedClass= new TestInjectedClass(_userRepository);
        _testInjectedClass.UserRepoRun();

        Console.ReadLine();
    }
    public Program()
    {                 
        Bootstrap.Start();
    }

}

BootStrap class here:

class Bootstrap
{
    public static void Start()
    {
        var container = new Container();

        // Register your types, for instance:
        container.Register<IUserRepository, UserRepository>(Lifestyle.Singleton);
        container.Register<ITestInjectedClass, TestInjectedClass>(Lifestyle.Singleton);
        //container.Register<IUserRepository, TestInjectedClass>(Lifestyle.Singleton);
        //container.Register<IUserContext, WinFormsUserContext>();
        container.Register<TestInjectedClass>();

        // Optionally verify the container.
        container.Verify();
    }
}

My problem when I run program, I am getting a value exception on the _userRepository inside TestInjectionClass. How can I properly inject TestInjectionClass and UserRepository to Main Program. Thanks

Answer

Damir Arh picture Damir Arh · Aug 9, 2015

You need to make Bootstrap.container available in Program.Main and then use it to create instances of classes instead of directly calling their constructors directly:

_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();

Of course you will need to expose it in Bootstrap for that to work:

class Bootstrap
{
    public static Container container;

    public static void Start()
    {
        container = new Container();

        // Register your types, for instance:
        container.Register<IUserRepository, UserRepository>(Lifestyle.Singleton);
        container.Register<ITestInjectedClass, TestInjectedClass>(Lifestyle.Singleton);
        //container.Register<IUserRepository, TestInjectedClass>(Lifestyle.Singleton);
        //container.Register<IUserContext, WinFormsUserContext>();
        container.Register<TestInjectedClass>();

        // Optionally verify the container.
        container.Verify();
    }
}

And call Bootstrap.Start from Program.Main:

static void Main(string[] args)
{ 
    Bootstrap.Start();

    _testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
    _testInjectedClass.UserRepoRun();

    Console.ReadLine();
}