Implementing the manager class

To create a manager class for your module, you must perform the following:

  1. From the context menu of the folder Data in the ProductsModule project, click Add » Class.
  2. Name the class file ProductsManager.cs and click Add.
  3. Open the file ProductsManager.cs.
  4. Add the following using statements:

    using ProductsModule.Configuration;
    using ProductsModule.Model;
    using Telerik.Sitefinity.Configuration;
    using Telerik.Sitefinity.Data;
    using Telerik.Sitefinity.Data.ContentLinks;
    using Telerik.Sitefinity.GenericContent.Model;
    using Telerik.Sitefinity.Lifecycle;
    using Telerik.Sitefinity.Modules;
    using Telerik.Sitefinity.Modules.GenericContent;
  5. Make the ProductsManager class inherit the ContentManagerBase<ProductsDataProvider> class and to implement the ILifecycleManagerinterface:

    public class ProductsManager : ContentManagerBase<ProductsDataProvider>, ILifecycleManager
    {
    }
  6. Create the following constructors in the class:

    public ProductsManager()
        : this(null)
    {
    }
     
    public ProductsManager(string providerName)
        : base(providerName)
    {
    }
     
    public ProductsManager(string providerName, string transactionName)
        : base(providerName, transactionName)
    {
    }
  7. Create the following static methods:

    public static ProductsManager GetManager()
    {
        return ManagerBase<ProductsDataProvider>.GetManager<ProductsManager>();
    }
     
    public static ProductsManager GetManager(string providerName)
    {
        return ManagerBase<ProductsDataProvider>.GetManager<ProductsManager>(providerName);
    }
     
    public static ProductsManager GetManager(string providerName, string transactionName)
    {
        return ManagerBase<ProductsDataProvider>.GetManager<ProductsManager>(providerName, transactionName);
    }
  8. Wire up the manager class to the data provider class by creating the following methods:

    public ProductItem CreateProduct()
    {
        return this.Provider.CreateProduct();
    }
     
    public ProductItem CreateProduct(Guid id)
    {
        return this.Provider.CreateProduct(id);
    }
     
    public virtual ProductItem CheckOut(ProductItem productItem)
    {
        return (ProductItem)this.Lifecycle.CheckOut(productItem);
    }
     
    public virtual ProductItem CheckIn(ProductItem productItem)
    {
        return (ProductItem)this.Lifecycle.CheckIn(productItem);
    }
     
    public ProductItem GetProduct(Guid id)
    {
        return this.Provider.GetProduct(id);
    }
     
    public IQueryable<ProductItem> GetProducts()
    {
        return this.Provider.GetProducts();
    }
     
    public void DeleteProduct(ProductItem product)
    {
        this.Provider.DeleteProduct(product);
    }
     
    public void DeleteProduct(Guid id)
    {
        this.Provider.DeleteProduct(this.Provider.GetProduct(id));
    }
     
    public ILifecycleDecorator Lifecycle
    {
        get
        {
            return LifecycleFactory.CreateLifecycle<ProductItem>(this, this.Copy);
        }
    }
     
    public void Copy(Telerik.Sitefinity.GenericContent.Model.Content source, Telerik.Sitefinity.GenericContent.Model.Content destination)
    {
        if (source == null)
        {
            throw new ArgumentNullException("source");
        }
     
        if (destination == null)
        {
            throw new ArgumentNullException("destination");
        }
     
        var productSource = source as ProductItem;
        var productDestination = destination as ProductItem;
        if (productSource == null || productDestination == null)
        {
            throw new ArgumentException("Source and destination must be of the same type");
        }
     
        this.Copy(productSource, productDestination);
    }
     
    public void Copy(ProductItem source, ProductItem destination)
    {
        destination.Urls.ClearDestinationUrls(source.Urls, this.Delete);
        source.Urls.CopyTo(destination.Urls, destination);
     
        ContentLinksExtensions.CopyContentLink("ProductImage", source, destination);
     
        destination.Price = source.Price;
        destination.QuantityInStock = source.QuantityInStock;
    }
  9. Implement the abstract methods of the ContentManagerBase class in the following way:

    public override IQueryable<TItem> GetItems<TItem>()
    {
        if (typeof(ProductItem).IsAssignableFrom(typeof(TItem)))
        {
            return this.GetProducts() as IQueryable<TItem>;
        }
     
        if (typeof(TItem) == typeof(UrlData) || typeof(TItem) == typeof(ProductItemUrlData))
        {
            return this.GetUrls<ProductItemUrlData>() as IQueryable<TItem>;
        }
     
        if (typeof(TItem) == typeof(Comment))
        {
            return this.GetComments() as IQueryable<TItem>;
        }
     
        throw new NotSupportedException();
    }
     
    protected override GetDefaultProvider DefaultProviderDelegate
    {
        get
        {
            return () => Config.Get<ProductsConfig>().DefaultProvider;
        }
    }
     
    public override string ModuleName
    {
        get
        {
            return ProductsModule.ModuleName;
        }
    }
     
    protected override ConfigElementDictionary<string, DataProviderSettings> ProvidersSettings
    {
        get
        {
            return Config.Get<ProductsConfig>().Providers;
        }
    }
    public LanguageData CreateLanguageData()
    {
        return this.CreateLanguageData();
    }
     
    public LanguageData CreateLanguageData(Guid id)
    {
        return this.Provider.CreateLanguageData(id);
    }
     
    public LanguageData GetLanguageData(Guid id)
    {
        return this.Provider.GetLanguageData(id);
    }

    NOTE: You will implement the ModuleName constant in the module class later in this tutorial. For more information, see Implementing the module class.

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK