`
feiliboos
  • 浏览: 665286 次
文章分类
社区版块
存档分类
最新评论

Spring.NET学习笔记8——集合类型的注入(基础篇)

 
阅读更多

Spring.NET还支持集合类型的注入。而且使用起来也比较方便。

  一、ILIst类型

  使用<list>元素作为ILIst的标签,value为集合中元素的值。也可以注入对象,甚至关联其它对象,使用 <ref/>元素表示关联的对象,object 属性为所关联对象的id或name。集合可以为空,用<null/>元素来标记。

  在<list>元素中设置 element-type 属性表示泛型T的类型,例如 element-type="int" ,代表int型。

  二、IDictionary类型

  使用<dictionary>元素来表示IDictionary接口的实现类型。<entry/>表示IDictionary集合的元素。key和value属性为元素的键值队,value-ref为关联的元素。

  同理,<dictionary>元素的key-type和value-type属性来表示泛型IDictionary,例如 <dictionary key-type="string" value-type="object"> 。

  完整代码如下:
Domain

  1. public class Happy
  2. {
  3. public override string ToString()
  4. {
  5. return "每天都开心,每天都有好心情";
  6. }
  7. }
  8. public class OneYear
  9. {
  10. public override string ToString()
  11. {
  12. return "快乐的一年";
  13. }
  14. }
  15. public class Person
  16. {
  17. public IList<Person> BestFriends { get; set; }
  18. public IList HappyYears { get; set; }
  19. public IList<int> Years { get; set; }
  20. public IDictionary HappyDic { get; set; }
  21. public IDictionary<string,object> HappyTimes { get; set; }
  22. }
复制代码

App.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <configSections>
  4. <sectionGroup name="spring">
  5. <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
  6. <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
  7. </sectionGroup>
  8. </configSections>
  9. <spring>
  10. <context>
  11. <resource uri="config://spring/objects" />
  12. </context>
  13. <objects xmlns="http://www.springframework.net">
  14. <object id="person" type="SpringNetDi.Person, SpringNetDi">
  15. <!--空集合属性-->
  16. <property name="BestFriends">
  17. <null/>
  18. </property>
  19. <!--System.Collections.IList注入 -->
  20. <property name="HappyYears">
  21. <list>
  22. <value>1992</value>
  23. <value>1998 年</value>
  24. <ref object="oneYear"/>
  25. </list>
  26. </property>
  27. <!--System.Collections.IList<int>注入 -->
  28. <property name="Years">
  29. <list element-type="int">
  30. <value>1992</value>
  31. <value>1998</value>
  32. <value>2000</value>
  33. </list>
  34. </property>
  35. <!--System.Collections.IDictionary注入-->
  36. <property name="HappyDic">
  37. <dictionary key-type="string" value-type="object">
  38. <entry key="第一开心" value="每天都能睡一个好觉"/>
  39. <entry key="第二开心" value-ref="happy"/>
  40. </dictionary>
  41. </property>
  42. <!--System.Collections.IDictionary<object,object>注入-->
  43. <property name="HappyTimes">
  44. <dictionary key-type="string" value-type="object">
  45. <entry key="第一开心" value="每天都能睡一个好觉"/>
  46. <entry key="第二开心" value-ref="happy"/>
  47. </dictionary>
  48. </property>
  49. </object>
  50. <object id="oneYear" type="SpringNetDi.OneYear,SpringNetDi"/>
  51. <object id="happy" type="SpringNetDi.Happy,SpringNetDi"/>
  52. </objects>
  53. </spring>
  54. </configuration>
复制代码

Program

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. IApplicationContext ctx = ContextRegistry.GetContext();
  6. Person person = ctx.GetObject("person") as Person;
  7. Console.WriteLine("空值");
  8. string bestFriend = person.BestFriends == null ? "我的朋友太多了" : "我只有一个好朋友";
  9. Console.WriteLine(bestFriend);
  10. Console.WriteLine();
  11. Console.WriteLine("IList");
  12. foreach (var item in person.HappyYears)
  13. {
  14. Console.WriteLine(item);
  15. }
  16. Console.WriteLine();
  17. Console.WriteLine("泛型Ilist<int>");
  18. foreach (int item in person.Years)
  19. {
  20. Console.WriteLine(item);
  21. }
  22. Console.WriteLine();
  23. Console.WriteLine("IDictionary");
  24. foreach (DictionaryEntry item in person.HappyDic)
  25. {
  26. Console.WriteLine(item.Key + " 是 " + item.Value);
  27. }
  28. Console.WriteLine();
  29. Console.WriteLine("泛型IDictionary<string,object>");
  30. foreach (KeyValuePair<string,object> item in person.HappyTimes)
  31. {
  32. Console.WriteLine(item.Key + " 是 " + item.Value);
  33. }
  34. Console.ReadLine();
  35. }
  36. }
复制代码

输入结果如下:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics