Note that Blog is not a component With a different design the Blog could be made into a component, but we would use a factory pattern to keep things tidy. The factory would internally invoke the container, but for our application perspective, its just an ordinary factory.
Ejemplo n.º 1
0
		public virtual IList Find(Blog blog)
		{
			// As you see, no transaction involved here

			IList list = new ArrayList();

			using(IDbConnection conn = _connFactory.CreateConnection())
			{
				IDbCommand command = conn.CreateCommand();

				// Not the best way, but the simplest
				command.CommandText = 
					String.Format("select id, title, contents from posts where blogid = {0} " + 
						"order by id", blog.Id);

				using (IDataReader reader = command.ExecuteReader())
				{
					while(reader.Read())
					{
						Post post = new Post( 
							reader.GetInt32(0), blog, reader.GetString(1), reader.GetString(2) );
						list.Add(post);
					}
				}
			}

			return list;
		}
Ejemplo n.º 2
0
		public virtual Blog Create(Blog blog)
		{
			using(IDbConnection conn = _connFactory.CreateConnection())
			{
				IDbCommand command = conn.CreateCommand();

				// Not the best way, but the simplest
				command.CommandText = 
					String.Format("INSERT INTO blogs (name, author) values ('{0}', '{1}');select @@identity", 
					blog.Name, blog.Author);

				object result = command.ExecuteScalar();
				blog.Id = Convert.ToInt32(result);
			}

			return blog;
		}
Ejemplo n.º 3
0
		public Post(int id, Blog blog, string title, string contents) : this(blog, title, contents)
		{
			_id = id;
		}
Ejemplo n.º 4
0
		public Post(Blog blog, string title, string contents)
		{
			_blog = blog;
			_title = title;
			_contents = contents;
		}
Ejemplo n.º 5
0
		public virtual IList Find()
		{
			// As you see, no transaction involved here

			IList list = new ArrayList();

			using(IDbConnection conn = _connFactory.CreateConnection())
			{
				IDbCommand command = conn.CreateCommand();

				// Not the best way, but the simplest
				command.CommandText = "Select id, name, author from blogs";

				using (IDataReader reader = command.ExecuteReader())
				{
					while(reader.Read())
					{
						Blog blog = new Blog( 
							reader.GetInt32(0), reader.GetString(1), reader.GetString(2) );
						list.Add(blog);
					}
				}
			}

			return list;
		}