我目前的解决方案很有魅力。。。目前为止但我想知道这是否会被视为不好的做法,或者你是否会从第一眼看到瓶颈jsut。
//< API user will call this kind of method handing in their requested Accessor!
public void Command( int a, int b, KomiAccessorBase accessor=null ){
if( !AccessGranted( accessor ) ) return;
this.SubController.CommandA( a, b , accessor );
}
//< Accessor is checked on validity
private bool AccessGranted(KomiAccessorBase accessor, [CallerMemberName] string callerName = null ) {
if (accessor == null) {
log("KOMI - Access to ["+callerName+"] denied: No Accessor handed in!", MsgType.LOG);
return false;
}
if ( !_kacc_inventory.ContainsKey(accessor.KACCHashId)) {
log("KOMI - Access to [" + callerName + "] denied: Accessor not registered in KOMI!", MsgType.LOG);
return false;
}
return true;
}
protected Dictionary<int, KomiAccessorBase> _kacc_inventory = new Dictionary<int, KomiAccessorBase>();
//< The Accessor base class. API user have to derive from that!
public abstract class KomiAccessorBase : RecieverBase, INameable {
public int KACCHashId { get; private set; }
private string _name;
public string Name {
get {
return _name;
}
private set {
}
}
protected KomiAccessorBase(string name, KOMI komi) : base(komi.Office) {
Name = name;
KACCHashId = -1;
KACCHashId = komi.RegisterAccessor(this); //Perform the real registration
}
}
//< Registeres the Accessor on KOMI for access checking!
public int RegisterAccessor(KomiAccessorBase acc) {
if (acc.KACCHashId != -1) return -1; //Abbort registration if already registered!
int salt = 1;
int hashId = (salt + "#" + acc.Name).GetHashCode();
while (_kacc_inventory.ContainsKey(hashId)) {
salt++;
hashId = (salt + "#" + acc.Name).GetHashCode();
}
_kacc_inventory.Add(hashId, acc);
return hashId;
}