JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » .Net Framework » .NET Open Source: Nhibernate, Castle  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Spring.NET中IOC的簡單應用
hugo_Ephebe





發文: 6
積分: 0
於 2008-05-16 13:27 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請去http://www.springframework.net/下載最新版Spring.NET-1.1.2.exe。然後初步研究參考要加入這三個,Common.Logging、Common.Logging.Log4Net、Spring.Core
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
   public enum Finger 
   {
       scissors,
       paper,
       rock
   }
 
   //猜拳遊戲的介面
   public interface IFingerGuessing
   {
       Finger NextFinger();
   }
 
   //猜拳遊戲的策略
   public interface IGuessingStrategy
   {
       Finger NextFinger();
   }
 
   //猜拳遊戲的實作
   public class FingerGuessing : IFingerGuessing
   {
       private IGuessingStrategy _oneGuessing;
       public FingerGuessing(IGuessingStrategy oneGuessing) 
       {
           _oneGuessing = oneGuessing;
       } 
 
       public Finger NextFinger() 
       {
           return _oneGuessing.NextFinger(); 
       }
    }
 
    //策略的實作
    public class FirstStrategy : IGuessingStrategy 
    {
        private Finger _myFinger;
        public FirstStrategy(string myFinger) 
        {
            _myFinger = (Finger)Enum.Parse(typeof(Finger),myFinger);
        }
 
        public Finger NextFinger() 
        {
            return _myFinger;
        }
    }

這是用同一個範例,用Spring.NET的寫法像這樣
1
2
3
4
5
6
7
8
9
10
11
    static void Main(string[] args)
    {
        //建立容器
        IApplicationContext ctx = ContextRegistry.GetContext();
        IFingerGuessing startGame = (IFingerGuessing)ctx.GetObject("GuessingGame");
 
        //使用组件
        Console.Write(startGame.NextFinger().ToString()); 
 
        Console.ReadLine();
    }

之所以語法那麼乾淨,是因為所有的細節都寫在設定檔了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
    <!-- 因為有使用Log4Net吧,所以多出這兩個-->
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="INLINE" />
      </factoryAdapter>
    </logging>
  </common>
  <spring>
    <context>
      <!-- 表示設定寫在同一設定檔-->
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <object name="MyStrategy" type="ConsoleTest.FirstStrategy,ConsoleTest">
        <constructor-arg name="myFinger" value="rock"/>
      </object>
      <object name="GuessingGame" type="ConsoleTest.FingerGuessing, ConsoleTest">
        <constructor-arg index ="0" ref="MyStrategy"/>
      </object>
    </objects>
  </spring>
  <log4net>
    <!-- 這裏是Log4Net的設定-->
    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="ConsoleAppender" />
    </root>
    <logger name="Spring">
      <level value="INFO" />
    </logger>
  </log4net>
</configuration>


hugo_Ephebe edited on 2008-05-17 22:38
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
3129 Spring.NET中IOC的簡單應用 hugo_Ephebe 3413 2008-05-16 13:27
» JWorld@TW »  .Net Framework » .NET Open Source: Nhibernate, Castle

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8