In these key/value pair instances when you have an "attribute" table in your
database, I prefer to create an object with an additional "active" boolean
field and set/clear this in the business logic before going to persistence.
The field does not actually exist in the database table.  On Select from the
DB, I set this flag to true for all current records in the table.  In the
business logic, I set the flag to true for all inserts and set it to false
for all (pending) deletes.  For updates, you leave it alone.  

I also implement a stored procedure that checks for existence of the pair
and adds it if it does not exist and active = 1, updates it if it exists and
active = 1 or deletes it if it exists and active = 0.  Then, you call the SP
in your mapper update tag and pass it the list of these objects.  A tiny bit
of business logic in this type of SP simplifies a lot of things if you're
willing to go that route.

Norm

-----Original Message-----
From: smartkid [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, January 16, 2008 8:22 AM
To: [email protected]
Subject: 答复: Selecting a key value pair 

Quote from MSDN:
============================================================================
==============
public class Dictionary<TKey,TValue> : IDictionary<TKey,TValue>,
ICollection<KeyValuePair<TKey,TValue>>, 
        IEnumerable<KeyValuePair<TKey,TValue>>, IDictionary, ICollection,
IEnumerable, 
        ISerializable, IDeserializationCallback
============================================================================
==============
The Dictionary<> class does not implement the IList interface.

May be you can convert your Dictionary<String,String> to an
List<KeyValuePair<String,String>> before calling " UpdateTestLCID":

List<KeyValuePair<String,String>>  list = new
List(testObject.Names); //each item in the list is an instance of
KeyValuePair<String,String>
SqlMapper.Update("UpdateTestLCID", list);

But I DON’T think this will solve your business requirement.

Quote from your previous mail:
============================================================================
==============
<select id="SelectDescLCID" parameterClass="string" resultmap="?????">
   SELECT LCID as Key, Descr as Value
   FROM ITEMDESC
   WHERE itemId=#value#
</select>
============================================================================
==============
I assume that each LC pair is stored as a database record.
So the batch of update statements generated by IBatis does not handle those
new or deleted LC pairs.

As the record size in the "ITEMDESC" table is so small, may be you can
delete all existing records for an "ItemId" and let the IBatis generate a
batch of INSERT statements instead.

-----邮件原件-----
发件人: Andrea Tassinari [mailto:[EMAIL PROTECTED] 
发送时间: 2008年1月16日 23:16
收件人: [email protected]
主题: Re: Selecting a key value pair 

At 09.34 11/01/2008, Andrea Tassinari wrote:

>At 17.04 10/01/2008, smartkid wrote:
>
>>Use "System.Collections.IDictionary" as the resultClass for
"SelectDescLCID"
>
>Nahhh, is that so simple and straightforward? I cannot believe it! I should
have tried this before. Let me give it a chance :-)
>
>thanks.

Ok, it is *not* so striaghtforward. Current SVN version of the datamapper
exposes a new set of QueryforDictionary<T,V> methods that help a lot getting
a Dictionary<string,string> from a mapped statement.

So, in order to get an object with a Dictionary<string,string> property the
best solution I found is to run 2 query statements. RIP for the lazy
loading... it is acceptable.

Now I'm facing the reverse problem: unroll the dictionary into an update
statement. It would be better to unroll the dictionary within a mapped sql
statement in the SqlMap file, somethig like this (Names is my
Dictionary<string,string> property for the class Test)

<update id="UpdateTestLCID" parameterClass="Test">
  <iterate conjunction=";" open="" close="" property="Names">
      UPDATE testLCID
        SET NOME='$Names[].Key$'
      WHERE refid=1
      AND LCID='$Names[].Value$'
  </iterate>
</update>

this does not work, the mapper complains about Names is not a list or an
array (which, I understand, it is true);

any idea?

Thanks
Andrea

public sealed class Test
{
        private int? _id;
        private IDictionary<string,string> _names;
        
        public Test()
        {
                _id     = null;
                _names = new Dictionary<string,string>();
        
        }
        
        public int? Id
        {
                get { return _id; }
                set { _id = value; }
        }
        public IDictionary<string,string> Names
        {
                get { return _nomi; }
                set     { _nomi = value; }
        }
}


>Andrea 
>
>
>
>-- 
>No virus found in this incoming message.
>Checked by AVG Free Edition. 
>Version: 7.5.516 / Virus Database: 269.19.0/1218 - Release Date: 1/10/2008
1:32 PM
>
>
>
>
>-- 
>No virus found in this incoming message.
>Checked by AVG Free Edition. 
>Version: 7.5.516 / Virus Database: 269.19.0/1218 - Release Date: 10/01/2008
13.32


__________ NOD32 2795 (20080116) Information __________

This message was checked by NOD32 antivirus system.
http://www.eset.com


Reply via email to