WinForms和StructureMap默认生命周期

时间:2014-09-23 14:17:53

标签: c# winforms dependency-injection structuremap3

请考虑以下代码

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的默认生命周期,那么为什么它们是同一个实例?

0 个答案:

没有答案