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();
}
}