简体   繁体   English

简单的注入器Web表单

[英]Simple Injector Web Forms

Okay so I have a UI layer and a BLL layer. 好的,我有一个UI层和一个BLL层。

namespace BLL 
{
   public class User : IUser
   {
        public void DoSomething(){}
   }
   public interface IUser
   {
        void DoSomething();
   }
   public static class TypeRegistry
   {
        public static void RegisterTypes(Container container)
        {
            container.Register<IUser, User>(Lifestyle.Singleton);
        }
    }
}

namespace UI
{
     public partial class Login : System.Web.UI.Page
     {
          private IUser user;
     }
}

I can't figure how to get user to not be null. 我不知道如何使用户不为空。 I have tried making a constructor but that caused an error 我曾尝试制作一个构造函数,但这导致了一个错误

    public Login(IUser user){ this.user = user;}

Compilation Error : Login does not contain a constructor that takes 0 arguments 编译错误:登录名不包含带有0个参数的构造函数

If you've done all the registration appropriately as demonstrated in the Web Forms integration guide of the documentation , then I think the problem is due to you setting the IUser accessibility level in your page to private . 如果您按照文档Web窗体集成指南中的说明正确地完成了所有注册,那么我认为问题是由于您将页面中的IUser可访问性级别设置为private所致。

So private IUser user; 因此, private IUser user; should become public IUser user; 应该成为public IUser user; . Since Web Forms requires you to do property injection over constructor injection, the property user should be publicly accessible to be injected. 由于Web窗体要求您进行属性注入而不是构造函数注入,因此应该公开访问属性用户以进行注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM