使用简单注入器注入控制台应用程序
Injection into Console Application with the Simple Injector
我正在使用 Simple Injector 进行测试,但在 OOP 上还很新。我正在尝试创建松耦合 classes。这是我的场景。
我有这样的用户库和界面。
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);
}
我的 TestInjectedClass
Class 和界面是这样的,我打算在 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();
}
我的控制台程序如下所示:
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 这里:
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();
}
}
我在 运行 程序时遇到的问题是 TestInjectionClass
中的 _userRepository
出现值异常。
如何正确地将 TestInjectionClass
和 UserRepository
注入主程序。谢谢
您需要使 Bootstrap.container
在 Program.Main
中可用,然后使用它来创建 类 的实例,而不是直接直接调用它们的构造函数:
_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
当然你需要在 Bootstrap
中公开它才能工作:
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();
}
}
然后从 Program.Main
调用 Bootstrap.Start
:
static void Main(string[] args)
{
Bootstrap.Start();
_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
_testInjectedClass.UserRepoRun();
Console.ReadLine();
}
问题是因为您在 Program
class 实例构造函数中调用 Bootstrap
代码。
因此,实际上当您启动程序的执行环境时,正在调用入口点方法Main
。而且您的实例构造函数永远不会执行。
尝试更改您的入口点方法 Main
和 'Bootstrap' class 代码:
static void Main(string[] args)
{
var container = new Container();
Bootstrap.Start(container);
_testInjectedClass = container.GetInstance<TestInjectedClass>();
_testInjectedClass.UserRepoRun();
Console.ReadLine();
}
class Bootstrap
{
public static void Start(Container container)
{
// Register your types, for instance:
container.Register<IUserRepository, UserRepository>(Lifestyle.Singleton);
container.Register<ITestInjectedClass, TestInjectedClass>(Lifestyle.Singleton);
container.Register<TestInjectedClass>();
// Optionally verify the container.
container.Verify();
}
}
请使用SimpleInjector
样品请参考
http://www.c-sharpcorner.com/UploadFile/4d9083/dependency-injection-using-simple-injector/
我正在使用 Simple Injector 进行测试,但在 OOP 上还很新。我正在尝试创建松耦合 classes。这是我的场景。
我有这样的用户库和界面。
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);
}
我的 TestInjectedClass
Class 和界面是这样的,我打算在 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();
}
我的控制台程序如下所示:
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 这里:
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();
}
}
我在 运行 程序时遇到的问题是 TestInjectionClass
中的 _userRepository
出现值异常。
如何正确地将 TestInjectionClass
和 UserRepository
注入主程序。谢谢
您需要使 Bootstrap.container
在 Program.Main
中可用,然后使用它来创建 类 的实例,而不是直接直接调用它们的构造函数:
_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
当然你需要在 Bootstrap
中公开它才能工作:
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();
}
}
然后从 Program.Main
调用 Bootstrap.Start
:
static void Main(string[] args)
{
Bootstrap.Start();
_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
_testInjectedClass.UserRepoRun();
Console.ReadLine();
}
问题是因为您在 Program
class 实例构造函数中调用 Bootstrap
代码。
因此,实际上当您启动程序的执行环境时,正在调用入口点方法Main
。而且您的实例构造函数永远不会执行。
尝试更改您的入口点方法 Main
和 'Bootstrap' class 代码:
static void Main(string[] args)
{
var container = new Container();
Bootstrap.Start(container);
_testInjectedClass = container.GetInstance<TestInjectedClass>();
_testInjectedClass.UserRepoRun();
Console.ReadLine();
}
class Bootstrap
{
public static void Start(Container container)
{
// Register your types, for instance:
container.Register<IUserRepository, UserRepository>(Lifestyle.Singleton);
container.Register<ITestInjectedClass, TestInjectedClass>(Lifestyle.Singleton);
container.Register<TestInjectedClass>();
// Optionally verify the container.
container.Verify();
}
}
请使用SimpleInjector
样品请参考
http://www.c-sharpcorner.com/UploadFile/4d9083/dependency-injection-using-simple-injector/