Sterling学习
1.启动和关闭引擎
private void _ActivateEngine() { _engine = new SterlingEngine(); _logger = new SterlingDefaultLogger(SterlingLogLevel.Verbose); _engine.Activate(); Database = _engine.SterlingDatabase.RegisterDatabase<ItemsDatabaseInstance>(new IsolatedStorageDriver()); } private void _DeactivateEngine() { Database.Flush(); _logger.Detach(); _engine.Dispose(); Database = null; _engine = null; }
2.加载/保存/删除:
public static void Save(this ListItem listItem) { int currentIndex = (Application.Current as App).Database.Query<ListItem, int>().Count; if (listItem.Key == -1) { listItem.Key = currentIndex; } (Application.Current as App).Database.Save(listItem); (App.Current as App).Database.Flush(); } public static void Delete(this ListItem listItem) { // List<ListItem> list = (Application.Current as App).Database.Query<ListItem, int>(); //(App.Current as App).Database.Delete(typeof(ListItem), listItem.Text); (App.Current as App).Database.Truncate(typeof(ListItem)); (App.Current as App).Database.Flush(); } public static IEnumerable<ListItem> Load(this IEnumerable<ListItem> listItem) { var list = (Application.Current as App).Database.Query<ListItem, int>(); return new ObservableCollection<ListItem>(list.Select(item => item.LazyValue.Value).ToList()); } public static int Count(this ListItem listItem) { int currentIndex = (Application.Current as App).Database.Query<ListItem, int>().Count; return currentIndex; }
3.删除指定的文本和指定的ID,以文本为例
public static void DeletebyText(string text) { var list = (Application.Current as App).Database.Query<ListItem, int>(); ObservableCollection<ListItem> collection = new ObservableCollection<ListItem>( list.Select(item => item.LazyValue.Value).ToList().Where(item => item.Text.Equals(text)) ); foreach (var cll in collection) (App.Current as App).Database.Delete(cll); }
4.用于自动生成键的基础触发器
public class IdentityTrigger<T> : BaseSterlingTrigger<T,int> where T: class, IBaseModel, new() { private static int _idx = 1; public IdentityTrigger(ISterlingDatabaseInstance database) { // If a record exists, set it to the highest value plus 1 if (database.Query<T,int>().Any()) { _idx = database.Query<T, int>().Max(key => key.Key) + 1; } } public override bool BeforeSave(T instance) { if (instance.Id < 1) { instance.Id = _idx++; } return true; } public override void AfterSave(T instance) { return; } public override bool BeforeDelete(int key) { return true; } }
5.自定义序列化程序
public class TypeSerializer : BaseSerializer { /// <summary> /// Return true if this serializer can handle the object, /// that is, if it can be cast to type /// </summary> /// <param name="targetType">The target</param> /// <returns>True if it can be serialized</returns> public override bool CanSerialize(Type targetType) { return typeof (Type).IsAssignableFrom(targetType); } /// <summary> /// Serialize the object /// </summary> /// <param name="target">The target</param> /// <param name="writer">The writer</param> public override void Serialize(object target, BinaryWriter writer) { var type = target as Type; if (type == null) { throw new SterlingSerializerException( this, target.GetType()); } writer.Write(type.AssemblyQualifiedName); } /// <summary> /// Deserialize the object /// </summary> /// <param name="type">The type of the object</param> /// <param name="reader">A reader to deserialize from</param> /// <returns>The deserialized object</returns> public override object Deserialize( Type type, BinaryReader reader) { return Type.GetType(reader.ReadString()); } }