MVP5层架构

1.TestWebUI

 public partial class _Default : System.Web.UI.Page, IProductListView
    {
        private ProductListPresenter _presenter;

        protected void Page_Init(object sender, EventArgs e)
        {
            _presenter = new ProductListPresenter(this, ObjectFactory.GetInstance<TestService.ProductService>());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != true)
                _presenter.Display(); 
        }

        public void Display(IList<TestService.ProductViewModel> Products)
        {
            rptProducts.DataSource = Products;
            rptProducts.DataBind(); 
        }

        public string ErrorMessage
        {
            set { lblErrorMessage.Text = String.Format("<p><strong>Error</strong><br/>{0}<p/>", value); }
        }
    }

2.TestPresentation

    public interface IProductListView
    {
        void Display(IList<ProductViewModel> Products);
        string ErrorMessage { set; }
    }


 

  public class ProductListPresenter
    {
        private IProductListView _productListView;
        private TestService.ProductService _productService;

        public ProductListPresenter(IProductListView ProductListView, TestService.ProductService ProductService)
        {
            _productService = ProductService;
            _productListView = ProductListView;
        }

        public void Display()
        {
            ProductListRequest productListRequest = new ProductListRequest();
            ProductListResponse productResponse = _productService.GetAllProductsFor(productListRequest);

            if (productResponse.Success)
            {
                _productListView.Display(productResponse.Products);
            }
            else
            {
                _productListView.ErrorMessage = productResponse.Message;
            }

        }
    }


 

3.TestService

  public class ProductListRequest
    {
        
    }


 

  public class ProductListResponse
    {
        public bool Success { get; set; }
        public string Message { get; set; }
        public IList<ProductViewModel> Products { get; set; }
    }


 

    public static class ProductMapperExtensionMethods
    {
        public static IList<ProductViewModel> ConvertToProductListViewModel(this IList<Product> products)
        {
            IList<ProductViewModel> productViewModels = new List<ProductViewModel>();

            foreach (Product p in products)
            {
                productViewModels.Add(p.ConvertToProductViewModel());
            }

            return productViewModels;
        }

        public static ProductViewModel ConvertToProductViewModel(this Product product)
        {
            ProductViewModel productViewModel = new ProductViewModel();
            productViewModel.ProductId = product.Id;
            productViewModel.Name = product.Name;
            return productViewModel;
        }
    }


 

    public class ProductService
    {
        private TestModel.ProductService _productService;

        public ProductService(TestModel.ProductService ProductService)
        {
            _productService = ProductService;
        }

        public ProductListResponse GetAllProductsFor(ProductListRequest productListRequest)
        {
            ProductListResponse productListResponse = new ProductListResponse();

            try
            {
                IList<Product> productEntities = _productService.GetAllProductsFor();

                productListResponse.Products = productEntities.ConvertToProductListViewModel();
                productListResponse.Success = true;
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                // Log the exception...

                productListResponse.Success = false;
                // Return a friendly error message
                productListResponse.Message = "Check that your database is in the correct place. Hint: Check the AttachDbFilename section within App.config in the project ASPPatterns.Chap3.Layered.Repository.";
            }
            catch (Exception ex)
            {
                // Log the exception...

                productListResponse.Success = false;
                // Return a friendly error message
                productListResponse.Message = "An error occured";
            }

            return productListResponse;
        }

    }


 

   public class ProductViewModel
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
    }


 

4.TestModel

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


 

  public class ProductService
    {
        private IProductRepository _productRepository;

        public ProductService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        public IList<Product> GetAllProductsFor()
        {
            return _productRepository.FindAll(); 
        }
    }


 

  public interface IProductRepository
    {
        IList<Product> FindAll();
    }


 

5.TestRepository

    public class ProductRepository : IProductRepository
    {
        public IList<Product> FindAll()
        {
            IList<Product> products = new List<Product>();
            products.Add(new Product { Id = 1, Name = "liufuchu" });
            return products.ToList();
        }
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值