éçºè ãç¯ãééãã®1ã€ïŒãã€ãŠç§ã¯ãããã®1ã€ã§ããïŒã¯ãäœæããã¢ããªã±ãŒã·ã§ã³ã«å¯ŸããŠORMæŠç¥ã1ã€ã ã䜿çšããå¿ èŠããããšããã¹ããŒãã¡ã³ãã§ãã ããã¯äžè¬çã«çå®ã§ã¯ãããŸããã æŠç¥ã®éžæãç¹å®ã®ã·ããªãªã«çµã³ä»ããããšãã§ãïŒãŸããããã¹ãã§ãïŒãç¹å®ã®ã±ãŒã¹ã«é©ããããŒã«ãéžæããããã«ããŠãã ããã
ADO.NETãçŽæ¥äœ¿çšããã¹ãã§ã¯ãªãæéã®99.9ïŒ ãããã«ããããŸãã ãŸã
dataReader.MoveNext
ã䜿çšããŠããå Žå-åæ¢ããŠãã ããïŒ
å€ãã®äººã¯ORMã奜ãã§ã¯ãããŸããã 圌ãã®è°è«ãèããåŸãMartin FowlerãOrmHateã§æžããããšã«åæããŸã ã
ORMããã®æ倧ã®å€±æã¯ãé«ãæåŸ ã«ãããŸãã
ORMã¯æªããããéè² è·ã§ãããšããèãã«åæããå¿ èŠããããŸã ã ORMã¯ãåé¡ã解決ããããã«èšèšãããŠããããã®ããã®ããŸããŸãªã¢ãããŒãããããŸãã ãããããããã®ã¢ãããŒããæ€èšããåã«ã解決ããããšããŠããåé¡ã調ã¹ãŠã¿ãŸãããã
ã®ã£ãããåãã
SQLã§ããŒã¿ãããŒããŸãã¯åã蟌ãå¿ èŠãããå Žåã¯ãSQLã§.NETããŒã¿åããããïŒãããããïŒããå¿ èŠããããŸãã .NETã®å Žåãããã¯ADO.NETã䜿çšããŠSQLã³ãã³ããSQLãµãŒããŒã«éä¿¡ããããšãæå³ããŸãã 次ã«ãSQLã¿ã€ãã.NETã¿ã€ãã«ãããããå¿ èŠããããŸãã ãŸãã埮åŠãªéãããããŸããããšãã°ãSQLã®æ¥ä»ã¯.NETã®æ¥ä»ãšã¯ç°ãªããŸãã
ADO.NETã¯ãããæ¯æŽããŸãããçããŒã¿ã»ããã®åŠçãš.NETãªããžã§ã¯ãã®äœæã®ä»äºãæ®ããŸãã æçµçã«ãå¿ èŠãªãã®ãåŸãããŸãããªããžã§ã¯ããš.NETã¿ã€ããæäœããŸãã ãŸããäžéšã®ã³ãŒãã¯ãããSQLã¯ãšãªã«å€æãããã®éãè¡ããŸãã
ORMã¯ãADO.NETã®äžã«ããŸããŸãªæœè±¡åã®ã¬ã€ã€ãŒãè¿œå ããããšã«ããããã®åé¡ã解決ããããã«èšèšãããŠããŸãã ããããããã«ã¯å€ãã®æŠç¥ããããŸãã ãããããèŠãŠãã©ããæãé©ããŠããããèŠãŠã¿ãŸãããã
ãšã³ãã£ãã£ããŒã¹ã®ãªã¬ãŒã·ã§ãã«ãããã³ã°
ãã®ãããªãããã³ã°ã§ã¯ãã»ãšãã©ã®å ŽåãããŒã¿ããŒã¹ããŒãã«ã¯ã·ã¹ãã ã®ãšã³ãã£ãã£ãš1察1ã§å¯Ÿå¿ããŠããŸãã ãªããžã§ã¯ãã«ããããã£ãè¿œå ãããšãããŒãã«ã«åãè¿œå ãããŸãã ãã®ã¡ãœããã®äœ¿çšã¯ããã®èå¥åã«ãã£ãŠãšã³ãã£ãã£ïŒãŸãã¯éçŽïŒãèªã¿èŸŒã¿ããã®ãªããžã§ã¯ããšãå Žåã«ãã£ãŠã¯é¢é£ãªããžã§ã¯ãã管çããORMã䜿çšããŠãã®ãªããžã§ã¯ããããŒã¿ããŒã¹ã«ä¿åããããšã«åºã¥ããŠããŸãã
ãã®å Žåã®ORMã¯ã次ã®ãããªå€ãã®æ©èœãæäŸããŸãã
- å€æŽè¿œè·¡
- é 延èªã¿èŸŒã¿ïŒé 延èªã¿èŸŒã¿ïŒ
- ããªããŒãïŒç©æ¥µçãªãã§ããïŒ
- ã«ã¹ã±ãŒã
- ãªããžã§ã¯ãã®äžææ§ã®ç¢ºä¿ïŒ ã¢ã€ãã³ãã£ãã£ããã ïŒ
- äœæ¥åäœã§äœæ¥ããïŒäœæ¥åäœ ïŒ
äžåºŠã«1ã€ã®ãšã³ãã£ãã£ãŸãã¯éèšã®ã¿ãæäœããå ŽåãNHibernateãªã©ã®ORMã¯éåžžã«é©ããŠããŸãã æå®ãããæ§æã䜿çšããŠãèªã¿èŸŒãŸãããšã³ãã£ãã£ã远跡ãããã©ã³ã¶ã¯ã·ã§ã³ã®ã³ãããäžã«å€æŽãèªåçã«ä¿åããŸãã ãŸããããã¯çŽ æŽãããããšã§ãããªããªããããŒã¿ãæ±ãç¬èªã®ã¬ã€ã€ãŒãæã¡æ©ãã¹ãã§ã¯ãªãããã§ãã NHibernateããã¹ãŠã®æ±ãä»äºãããŠãããŸãã
ãªããžã§ã¯ããå€æŽãããšããå¯äžã®ç®çã§Idã«ãã£ãŠãªããžã§ã¯ããããŒãããéããããã¯ãã¹ãŠæ£åžžã«æ©èœããŸãã ããã«ããããªããžã§ã¯ãã®è¿œå ãä¿åãªã©ã远跡ããããã«äœæããå¿ èŠã®ãã倧éã®ã³ãŒããäžèŠã«ãªããŸãã
ãã®ã¢ãããŒãã®è£åŽã¯ããªããžã§ã¯ããèªã¿èŸŒãã ããããããå€æŽããããã«ãšã³ãã£ãã£ãããŒãããããORMãèªèããªãããšã§ãã ããã©ã«ãã§ã¯ãå€æŽè¿œè·¡ãæå¹ã«ãªã£ãŠããããšã«æ°ã¥ããªããšã人ã ã¯ã€ãŸãããŸãã
ãšã³ãã£ãã£ãããŒãããŠå€æŽãä¿åããïŒãŸãã¯æ°ãããšã³ãã£ãã£ãäœæããïŒããã«ãšã³ãã£ãã£ãããŒãããå Žåããã®ã¢ãããŒãã«ãããã€ã³ãã©ã¹ãã©ã¯ãã£ã¬ã€ã€ãŒã«ããŒã¿ã¢ã¯ã»ã¹ã¬ãã«ãå«ããããšã§æè»æ§ãé«ãŸãããšã³ãã£ãã£ã¿ã€ããä¿åæ¹æ³ããæ¯èŒçç¬ç«ãããããšãã§ããŸãã ãã®ç¬ç«æ§ã¯ãCïŒã¢ãã«ãšããŒã¿ã¹ããŒããç°ãªãå¯èœæ§ãããããšãæå³ãããã®ã§ã¯ãããŸããã ããã©ããããããã¯ãããŒã¿ã¢ã¯ã»ã¹å±€ããªããžã§ã¯ãã¢ãã«ã貫éããªãããšãæå³ããŸãã代ããã«ãããžãã¹ã«ãŒã«ãããŒãããããšã奜ã¿ãŸãã
ããŒã¿ã»ãããžã®ãããã³ã°ïŒçµæã»ããããŒã¹ã®ãªã¬ãŒã·ã§ãã«ãããã³ã°ïŒ
ã»ãšãã©ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãããŒã¿èªã¿åãèŠä»¶ãã¬ã³ãŒãæ°ãã¯ããã«è¶ ããŠããŸãã æè¿ã®ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãSELECTãšINSERT / UPDATE / DELETEã®æ¯çã100ïŒ1ã§ããã SQLãæ¬åœã«åŸæãšãããã®ãèŠããšãSQLã¯ã»ããïŒã»ããïŒå ã®ããŒã¿ãåŠçããŠããŸãã SQLãµãŒããŒããããŒã¿ã»ãããéžæããå Žåããã®ããŒã¿ããšã³ãã£ãã£ã«çŽæ¥ãããããããšããããšã¯æå³ããããŸããã
ãã ããIDataReaderãŸãã¯DataTableãçŽæ¥æäœããããšã¯é¿ããŸãã ãããã¯ãã¢ããªã±ãŒã·ã§ã³ã®äžäœå±€ã«è»¢éããã®ãé£ãããåæå®ã®æªããªããžã§ã¯ãã§ãã ããã©ããããããŒã¿ã«é©åãããªããžã§ã¯ããäœæããããšããããããŸãã ãããã®ãªããžã§ã¯ãã¯ãå€ãã®å ŽåãDTOïŒããŒã¿è»¢éãªããžã§ã¯ãïŒãŸãã¯èªã¿åãã¢ãã«ãšåŒã°ããŸãã åã ã®SQLãµã³ãã«ã«å¯ŸããŠãã®ãããªDTOãäœæããŸãããä»ã®ã¯ãšãªã§ããããåå©çšããããšã¯ã»ãšãã©ãããŸããã
å€ãã®ORMã«ã¯ããã®ãããªã·ããªãªåãã«æé©åãããæ©èœããããŸãã NHibernateã§ã¯ãæ圱ã䜿çšããŠè¿œè·¡ããªãã«ããããŒã¿ãDTOã«çŽæ¥è¡šç€ºã§ããŸãã SQLã¯ãšãªã䜿çšããŠãããè¡ãããšãã§ãããããã³ã°æ§æã¯å¿ èŠãããŸããã ãŸãã¯ãPetaPocoã®ãããªãã€ã¯ãORMã䜿çšã§ããŸãã
ãããã®èªã¿åãã¯ãèªã¿åãæã«DTOãªããžã§ã¯ããçæããããšãã§ããŸãã NHibernateãšè€æ°ã®micro-ORMã®äž¡æ¹ã䜿çšãããšãã¯ãšãªçµæè¡ãèªã¿åããªããåã ã®DTOãªããžã§ã¯ãã1ã€ãã€é çªã«åä¿¡ã§ãããããã¡ã¢ãªã«å«ãŸãããªããžã§ã¯ãã®éãæå°éã«æããããšãã§ããŸãã
ã¢ããªã±ãŒã·ã§ã³ã§ã¯ãèªã¿åãã«NHiberanteã䜿çšããããšããããããŸããããšã³ãã£ãã£ãªããžã§ã¯ãã¯äœ¿çšããã代ããã«çã®SQLã䜿çšããŸãã æé©åãããNHiberanateããããŒã䜿çšããŠãDTOã¿ã€ããæäŸããã ãã§ãéžæã®çµæãèªåçã«è¡šç€ºãããŸãã
ããžãã¹ã«ãŒã«ãé©çšããŠæ å ±ãä¿åããå¿ èŠãããå Žåããã®ã¢ãããŒãã¯ããŸãæ©èœããŸããã ãããã®ã¢ãã«ã¯éåžžãåå¥ã®ããŒã¿ã»ããã«è¡šç€ºãããŸãããããŒã¿ããŒã¹ããŒãã«ã«ã¯è¡šç€ºãããŸããã
ã¢ã¯ãã£ãã¬ã³ãŒãã¯ãããŒã¿ãæäœããããã®æ©èœããªããžã§ã¯ãã¢ãã«èªäœã«å«ãŸããŠããããŒã¿ã®åºæ¬çãªè¡šç€ºã®å¥ã®äŸã§ãã
DMLããŒã¹ã®ãªã¬ãŒã·ã§ãã«ãããã³ã°
CRUDã®å®è£ ã«å¿ èŠãªSQLãããã£ãŠããŠãæåã§äœæãããå Žåã¯ãDMLã³ãã³ããADO.NETãããé«ãã¬ãã«ã«å¹æçã«æœè±¡åãããã®ããã§ã«æ¢ããŠããŸãã
ãããŠãããããã€ã¯ãORMã¢ãªãŒãã§ãã PetaPoco ã Dapper ã Massiveãªã©ã®ãã¬ãŒã ã¯ãŒã¯ã¯ãADO.NETã®åé¡ã解決ããããã«äœæãããŠããŸãã éåžžã¯ãADO.NETãªããžã§ã¯ããæäœã§ããŸãããæäœã¯å€§å¹ ã«ç°¡çŽ åãããŠããŸãã æ¥ç¶ãå¿ èŠãªã ãã§ããããã®ãã¬ãŒã ã¯ãŒã¯ã§ã¯ãADO.NETèªäœãããã¯ããã«åçŽãªã³ãŒããæäŸãã圢åŒã§ãã¹ãŠã®CRUDæäœãåŠçã§ããŸãã
ãšã³ãã£ãã£ããªãããšã³ãã£ãã£ãããŒãã«ã«è¡šç€ºããå¿ èŠãããå ŽåããŸãã¯ãã®éã®å Žåãmicro-ORMã¯ã¯ããã«ç°¡åãªã¢ãããŒããæäŸããŸãã ãŸãããã€ã¯ãORMã¯äºåã®æ§æãå¿ èŠãšããªããããé 延å®è¡ãšæé©åããããã£ãã·ã³ã°ãã¯ããã¯ã«äŸåããŠãSQLãã©ã¡ãŒã¿ãŒããããããçµæãã¯ãšãªããŸãã å€ãã®ã¢ããªã±ãŒã·ã§ã³ã¯DMLããŒã¹ã®mappigã§éå§ã§ããé¢ä¿ãŸãã¯ãšã³ãã£ãã£ãå¿ èŠã«å¿ããŠããã«æ¬æ ŒçãªORMã«åãæ¿ããããšãã§ããŸãã
äžæ¬èªã¿èŸŒã¿ããŒã«
ããã¯ç¹å¥ãªå Žæãå æãããã®ã§ã-ãªããžã§ã¯ãã®æ¹æ³ã§ããŒã¿ãæ¿å ¥/ããŒãããããªãå ŽåããããŸãã 代ããã«ããã¹ãŠã®ã»ããããŸãšããŠäœ¿çšããããšããå§ãããŸãã SQL Bulk Copyãªã©ã®ããŒã«ã䜿çšãããšãããŒã¿ãCSV圢åŒãŸãã¯è¡šåœ¢åŒã§ååŸããã³ã¢ããããŒãã§ããŸãã
ãããã®ãŠãŒãã£ãªãã£ã¯ããºãŒã«ã®ããã«æ©èœãããã¹ãŠã®ããŒã¿ãããã«ããåãããŸããããã以å€ã¯æäŸããŸããã ããŒã¿ãæŽæ°ãŸãã¯åé€ããããšã¯ã§ããŸããããSQLãã倧éã®ããŒã¿ãååŸããã«ã¯ããããã®ãŠãŒãã£ãªãã£ãå¿ èŠã§ãã
ããŒã¿ãã¡ã€ã«ãå€éšããŒãããŒã«æäŸããããŸãã¯ãã®éã®å€ãã®çµ±åã·ããªãªã§ã¯ããããã®ããŠã³ããŒããŒã䜿çšããŠããã¡ã€ã«ãããŒãã«ãšããŠäœ¿çšããããŒã¿ããŒã¹ã«çŽæ¥ã¢ããããŒãã§ããŸãã
ãããã®ãŠãŒãã£ãªãã£ã¯ãããŒã¿ã解æ/ããŒãããåŸæ¥ã®æ¹æ³ãããã¯ããã«é«éã§ãã äžéšã®ãã¹ãã§ã¯ãããã°ã¬ãã·ãããŒããšæ¯èŒããŠé åºã«éããèŠãããŸããã ãããŠãããã±ãŒã¹ã§ã¯ãæ°æéãš1åéã®éããèŠãŸããã ããããã¹ãŠã®è£è¿ãã¯ãæ©èœãINSERTãšSELECTã®ã¿ã«å¶éãããŠããããšã§ãã ä»ã®ãã¹ãŠã«ã¯ãä»ã®ã¢ãããŒããå¿ èŠã§ãã
ã¿ã¹ã¯ã«æé©ãªããŒã«
æè¿ã®ãããžã§ã¯ãã§ã¯ãäžèšã®åã¢ãããŒãã䜿çšããŠã1ã€ã®ããŒã¿ããŒã¹ãš1ã€ã®ã³ãŒããæäœããŸããã ãšã³ãã£ãã£/éèšãããã³ã°çšã®NHibernateãããŒã¿ã»ãããèªã¿èŸŒãããã®æ¢è£œã®çµæã»ããã®éžæïŒããã³çµæã®ã¡ãã»ãŒãž/ãšã¯ã¹ããŒã/ãã¥ãŒã®è¿œå æºåïŒãã·ã³ãã«ãªããŒãã«ãšã¢ãžã¥ãŒã«ã®DMLãããã³ã°ãããã³ããŒãããŒãããã¡ã€ã«ãããŠã³ããŒãããããã®ãã«ã¯ããŒãããŒã«äœçŸäžãã®ã©ã€ã³ã§ã
éèŠãªç¹ã¯ãç¹å®ã®ããŒã«ãã¢ãããŒãã«çžãããå¿ èŠããªãããšã§ãã ãã¹ãŠã®ã·ããªãªã§ORMæŠç¥ã¯æ©èœããŸããã NHibernateã¯ä»ã®å€ãã®ã·ããªãªïŒãšã³ãã£ãã£ã®çŽæ¥ãããã³ã°ãé€ãïŒã§åäœã§ããŸãããäžçã§ã¯äœãããŸããã åžžã«åãã¢ãããŒãã䜿çšããããšããè©Šã¿ããè€éãããã°ãã°çããã
SQLãµãŒããŒã®å€éšã§èšè¿°ããããã¹ãŠã®ã¢ããªã±ãŒã·ã§ã³ã¯ORMã䜿çšããŸãã ãã®ææžãADO.NETã³ãŒããŸãã¯NHibernateã®ãããã-.NETãšSQLã®éã®ã®ã£ãããåããå¿ èŠããããŸãã ãã®å æã¯å°é£ãªäœæ¥ã§ãããåââé¡ãå®å šã«è§£æ±ºãããã®ã¯ãããŸããã ãããŠããã¹ãã§ã¯ãããŸããïŒ
ç¹å®ã®åé¡ã解決ããã¢ãããŒããéžæããŠãã ããã 1ã€ã®ãããžã§ã¯ãã«è€æ°ã®ORMæŠç¥ãããããšãå¿é ããªãã§ãã ããã ããã¯ãéäœç³»çãªè§£æ±ºçãåãå ¥ãããããšããæå³ã§ã¯ãããŸããã ãããããã©ããããå¯èœãªãªãã·ã§ã³ã®ç¥èã«åºã¥ããŠæ€èšŒæžã¿ã®ãœãªã¥ãŒã·ã§ã³ãé©çšããããšã¯åžžã«è¯ãèãã§ãã