TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
tharinda dayal
NA
19
667
Ninject for winforms - how to apply ninject DI for MDI child
May 25 2020 12:37 AM
I am developing desktop base application using c# windows form. Recently I added NInject to my project, it is working good but when i call to child forms from MDI form it's asking constructor parameter of child form because i am using form constructor to pass interface parameter. i don't know how to resolve it.
please can any one help me to resolve it. this is my sample project
CompositionRoot
public
class
CompositionRoot
{
private
static
IKernel _ninjectKernel;
public
static
void
Wire(INinjectModule module)
{
_ninjectKernel =
new
StandardKernel(module);
}
public
static
T Resolve<T>()
{
return
_ninjectKernel.Get<T>();
}
}
ApplicationModule
public
class
ApplicationModule : NinjectModule
{
public
override
void
Load()
{
Bind(
typeof
(IRepository<>)).To(
typeof
(GenericRepository<>));
Bind(
typeof
(ITest)).To(
typeof
(TestImpl));
}
}
Program
static
class
Program
{
[STAThread]
static
void
Main()
{
CompositionRoot.Wire(
new
ApplicationModule());
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false
);
//Application.Run(new Form1());
Application.Run(CompositionRoot.Resolve<Form1>());
}
}
MDI Form
public
partial
class
Form1 : Form
{
private
IRepository<Process> _processRepository;
public
Form1(IRepository<Process> productionRepository, ITest test)
{
this
._processRepository = productionRepository;
this
._test = test;
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
Form2 frm =
new
*Form2()*;
frm.Show();
}
}
Child form
public
partial
class
Form2 : Form
{
private
ITest _test;
public
Form2(ITest test)
{
_test = test;
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
var a = _test.DisplayRow();
MessageBox.Show(a);
}
}
ITest
public
interface
ITest
{
string
DisplayRow();
}
TestImpl
public
class
TestImpl : ITest
{
public
string
DisplayRow()
{
return
"Test service"
;
}
}
GenericRepository
public
class
GenericRepository<T> : IRepository<T>
{
public
override
string
ToString()
{
return
"MyRepository with type : "
+
typeof
(T).Name;
}
}
IRepository
public
interface
IRepository<T>
{
}
Reply
Answers (
1
)
calculate sum using another gridview another form in c#
How to set a timer in Windows application when user is idle