コード例 #1
0
        public override void Validate()
        {
            base.Validate();

            if (this.IsValid)
            {
                string sku = (string)this.Value;

                // 1. Check to make sure the product sku is not empty
                if (string.IsNullOrEmpty(sku))
                {
                    this.IsValid      = false;
                    this.ErrorMessage = Resources.ProductCanNotBeEmpty;
                    return;
                }
                else
                {
                    // 2. Validate that the product sku is in the right format
                    RegexStringValidator regexStringValidator = new RegexStringValidator(ProductSkuRegEx);
                    try
                    {
                        regexStringValidator.Validate(sku);
                    }
                    catch (ArgumentException)
                    {
                        this.IsValid      = false;
                        this.ErrorMessage = Resources.InvalidProductSkuFormat;
                        return;
                    }

                    // 3. Validate that the product sku exists in the product catalog system
                    IProductCatalogRepository productCatalogRepository =
                        SharePointServiceLocator.Current.GetInstance <IProductCatalogRepository>();

                    if (productCatalogRepository.GetProductBySku(sku) == null)
                    {
                        this.IsValid      = false;
                        this.ErrorMessage = Resources.ProductNotFound;
                        return;
                    }
                }
            }
        }
コード例 #2
0
        public void LoadProduct(string sku)
        {
            try
            {
                // Write a trace message, that might help in debugging a particular problem. Trace messages should be directed
                // towards developers, and can contain deep technical information. In contrast, Eventlog messages are targeted to operations
                // and should be more actionable.
                logger.TraceToDeveloper(Resources.StartLoadingMessage);

                // Get the product catalog from the ServiceLocator. Because this presenter only knows about the IProductCatalog
                // interface, it can be tested in isolation. In the unit tests, you'll see a MockProductDatalogRepository being used.
                // The actual implementation is provided by the Contoso.LOB.Services.Client project. In the it's WebAppFeatureReceiver
                // You'll see how the actual ProductCatalogRepository is registered with the ServiceLocatorConfig.
                IProductCatalogRepository productCatalogRepository =
                    SharePointServiceLocator.Current.GetInstance <IProductCatalogRepository>();

                Product product = productCatalogRepository.GetProductBySku(sku);

                if (product == null || product.Sku == null)
                {
                    // Show an error message to the user.
                    new ViewExceptionHandler().ShowFunctionalErrorMessage(Resources.CouldNotFindProductInformation,
                                                                          this.ErrorVisualizer);
                }
                else
                {
                    this.view.Product = product;
                    this.view.DataBind();
                }

                logger.TraceToDeveloper(Resources.EndLoadProductMessage
                                        );
            }
            catch (Exception ex)
            {
                // If something goes wrong, make sure the error gets logged
                // and a non technical message is displayed to the user
                new ViewExceptionHandler().HandleViewException(ex, this.ErrorVisualizer,
                                                               Contoso.PartnerPortal.ProductCatalog.Properties.Resources.ProductDetailsErrorMessage);
            }
        }