请考虑以下代码
private static void InitializeStructureMap()
{
ObjectFactory.Initialize(x =>
{
x.For<IUnitOfWork>().Use<MyContext>();
x.For<IUserService>().Use<UserService>();
x.For<IProductService>().Use<ProductService>();
});
x.Policies.SetAllProperties(
y =>
{
y.OfType<IUnitOfWork>();
y.OfType<IUserService>();
y.OfType<IProductService>();
});
}
namespace TestApp
{
public partial class Frm1 : BaseForm
{
public IUnitOfWork Uow { get; set; }
public IUserService UserService { get; set; }
public IProductService ProductService { get; set; }
public Frm1()
{
InitializeComponent();
}
}
}
当我调试上面的代码时,我意识到 (Uow , UserService.Uow , ProductService.Uow ) 对象实例是同一个实例。但是 PerRequest 是 StructureMap 默认的生命周期,那么为什么它们是同一个实例呢?