DCSIMG
August 2008 - Posts - Gady Elkarif's Blog

Gady Elkarif's Blog

August 2008 - Posts

Acyclic Visitor Design Pattern

Acyclic Visitor Design Pattern

The Visitor Design Pattern allow new functions to be added to existing class hierarchies without affecting those hierarchies. The Acyclic Visitor Design Pattern is saved you from implementing some unrequired methods using downcasting.

The following code sample display 3 elements: Element1, Element2 and Element3, and two concrete visitors: Visitor12 and Visitor3.

The Acyclic Visitor Design Pattern is preventing you from implement method Visit(Element3) in Visitor12, and methods Visit(Elemen1), Visit(element2) in Visitor3.

The Source Code

    public interface IVisitor

    {

    }

 

    public interface IElement1Visitor

    {

        void Visit(Element1 element);

    }

 

    public interface IElement2Visitor

    {

        void Visit(Element2 element);

    }

 

    public interface IElement3Visitor

    {

        void Visit(Element3 element);

    }

 

    public class Visitor12 : IVisitor, IElement1Visitor, IElement2Visitor

    {

        public void Visit(Element1 element)

        {

 

        }

 

        public void Visit(Element2 element)

        {

 

        }

    }

 

    public class Visitor3 : IVisitor, IElement3Visitor

    {

        public void Visit(Element3 element)

        {

 

        }

    }

 

    public abstract class AbstractElement

    {

        public abstract void Accept(IVisitor visitor);

    }

 

    public class Element1 : AbstractElement

    {

        public override void Accept(IVisitor visitor)

        {

            IElement1Visitor element1Visitor = visitor as IElement1Visitor;

            if (element1Visitor == null)

            {

                return;

            }

 

            element1Visitor.Visit(this);

        }

    }

 

    public class Element2 : AbstractElement

    {

        public override void Accept(IVisitor visitor)

        {

            IElement2Visitor element2Visitor = visitor as IElement2Visitor;

            if (element2Visitor == null)

            {

                return;

            }

 

            element2Visitor.Visit(this);

        }

    }

 

    public class Element3 : AbstractElement

    {

        public override void Accept(IVisitor visitor)

        {

            IElement3Visitor element3Visitor = visitor as IElement3Visitor;

            if (element3Visitor == null)

            {

                return;

            }

 

            element3Visitor.Visit(this);

        }

    }

 


Posted: Aug 19 2008, 03:29 AM by egady | with 1 comment(s)
תגים:,

Serializable CollectionBase Class

Serializable CollectionBase Class

The idea of class CollectionBase is to have a base class which is based on list for serialization, and also on dictionary for fast retrieval.

So I have tried to create class CollectionBase class which is serializable, and when I try to serialize it with XmlSerializable, I got the following runtime exception:

"To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance
hierarchy."

The following sample code show the correct implementation of CollectionBase:

CollectionBase Usage

public class MyDerivedClass : CollectionBase<MyItem>

{

 

}

 

public class MyItem: IIdentifier

{

    public string Key

    {

        get;

        set;

    }

}

 

 

CollectionBase Implementation

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace MyCollections

{

    public interface IIdentifier

    {

        string Key

        {

            get;

            set;

        }

    }

 

    public class CollectionBase<TItem> : IList<TItem> where TItem : class, IIdentifier

    {

        private IDictionary<string, TItem> _itemsMap = new Dictionary<string, TItem>();

 

        private List<TItem> _items = new List<TItem>();

 

        public virtual void Add(Object item)

        {

            TItem ii = item as TItem;

 

            if (ii == null)

            {

                return;

            }

            this[ii.Key] = ii;

        }

 

 

        public TItem this[string key]

        {

            get

            {

                return _itemsMap[key];

            }

            set

            {

                try

                {

                    if (value == null)

                    {

                        return;

                    }

                    _items.Add(value);

 

                    _itemsMap[value.Key] = value;

                }

                catch (ArgumentOutOfRangeException)

                {

 

                }

            }

        }

 

        #region IList<TItem> Members

 

        int IList<TItem>.IndexOf(TItem item)

        {

            return _items.IndexOf(item);

        }

 

        void IList<TItem>.Insert(int index, TItem item)

        {

            try

            {

                if (item == null)

                {

                    return;

                }

                _items.Insert(index, item);

 

                _itemsMap[item.Key] = item;

            }

            catch (ArgumentOutOfRangeException)

            {

 

            }

        }

 

        void IList<TItem>.RemoveAt(int index)

        {

            try

            {

                TItem item = _items[index];

 

                _items.RemoveAt(index);

 

                _itemsMap.Remove(item.Key);

            }

            catch (ArgumentOutOfRangeException)

            {

 

            }

        }

 

        TItem IList<TItem>.this[int index]

        {

            get

            {

                return _items[index];

            }

            set

            {

                try

                {

                    if (value == null)

                    {

                        return;

                    }

                    _items[index] = value;

 

                    _itemsMap[value.Key] = value;

                }

                catch (ArgumentOutOfRangeException)

                {

 

                }

            }

        }

 

        #endregion

 

        #region ICollection<TItem> Members

 

        void ICollection<TItem>.Add(TItem item)

        {

            if (item == null)

            {

                return;

            }

            _items.Add(item);

 

            _itemsMap[item.Key] = item;

        }

 

        void ICollection<TItem>.Clear()

        {

            _items.Clear();

 

            _itemsMap.Clear();

        }

 

        bool ICollection<TItem>.Contains(TItem item)

        {

            if (item == null)

            {

                return false;

            }

            return _itemsMap.ContainsKey(item.Key);

        }

 

        void ICollection<TItem>.CopyTo(TItem[] array, int arrayIndex)

        {

            throw new NotImplementedException();

        }

 

        int ICollection<TItem>.Count

        {

            get { return _items.Count(); }

        }

 

        bool ICollection<TItem>.IsReadOnly

        {

            get { return false; }

        }

 

        bool ICollection<TItem>.Remove(TItem item)

        {

            if (item == null)

            {

                return false;

            }

            if (_itemsMap.ContainsKey(item.Key) == true)

            {

                _itemsMap.Remove(item.Key);

 

                _items.Remove(item);

 

                return true;

            }

            return false;

        }

 

        #endregion

 

        #region IEnumerable<TItem> Members

 

        IEnumerator<TItem> IEnumerable<TItem>.GetEnumerator()

        {

            return _items.GetEnumerator();

        }

 

        #endregion

 

        #region IEnumerable Members

 

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

        {

            return _items.GetEnumerator();

        }

 

        #endregion

    }

}

Enjoy!

Posted: Aug 18 2008, 04:36 AM by egady | with no comments
תגים:,

Create a WPF UserControl Library Project

Create a WPF UserControl Library Project

In Visual Studio 2008 you can create a Windows Presentation Foundation (WPF) UserControl Library Project, and developing your own controls.

WPF UserControl Library Project

Then, you need an application to run those controls, I will show here a static way to run one of the controls.

Add WPF Application Project to the solution, and set a reference to the UserControl Library.

WPF Apllication Project

App.xaml

Your App.xaml is like the following xaml:

<Application x:Class="MyApp.App"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="Window1.xaml">

    <Application.Resources>

 

    </Application.Resources>

</Application>

Windows1.xaml

Go to Windows1.xaml and add a reference to your UserControl Library, and then add a namespace directive (I used 'my') into your Windows1.xaml Page.

<Window x:Class="MyApp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:my="clr-namespace:MyComponentsNS;assembly=MyComponents"

    Title="Window1" Height="300" Width="300">

    <my:MyUserControl></my:MyUserControl>

</Window>


Then, you can use your own UserControl.

Conclusion

This is a static way to upload user control, which stored in different assembly. Next post I will show how to do it dynamically.

Technorati Profile