ã¯ããã¦ã®S2Dao.NET
S2Dao.NETã¨ã¯
S2Dao.NETã¯ãããã³ã°æ å ±ãXMLã«è¨è¿°ããªãO/Rãããã³ã°ãã¬ã¼ã ã¯ã¼ã¯ã§ãã S2Container.NETä¸ã§åä½ãã¾ãã
ãããã³ã°ã®ããã«XMLãã¬ãªã¬ãªæ¸ãã®ãé¢åãªã®ã§å¬ããã¨ãããã¨ããããã§ãOracleã«æ¥ç¶ãã¦åç´ãªCRUDãè¡ããµã³ãã«ãæ¸ãã¦ã¿ã¾ããã
å
ãã¯å¯¾è±¡ã¨ãªããã¼ãã«ããã
create table Greeting( id integer, message varchar (128) ); -- IDã主ãã¼ã¨ãã alter table Greeting add constraint Greeting_P primary key (id); -- ã·ã¼ã±ã³ã¹ãä½æãã create sequence GreetingID;
主ãã¼ã¨ã¡ãã»ã¼ã¸ã ãã®åç´ãªãã¼ãã«ã§ãããããSQL*Plusãªã©ã§å®è¡ãã¦ãã ããã
次ã«ãã¼ãã«ã®ãããã³ã°å ã¨ãªãã¯ã©ã¹ã§ãã
using System; using Seasar.Dao.Attrs; // Entity [Table("Greeting")] public class Greeting { private int id; private string message; [ID ("sequence", "GreetingID")] public int ID { get { return id; } set { id = value; } } public string Message { get { return message; } set { message = value; } } public override string ToString () { return String.Format ("{0}:{1}", ID, Message); } }
Table/IDã¨ããå±æ§ãã¤ãã¦ãã以å¤ã¯æ®éã®ã¯ã©ã¹ã§ãããTableå±æ§ã¯ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«åãæå®ãã¾ããã¯ã©ã¹åã¨ãã¼ãã«åãåãå ´åã¯ä¸è¦ã ã£ãæ°ããã¾ãããèªä¿¡ãç¡ãã£ãããä»ãã¦ãã¾ãã¾ããã(^^; IDå±æ§ã§ã¯ã·ã¼ã±ã³ã¹ã使ããã¨ã¨ããã®ã¨ãã«ä½¿ç¨ããã·ã¼ã±ã³ã¹ãªãã¸ã§ã¯ãåãæå®ãã¦ãã¾ããä»åã¯S2Dao.NETã«èªåçã«IDãä»ãããã¾ãããæåã®å ´åã¯IDå±æ§ã¯ä¸è¦ã§ãã
ãã¦ãã¯ã©ã¹ã¨ãã¼ãã«ãåºæ¥ã¾ããã®ã§æ¬¡ã¯ä¸¡è ã®ãããã³ã°ã§ãããDAO(Data Access Object)ãä½æãã¾ããä½æã¨è¨ã£ã¦ãå®ã¯ãã¤ã³ã¿ãã§ã¼ã¹ãå®ç¾©ããã ãã§ãå®è£ ã¯S2Dao.NETãåæã«ãã£ã¦ããã¾ããAOPä¸æ³ã
ã§ããã®DAOã®ã¤ã³ã¿ãã§ã¼ã¹ã§ããã
using System; using System.Collections; using Seasar.Dao.Attrs; [Bean (typeof (Greeting))] public interface IGreetingDao { int InsertGreeting (Greeting greeting); int UpdateGreeting (Greeting greeting); int DeleteGreeting (Greeting greeting); IList GetAllList (); [Query ("id=/*id*/")] Greeting GetGreetingByID (int id); }
ãããªæããBeanå±æ§ã§å¯¾è±¡ã¨ãªãã¯ã©ã¹ãæå®ãã¾ããInsertã¯ã©ã¹å/Updateã¯ã©ã¹å/Deleteã¯ã©ã¹åãªã©ã®ã¡ã½ããåã¯ã«ã¼ã«ãªã®ã§åããã¦ãã ãããã¾ããQueryå±æ§ã§æå®ããå 容ã¯whereç¯ã«åãè¾¼ã¾ãã¾ããã³ã¡ã³ãã¨ãªã£ã¦ããå¤æ°ã«ã¡ã½ããã®å¼æ°ãå±éããã¾ãã
ãã¦ãæºåã¯åºæ¥ã¾ããã®ã§DIãã¦ã¿ã¾ãããã
æ§æãã¡ã¤ã«ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="seasar" type="Seasar.Framework.Xml.S2SectionHandler, Seasar" /> </configSections> <appSettings> <!-- oracleex.diconã§ä½¿ç¨ --> <add key="ConnectionString" value="Data Source=mydb;User Id=user;Password=pass;" /> </appSettings> <seasar> <assemblys> <assembly>Seasar.Dao</assembly> <assembly>System.Data.OracleClient, version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</assembly> </assemblys> </seasar> </configuration>
assemblyã¨ãã¦Seasar.Daoã¨System.Data.OracleClientã追å ãã¾ããã¾ããappSettingsã§æ¥ç¶æååãè¨å®ãã¦ã¿ã¾ããã
次ã«ãapp.diconãã¡ã¤ã«ã§ãã
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components> <!-- S2Dao.NETã§Oracleã«æ¥ç¶ --> <include path="OracleEx.dicon" /> <component class="IGreetingDao" name="dao"> <aspect>OracleEx.DaoInterceptor</aspect> </component> </components>
aspectã§æå®ããã¦ããOracleEx.DaoInterceptorããã½ã§ãã³ã¤ããIGreetingDaoã¤ã³ã¿ãã§ã¼ã¹ã®å®è£ ãèªåçæãã¦ããã¾ãã
Oracleã使ãã«ã¯ããå°ãè¨å®ãå¿ è¦ã«ãªãã¾ãããOracleEx.diconã®ä¸ã«ã¾ã¨ãã¦ãã¾ããä»ããã¤ã³ã¯ã«ã¼ãããã ãã«ãªã£ã¦ãã¾ããOracleEx.diconã¯ãã©ãã¯ããã¯ã¹ã«ãã¦ããã¦åé¡ãªãã¨æãã¾ãããä¸å¿ãä¸èº«ãè¦ã¦ããã¾ãããã
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE components PUBLIC "-//SEASAR2.1//DTD S2Container//EN" "http://www.seasar.org/dtd/components21.dtd"> <components namespace="OracleEx"> <!-- ãã¼ã¿ãããã¤ã --> <component name="Oracle" class="Seasar.Extension.ADO.DataProvider"> <property name="ConnectionType">"System.Data.OracleClient.OracleConnection"</property> <property name="CommandType">"System.Data.OracleClient.OracleCommand"</property> <property name="ParameterType">"System.Data.OracleClient.OracleParameter"</property> <property name="DataAdapterType">"System.Data.OracleClient.OracleDataAdapter"</property> </component> <!-- ãã¼ã¿ã½ã¼ã¹ --> <component name="SqlDataSource" class="Seasar.Extension.Tx.Impl.TxDataSource"> <property name="DataProvider">Oracle</property> <!-- æ¥ç¶æååã¯æ§æãã¡ã¤ã«ããåå¾ --> <property name="ConnectionString"> appSettings['ConnectionString']</property> </component> <!-- DaoInterceptor --> <component class="Seasar.Extension.ADO.Impl.BasicDataReaderFactory" /> <component class="Seasar.Extension.ADO.Impl.BasicCommandFactory" /> <component class="Seasar.Dao.Impl.DaoMetaDataFactoryImpl" /> <component name="DaoInterceptor" class="Seasar.Dao.Interceptors.S2DaoInterceptor"/> <!-- ãã¼ã«ã«ãã©ã³ã¶ã¯ã·ã§ã³ç¨ã®ã¤ã³ã¿ã¼ã»ãã¿ã¼ --> <component name="LocalRequiredTx" class="Seasar.Extension.Tx.TransactionInterceptor"> <arg><component class="Seasar.Extension.Tx.Impl.LocalRequiredTxHandler" /></arg> <property name="TransactionStateHandler">TransactionContext</property> </component> <!-- åé¢ã¬ãã«ã®æå® --> <component name="TransactionContext" class="Seasar.Extension.Tx.Impl.TransactionContext"> <property name="IsolationLevel">System.Data.IsolationLevel.ReadCommitted</property> </component> </components>
æ¥ç¶æååãæ§æãã¡ã¤ã«ã«åºããã®ã§ããã¾ãè¦ããã¨ã¯ãªãã§ãããã
æºåãæ´ã£ãã®ã§ããããã使ã£ã¦Oracleã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãããã
using System; using System.Collections; using Seasar.Framework.Container; using Seasar.Framework.Container.Factory; public class App { public static void TestSelect (IGreetingDao dao) { IList list = dao.GetAllList (); if (list.Count > 0) foreach (Greeting g in dao.GetAllList ()) Console.WriteLine (g); else Console.WriteLine ("Greeting is empty."); } public static void TestInsert (IGreetingDao dao) { Console.WriteLine ("Insert"); Greeting entity = new Greeting (); entity.Message = "Hello"; dao.InsertGreeting (entity); entity.Message = "Goodby"; dao.InsertGreeting (entity); } public static void TestUpdate (IGreetingDao dao) { Console.WriteLine ("Update"); foreach (Greeting g in dao.GetAllList ()) { g.Message += ", World"; dao.UpdateGreeting (g); } } public static void TestDelete (IGreetingDao dao) { Console.WriteLine ("Delete"); foreach (Greeting g in dao.GetAllList ()) dao.DeleteGreeting (g); } public static void Main () { SingletonS2ContainerFactory.Init (); IS2Container container = SingletonS2ContainerFactory.Container; IGreetingDao dao = container.GetComponent (typeof(IGreetingDao)) as IGreetingDao; TestSelect (dao); TestInsert (dao); TestSelect (dao); TestUpdate (dao); TestSelect (dao); TestDelete (dao); TestSelect (dao); } } /* Greeting is empty. Insert 5:Hello 6:Goodby Update 5:Hello, World 6:Goodby, World Delete Greeting is empty. */
SQLç¡ãã§ãã¼ã¿ãã¼ã¹ã¢ã¯ã»ã¹ãåºæ¥ãããã«ãªãã¾ãããã