📄 entityset.cs
字号:
using System;
using System.IO;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Diagnostics;
namespace EnterpriseObjects
{
/// <summary>
/// Summary description for EntitySet.
/// </summary>
public class EntitySet : DataSet, IEnumerable, ICounterProvider
{
// members...
public Type EntityType = typeof(Entity);
public Hashtable _entityCache;
public bool UseCaching = true;
private Hashtable _boundCollections;
private static PerformanceCounter _entitySetsPerSecond;
// const
private const string EntitySetsPerSecondCounterName = "EntitySetsPerSecond";
public EntitySet()
{
//
// TODO: Add constructor logic here
//
}
protected EntitySet(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{
}
// GetEnumerator - create an enumerator for a table...
public virtual IEnumerator GetEnumerator()
{
return GetEnumerator(0);
}
public virtual IEnumerator GetEnumerator(int tableIndex)
{
return new EntitySetEnumerator(tableIndex, this);
}
public int Count
{
get
{
return GetCount(0);
}
}
public int GetCount(int tableIndex)
{
return Tables[tableIndex].Rows.Count;
}
public Entity GetEntity(int index)
{
return GetEntity(0, index);
}
public Entity GetEntity(int tableIndex, int index)
{
// do we have it in the cache?
Entity newEntity = null;
// caching?
if(UseCaching == true && tableIndex == 0)
{
// create it...
if(_entityCache == null)
_entityCache = new Hashtable();
// find it...
if(_entityCache.Contains(index) == true)
return (Entity)_entityCache[index];
}
// create it...
newEntity = (Entity)System.Activator.CreateInstance(EntityType);
newEntity.Populate(Tables[tableIndex].Rows[index]);
// add it...
if(UseCaching == true && tableIndex == 0)
_entityCache.Add(index, newEntity);
// return...
return newEntity;
}
public void CreateCounters(EnterpriseObjects.EnterpriseCounters counters)
{
EnterpriseApplication.Application.Counters.Counters.Add(new EnterpriseCounter(EntitySetsPerSecondCounterName, "The number of EntitySets created each second", PerformanceCounterType.RateOfCountsPerSecond32));
}
public void CountersCreated(EnterpriseObjects.EnterpriseCounters counters)
{
_entitySetsPerSecond = counters.Counters.Find(EntitySetsPerSecondCounterName).Counter;
}
// CreateBoundCollection - create a bound collection...
public EntityBoundCollection GetBoundCollection(EntitySet entitySet, int tableIndex, Type collectionType)
{
// do we have the collection?
bool inCache = false;
if(_boundCollections != null)
inCache = _boundCollections.Contains(tableIndex);
else
_boundCollections = new Hashtable();
// in...
if(inCache == false)
{
// create one...
EntityBoundCollection collection = (EntityBoundCollection)System.Activator.CreateInstance(collectionType);
collection.EntitySet = entitySet;
collection.TableIndex = tableIndex;
_boundCollections.Add(tableIndex, collection);
}
// return
return (EntityBoundCollection)_boundCollections[tableIndex];
}
public void Serialize(Stream stream)
{
// formatter...
BinaryFormatter formatter = new BinaryFormatter();
Serialize(stream, formatter);
}
public void Serialize(Stream stream, IFormatter formatter)
{
formatter.Serialize(stream, this);
}
public void Serialize(string filename, IFormatter formatter)
{
FileStream stream = new FileStream(filename, FileMode.Create);
Serialize(stream, formatter);
stream.Close();
}
// Deserialize - retrieve from a stream...
public static EntitySet Deserialize(string filename)
{
FileStream stream = new FileStream(filename, FileMode.Open);
EntitySet newEntitySet = Deserialize(stream);
stream.Close();
return newEntitySet;
}
public static EntitySet Deserialize(Stream stream)
{
// formatter...
BinaryFormatter formatter = new BinaryFormatter();
EntitySet newEntitySet = Deserialize(stream, formatter);
return newEntitySet;
}
public static EntitySet Deserialize(string filename, IFormatter formatter)
{
FileStream stream = new FileStream(filename, FileMode.Open);
EntitySet myObject = (EntitySet)Deserialize(stream, formatter);
stream.Close();
return myObject;
}
public static EntitySet Deserialize(Stream stream, IFormatter formatter)
{
EntitySet myObject = (EntitySet)formatter.Deserialize(stream);
return myObject;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -