View Full Version : How to save object and get his values?
I have this code:
public class WebForm1 : Spring.Web.UI.Page
{
[Binding ("Text", "u.Name")]
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label lbFactoryUserName;
protected System.Web.UI.WebControls.Button bSubmit;
public User u;
public IApplicationContext ctx;
private void Page_Load(object sender, System.EventArgs e)
{
ctx = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
u = (User) ctx["personalUser"];
lbFactoryUserName.Text = u.Name;
}
private void bSubmit_Click(object sender, System.EventArgs e)
{
string newName = TextBox1.Text;
if (newName != u.Name)
{
u.Name = newName;
ctx.ConfigureObject(u, "personalUser");
}
}
}
after I press button I have the same value of User.Name on my page, and it refresh after I press the button for the second time. Is that right?
Rick Evans
07-20-2005, 01:40 PM
Hi Dmitrij
The IObjectFactory.ConfigureObject(object, string) (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectFactory.ConfigureObj ect_overload_1.html) method does not update your Spring.NET configuration.
What your code is doing is retrieving an object from the application context in this line...
u = (User) ctx["personalUser"];
You are then typing a new name into the textbox on your web page, which is indeed being bound by Spring.NET's bidirectional databinding feature. At the time the button is clicked, the User object now contains a different name (the value of which is whetever you typed into the textbox). Great, this is what you expected, and its what we expected too.
You are then attempting to update your configuration with the new value... this is not supported. When you pass your User object instance into the IObjectFactory.ConfigureObject(object, string) (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectFactory.ConfigureObj ect_overload_1.html) method, it is simply going to have all of its properties reset to the values that are present in the configuration. This is the expected behaviour, even if it is not what you want.
The IObjectFactory.ConfigureObject(object, string) (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectFactory.ConfigureObj ect_overload_1.html) method exists to serve the use case where one has an object that simply cannot be instantiated by the Spring.NET IoC container, and you still want to apply Dependency Injection to such an object. The canonical example is an ASP.NET ASPX page... it cannot be instantiated via the 'new' operator.
So in your case, try putting this code in the body of your Page_Load event handler, just to see the expected behaviour of the IObjectFactory.ConfigureObject(object, string) (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectFactory.ConfigureObj ect_overload_1.html) method...
private void Page_Load(object sender, System.EventArgs e)
{
ctx = ConfigurationSettings.GetConfig("spring/context") as IApplicationContext;
u = new User(); // no name value or anything set
u = ctx.ConfigureObject(u, "personalUser"); // properties get injected here...
lbFactoryUserName.Text = u.Name;
}
Spring.NET does not currently support the dynamic reloading or updating of an application context configuration across the lifetime of an application context. Support for this will be needed if such a need ever arises... it hasn't as of yet. I will agree that the API documentation for what the IObjectFactory.ConfigureObject(object, string) (http://www.springframework.net/doc/api/html/Spring.Objects.Factory.IObjectFactory.ConfigureObj ect_overload_1.html) method is perhaps a tad confusing... I will remedy this for the upcoming release (mmm, the Spring.NET JIRA would appear to be down, but I will raise a JIRA issue so that I don't forget).
More importantly, why do you want to update the configuration anyway?
Ciao
Rick
Thanks Erick!
You are right, I do not need to update the configuration. I have choose not right way to solve the problem.
Aleks Seovic
08-13-2005, 02:25 PM
As a sidenote, there is no reason to retrieve application context manually in this case:
1. Because your page extends Spring.Web.UI.Page, which is IApplicationContextAware, you can simply use ApplicationContext property that is exposed by the Spring.Web.UI.Page class.
2. It is probably a bad idea to do even that in this particular case -- you should simply inject your user object into the page using Spring configuration file.
Regards,
Aleks
vBulletin® v3.7.3, Copyright ©2000-2009, Jelsoft Enterprises Ltd.