You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.8 KiB
70 lines
1.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Diagnostics;
|
|
|
|
namespace FLocal.Core {
|
|
|
|
abstract public class DataObject<TKey, T> : IDataObject<TKey, T>
|
|
where T : DataObject<TKey, T>, 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<TKey, T> LoadByIdsForLoadingFromHash(List<TKey> ids) {
|
|
Dictionary<TKey, T> res = new Dictionary<TKey,T>();
|
|
foreach(TKey id in ids) {
|
|
res[id] = registry.Get(id, true);
|
|
}
|
|
return res;
|
|
}
|
|
|
|
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;
|
|
this.AfterCreate(forLoadingFromHash);
|
|
}
|
|
|
|
internal override void markAsDeletedFromRegistry() {
|
|
}
|
|
|
|
private static Registry<TKey, T> registry {
|
|
get {
|
|
return Registry<TKey, T>.instance;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|