MMC 3.0开发 - 数据持久化设计
在MMC 3.0的Snap-In的设计中,如果需要存储自定义数据,那么可以参考下面的例子
public class PersistentScopeNode : ScopeNode { /// <summary> /// Constructor /// </summary> public PersistentScopeNode() { this.DisplayName = defaultDisplayName; this.EnabledStandardVerbs = StandardVerbs.Rename; } /// <summary> /// Handles Rename. Marks snapin as modified so that it saves changes /// </summary> /// <param name="newText">text the displayname is changing to</param> /// <param name="status">status for updating the console</param> protected override void OnRename(string newText, SyncStatus status) { this.DisplayName = newText; this.SnapIn.IsModified = true; } }
以上代码是实现了一个特殊的ScopeNode,有几点是值得关心的
this.EnabledStandardVerbs = StandardVerbs.Rename; //这是为该节点启用标准的一些操作,例如重命名,复制,删除,属性等,这个StandardVerbs的枚举是下面这样的
[Flags] public enum StandardVerbs { None = 0, Cut = 1, Copy = 2, Paste = 4, Delete = 8, Properties = 16, Rename = 32, Refresh = 64, Print = 128, }
第二点,当修改了节点的名称之后,为了通知MMC要进行保存,应该调用下面这句话
this.SnapIn.IsModified = true;
然后,我们还要稍微注意下SnapIn的设计
[SnapInSettings("{D97B71CA-5F46-4584-A89B-D24FF6D6190B}", DisplayName = "- Persistence SnapIn", Description = "Sample - Renames marks as dirty and saves changes")] public class PersistenceSnapIn : SnapIn { private const string defaultDisplayName = "Rename Me and I Save Changes"; /// <summary> /// Constructor /// </summary> public PersistenceSnapIn() { this.RootNode = new PersistentScopeNode(); } /// <summary> /// Snap-in has data? then load /// </summary> /// <param name="status">asynchronous status for updating the console</param> /// <param name="persistenceData">binary data stored in console file</param> protected override void OnLoadCustomData(AsyncStatus status, byte[] persistenceData) { // saved name? then set snap-in to the name if (string.IsNullOrEmpty(Encoding.Unicode.GetString(persistenceData))) { this.RootNode.DisplayName = defaultDisplayName; } else { this.RootNode.DisplayName = Encoding.Unicode.GetString(persistenceData); } }
/// <summary> /// if snapin 'ismodified', then save data /// </summary> /// <param name="status">status for updating the console</param> /// <returns>true for success</returns> protected override byte[] OnSaveCustomData(SyncStatus status) { return Encoding.Unicode.GetBytes(this.RootNode.DisplayName); }
}
其实很好理解的,我们需要重写两个方法,分别是SaveCustomData和LoadCustomData
但是,这些数据到底放在哪里去了呢?
秘密就在那个msc文件里面,就是说如果我们使用CustomData这种方式的话,那么这些数据其实是保存在一个msc文件中的,用二进制编码。
既然是这个原理,可以想到这样不是很理想,因为这样就依赖那个msc文件了。换句话说,如果我直接用mmc再去添加这个snapIn的时候,那么自定义数据就丢失了