我使用Simple Injector进行测试,但在OOP上使用了新的。我正在努力创造松散的夫妻班。这是我的情景。
我有这样的用户回购和界面。
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
类和接口类似于我计划在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();
}
我的控制台程序如下所示:
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 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
内的值{0}}。
如何正确地将TestInjectionClass
和UserRepository
注入主程序。感谢
答案 0 :(得分:11)
您需要在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();
}
}
从Bootstrap.Start
致电Program.Main
:
static void Main(string[] args)
{
Bootstrap.Start();
_testInjectedClass = Bootstrap.container.GetInstance<ITestInjectedClass>();
_testInjectedClass.UserRepoRun();
Console.ReadLine();
}
答案 1 :(得分:6)
问题是因为您在Bootstrap
类实例构造函数中调用了Program
代码。
因此,实际上当您启动程序执行环境时,正在调用入口点方法Main
。而且你的实例构造函数永远不会被执行。
尝试更改入口点方法Main
和'Bootstrap'类代码:
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();
}
}
答案 2 :(得分:-2)
请使用SimpleInjector
样品请参考
http://www.c-sharpcorner.com/UploadFile/4d9083/dependency-injection-using-simple-injector/