当我在下面的代码中解析Reminder
时,contact
和profile
的属性未初始化。我错过了什么?注册的对象属性实例在ioc容器解析后重置
public class Reminder : IReminder
{
IProfile profile;
IContact contact;
public Reminder(IProfile profile, IContact contact)
{
this.profile = profile;
this.contact = contact;
}
}
IProfile profile;
IContact contact;
IReminder reminder;
[TestInitialize]
public void TestInit()
{
var container = new UnityContainer();
profile = new Profile() { ID = 1, Active = true };
container.RegisterInstance(profile);
contact = new Contact() { ID = 1, Active = true };
container.RegisterInstance(contact);
container.RegisterType<IContact, Contact>(new ContainerControlledLifetimeManager());
container.RegisterType<IProfile, Profile>(new ContainerControlledLifetimeManager());
reminder = container.Resolve<Reminder>();
}
当我审视reminder
,我希望接触ID
和Active
应分别1和真实的。但我看到它是0和错误的。与profile
相同。为什么?
如果这些RegisterInstance调用看起来更像这样:'container.RegisterInstance(profile);'?在这种情况下,你可以跳过'RegisterType'(或者做RegisterType)并跳过'RegisterInstance'。 –
DWright