More working tests.
[castle.git] / Samples / InversionOfControl / AnotherExtendingSample / Dao / BlogDao.cs
blobb8a639d77cbf681d989fde4a8e18ab9802951e0a
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Data;
19 using System.Collections;
21 [Transactional]
22 public class BlogDao
24 private IConnectionFactory _connFactory;
26 public BlogDao(IConnectionFactory connFactory)
28 _connFactory = connFactory;
31 [RequiresTransaction]
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
39 command.CommandText =
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);
47 return blog;
50 [RequiresTransaction]
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();
62 command.CommandText =
63 String.Format("Select id, name, author from blogs where id={0} order by id", id);
65 using (IDataReader reader = command.ExecuteReader())
67 if(reader.Read())
69 return new Blog(
70 reader.GetInt32(0), reader.GetString(1), reader.GetString(2) );
72 else
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())
95 while(reader.Read())
97 Blog blog = new Blog(
98 reader.GetInt32(0), reader.GetString(1), reader.GetString(2) );
99 list.Add(blog);
104 return list;