mvc demo

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using DataAcess;
using Models;


namespace Repository
{

 

  public interface IProductsRepository
    {
        IList<Product> GetProductsList();
    }


    public class ProductsRepository : IProductsRepository
    {
        private string connectString = String.Empty;

        private IDbConnection dbConnection;

        public ProductsRepository(string connectstring)
        {
            connectString = connectstring;
            string ProviderName = ConfigurationManager.ConnectionStrings["connection"].ProviderName;
            if (dbConnection == null)
            {
                dbConnection = DbFactory.GetDbConnectionForm(ProviderName);
                dbConnection.ConnectionString = connectString;
            }
        }


        #region IProductsRepository Members

        public  IList<Product> GetProductsList()
        {
            List<Product> Products = new List<Product>();
            try
            {
                dbConnection.Open();
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message.ToString());
            }

            try
            {
                IDbCommand dbCommand = dbConnection.CreateCommand();
                dbCommand.CommandType = CommandType.Text;
                dbCommand.CommandText = "select * from  aProduct where id = 3 ";
                IDataReader dbReader = dbCommand.ExecuteReader();
                while (dbReader.Read())
                {
                    var Product = new Product();
                    {
                        Product.ID = Convert.ToInt32(dbReader["ID"]);
                        Product.Name = (string)dbReader["Name"];
                        Product.Category = (string)dbReader["Category"];
                        Product.Description = (string)dbReader["Description"];
                        Product.Package = (string)dbReader["Package"];
                        Product.Price = Convert.ToDouble(dbReader["Price"]);
                    }
                    Products.Add(Product);
                }
                dbReader.Close();
            }
            catch (Exception exception)
            {
                throw new Exception(exception.Message);
            }
            finally
            {
                dbConnection.Close();
            }
            return Products;
        }

        #endregion

 
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Models;


namespace IocDemo.Controllers
{
    public class ProductsController : Controller
    {
        //
        // GET: /Products/
        private IProductsRepository productsRepository;

        public ProductsController(IProductsRepository repository)
        {
            productsRepository = repository;
        }


        public ActionResult Index()
        {
            var Products = productsRepository.GetProductsList();

            return View(Products);
        }
      

    }
}

 

    public static class DbFactory
    {

        static IDbConnection DbConnection = null;

        public static IDbConnection GetDbConnectionForm(string providerName)
        {
            if (DbConnection == null)
            {
                DbProviderFactory factory = DbProviderFactories.GetFactory(providerName);
                DbConnection = factory.CreateConnection();
            }
            return DbConnection;
        }


    }

 

 

 

  public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public string Category { get; set; }
        public string Description { get; set; }

        public string Package { get; set; }
        public double Price { get; set; }
    }

posted @ 2012-04-01 19:51  rayray2  阅读(143)  评论(0编辑  收藏  举报