我正在尝试为Windows Phone 7编写一个任务应用程序吗?将所有数据存储在静态/单例类中是不好的做法吗?如果是这样,我有什么选择?谢谢。Silverlight的Windows Phone 7使用静态类来存储数据
0
A
回答
1
不,它的做法不错。
这样做可以将所有设置保存在一个地方,以实现简单的持久性。
0
使用可以创建静态类等,实现持久性是很容易和其他执行选项结合使用可以使用它的应用是
选项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);
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
}
}
之所以要实现这是单按设计班是很难正确地测试,更何况,它使类稍微复杂些。最好让他们成为愚蠢的班级,让其他班级专门管理各类班级的奇点。
相关问题
- 1. 在Windows Phone 7中存储数据
- 2. 在C#中使用大型静态数组(Windows Phone 7上的Silverlight)
- 3. Silverlight Windows Phone 7 ItemsControl数据绑定
- 4. 使用类来存储Java中的静态数据?
- 5. Silverlight类库到Windows Phone 7类库
- 6. Windows Phone 7使用会话的Silverlight
- 7. WPF的Silverlight/Windows Phone 7的
- 8. Windows Phone 7 Silverlight动态网格
- 9. Windows Phone 7/Silverlight上的Hessian?
- 10. 从Windows Phone 7/Silverlight调用Javascript函数
- 11. Windows Phone 7的全景/数据透视DataBinding与静态页面
- 12. 如何在Windows Phone 7的禁用事件中存储数据
- 13. 数据存储的静态类
- 14. 使用windows phone 7的mySQL数据库
- 15. Windows phone 7 silverlight墓碑
- 16. 的Windows Phone 7 - 保存图像状态在独立存储
- 17. 如何绑定到Windows Phone 7中的静态类的字段?
- 18. windows phone 7的隔离存储位置?
- 19. 适用于Silverlight/Windows Phone 7的数据解决方案
- 20. 来自Windows Phone的Windows Azure存储
- 21. 全新Windows Phone 7中找不到静态资源Silverlight应用程序
- 22. Windows 7 Phone应用程序存储凭据的最佳方式
- 23. 绑定来自孤立的存储在Windows Phone 7的图像
- 24. Windows phone 7-将现有数据库加载到隔离存储
- 25. 存储资源为Windows Phone 7
- 26. Windows Phone 7:解密来自孤立存储的许多文件
- 27. 的Windows Phone 7使用Silverlight - MediaElement的不使用此代码
- 28. Windows Phone 7数据库
- 29. 的Windows Phone 7 \ Silverlight的SSH或Telnet库
- 30. Windows Phone 7上的Silverlight Web内容