Skip to content

Windows form how to call forms correctly from container #97

@Kingdom-Of-Heaven

Description

@Kingdom-Of-Heaven

I've registered my FormUserNew:

_container.Register<FormUsersNew>();

which is called from FormMain (starting form), however i am not sure how should i take instance of that in FormMain then call it.

Here's my code below:

static class Program
  {
      static void Main()
      {        
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Bootstrap();

          Application.Run(_container.GetInstance<FormMain>());

      private static void Bootstrap()
      {
          _container = new Container();
          _container.Options.DefaultScopedLifestyle = new ThreadScopedLifestyle();
          _container.Register<IUserService, UserService>(Lifestyle.Scoped);
          _container.Register<IUserTypeService, UserTypeService>(Lifestyle.Scoped);     
          _container.Register<FormMain>();
          _container.Register<FormUsersNew>();   //<=========== form
          _container.Verify();         
      }
  }

FormUsersNew:

 public partial class FormUsersNew
    {
        private readonly Sektor _sektor;
        private readonly IUserService _userService;
        private readonly IUserTypeService _userTypeService;

        public FormUsersNew(IUserService userService, IUserTypeService userTypeService, EnumSector sector)
        {
            InitializeComponent();
            _sektor = sektor;
            _userService = userService;
            _userTypeService = userTypeService;
        }

How then to call FormUsersNew to use my dependencies from container? :

public partial class FormMain
{

   private void BtnOpen_Click(object sender, EventArgs e)
   {
         //call FormUserNew from container <=======================
         //var form = new FormUsersNew();
         //form.ShowDialog();
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions