using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace Web.Core { abstract public class DataObject : IDataObject where T : DataObject, new() where TKey : struct { private TKey? _id; public override TKey id { get { if(this.isNewObject) { throw new ObjectDoesntHaveAnIdException(); } return this._id.Value; } } private bool isNewObject { get { return !this._id.HasValue; } } private bool isJustCreated; protected DataObject() { Debug.Assert(this is T); this.isJustCreated = true; this._id = null; } public static T LoadById(TKey id) { return registry.Get(id, false); } protected static Dictionary LoadByIdsForLoadingFromHash(IEnumerable ids) { Dictionary res = new Dictionary(); foreach(TKey id in ids) { res[id] = registry.Get(id, true); } return res; } /// /// This method should consume no time /// /// protected virtual void AfterCreate(bool forLoadingFromHash) { } internal override void CreateByIdFromRegistry(TKey id, bool forLoadingFromHash) { if(!this.isJustCreated) throw new CriticalException("Object already has an id"); this._id = id; this.isJustCreated = false; //System.Threading.ThreadPool.QueueUserWorkItem(state => this.AfterCreate(forLoadingFromHash)); this.AfterCreate(forLoadingFromHash); } internal override void markAsDeletedFromRegistry() { } private static Registry registry { get { return Registry.instance; } } protected void deleteFromRegistry() { registry.Delete(new TKey[] { this.id }); } } }