I have a sample as shown in the bottom on the mail. I am trying to see
without changing anything in the way the As and Bs handled. I want to
make it DRY using generics. I am really interested in the
AddCollection method, I see the code repeatation just because the
collection is different type. Any thoughts?
Thanks,
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
namespace TestGeneric
{
class Program
{
ACollection<AAs = new ACollection<A>( );
BCollection<BBs = new BCollection<B>( );
static void Main(string[] args)
{
Program pg = new Program();
}
public void AddCollection()
{
AddAs();
AddBs();
}
private void AddBs()
{
Bs.Add("Mary", new B() { Name = "Mary", Age = 30 });
Bs.Add("Helen", new B() { Name = "Helen", Age = 40 });
}
private void AddAs()
{
As.Add("Jim", new A() { Name = "Jim", Age = 30 });
As.Add("John", new A() { Name = "John", Age = 40 });
}
}
public abstract class BaseClass
{
public virtual string Name;
public virtual int Age;
public virtual void ShowName()
{
Console.WriteLi ne("Hi my name is {0}", Name);
}
}
public class A : BaseClass
{
}
public class B : BaseClass
{
}
public abstract class BaseDictionary< TValue: IDictionary<str ing,
TValue>
{
#region IDictionary<str ing,TValueMembe rs
public void Add(string key, TValue value)
{
throw new NotImplementedE xception();
}
public bool ContainsKey(str ing key)
{
throw new NotImplementedE xception();
}
public ICollection<str ingKeys
{
get { throw new NotImplementedE xception(); }
}
public bool Remove(string key)
{
throw new NotImplementedE xception();
}
public bool TryGetValue(str ing key, out TValue value)
{
throw new NotImplementedE xception();
}
public ICollection<TVa lueValues
{
get { throw new NotImplementedE xception(); }
}
public TValue this[string key]
{
get
{
throw new NotImplementedE xception();
}
set
{
throw new NotImplementedE xception();
}
}
#endregion
#region ICollection<Key ValuePair<strin g,TValue>Member s
public void Add(KeyValuePai r<string, TValueitem)
{
throw new NotImplementedE xception();
}
public void Clear()
{
throw new NotImplementedE xception();
}
public bool Contains(KeyVal uePair<string, TValueitem)
{
throw new NotImplementedE xception();
}
public void CopyTo(KeyValue Pair<string, TValue>[] array, int
arrayIndex)
{
throw new NotImplementedE xception();
}
public int Count
{
get { throw new NotImplementedE xception(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedE xception(); }
}
public bool Remove(KeyValue Pair<string, TValueitem)
{
throw new NotImplementedE xception();
}
#endregion
#region IEnumerable<Key ValuePair<strin g,TValue>Member s
public IEnumerator<Key ValuePair<strin g, TValue>>
GetEnumerator()
{
throw new NotImplementedE xception();
}
#endregion
#region IEnumerable Members
System.Collecti ons.IEnumerator
System.Collecti ons.IEnumerable .GetEnumerator( )
{
throw new NotImplementedE xception();
}
#endregion
}
public class ACollection<A: BaseDictionary< A>
{
}
public class BCollection<B: BaseDictionary< B>
{
}
}
without changing anything in the way the As and Bs handled. I want to
make it DRY using generics. I am really interested in the
AddCollection method, I see the code repeatation just because the
collection is different type. Any thoughts?
Thanks,
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
namespace TestGeneric
{
class Program
{
ACollection<AAs = new ACollection<A>( );
BCollection<BBs = new BCollection<B>( );
static void Main(string[] args)
{
Program pg = new Program();
}
public void AddCollection()
{
AddAs();
AddBs();
}
private void AddBs()
{
Bs.Add("Mary", new B() { Name = "Mary", Age = 30 });
Bs.Add("Helen", new B() { Name = "Helen", Age = 40 });
}
private void AddAs()
{
As.Add("Jim", new A() { Name = "Jim", Age = 30 });
As.Add("John", new A() { Name = "John", Age = 40 });
}
}
public abstract class BaseClass
{
public virtual string Name;
public virtual int Age;
public virtual void ShowName()
{
Console.WriteLi ne("Hi my name is {0}", Name);
}
}
public class A : BaseClass
{
}
public class B : BaseClass
{
}
public abstract class BaseDictionary< TValue: IDictionary<str ing,
TValue>
{
#region IDictionary<str ing,TValueMembe rs
public void Add(string key, TValue value)
{
throw new NotImplementedE xception();
}
public bool ContainsKey(str ing key)
{
throw new NotImplementedE xception();
}
public ICollection<str ingKeys
{
get { throw new NotImplementedE xception(); }
}
public bool Remove(string key)
{
throw new NotImplementedE xception();
}
public bool TryGetValue(str ing key, out TValue value)
{
throw new NotImplementedE xception();
}
public ICollection<TVa lueValues
{
get { throw new NotImplementedE xception(); }
}
public TValue this[string key]
{
get
{
throw new NotImplementedE xception();
}
set
{
throw new NotImplementedE xception();
}
}
#endregion
#region ICollection<Key ValuePair<strin g,TValue>Member s
public void Add(KeyValuePai r<string, TValueitem)
{
throw new NotImplementedE xception();
}
public void Clear()
{
throw new NotImplementedE xception();
}
public bool Contains(KeyVal uePair<string, TValueitem)
{
throw new NotImplementedE xception();
}
public void CopyTo(KeyValue Pair<string, TValue>[] array, int
arrayIndex)
{
throw new NotImplementedE xception();
}
public int Count
{
get { throw new NotImplementedE xception(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedE xception(); }
}
public bool Remove(KeyValue Pair<string, TValueitem)
{
throw new NotImplementedE xception();
}
#endregion
#region IEnumerable<Key ValuePair<strin g,TValue>Member s
public IEnumerator<Key ValuePair<strin g, TValue>>
GetEnumerator()
{
throw new NotImplementedE xception();
}
#endregion
#region IEnumerable Members
System.Collecti ons.IEnumerator
System.Collecti ons.IEnumerable .GetEnumerator( )
{
throw new NotImplementedE xception();
}
#endregion
}
public class ACollection<A: BaseDictionary< A>
{
}
public class BCollection<B: BaseDictionary< B>
{
}
}
Comment