1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Extend TelerikProfileProvider with custom type

Extend TelerikProfileProvider with custom type

3 posts, 0 answered
  1. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    14 Apr 2010
    Link to this post
    Hi team,

    Adding my shopping cart i have some troubles during using at runtime. The profile providers is not returning my custom type and i can´t run my shopping basket in profile. I search to extend TelerikProfileProvider to add one custom type returned to be able to cast the new object. Actually i have one error like unable to cast string type to MyType at runtime.

    <add name="ShCart" serializeAs="Binary" type="Cart" allowAnonymous="true"/>

    using System.Web.Profile;
    /// <summary>
    /// Summary description for ProfileExtension
    /// </summary>
    public partial class ProfileExtension : TelerikProfileProvider
    {
        public Cart Cart
        {
            get { return (Cart)base.GetPropertyValues("ShCart"); }//error!
        }
    }

    The Cart is well serialized
    sample like : this tip

    Could you explian how to extend the TelerikProfileProvider without use a new provider to extend the base adding one simple getting type.

    Thanks. Romi
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    14 Apr 2010
    Link to this post
    Hello Pierre,

    You call GetPropertyValues and pass a string value, since your object requires another type which is not a string.
    We use Xml and String serialization. There are two more types that we do not support.

    •  String,
    •  Xml
    •  Binary
    •  ProviderSpecific

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. Pierre
    Pierre avatar
    433 posts
    Registered:
    16 Feb 2006
    14 Apr 2010
    Link to this post
    Hi Ivan,

    My Cart object implement some others simple types but i searching to create one exterder from TelkerikProfileProvider to handle-it. May be the solution is storing in one external content like other nolics object for binaries and retrieve the Cart object overridingSet,GetPropertyValues.

    object
    /// <summary>
        /// Summary description for ShoppingCart
        /// </summary>
        [Serializable]
        public class CartItem
        {
     
            private int _productID;//ID[AutoGenID=True];
            private string _productName;//ProductName[Length=30];
            private string _basevista;
            private string _imageUrl;//ImageUrl[Length=50];
            private int _quantity;
            private double _price;//PriceClp[Length=10];
            private double _promo;//Promo[Length=10];
            private string _devise;//MoneyCode[Length=10];
            private double _subTotal;
     
            public CartItem()
            {
            }
            public CartItem(int ProductID, string ProductName, string ImageUrl, int Quantity, double Price, string Devise,string BaseVista, double Promo)
            {
                _productID = ProductID;
                _productName = ProductName;
                _imageUrl = ImageUrl;
                _quantity = Quantity;
                _price = Price;
                _promo = Promo;
                _devise = Devise;
                _basevista = BaseVista;
                _subTotal = Quantity * Price;
     
            }
            public int ProductID
            {
                get
                {
                    return _productID;
                }
                set
                {
                    _productID = value;
                }
            }
            public string BaseVista
            {
                get { return _basevista; }
                set { _basevista = value; }
            }
             public string ProductName
             {
                get { return _productName; }
                set { _productName = value; }
             }
     
             public string ImageUrl
             {
                 get { return _imageUrl; }
                 set { _imageUrl = value; }
             }
     
             public int Quantity
             {
                 get { return _quantity; }
                 set { _quantity = value; }
             }
     
             public double Price
             {
                 get { return _price; }
                 set { _price = value; }
             }
            public double Promo
            {
                get
                {
                    return _promo;
                }
                set { _promo = value; }
            }  
             public string Devise
             {
                 get { return _devise; }
                 set { _devise = value; }
             }
             public double SubTotal
             {
                 get
                 {
                     if (_promo == null)
                         return _quantity * _price;
                     else
                         return _quantity * _price * (1.0 - _promo / 100);  
                 }
             }
        
         }
        [Serializable]
        public class Cart
        {
            private DateTime _dateCreated;
            private DateTime _lastUpdate;
            private List<CartItem> _items;
     
            public Cart()
            {
                if (this._items == null)
                {
                    this._items = new List<CartItem>();
                    this._dateCreated = DateTime.Now;
                }
            }
     
            public List<CartItem> Items
            {
            get { return _items;}
            set { _items = value;}
            }
     
            public void Insert(int ProductID, double Price, int Quantity, string ProductName, string ImageUrl, string Devise, string BaseVista, double Promo)
            {
                int ItemIndex = ItemIndexOfID(ProductID);
                if (ItemIndex == -1)
                {
                    CartItem NewItem = new CartItem();
                    NewItem.ProductID = ProductID;
                    NewItem.Quantity = Quantity;
                    NewItem.Price = Price;
                    NewItem.Devise = Devise;
                    NewItem.ProductName = ProductName;
                    NewItem.ImageUrl = ImageUrl;
                    NewItem.BaseVista = BaseVista;
                    NewItem.Promo = Promo;
                    _items.Add(NewItem);
                }
                else
                {
                    _items[ItemIndex].Quantity += 1;
                }
                _lastUpdate = DateTime.Now;
            }
     
            public void Update(int RowID, int ProductID, int Quantity, double Price, double Promo)
            {
                CartItem Item = _items[RowID];
                Item.ProductID = ProductID;
                Item.Quantity = Quantity;
                Item.Price = Price;
                Item.Promo = Promo;
                _lastUpdate = DateTime.Now;
            }
     
            public void DeleteItem(int rowID)
            {
                _items.RemoveAt(rowID);
                _lastUpdate = DateTime.Now;
            }
     
            private int ItemIndexOfID(int ProductID)
            {
                int index = 0;
                foreach (CartItem item in _items)
                {
                    if (item.ProductID == ProductID)
                    {
                        return index;
                    }
                    index += 1;
                }
                return -1;
            }
     
            public double Total
            {
                get
                {
                    double t = 0;
                    if (_items == null)
                    {
                        return 0;
                    }
                    foreach (CartItem Item in _items)
                    {
                        t += Item.SubTotal;
                    }
                    return t;
                }
            }
    Basic implementation to play around
    public partial class ProfileExtension : TelerikProfileProvider
    {
        public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection properties)
        {
            SettingsPropertyValueCollection _settingsValues = new SettingsPropertyValueCollection();
            string userName = (string)context("UserName");
            //Get with username the Scart Object and return to use from somewhere
            Cart Scartobj = GetSCartByUsername(userName); //Basic
            //
            foreach (SettingsProperty _property in properties)
            {
                SettingsPropertyValue _settingsPropertyValue = new SettingsPropertyValue(_property);
                if(_property.Name=="SCart")
                   _settingsPropertyValue.PropertyValue = Scartobj;//from static file or object
                _settingsValues.Add(_settingsPropertyValue);
            }
            return _settingsValues;
        }
        public override void  SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection properties)
        {
             base.SetPropertyValues(context, properties);//....to make
        }
    }

    Others workaround?
    Thanks.
Register for webinar
3 posts, 0 answered