J
JimmyJimm
Guest
I use simple injector. I've registered my FormUserNew:
_container.RegisterForm<FormUsersNew>();
which is called from FrmMain (main form), however i am not sure how should i take instance of that in FrmMain 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.RegisterForm<FormUsersNew>(); //<===========
_container.Verify();
}
}
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 dependency injection ? :
public partial class FormMain
{
private void BtnOpen_Click(object sender, EventArgs e)
{
//call FormUserNew <=======================
//var form = new FormUsersNew(EnumSector.Fire);
//form.ShowDialog();
}
}
Continue reading...
_container.RegisterForm<FormUsersNew>();
which is called from FrmMain (main form), however i am not sure how should i take instance of that in FrmMain 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.RegisterForm<FormUsersNew>(); //<===========
_container.Verify();
}
}
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 dependency injection ? :
public partial class FormMain
{
private void BtnOpen_Click(object sender, EventArgs e)
{
//call FormUserNew <=======================
//var form = new FormUsersNew(EnumSector.Fire);
//form.ShowDialog();
}
}
Continue reading...