present@わんくま

本家はこちら

目次

Blog 利用状況

ニュース

アクセサリ

書庫

日記カテゴリ

ギャラリ

リンク

テーブルデータゲートウェイ

ただ今 PofEAA の勉強中

まずはテーブルデータゲートウェイに挑戦。つっこみ歓迎です。

テーブルデータゲートウェイって?

「エンタープライズアプリケーションアーキテクチャパターン」によると

データベーステーブルに対して「ゲートウェイ」の役割を果たすオブジェクト。1つのインスタンスがテーブル内のすべての行を処理する。

というもの。

開発者みんなが SQL に精通しているとは限らないから、テーブルやビューにアクセス(選択・挿入・更新・削除)するための SQL をテーブルデータゲートウェイがラップして、それらの機能をメソッドで提供する・・・と。

複雑なパターンではないので、サンプルコードを書いた方が理解しやすいかも。

例えばこんなテーブルがあります

items テーブル
列名 説明など
id INTEGER 主キー, UNSIGNED, AUTO INC
title VARCHAR  
description TEXT  

このテーブルにパターンを適用します

public class ItemsGateway
{
    public IDataReader Find(uint id)
    {
        using (DbConnection connection = CreateConnection())
        {
            connection.Open();
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = @"select * from items where id = ?id";
                AddInParameter(command, "?id", DbType.UInt32, id);
                return command.ExecuteReader();
            }
        }
    }

    public void Insert(string title, string description)
    {
        using (DbConnection connection = CreateConnection())
        {
            connection.Open();
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = @"insert into items (title, description) values (?title, ?description)";
                AddInParameter(command, "?title", DbType.String, title);
                AddInParameter(command, "?description", DbType.String, description);
                command.ExecuteNonQuery();
            }
        }
    }

    public void Update(uint id, string title, string description)
    {
        using (DbConnection connection = CreateConnection())
        {
            connection.Open();
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = @"update items set title = ?title, description = ?description where id = ?id";
                AddInParameter(command, "?id", DbType.UInt32, id);
                AddInParameter(command, "?title", DbType.String, title);
                AddInParameter(command, "?description", DbType.String, description);
                command.ExecuteNonQuery();
            }
        }
    }

    public void Delete(uint id)
    {
        using (DbConnection connection = CreateConnection())
        {
            connection.Open();
            using (DbCommand command = connection.CreateCommand())
            {
                command.CommandText = @"delete from items where id = ?id";
                AddInParameter(command, "?id", DbType.UInt32, id);
                command.ExecuteNonQuery();
            }
        }
    }

    private void AddInParameter(DbCommand command, string name, DbType type, object value)
    {
        DbParameter parameter = command.CreateParameter();
        parameter.DbType = type;
        parameter.ParameterName = name;
        parameter.Value = value;
        command.Parameters.Add(parameter);
    }

    // 接続文字列は環境に合わせて変更する必要アリ!!
    private const string CONNECTION_STRING = @"Persist Security Info=False;database=test;server=MySqlServer;user id=myUser;Password=myPass";

    private DbConnection CreateConnection()
    {
        return new MySqlConnection(CONNECTION_STRING);
    }
}

これって・・・

普段データアクセスするクラスを作るときに使っている方法だ!「テーブルデータゲートウェイ」っていう名前が付いてたのかぁ。

あと今回は id で絞り込む Find メソッドだけ書いたけど、実際にこのパターンを使う場合は、title で絞り込むものや description で絞り込むものなど、複数用意するほうが便利。

投稿日時 : 2008年8月15日 15:51

コメントを追加

# re: テーブルデータゲートウェイ 2008/08/15 16:48 シャノン

Update、Insert、Delete はいいとして、複数のテーブルを Join しての Select はどうすんだろ…
あ、IEnumerable< テーブルの1レコードの情報を持ったクラス > を返せば Linq でいけるのかな。

# re: テーブルデータゲートウェイ 2008/08/16 1:15 なかむら

>IEnumerable< テーブルの1レコードの情報を持ったクラス > を返せば Linq でいけるのかな。

例えば、TableDataGateway を使って取得した DataTable に対し、下のようなコードで Join させることは可能ですね(試しました)。

// users と items は DataTable です
var result = from item in items.AsEnumerable()
join user in users.AsEnumerable() on item["user_id"] equals user["id"]
select new
{
UserName = user["name"],
ItemId = item["id"],
ItemTitle = item["title"]
};

LINQ の Join 初めて使った…。

# re: テーブルデータゲートウェイ 2018/08/28 11:53 wwwe

http://www.yeezyboost.com.co
http://www.nikemercurial.us.com
http://www.adidas-pureboost.us.com
http://www.long-champhandbags.org.uk
http://www.cheaprealjordans.us.com
http://www.longchamphandbagsonlines.com

# NBA Jerseys 2019/04/16 20:44 urnkwlhph@hotmaill.com

caafej,If you have any struggle to download KineMaster for PC just visit this site.

# Kanye West Yeezys Boost Shoes 2019/05/04 15:34 xqokmgmr@hotmaill.com

"Everything's always on the table," Kerr said. "Every playoff game, everything is always on the table. We consider everything. We go over every possibility. We hash it out. We ask the players their opinions on stuff and we make adjustments.

# NFL Jerseys 2019 2019/05/06 1:03 dsrczvsmcap@hotmaill.com

Earlier this week, President Trump met with Twitter chief executive Jack Dorsey in the Oval Office. While the meeting was supposed to be about the social media network’s efforts to fight opioid abuse, the Washington Post reported that a significant portion of the meeting focused on Trump’s concerns that Twitter has quietly and deliberately limited or removed some of his followers.

# Nike Store 2019/05/12 13:41 fqgvtszbtr@hotmaill.com

Both Kashala's family and friends are mourning over the loss of the young girl. "She was a sweet baby, real sweet," her mother said. "She always said she loved me, gave me hugs, kisses."

# jordan 11 concord 2018 2019/05/19 0:39 brgxfajgss@hotmaill.com

http://www.nikestores.us.com/ Nike Outlet store

# NFL Jerseys 2019/05/22 19:33 jciqomap@hotmaill.com

http://www.cs7boots1.com/ Cheap Yeezy Boost

# NFL Jerseys 2019 2019/05/31 16:41 fdevnic@hotmaill.com

http://www.pandora-jewelryoutlet.us/ Pandora jewelry Outlet

# Travis Scott Air Jordan 1 2019/05/31 18:47 qrendtfd@hotmaill.com

After months of brain fog,Jordan memory problems,Jordan and word-finding issues I’d assumed were related to the stress of grad school,Jordan an MRI revealed a tumor in my brain?anaplastic astrocytoma grade 3,Jordan to be exact,Jordan in the same class as highly terminal glioblastoma. One craniotomy,Jordan an ICU stay,Jordan and a pathology report later,Jordan I found myself staring blankly as a doctor explained that the chemo I needed would likely destroy my chance of getting pregnant someday,Jordan should I ever want to do so.

# Nike Outlet Store 2019/06/11 12:55 avmiiun@hotmaill.com

http://www.yeezy500utilityblack.com/ Yeezy 500

# cheap custom nfl jerseys 2019/06/25 21:37 qftsusk@hotmaill.com

http://www.basketball-jersey.us/ Basketball Jersey

# Yeezy 700 2019/06/27 23:27 lhizmc@hotmaill.com

http://www.nikeshoxoutlet.us/ Nike Shox

# Vapor Max 2019/07/05 6:04 prbowii@hotmaill.com

http://www.nikereactelement87.us.com/ Nike React Element 87

# Adidas Yeezy 2019/07/29 8:12 jhjtzffdom@hotmaill.com

http://www.nikeshoes.us.org/ Nike Shoes

# Nike Outlet Store 2019/08/01 8:35 lmgwntdxgd@hotmaill.com

http://www.nikeoutletonlineshopping.us/ Nike Outlet

# Yeezy 2019/08/04 19:29 nzgpjyjliag@hotmaill.com

http://www.yeezys.me.uk/ Yeezy Shoes

# Yeezy 2019/08/07 9:47 qkscxlkcrmz@hotmaill.com

http://www.yeezy350.org.uk/ Yeezy 350

# Yeezy 2019/08/09 17:01 wsowzhj@hotmaill.com

http://www.nikeshoes.us.org/ Nike Shoes

# jordan 11 concord 2019/08/15 18:12 ijacms@hotmaill.com

http://www.nfl-jerseys.us.org/ Cheap NFL Jerseys

# ivermectin 1 cream 45gm 2021/09/28 12:36 MarvinLic

ivermectin coronavirus http://stromectolfive.online# stromectol south africa

# ivermectin humans 2021/10/31 23:11 DelbertBup

ivermectin stromectol https://stromectolivermectin19.com/# ivermectin price canada
ivermectin cost canada

# ivermectin pill cost 2021/11/01 16:58 DelbertBup

oral ivermectin cost https://stromectolivermectin19.com/# ivermectin 200mg
ivermectin online

# buy ivermectin canada 2021/11/02 20:32 DelbertBup

cost of ivermectin lotion http://stromectolivermectin19.online# ivermectin price comparison
ivermectin pills canada

# ivermectin 6mg 2021/11/04 8:47 DelbertBup

ivermectin buy nz https://stromectolivermectin19.com/# ivermectin 3
ivermectin lice

# sildenafil 20 mg tablet 2021/12/08 0:05 JamesDat

https://viasild24.com/# how to take sildenafil 20 mg

# best place to buy careprost 2021/12/12 3:01 Travislyday

https://baricitinibrx.com/ barikind

# bimatoprost buy online usa 2021/12/12 22:28 Travislyday

https://plaquenils.com/ plaquenil cost

# buy bimatoprost 2021/12/14 13:55 Travislyday

http://stromectols.com/ stromectol cvs

# stromectol 6 mg dosage 2021/12/18 20:41 Eliastib

kqustg https://stromectolr.com stromectol tab 3mg

# JicCcJzttIs 2022/04/19 12:05 markus

http://imrdsoacha.gov.co/silvitra-120mg-qrms

# pain medications without a prescription: https://medrxfast.com/ 2022/08/07 8:48 MedsRxFast

pain medications without a prescription: https://medrxfast.com/

# prednisone 5084 https://deltasone.icu/
prednisone purchase online 2022/08/22 17:36 Prednisone

prednisone 5084 https://deltasone.icu/
prednisone purchase online

# best pills for ed https://ed-pills.xyz/
ed medication online 2022/09/16 2:34 EdPills

best pills for ed https://ed-pills.xyz/
ed medication online

# erectile dysfunction drug https://ed-pills.xyz/
ed pills 2022/09/17 2:57 EdPills

erectile dysfunction drug https://ed-pills.xyz/
ed pills

# medication for ed https://cheapestedpills.com/
buy ed pills 2022/12/10 22:15 CheapPills

medication for ed https://cheapestedpills.com/
buy ed pills

# pillole per erezioni fortissime https://viasenzaricetta.com/# 2023/04/16 14:30 ViaSenza

pillole per erezioni fortissime https://viasenzaricetta.com/#

# buy cytotec - https://cytotecsale.pro/# 2023/04/29 6:30 Cytotec

buy cytotec - https://cytotecsale.pro/#

# medication for ed: https://edpills.pro/# 2023/05/15 22:16 EdPillsPro

medication for ed: https://edpills.pro/#

# canada prescription https://fastpills.pro/# top 10 mail order pharmacies 2023/06/30 1:04 FastPills

canada prescription https://fastpills.pro/# top 10 mail order pharmacies

# antiplatelet drug https://plavix.guru/ Cost of Plavix on Medicare 2023/10/24 1:58 Plavixxx

antiplatelet drug https://plavix.guru/ Cost of Plavix on Medicare

# how to order doxycycline https://doxycycline.forum/ doxycycline 100mg price 2023/11/25 14:39 Doxycycline

how to order doxycycline https://doxycycline.forum/ doxycycline 100mg price

# farmacia online senza ricetta https://farmaciait.pro/ farmacia online senza ricetta 2023/12/04 11:16 Farmacia

farmacia online senza ricetta https://farmaciait.pro/ farmacia online senza ricetta

# buy ed pills online https://edpills.tech/# ed treatments 2023/12/23 9:32 EdPills

buy ed pills online https://edpills.tech/# ed treatments

タイトル
名前
URL
コメント