Spring for .NET Community Forums    

Go Back   Spring for .NET Community Forums > General > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old 10-23-2006, 10:03 AM
Erich Eichinger Erich Eichinger is offline
Senior Member
Spring User
 
Join Date: Jan 2006
Location: Oslo, Norway
Posts: 1,337
Default How to initialize Property with empty string?

Hi all,

I can't initialize a property with an empty string:

Code:
<property name="mystringprop" value="" />
because according to the xml-schema value is of type "nonNullString" which need a minimum length of 1.

Is this a bug or are there some reasons for this?

cheers,
Erich
Reply With Quote
  #2  
Old 10-23-2006, 11:44 AM
Aleks Seovic Aleks Seovic is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: Belgrade, Serbia
Posts: 613
Default

I don't think there is a good reason, even if there is one. What you are trying to do should definitely be allowed, although I would probably prefer to set it to String.Empty explicitly...

- Aleks

Last edited by Aleks Seovic; 10-23-2006 at 11:48 AM.
Reply With Quote
  #3  
Old 10-23-2006, 12:20 PM
Erich Eichinger Erich Eichinger is offline
Senior Member
Spring User
 
Join Date: Jan 2006
Location: Oslo, Norway
Posts: 1,337
Default

Quote:
Originally Posted by Aleks Seovic View Post
I would probably prefer to set it to String.Empty explicitly
How would you do that?

Writing
Code:
<property name="mystringprop" value="string.Empty" />
results in mystringprop="string.Empty".

Code:
<property name="TestData"><object type="System.String" /></property>
gives an error since System.String doesn't have a public no-argument ctor. Passing an argument results in the same problem of how to pass string.Empty.

And coming up with a PropertyRetrievingFactoryObject is imho a slight overkill for injecting an empty string...


Edit:

I just found an easy solution:
Code:
<property name="TestData"><value /></property>
works.



Btw:
I was looking at Spring.Expressions to find a way, but had no luck (they are not used in this case). But I had an idea: What about adding some "ExpressionFactoryObject" to Spring.Core? Since expressions are a real mighty tool, one could easily inject many things with less xml-verbosity.

e.g. above problem could be solved with

Code:
<property name="TestData"><value type="ExpressionEvaluatingFactoryObject">String.Empty</value></property>
Of course any expression may be used here. It's result just needs to be convertible to string (in this case)

cheers,
Erich

Last edited by Erich Eichinger; 10-23-2006 at 12:47 PM. Reason: found solution
Reply With Quote
  #4  
Old 10-24-2006, 03:06 AM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,332
Default

Quote:
Originally Posted by Erich Eichinger View Post
Btw:
I was looking at Spring.Expressions to find a way, but had no luck (they are not used in this case). But I had an idea: What about adding some "ExpressionFactoryObject" to Spring.Core? Since expressions are a real mighty tool, one could easily inject many things with less xml-verbosity.

e.g. above problem could be solved with

Code:
<property name="TestData"><value type="ExpressionEvaluatingFactoryObject">String.Empty</value></property>
Of course any expression may be used here. It's result just needs to be convertible to string (in this case)

cheers,
Erich
Hi,

or something like this :
Code:
<property name="TestData"><eval>String.Empty</eval></property>
That's something I'm doing for a basic WPF integration :
Code:
<ItemsControl ItemsSource="{s:Eval '@TheMovieLister.GetAllMovies()'}" />
- Bruno
__________________
My english is as poor as my taylor is rich
Reply With Quote
  #5  
Old 10-24-2006, 07:51 AM
Erich Eichinger Erich Eichinger is offline
Senior Member
Spring User
 
Join Date: Jan 2006
Location: Oslo, Norway
Posts: 1,337
Default

Hi,

I made JIRA entries for both the bug above (#SPRNET-379) and the "DI by expressions"-feature (#SPRNET-378)

cheers,
Erich
Reply With Quote
  #6  
Old 10-30-2006, 06:30 PM
Aleks Seovic Aleks Seovic is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: Belgrade, Serbia
Posts: 613
Default

Yeah, I agree that we should be able to use expressions to set property values.

Probably the best approach would be to add expression attribute and element to the configuration schema, which could be used wherever value attribute/element is used.

That way you would be able to set your empty string like this:

<property name="mystringprop" expression="String.Empty" />

or this

<property name="mystringprop" expression="''" /> (single quotes are used to specify an empty string expression)

- Aleks

Last edited by Aleks Seovic; 10-30-2006 at 06:33 PM.
Reply With Quote
  #7  
Old 11-05-2006, 11:16 PM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,332
Default

Hi,

Added an issue in JIRA about the expression attribute/element and an IFactoryObject Implementation that returns an Expression evaluation :
Code:
<object id="logger" type="ExpressionEvaluatorFactoryObject">
   <property name="expression" value="IMpsLogMgr.getLogger("com.foo.bar")"/>
</object>
[Edit]
Oups, just remember that Erich already added one issue about this...
[/Edit]

Bruno
__________________
My english is as poor as my taylor is rich

Last edited by Bruno Baia; 11-05-2006 at 11:25 PM.
Reply With Quote
  #8  
Old 11-08-2006, 05:30 PM
Aleks Seovic Aleks Seovic is offline
Spring.NET Co-Lead
Spring TeamSpring User
 
Join Date: Sep 2004
Location: Belgrade, Serbia
Posts: 613
Default

I see that you two started to create duplicate issues for this and decided that it should be fixed before there are 10 more related issues in JIRA

This feature has been implemented and tested and it's in the CVS. It should be in the nightly build as soon as CI server issues are resolved.

- Aleks
Reply With Quote
  #9  
Old 11-09-2006, 08:49 PM
Bruno Baia Bruno Baia is offline
Senior Member
Spring TeamSpring User
 
Join Date: Oct 2005
Location: Toulouse, France
Posts: 1,332
Default

Quote:
Originally Posted by Aleks Seovic View Post
I see that you two started to create duplicate issues for this and decided that it should be fixed before there are 10 more related issues in JIRA
Don't think you are safe by fixing it, I can still create another issue for the documentation

Bruno
__________________
My english is as poor as my taylor is rich
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:10 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.