Magelia WebStore

  • Summary

How to use the WebStoreClient Class

Overview: concept and basic samples

Download the code sample of this tutorial:
How to use the WebStoreContext class - ZIP - 25.1 mo

During installation of the NuGet package, a new class WebStoreContext was added in the Magelia file of your project. This WebStoreContext class was specially created to help you with necessary interactions with the different Magelia WebStore services.

FYI: During instantiation, the constructor will automatically base itself on the WebStoreContextSettings that is defined in the Web.Config file, in order to determine which StoreIDs and specific service definitions to take into account for each customer. For more information on the necessary configuration of WebStoreContextSettings, consult the How to use Magelia WebStore in a Visual Studio project page.

public WebStoreContext()
    : this(new WebStoreContextSettings())
{ }
public WebStoreContext(WebStoreContextSettings settings)
    : base(settings)
{ }

In the following example, we retrieve the top ten customers.

using (WebStoreContext client = new WebStoreContext())
{
    int nbResult = 0;
    foreach (var customer in client.CustomerClient.GetAllCustomers(0, 10, out nbResult))
    {
        Console.WriteLine(customer.UserName);
    }
}

In this second sample, we retrieve all the available catalogs in terms of our store and display their names in an application console.

using (WebStoreContext client = new WebStoreContext())
{
    foreach (var catalog in client.GetCatalogClient().Catalogs)
    {
        Console.WriteLine(catalog.Name);
    }
}

FYI: We recommended the using directive, so that the WebStoreContext is instantiated then closed after our code block.

Warning: As you can see, there is a difference in these two samples. For customers we use the CustomerClient object that uses the WCF service CustomerServiceClient, but for catalogs we use a method call, GetCatalogClient, to retrieve the CatalogServiceClient object that is an OData service. For more information, refer to the following sections: How to use Magelia WebStore in a Visual Studio project and How to access catalog information.

If you take a detailed look at the class WebStoreContext, you will notice that a certain number of code is put in commentary. This code can be un-commented to facilitate even more the use of Magelia WebStore through the different functionalities already ready for you. These functionalities are adapted to a web context only, we recommend using these functionalities when possible.

When you use these additional functionalities, you will need to add the call to the Initialize method in the WebStoreContext class’s constructor, as in the following example:

public WebStoreContext()
    : this(new WebStoreContextSettings())
{ }
public WebStoreContext(WebStoreContextSettings settings)
    : base(settings)
{ 
    this.Initialize();
}

Thus by de-commenting this code, you will benefit from supplementary methods and properties for interacting with the Magelia WebStore services.

using (WebStoreContext client = new WebStoreContext())
{
    foreach (var catalog in client.CatalogClient.Catalogs)
    {
        Console.WriteLine(catalog.Name);
    }
}

FYI: among the best practices, we advise you to create your own Context by creating a class that will inherit from WebStoreContext. Thus you will be able to more easily put your personalized code in your context while remaining independent of the one created during the installation of Magelia WebStore client library. Note: the following examples will use a personalized Context like in the samples below.

using Magelia.WebStore.Client.Web.UI.Sample.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace Magelia.WebStore.Client.Web.UI.Sample.Runtime
{
    public class SampleSiteContext : WebStoreContext
    {
        #region Constants
 
        public static readonly IEnumerable<SelectListItem> Titles = new SelectListItem[] { 
            new SelectListItem{ Value = 1.ToString(), Text = Global.MissTitle },
            new SelectListItem{ Value = 2.ToString(), Text = Global.MrsTitle },
            new SelectListItem{ Value = 3.ToString(), Text = Global.MTitle }
        };
 
        #endregion
 
        #region Constructors
 
        public SampleSiteContext()
            : base()
        { }
        public SampleSiteContext(WebStoreContextSettings settings)
            : base(settings)
        { }
 
        #endregion
    }
}

You may also create a base controller that will implement common functionalities across the entirety of your controllers as a property for your context.

using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
 
namespace Magelia.WebStore.Client.Web.UI.Sample.Runtime.MVC
{
    public class BaseController : Controller, IDisposable
    {
        public BaseController(SampleSiteContext sampleSiteContext)
        {
            this.SampleSiteContext = new SampleSiteContext();
        }
 
        public SampleSiteContext SampleSiteContext { get; private set; }
 
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);
            this.SetRegionalInformation();
        }
 
        private void SetRegionalInformation()
        {
            Thread.CurrentThread.CurrentCulture = this.SampleSiteContext.Culture;
            Thread.CurrentThread.CurrentUICulture = this.SampleSiteContext.Culture;
 
            Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol = this.SampleSiteContext.Currency.Symbol;
            Thread.CurrentThread.CurrentUICulture.NumberFormat.CurrencySymbol = this.SampleSiteContext.Currency.Symbol;
        }
 
        public void Dispose()
        {
              this.SampleSiteContext.Dispose(); 
        }
    }
}

Now your controllers will no longer inherit from Controller but from BaseController.

public class AccountController : BaseController
{
    ...
}

How to use WebStoreClient with Ninject

FYI: Ninject is a lightweight dependency injection framework for .NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner. By using Ninject to support your software’s architecture, your code will become easier to write, reuse, test, and modify. For more information about Ninject, please visit http://www.ninject.org/.

To install Ninject, the simplest method is to use NuGet. In the context of the examples, you will need to install the following packages:
• Ninject
• Ninject.MVC3
• Ninject.Web.Common

To install Ninject

After creating a new MVC3 application, run Install-Package Ninject.MVC3 from the package manager console. Make sure that you are using NuGet 1.6 or higher. Using an older version will result in missing references. This will download and install all required assemblies and add some source code to integrate it into the application. After the installation you will find the NinjectWebCommon class in the App_Start folder.

Modify the CreateKernel method to create the kernel as you need it in your application. For almost all applications no change will be necessary here. Now load your modules or define bindings in the RegisterServices method.

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<SampleSiteContext>().ToSelf().InRequestScope();
}     

Now you will no longer have to worry about the instantiation of your context: Ninject takes care of this for you.

FYI: From now on, the different examples will use Ninject.

Download the code sample of this tutorial:
How to use the WebStoreContext class - ZIP - 25.1 mo