silverlight windows phone 7使用静态类存储数据

时间:2011-04-27 20:55:32

标签: silverlight windows-phone-7

我正在尝试为Windows Phone 7编写任务应用程序。将所有数据存储在静态/单例类中是不是很糟糕?如果是这样,我的选择是什么?感谢。

3 个答案:

答案 0 :(得分:1)

不,这不是坏习惯。

这样做可以将所有设置保存在一个地方,以便于持久化。

答案 1 :(得分:0)

use可以创建静态类,使用可以在应用程序中使用它,这样实现持久性很容易,其他选项可以实现

选项1:

public static void SaveNote(string content)
        {
                var fileName = "myNotes.dat";
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!store.FileExists(fileName))
                {
                    using (var writeStream = new IsolatedStorageFileStream(fileName, FileMode.Create, store))
                    {
                        using (var writer = new StreamWriter(writeStream))
                        {
                            writer.Write(content);
                        }
                    }
                }
                }
            }

        public static string LoadNote()
        {
            var fileName = "myNotes.dat";
            try
            {

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (store.FileExists(fileName))
                    {
                        using (var readStream = new IsolatedStorageFileStream(fileName, FileMode.Open, store))
                        using (var reader = new StreamReader(readStream))
                        {
                            return reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (IsolatedStorageException e)
            {

            }
                return String.Empty;
        }

Option 2:

var note = IsolatedStorageSettings.ApplicationSettings;
           note.Add("Note", txtNote.Text);

答案 2 :(得分:0)

最好不要通过设计强制一个类成为单例,而是通过使用来成为单例。让您的数据访问对象(DAO)成为常规类。实现一个充当对象注册表的Service Locator,让它成为维护DAO奇点的类。

代码示例:

public interface INotesDao {
    void Save (string s);
    String Load ();
}

public class NotesDaoImpl : INotesDao {
    // etc.
}

public interface IServiceLocator {
    public TIntfc GetImpl<TIntfc> () {
        return Activator.CreateInstance (m_Mapping[typeof(TIntfc)]);
    }
}

public static class ServiceLocator {
    public static IServiceLocator Instance { get; set; }
}

// this isn't a robust implementation, just meant to get the point across
public class MyAppServiceLocatorImpl : IServiceLocator {

    private Dictionary<Type,Type> m_Mapping = new Dictionary<Type,Type> ();
    private Dictionary<Type,Object> m_Cache = new Dictionary<Type,Object> ();

    public MyServiceLocatorImpl () {
        AddMapping<INotesDao, NotesDaoImpl> ();
    }

    private void AddMapping<TIntfc, TImpl> () {
        m_Mapping[typeof(TIntfc)] = typeof(TImpl);
    }

    public TIntfc GetImpl<TIntfc> () {
        var implType = m_Mapping[typeof(TIntfc)];
        if (!m_Cache.ContainsKey (implType))
            m_Cache[implType] = Activator.CreateInstance (implType);
        return m_Cache[implType];
    }
}

public class MyApp {
    public MyApp () {
        ServiceLocator.Instance = new MyAppServiceLocatorImpl ();

        var dao = ServiceLocator.Instance.GetImpl<INotesDao> ();
        var notes = dao.Load ();
        // etc
    }
}

实现这一点的原因是单个设计类很难正确测试,更不用说它使类稍微复杂一些。最好让他们成为愚蠢的课程,让另一个班级专门管理所有类型的单一性。