【发布时间】:2017-07-31 06:03:59
【问题描述】:
我的应用程序有一个业务逻辑层和一个数据访问层。我只想让数据访问层访问数据库模型。现在,我可以轻松地做到这一点,但是我的 UI 类无法访问像 Reminder 这样的数据库类:
namespace Database
{
using System;
using System.Collections.Generic;
public partial class Reminder
{
public long Id { get; set; }
public string Name { get; set; }
public string Date { get; set; }
public string RepeatType { get; set; }
public string Note { get; set; }
public long Enabled { get; set; }
public string SoundFilePath { get; set; }
public string PostponeDate { get; set; }
public Nullable<long> EveryXCustom { get; set; }
public string RepeatDays { get; set; }
public Nullable<long> DayOfMonth { get; set; }
}
}
在数据库类库里面
我使用这个提醒类来存储提醒。在我的 UI 类中,我出于各种原因使用这个类。
为了使用这个Reminder 类,我只需添加一个对需要使用它的类库的引用。这很好用,但问题是每个引用它的类库都可以像这样改变数据库。
如果我不使用实体框架,我可以简单地在模型之外有一个Reminder 类(因为没有模型)并将提醒从数据库加载到其中并在不使用实体框架的情况下提取它们。
这是一个示例,说明为什么我需要在我的 UI 类中使用 Reminder 类(这只是一个 UI 类的一小段代码示例)
此代码位于每 30 秒计时一次的计时器内
// We will check for reminders here every 30 seconds.
foreach (Reminder rem in BLReminder.GetReminders())
{
// Create the popup. Do the other stuff afterwards.
if(rem.PostponeDate != null && Convert.ToDateTime(rem.PostponeDate) <= DateTime.Now && rem.Enabled == 1)
{
allowRefreshListview = true;
// temporarily disable it. When the user postpones the reminder, it will be re-enabled.
rem.Enabled = 0;
BLReminder.EditReminder(rem);
MakePopup(rem);
}
else if(Convert.ToDateTime(rem.Date.Split(',')[0]) <= DateTime.Now && rem.PostponeDate == null && rem.Enabled == 1)
{
allowRefreshListview = true;
// temporarily disable it. When the user postpones the reminder, it will be re-enabled.
rem.Enabled = 0;
BLReminder.EditReminder(rem);
MakePopup(rem);
}
}
GetReminders 会从数据库中获取提醒并将它们放入提醒对象中
using (RemindMeDbEntities db = new RemindMeDbEntities())
{
localReminders = (from g in db.Reminder select g).ToList();
db.Dispose();
}
【问题讨论】:
标签: c# winforms entity-framework sqlite oop