PDA

View Full Version : Dependency Injection via property


outia24
10-05-2007, 05:48 AM
I am getting some test with winform.
But, when I clicked button1, "Test" property has nothing.
Do I need another code or something?

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<spring>
<context>
<resource uri="config://spring/objects"/>
</context>

<objects xmlns="http://www.springframework.net">
<object id="form1" type="MySpringSample_03.Form1, MySpringSample_03">
<property name="Test" value="hello world"/>
</object>
</objects>
</spring>


</configuration>



Form1.cs

public partial class Form1 : Form, IForm
{
private string test;

public string Test
{
get { return test; }
set { test = value; }
}

public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Test=" + Test);
}
}



Thanks in advance.

Erich Eichinger
10-06-2007, 08:31 AM
Hi,

to get support for WinForms DI, please have a look at Harald Radi's Spring.NET RichClient (http://richclient.fluffnstuff.org/) project. Unfortunately I'm unsure about the current state of this project. It seems that Harald hadn't worked on it for quite a while now.

cheers,
Erich

rolandz
10-10-2007, 09:47 AM
to get support for WinForms DI, please have a look at Harald Radi's Spring.NET RichClient (http://richclient.fluffnstuff.org/) project. Unfortunately I'm unsure about the current state of this project. It seems that Harald hadn't worked on it for quite a while now.

My way of DI for Windows.Forms is as following:


static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);

// Creating a Spring.Net application context...
IApplicationContext ctx = ContextRegistry.GetContext();

// Getting the main window object from context...
Form mainForm = (Form)ctx["mainForm"];

// Running the Windows.Forms application...
Application.Run(mainForm);
}
catch(Exception x)
{

}
}
}
This code requires "mainForm" to be defined:


<object id="mainForm" type="YourTypeHere"></object>

This way I get a FULL set of spring feature set working... Maybe this helps you.

outia24
10-12-2007, 03:43 AM
Thanks Roland

You got exactly What I want.

Cheers