本文介绍了注入控制台应用程序与简易注射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单的喷油器试验的目的,但对OOP prety新。我想创建松耦合夫妇类。这里是我的方案。

I am using Simple Injector for test purpose but prety new on OOP. I am trying to create loosly 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.SureName);
    }

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

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



我的 TestInjectedClass 类以及接口这样的事情,我很planng在PROGRAME主要使用

My TestInjectedClass Class and interface something like this which I am planng to use in Programe 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",SureName = "stack"};
        _userRepository.Add(user);
    }
}
public interface ITestInjectedClass
{
    void UserRepoRun();
}

和我的控制台程序是这样的:

And My console programme 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();
    }



}

}

引导类在这里:

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();

    }
}



我的问题,当我运行程序,我得到的 _userRepository 值异常中 TestInjectionClass
如何能够正确地注射 TestInjectionClass UserRepository 主PROGRAME。谢谢

My problem when I run programme,I am getting value exception on the _userRepository inside TestInjectionClass.How can properly injected TestInjectionClass and UserRepository to Main Programe. Thanks

推荐答案

您需要让 Bootstrap.container 提供 Program.Main ,然后用它来创建类的实例,而不是直接直接调用它们的构造函数:

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>();



当然,你需要将其暴露在引导对于工作:

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();
    }
}

和调用 Bootstrap.Start Program.Main

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

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

    Console.ReadLine();
}

这篇关于注入控制台应用程序与简易注射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:53