1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Extending2
.Dao
19 using System
.Collections
;
24 private IConnectionFactory _connFactory
;
26 public BlogDao(IConnectionFactory connFactory
)
28 _connFactory
= connFactory
;
32 public virtual Blog
Create(Blog blog
)
34 using(IDbConnection conn
= _connFactory
.CreateConnection())
36 IDbCommand command
= conn
.CreateCommand();
38 // Not the best way, but the simplest
40 String
.Format("INSERT INTO blogs (name, author) values ('{0}', '{1}');select @@identity",
41 blog
.Name
, blog
.Author
);
43 object result
= command
.ExecuteScalar();
44 blog
.Id
= Convert
.ToInt32(result
);
51 public virtual void Delete(String name
)
53 // We pretend to delete the blog here
56 public virtual Blog
Find(int id
)
58 using(IDbConnection conn
= _connFactory
.CreateConnection())
60 IDbCommand command
= conn
.CreateCommand();
63 String
.Format("Select id, name, author from blogs where id={0} order by id", id
);
65 using (IDataReader reader
= command
.ExecuteReader())
70 reader
.GetInt32(0), reader
.GetString(1), reader
.GetString(2) );
74 throw new ApplicationException("Blog not found");
80 public virtual IList
Find()
82 // As you see, no transaction involved here
84 IList list
= new ArrayList();
86 using(IDbConnection conn
= _connFactory
.CreateConnection())
88 IDbCommand command
= conn
.CreateCommand();
90 // Not the best way, but the simplest
91 command
.CommandText
= "Select id, name, author from blogs";
93 using (IDataReader reader
= command
.ExecuteReader())
98 reader
.GetInt32(0), reader
.GetString(1), reader
.GetString(2) );