Last active
December 11, 2018 12:15
-
-
Save daisukenishino2/90b303d3bf0e5e4500e0d6d1d897d8d6 to your computer and use it in GitHub Desktop.
Open棟梁:POCOフィッティングのサンプル・プログラム https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Samples/WebApp_sample/MVC_Sample/MVC_Sample/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 一部、DataToDictionaryのテストコード | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToDictionary.cs | |
DataToDictionary d2d = null; | |
List<Dictionary<string, string>> list = null; | |
// マップなし | |
d2d = new DataToDictionary(null, null, null); | |
list = d2d.DataTableToDictionaryList(dt); | |
// マップあり | |
// Dictionary の Key-Value で src-dst のキー名を書く | |
d2d = new DataToDictionary( | |
new Dictionary<string, string>() | |
{ | |
{ "ShipperID", "_ShipperID"}, | |
{ "CompanyName", "_CompanyName"}, | |
{ "Phone", "_Phone"} | |
}, | |
null, null); | |
list = d2d.DataTableToDictionaryList(dt); | |
ret = new { Message = "", Result = list }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 共通Daoを生成 | |
CmnDao cmnDao = new CmnDao(this.GetDam()); | |
// 静的SQLを指定 | |
cmnDao.SQLText = "SELECT * FROM Shippers"; | |
// 共通Daoを実行 | |
IDataReader idr = cmnDao.ExecSelect_DR(); | |
// DataReaderToList | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs | |
List<ShipperViweModel> list = DataToPoco.DataReaderToList<ShipperViweModel>(idr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 共通Daoを生成 | |
CmnDao cmnDao = new CmnDao(this.GetDam()); | |
// 静的SQLを指定 | |
cmnDao.SQLFileName = "ShipperSelect.sql"; | |
// 1件参照のため、主キー値を設定 | |
cmnDao.SetParameter("P1", 1); | |
// 共通Daoを実行 | |
IDataReader idr = cmnDao.ExecSelect_DR(); | |
// DataTableToPOCO | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs | |
ShipperViweModel svm = DataToPoco.DataReaderToPOCO<ShipperViweModel>(idr); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 共通Daoを生成 | |
CmnDao cmnDao = new CmnDao(this.GetDam()); | |
// 静的SQLを指定 | |
cmnDao.SQLText = "SELECT * FROM Shippers"; | |
// 戻り値 dt | |
DataTable dt = new DataTable(); | |
// 共通Daoを実行 | |
cmnDao.ExecSelectFill_DT(dt); | |
// DataTableToList | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs | |
List<ShipperViweModel> list = DataToPoco.DataTableToList<ShipperViweModel>(dt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 共通Daoを生成 | |
CmnDao cmnDao = new CmnDao(this.GetDam()); | |
// 静的SQLを指定 | |
cmnDao.SQLFileName = "ShipperSelect.sql"; | |
// 1件参照のため、主キー値を設定 | |
cmnDao.SetParameter("P1", 1); | |
// 戻り値 dt | |
DataTable dt = new DataTable(); | |
// 共通Daoを実行 | |
cmnDao.ExecSelectFill_DT(dt); | |
// DataTableToPOCO | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs | |
ShipperViweModel svm = DataToPoco.DataTableToPOCO<ShipperViweModel>(dt); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 共通Daoを生成 | |
CmnDao cmnDao = new CmnDao(this.GetDam()); | |
// 静的SQLを指定 | |
cmnDao.SQLFileName = "ShipperSelect.sql"; | |
// 1件参照のため、主キー値を設定 | |
cmnDao.SetParameter("P1", 1); | |
// 戻り値 dt | |
DataTable dt = new DataTable(); | |
// 共通Daoを実行 | |
cmnDao.ExecSelectFill_DT(dt); | |
// DataTableToPOCO | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs | |
TestShipperViweModel tsvm = DataToPoco.DataTableToPOCO<TestShipperViweModel>(dt, | |
// mapの書き方は、Key-Valueでdst-srcのproperty field名を書く | |
new Dictionary<string, string>() | |
{ | |
{ "_ShipperID", "ShipperID"}, | |
{ "_CompanyName", "CompanyName"}, | |
{ "_Phone", "Phone"} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ShipperViweModel svm = new ShipperViweModel(); | |
TestShipperViweModel tsvm = TestShipperViweModel() | |
{ | |
_ShipperID = "_ShipperID", | |
_CompanyName = "_CompanyName", | |
_Phone = "_Phone", | |
... | |
}; | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/PocoToPoco.cs#L81 | |
// src: TestShipperViweModel, dst: ShipperViweModel | |
PocoToPoco.Map<TestShipperViweModel, ShipperViweModel>( | |
tsvm, svm, | |
// PocoToPoco.Map の mapの書き方は、 | |
// Dictionary の Key-Value で dst-src のproperty field名を書く | |
new Dictionary<string, string>() | |
{ | |
{ "ShipperID", "_ShipperID"}, | |
{ "CompanyName", "_CompanyName"}, | |
{ "Phone", "_Phone"} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ShipperViweModel svm = new ShipperViweModel() | |
{ | |
_ShipperID = "_ShipperID", | |
_CompanyName = "_CompanyName", | |
_Phone = "_Phone", | |
... | |
}; | |
TestShipperViweModel tsvm = null; | |
// https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/PocoToPoco.cs#L81 | |
// src: ShipperViweModel, dst: TestShipperViweModel | |
tsvm = PocoToPoco.Map<ShipperViweModel, TestShipperViweModel>( | |
svm, null, | |
// dstがnullの場合、戻り値から生成されたobjectを入手できる。 | |
new Dictionary<string, string>() | |
{ | |
{ "_ShipperID", "ShipperID"}, | |
{ "_CompanyName", "CompanyName"}, | |
{ "_Phone", "Phone"} | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class POCO_FL | |
{ | |
public string aaa = ""; | |
public string bbb = ""; | |
public string xxx { set; get; } = ""; | |
public string yyy { set; get; } = ""; | |
} | |
public class POCO_FU | |
{ | |
public string AAA = ""; | |
public string BBB = ""; | |
public string XXX { set; get; } = ""; | |
public string YYY { set; get; } = ""; | |
} | |
public class POCO_ML | |
{ | |
public string aaa = ""; | |
public string bbb = ""; | |
public string ccc = ""; | |
public string xxx { set; get; } = ""; | |
public string yyy { set; get; } = ""; | |
public string zzz { set; get; } = ""; | |
} | |
public class POCO_MU | |
{ | |
public string AAA = ""; | |
public string BBB = ""; | |
public string CCC = ""; | |
public string XXX { set; get; } = ""; | |
public string YYY { set; get; } = ""; | |
public string ZZZ { set; get; } = ""; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
POCO_MU pmu = null; | |
POCO_FU pfu = null; | |
POCO_ML pml = null; | |
POCO_FL pfl = null; | |
#region POCO_MU -> POCO_FU | |
pmu = new POCO_MU() | |
{ | |
AAA = "AAA1", | |
BBB = "BBB1", | |
CCC = "CCC1", | |
XXX = "XXX1", | |
YYY = "YYY1", | |
ZZZ = "ZZZ1" | |
}; | |
pfu = PocoToPoco.Map<POCO_MU, POCO_FU>(pmu); | |
WriteLine.OutPutDebugAndConsole(ObjectInspector.Inspect(pfu)); | |
#endregion | |
#region POCO_FU -> POCO_MU | |
pfu = new POCO_FU() | |
{ | |
AAA = "AAA2", | |
BBB = "BBB2", | |
XXX = "XXX2", | |
YYY = "YYY2" | |
}; | |
pmu = PocoToPoco.Map<POCO_FU, POCO_MU>(pfu); | |
WriteLine.OutPutDebugAndConsole(ObjectInspector.Inspect(pmu)); | |
#endregion | |
#region POCO_MU -> POCO_FL | |
pmu = new POCO_MU() | |
{ | |
AAA = "AAA3", | |
BBB = "BBB3", | |
CCC = "CCC3", | |
XXX = "XXX3", | |
YYY = "YYY3", | |
ZZZ = "ZZZ3" | |
}; | |
pfl = PocoToPoco.Map<POCO_MU, POCO_FL>(pmu, new Dictionary<string, string>() | |
{ | |
{ "aaa", "AAA" }, | |
{ "bbb", "BBB" }, | |
{ "xxx", "XXX" }, | |
{ "yyy", "YYY" } | |
}); | |
WriteLine.OutPutDebugAndConsole(ObjectInspector.Inspect(pfl)); | |
#endregion | |
#region POCO_FU -> POCO_ML | |
pfu = new POCO_FU() | |
{ | |
AAA = "AAA4", | |
BBB = "BBB4", | |
XXX = "XXX4", | |
YYY = "YYY4" | |
}; | |
pml = PocoToPoco.Map<POCO_FU, POCO_ML>(pfu, new Dictionary<string, string>() | |
{ | |
{ "aaa", "AAA" }, | |
{ "bbb", "BBB" }, | |
{ "xxx", "XXX" }, | |
{ "yyy", "YYY" } | |
}); | |
WriteLine.OutPutDebugAndConsole(ObjectInspector.Inspect(pml)); | |
#endregion | |
Console.ReadKey(); | |
} |
利用方法
サンプル
C#
MVC_Sample
ASPNETWebService
VB
インストール
v 2.3.0 以降で利用可能。
Install-Package Touryo.Infrastructure.Public
もしくは、
Install-Package Touryo.Infrastructure.Public -Version 2.3.0
名前空間
Touryo.Infrastructure.Public.Dto
必要に応じて、using, Importsする。
using Touryo.Infrastructure.Public.Dto;
Imports Touryo.Infrastructure.Public.Dto
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
概要
ASP.NET MVCやWebAPIなど、Bean/POCO/DTOとの親和性が高い開発に利用可能。
実装
https://github.com/OpenTouryoProject/OpenTouryo/tree/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto
PocoToPoco
Bean/POCO/DTO間の項目移送処理
https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/PocoToPoco.cs
DataToPoco
System.DataからBean/POCO/DTO変換への変換。
https://github.com/OpenTouryoProject/OpenTouryo/blob/develop/root/programs/CS/Frameworks/Infrastructure/Public/Dto/DataToPoco.cs
仕様
ToPOCO系クラスについて
基本的に、dst で回すので、dst プロパティ・フィールド名で src から引っ張る。
従って、dst に無い プロパティ・フィールド名で src から引っ張らない。
マップは、dst に有るプロパティ・フィールドからマッピングを探したプロパティ・フィールド名で src から引っ張る。
マッピングが無い場合は、既定の dst プロパティ・フィールド名で src から引っ張る。
また、既定 or マッピング後のプロパティ・フィールド名が src に無い場合は、引っ張らない。
ToDictionaryList系クラスについて
基本的に、src で回すので、src キー名で dst にコピーする。
マップは、src に有るキー名からマッピングを探し、dst にコピーする。
マッピングが無い場合は、既定の src キー名で dst にコピーする。
マップの作成について
上記のような方式になっているので、マップの作製は、
・ToPOCOは、Dictionary の Key-Value で dst-src のプロパティ・フィールド名を書く。
・ToDictionaryListは、Dictionary の Key-Value で src-dst のキー名を書く。