views:

31

answers:

1

The end goal for this post is to override the ToString() method of a concrete implementation of a generic base class while still being able to search the implementation using Linq flattening technique. So if you read this and see a better way let me know. I'm using Telerik controls for Silverlight and they won't change their api to allow some of their control properties to be data-bound and instead rely on the ToString() method of whatever object they are bound to. yea, stupid.. Anyway here is what I've got.

RadTreeView control on my page. The FullPath property of each node in the treeview uses the ToString() method of each item its bound to (so this is what I need to override).

I had to create an "intermediary" class to enhance my base model class so it can be bound as a heirarchy in the tree view and then a concrete implementation of that generic class to override ToString(). Now the problem is I have a Linq extension that explodes because it cannot convert the concrete implementation back to the base generic class. I love generics but this is too much for me. Need help on solving the extension method issue.

Intermediary generic class:

public class HeirarchicalItem<T> : NotifyPropertyChangedBase, INotifyCollectionChanged where T : class
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;

    public virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs ea)
    {
        if (CollectionChanged != null)
            CollectionChanged(this, ea);
    }

    public HeirarchicalItem() { }

    public HeirarchicalItem(T item)
    {
        Item = item;
    }

    public HeirarchicalItem(IEnumerable<T> collection)
    {
        CopyFrom(collection);
    }

    private T _item;
    public T Item
    {
        get
        {
            return _item;
        }
        set
        {
            _item = value;
            RaisePropertyChanged<HeirarchicalItem<T>>(a => a.Item);
        }
    }

    private ObservableCollection<HeirarchicalItem<T>> _children = new ObservableCollection<HeirarchicalItem<T>>();
    public virtual ObservableCollection<HeirarchicalItem<T>> Children
    {
        get { return _children; }
        set
        {
            _children = value;
            RaisePropertyChanged<HeirarchicalItem<T>>(a => a.Children);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }

    private void CopyFrom(IEnumerable<T> collection)
    {
        if ((collection != null))
        {
            using (IEnumerator<T> enumerator = collection.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    HeirarchicalItem<T> newHeirarchicalItem = new HeirarchicalItem<T>(enumerator.Current);
                    Children.Add(newHeirarchicalItem);
                    RaisePropertyChanged<HeirarchicalItem<T>>(a => a.Children);
                    OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
                }
            }
        }
    }
}

Base model class: (data is shuttled to and from WCF Ria service using this class)

public class tbl_Path : EntityBase, IFullPath, IEquatable<tbl_Path>, IEqualityComparer<tbl_Path>
{
    public tbl_Path();
    public int GetHashCode(tbl_Path obj);
    public override string ToString();

    public DateTime CreateDate { get; set; }
    public short Depth { get; set; }
    public string FullPath { get; set; }
    public bool IsAuthorized { get; set; }
    public bool IsSelected { get; set; }
    public string Name { get; set; }
    public override IEnumerable<Operation> Operations { get; }
    public int? ParentPathID { get; set; }
    public int PathID { get; set; }
    public Guid SecurityKey { get; set; }
    public EntityCollection<tbl_Configuration> tbl_Configuration { get; set; }
    public EntityCollection<tbl_Key> tbl_Key { get; set; }
    public EntityCollection<tbl_SecurityACL> tbl_SecurityACL { get; set; }
    public EntityCollection<tbl_SecurityInheriting> tbl_SecurityInheriting { get; set; }
    public EntityCollection<tbl_Variable> tbl_Variable { get; set; }
}

Concrete Implementation so that I can override ToString():

public class HeirarchicalPath : HeirarchicalItem<tbl_Path>
{
    public HeirarchicalPath()
    {

    }
    public HeirarchicalPath(tbl_Path item)
        : base(item)
    {

    }
    public HeirarchicalPath(IEnumerable<tbl_Path> collection)
        : base(collection)
    {

    }

    public override string ToString()
    {
        return Item.Name; **// we override here so Telerik is happy**
    }
}

And finally here is the Linq extension method that explodes during compile time because I introduced a concrete implementation of my generic base class.

    public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse)
    {
        foreach (T item in source)
        {
            yield return item;

            IEnumerable<T> seqRecurse = fnRecurse(item);

            if (seqRecurse != null)
            {
                foreach (T itemRecurse in Traverse(seqRecurse, fnRecurse))
                {
                    yield return itemRecurse;
                }
            }
        }
    }

Actual code that is breaking: (x.Children is highlighted with the error)

Cannot implicitly convert type 
'System.Collections.ObjectModel.ObservableCollection<HeirarchicalItem<tbl_Path>>' to 
'System.Collections.Generic.IEnumerable<HeirarchicalPath>'. An explicit conversion 
exists (are you missing a cast?)


            HeirarchicalPath currentItem = this.Paths.Traverse(x => x.Children).Where(x => x.Item.FullPath == "$/MyFolder/Hello").FirstOrDefault();
A: 

Figured it out. Been working on this all day and minutes after posting the question I resolve it as always.

Just needed to add this bit to my concrete implementation and no more compiler errors.

    private ObservableCollection<HeirarchicalPath> _children = new ObservableCollection<HeirarchicalPath>();
    public new ObservableCollection<HeirarchicalPath> Children
    {
        get
        {
            return _children;
        }
        set
        {

            if (value == null)
                return;

            _children = value;
            RaisePropertyChanged<HeirarchicalPath>(a => a.Children);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
    }
JoeyBradshaw