デザインパターンã®ã‚µãƒ³ãƒ—ルを掲載ã—ã¦ã¾ã™ã€‚ã“ã®ã‚«ãƒ†ã‚´ãƒªã®æ›´æ–°ã¯ã‹ãªã‚Šä¸å®šæœŸã§ã™ã€‚
Factory Methodƒpƒ^[ƒ“ Œã•Ò
‘O•Ò‚ÌÝŒv‚©‚çŽÀ‘•‚ÖˆÚ‚è‚Ü‚·B
‚Ü‚¸‚ÍDataBaseƒNƒ‰ƒX‚ÌŠî’êƒNƒ‰ƒX‚©‚ç
Šî’êƒNƒ‰ƒX‚Å‚ÍOpenACloseAFillAUpdate‚̉¼‘zƒƒ\ƒbƒh‚ð’è‹`‚µ‚Ü‚·B
‚³‚ç‚ɃvƒƒpƒeƒB‚É‚ÍConnectionACommand‰¼‘zƒvƒƒpƒeƒB‚ð—pˆÓ‚µ‚Ü‚·B
@@
DataBase.cs Šî’êƒNƒ‰ƒX
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;

namespace Cancer.ADO
{
    public class  DataBase
    {
        // ƒƒ“ƒo•Ï”
        private DbConnection _connection = null;
        private DbCommand _command = null;

        // ƒvƒƒpƒeƒB
        public virtual DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public virtual DbCommand Command
        {
            get
            {
                return _command;
            }
        }

        // ƒƒ\ƒbƒh
        public virtual void Open()
        {
            Connection.Open();
        }

        public virtual void Close()
        {
            Connection.Close();
        }
        public virtual void Fill(DataSet ds)
        {
            ;
        }
        public virtual void UpDate(DataSet ds)
        {
            ;
        }
    }
}

ŽŸ‚ÉŠî’êƒNƒ‰ƒX‚ðŒp³‚µ‚½MSAccess‚ð‘€ì‚·‚éƒNƒ‰ƒX‚ðŽÀ‘•‚µ‚Ü‚·B
OleDBƒf[ƒ^ƒvƒƒoƒCƒ_[‚ÌConnection‚ÆCommand‚ðƒRƒ“ƒXƒgƒ‰ƒNƒ^‚Ŷ¬‚µ‚Ä‚Ü‚·B
ƒvƒƒpƒeƒB‚Íoverride‚ÅÄ’è‹`‚µ‚Ä‚¢‚Ü‚·B
‘O•Ò‚ňê‚ÉŽg‚Á‚Ä—~‚µ‚¢ƒCƒ“ƒXƒ^ƒ“ƒX‚̶¬•û–@‚ð—pˆÓ‚·‚é‚Æ‚ ‚Á‚½‚Ì‚Å‚·‚ªA
‚±‚±‚Å‚ÍŠù‚ɶ¬‚µ‚Ä‚Ü‚·BB‚±‚̃vƒƒpƒeƒB‚ªˆê‚ÉŽg‚Á‚Ä—~‚µ‚¢ƒCƒ“ƒXƒ^ƒ“ƒX‚Æ‚È‚è‚Ü‚·B
ƒfƒUƒCƒ“ƒpƒ^[ƒ“‚̃Tƒ“ƒvƒ‹‚È‚Ì‚ÅŽÀÛƒf[ƒ^‚ð‘€ì‚·‚鈗‚ÍŽÀ‘•‚µ‚Ä‚Ü‚¹‚ñB

DBMSAccess.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace Cancer.ADO
{
    public class DBMSAccess : DataBase
    {
        // ƒƒ“ƒo•Ï”
        private OleDbConnection _connection = null;
        private OleDbCommand _command = null;

        // ƒRƒ“ƒXƒgƒ‰ƒNƒ^
        public DBMSAccess()
        {
            _connection = new OleDbConnection();
            _command = new OleDbCommand();
        }

        // ƒvƒƒpƒeƒB
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }
    }
}

“¯‚¶‚悤‚ÉSQLServer‚âPostgreSQL‚à쬂µ‚Ü‚·B

DBSQLServer.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;

namespace Cancer.ADO
{
    public class DBSQLServer : DataBase
    {
        private SqlConnection _connection = null;
        private SqlCommand _command = null;

        public DBSQLServer()
        {
            _connection = new SqlConnection();
            _command = new SqlCommand();
        }
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }
    }
}

DBPostgreSQL.cs
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
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using Npgsql;

namespace Cancer.ADO
{
    public class DBPostgreSQL : DataBase
    {
        // ƒƒ“ƒo•Ï”
        private NpgsqlConnection _connection = null;
        private NpgsqlCommand _command = null;

        // ƒRƒ“ƒXƒgƒ‰ƒNƒ^
        public DBPostgreSQL()
        {
            _connection = new NpgsqlConnection();
            _command = new NpgsqlCommand();
        }

        // ƒvƒƒpƒeƒB
        public override DbConnection Connection
        {
            get
            {
                return _connection;
            }
        }
        public override DbCommand Command
        {
            get
            {
                return _command;
            }
        }

        // ƒƒ\ƒbƒh
        public override void Fill(DataSet ds)
        {
            using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
            {
                _command.Connection = _connection;
                adapter.SelectCommand = _command;
                adapter.Fill(ds);
            }
        }
        public override void UpDate(DataSet ds)
        {
            using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter())
            {
                _command.Connection = _connection;
                adapter.Update(ds);
            }
        }

    }
}

ŽŸ‚ÍFactoryƒNƒ‰ƒX‚ÌŽÀ‘•‚Æ‚È‚è‚Ü‚·B
FactoryƒNƒ‰ƒX‚𶬂µ‚ăƒ\ƒbƒh‚ðŽÀs‚Æ‚¢‚¤‚Ì‚ÍŽèŠÔ‚È‚Ì‚Å
ÓIƒƒ\ƒbƒh‚É‚µ‚Ä‚Ü‚·B‚Ü‚½ˆø”‚É•¶Žš—ñ‚ð—p‚¢‚é‚Æ“ü—̓~ƒX‚ª”­¶‚·‚é‚Ì‚Å
—ñ‹“’l‚ðÝ’è‚·‚é‚悤‚É‚µ‚Ä‚Ü‚·B

DBFactory.cs
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
using System;
using System.Collections.Generic;
using System.Text;

namespace Cancer.ADO
{
    public class DBFactory
    {
        // —ñ‹“‘Ì
        public enum DBType
        {
            MSAccess,
            SQLServer,
            PostgreSQL,
        }

        // ƒƒ\ƒbƒh
        public static DataBase DataBaseFactory(DBType type)
        {
            switch (type)
            {
                case DBType.MSAccess:
                    return new DBMSAccess();

                case DBType.SQLServer:
                    return new DBSQLServer();

                case DBType.PostgreSQL:
                    return new DBPostgreSQL();
            }
            return new DataBase();
        }
    }
}

‚Å‚ÍAŽÀÛ‚ÉŽg‚Á‚Ä‚Ý‚Ü‚·B
Ú‘±‚·‚éDB‚ÍPostgreSQL‚Æ‚µ‚Ü‚·B

ƒeƒXƒgƒR[ƒh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void button1_Click(object sender, EventArgs e)
{
    // PosgreSQLê—p‚̃Nƒ‰ƒX‚̃Cƒ“ƒXƒ^ƒ“ƒX‚ð쬂µ‚Ü‚·B
    // ‚±‚±‚Å‚ÍDBPostgreSQLƒNƒ‰ƒX‚Æ‚È‚è‚Ü‚·B
    DataBase db = DBFactory.DataBaseFactory(DBFactory.DBType.PostgreSQL);

    // DBPostgreSQLƒNƒ‰ƒX‚̃Cƒ“ƒXƒ^ƒ“ƒX‚ð쬂µ‚½Žž“_‚Å
    // ŠÖ˜AƒNƒ‰ƒX‚ÌDbConnection‚ÍNpgsqlConnection‚Ŷ¬‚µ‚Ü‚·B
    db.Connection.ConnectionString = "Server=localhost;Port=5432;User Id=[ƒ†[ƒU–¼];Password=[ƒpƒXƒ[ƒh];Database=[ƒf[ƒ^ƒx[ƒX–¼];";

    // Ú‘±‚µ‚Ü‚·B
    db.Open();

    // ƒf[ƒ^‚ðŽæ“¾‚µ‚Ü‚·B
    DataSet ds = new DataSet();
    db.Command.CommandText = "SELECT * FROM m_hyouhin";
    db.Fill(ds);

    // ‰ðœ‚µ‚Ü‚·B
    db.Close();
}

db.Connection.ConnectionString‚Ì[ƒ†[ƒU–¼][ƒpƒXƒ[ƒh][ƒf[ƒ^ƒx[ƒX]‚Í“K“–‚È’l‚ð‚¢‚ê‚Ä‚­‚¾‚³‚¢B
DBPostgreSQL‚ÍNpgsqlƒvƒƒoƒCƒ_[‚ð—˜—p‚µ‚Ä‚¢‚Ü‚·B
Npgsql‚̓lƒbƒg‚©‚çƒ_ƒEƒ“ƒ[ƒh‚µ‚Ä‚­‚¾‚³‚¢B
http://pgfoundry.org/frs/?group_id=1000140



Copyright (C) 2011 ”L‚Ì‹C‚Ü‚Ü‚ÈC#“ú‹L