概述
对于实体层,在学习三层架构的时候,我们大家都接触过,并且在之后的项目中也运用过,我们也知道,在层和层之间传递的是实体或者是实体集合,那么,在这里问一下大家,你们是如何构建自己的实体集的呢?是通过数组,是通过系统提供的集合,或者是自己针对于每个实体都创建一个自己的集合(每个集合和都是一个套代码),还是其它的方法。
本片博客,本人会通过实例向大家阐释两种方式,这两种方式都是创建自己的实体集合类,但是,这两种方式也是非常不一样的。
复用性差的实体集合类
该方式是为每一个实体类创建一个实体集合类,并且,每个实体集合类里面的功能是一样的,那么,每个实体集合类的不同之处是什么呢?他们的不同之处就是,他们具体的服务对象的类型是固定的。下面是一个实例,大家可以看一下。
实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 集合.集合.灵活性差的ORMapping;
namespace TableToCollection.实体
{
public class Person
{
public string strID { get; set; }
public string strName { get; set; }
public int intAge { get; set; }
}
}
实体集合类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using TableToCollection.实体;
namespace TableToCollection.集合.灵活性差的集合
{
public class PersonCollection_bad:IEnumerable
{
private ArrayList list;
public int Count
{
get { return list.Count; }
}
#region 构造函数
public PersonCollection_bad()
{
list = new ArrayList();
}
#endregion
#region 添加
public void Add(Person p)
{
this.list.Add(p);
}
public void AddAr(Person[] ps)
{
this.list.AddRange(ps);
}
#endregion
#region 删除
public void Remove(Person p)
{
this.list.Remove(p);
}
public void RemoveAt(int index)
{
this.list.RemoveAt(index);
}
public void RemoveRange(int index, int count)
{
this.list.RemoveRange(index, count);
}
public void Clear() {
this.list.Clear();
}
#endregion
#region 自定义索引器
public Object this[int index]
{
get
{
return list[index];
}
set
{
list[index] = value;
}
}
#endregion
#region 支持foreach语句遍历
public IEnumerator GetEnumerator()
{
return list.GetEnumerator();
}
#endregion
}
}
复用性好的实体集合类
对于上面实体集合类的缺点,我们通过泛型的方式解决,也就是类型后绑定的方式。具体思路,创建一个基础实体集合类,然后,将具体集合类继承这个类,并且指定相应的类型,从而达到代码的复用性号,程序的安全性号的目的。实体类使用上面的实体类。
基础实体集合类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 集合.集合.灵活性好的集合
{
public class BaseCollection<T>:IEnumerable<T>,IEnumerator<T>,IList<T>
{
private List<T> list;
//定义索引
private int position;
public int Count
{
get {
if (list != null)
{
return list.Count;
}
return -1;
}
}
/// <summary>
/// 确定集合中特定对象的索引
/// </summary>
/// <param name="TObject">特定的对象</param>
/// <returns>如果在列表中找到,则为item 的索引;否则为 -1</returns>
public int IndexOf(T TObject)
{
return this.list.IndexOf(TObject);
}
#region 构造方法
public BaseCollection(){
this.list = new List<T>();
this.position = -1;
}
#endregion
#region 添加
/// <summary>
/// 将对象添加到集合的尾部
/// </summary>
/// <param name="TObject">要添加到集合尾部的对象</param>
public void Add(T TObject)
{
this.list.Add(TObject);
}
/// <summary>
/// 将指定集合里的元素添加到集合尾部
/// </summary>
/// <param name="TCollection">要添加到集合尾部的集合对象</param>
public void AddRange(ICollection<T> TCollection)
{
this.list.AddRange(TCollection);
}
#endregion
#region 删除
/// <summary>
/// 从集合中移除特定对象的第一个匹配项
/// </summary>
/// <param name="TObject">TObject</param>
/// <returns>true:移除成功;false:移除失败</returns>
public bool Remove(T TObject) {
return this.list.Remove(TObject);
}
/// <summary>
/// 从集合中移除指定索引处的对象
/// </summary>
/// <param name="index">索引值,从0开始</param>
public void RemoveAt(int index)
{
this.list.RemoveAt(index);
}
/// <summary>
/// 集合删除所有对象
/// </summary>
public void Clear() {
this.list.Clear();
}
#endregion
#region 插入
/// <summary>
/// 在集合的特定位置插入对象
/// </summary>
/// <param name="index">索引,从0开始</param>
/// <param name="TObject">被插入的对象</param>
public void Insert(int index, T TObject)
{
this.list.Insert(index, TObject);
}
#endregion
#region 取值
/// <summary>
/// 获取或设置指定索引处的元素
/// </summary>
/// <param name="index">索引值从0开始</param>
/// <returns>TObject</returns>
public T this[int index]
{
get
{
return this.list[index];
}
set
{
this.list[index] = value;
}
}
#endregion
#region 实现ICollection<T>,因为IList<T>继承ICollection<T>
//获取一个值,该值指示 System.Collections.Generic.ICollection<T> 是否为只读。
public bool IsReadOnly
{
get
{
return true;
}
}
//确定 System.Collections.Generic.ICollection<T> 是否包含特定值。
public bool Contains(T item)
{
return this.list.Contains(item);
}
//从特定的 System.Array 索引处开始,将 System.Collections.Generic.ICollection<T> 的元素复制到一个
public void CopyTo(T[] array, int arrayIndex)
{
this.list.CopyTo(array, arrayIndex);
}
#endregion
#region 支持foreach语句遍历
#region 实现IEnumerable<T>接口
System.Collections.Generic.IEnumerator<T> System.Collections.Generic.IEnumerable<T>.GetEnumerator()
{
return (System.Collections.Generic.IEnumerator<T>)this;
}
#region 实现IEnumerable接口,因为IEnumerable<T>继承IEnumerable
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return (System.Collections.IEnumerator)this;
}
#endregion
#endregion
#region 实现IEnumerator<T>接口
//获取集合中的当前元素。
T System.Collections.Generic.IEnumerator<T>.Current
{
get {
try
{
return this.list[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
#region 实现IEnumerator接口,因为IEnumerator<T>继承IEnumerator
//获取集合中的当前元素。
object System.Collections.IEnumerator.Current
{
get
{
try
{
return this.list[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
//将枚举数推进到集合的下一个元素。
bool System.Collections.IEnumerator.MoveNext()
{
this.position++;
return (this.position < this.list.Count);
}
//将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
void System.Collections.IEnumerator.Reset()
{
this.position = -1;
}
#endregion
#region 实现IDisposable接口,因为IEnumerator<T>继承IDisposable
public void Dispose()
{
this.position = -1;
}
#endregion
#endregion
#endregion
}
}
具体实体集合类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableToCollection.实体;
namespace 集合.集合.灵活性好的集合
{
public class PersonBaseCollection:BaseCollection<Person>
{
}
}
客户端代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableToCollection.实体;
using TableToCollection.集合.灵活性差的集合;
using 集合.集合.灵活性好的集合;
using 集合.灵活性差的ORMapping.ORMapping;
namespace 集合
{
class Program
{
static void Main(string[] args)
{
#region 灵活性差的集合
//#region 灵活性差的集合
//PersonCollection_bad personCollection = new PersonCollection_bad();
集合添加元素
//for (int i = 0; i < 10; i++)
//{
// Person p = new Person() { strID=i.ToString(),strName="zhang",intAge=i};
// personCollection.Add(p);
//}
//Console.WriteLine("元素个数:"+personCollection.Count);
输出集合
//Console.WriteLine("使用foreach输出");
//foreach (var item in personCollection)
//{
// Console.WriteLine(((Person)item).strID + ((Person)item).strName + ((Person)item).intAge);
//}
//Console.WriteLine("使用for输出");
//for (int i = 0; i < personCollection.Count; i++)
//{
// Console.WriteLine(((Person)personCollection[i]).strID + ((Person)personCollection[i]).strName + ((Person)personCollection[i]).intAge);
//}
//#endregion
#endregion
#region 灵活性好的集合
PersonBaseCollection personBaseCollection = new PersonBaseCollection();
DoAdd(personBaseCollection);
DoForPrint(personBaseCollection);
DoInsert(0,personBaseCollection);
DoForEachPrint(personBaseCollection);
personBaseCollection.RemoveAt(1);
DoForEachPrint(personBaseCollection);
personBaseCollection.Clear();
DoForEachPrint(personBaseCollection);
#endregion
Console.ReadKey();
}
static void DoAdd(PersonBaseCollection personBaseCollection)
{
personBaseCollection.Add(new Person() { strID = "1", strName = "qs1(Add)", intAge = 21 });
Person[] persons = { new Person() { strID = "2", strName = "qs2(AddRange)", intAge = 22 }, new Person() { strID = "3", strName = "qs3(AddRange)", intAge = 23 } };
personBaseCollection.AddRange(persons);
}
static void DoInsert(int index, PersonBaseCollection personBaseCollection)
{
personBaseCollection.Insert(index, new Person() { strID = "4", strName = "qs4(Insert)", intAge = 24 });
}
static void DoForPrint(PersonBaseCollection personBaseCollection)
{
Console.WriteLine("For语句输出:");
if (personBaseCollection.Count == 0)
{
Console.WriteLine("集合里面没有元素!");
return;
}
for (int i = 0; i < personBaseCollection.Count; i++)
{
Console.WriteLine(personBaseCollection[i].strID + " " + personBaseCollection[i].strName + " " + personBaseCollection[i].intAge);
}
}
static void DoForEachPrint(PersonBaseCollection personBaseCollection) {
Console.WriteLine("ForEach语句输出:");
if (personBaseCollection.Count == 0)
{
Console.WriteLine("集合里面没有元素!");
return;
}
foreach (var item in personBaseCollection)
{
Console.WriteLine(item.strID +" "+ item.strName +" "+ item.intAge);
}
}
}
}
总结
上面说的内容中还有一块没有说,就是迭代器设计模式,其实上面的程序中已经运用了,只不过相应的接口是微软给定义好的,为什么微软给定义好了的呢?因为微软自己也要用(.net framework提供的具体集合类)。