tags:

views:

2174

answers:

5

I have a class like this

public SomeClass
{
   private List<string> _strings = new List<string>();

   public IEnumerable<string> Strings
   {
      {  get return _strings; }
   }
}

How would I do the mapping for _strings?

I tried this, but it complains about the List typehandler not being found, which it doesn't complain about if I mapped it as an object.

<result property="_strings" column="value" />

So I searched Google and found this workaround (originally for a Java issue, no idea if it's suppose to work in C#)

<result property="_strings" resultMapping="someMapping.StringList"/>

<resultMap id="StringList" class="System.String">
  <result property="" column="Value"/>
</resultMap>

This at least lets the test run, and it returns the rest of my object fine, and my list has the right number of entries, except they're all blank.

I think the problem is that the property attribute is blank, but I'm not sure whats suppose to go there. (I also tried using 'value', but that didn't work either). This seems like it should be a lot simpler and I'm just overlooking something obvious.

Thanks.

A: 

Since no solution was found I just went with a method I'm not particularly proud of.

I mapped a class that had no other property other than a string value.

public class StringValue
{
    public String Name { get; set; }
}

<resultMap id="StringList" class="StringValue" >
  <result property="Name" column="Value"/>
</resultMap>

iBatis seems to have no problem with that, just with mapping to a collection of strings.

Brandon
A: 

At least in iBATIS3 for Java your above could just use a resultMap like:

<resultMap id="someClassMap" type="SomeClass"> 
    <collection property="Strings" ofType="String"/> 
</resultMap>
A: 

My experience is the with Java version of iBATIS, but the solution should still work for the C# peeps out there.

Given a class

class MyClass {
  String name;
  List<String> someStrings;
}

you can populate the list of strings or other simple types (classes without attributes, such as Integer, String, etc.) with the following two resultMaps

<resultMap id="MyStringListMap" class="string">
  <result property="someStrings" columnName="MyStringColumn" 
          javaType="string" jdbcType="VARCHAR"/>
</resultMap>

<resultMap id="MyPrimaryResultMap" class="MyClass">
  <result property="name" columnName="name" 
          javaType="string" jdbcType="VARCHAR"/>
  <result resultMap="MyStringListMap" javaType="java.util.List" />
</resultMap>

<select id="MySuperQuery" resultMap="MyPrimaryResultMap" parameterClass="int">
  select firstname as name, coolattributes as MyStringColumn
  from someSwellTable
  where id=#value#
</select>

Hope this helps.

G.

Gabriel
This throws an exception when iBatis tries loading the <sqlMap> with the message `There is no Set member named '' in class 'MyClass'`. I guess in the .NET version every <result> needs the property attribute.
Tinister
Hm... and adding property="someStrings" to your <result> didn't work, I assume? :(
Gabriel
A: 

you can just use 'System.String' or 'java.lang.String' as the property

rahul parashar
A: 
nabeelalimemon