[英]OOP: use Entity Framework only in the data access layer
My application has a business logic layer, and a data access layer. 我的应用程序具有业务逻辑层和数据访问层。 I want to give only the data access layer access to the database model.
我只想给数据访问层访问数据库模型的权限。 Now, I can easily do this, but then my UI classes cannot access the database classes like
Reminder
: 现在,我可以轻松地做到这一点,但是随后我的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; }
}
}
which is inside the database class library 在数据库类库中
I use this reminder class to store reminders. 我使用此提醒类来存储提醒。 In my UI classes I use this class for various reasons.
在我的UI类中,出于各种原因使用此类。
To make use of this Reminder
class, I simply add a reference to the class library that needs to use it. 要使用此
Reminder
类,我只需要添加对需要使用它的类库的引用。 This works fine, but the problem is that every class library that references this, can alter the database like this. 这可以很好地工作,但是问题是每个引用该类的类库都可以像这样更改数据库。
If I'm not using Entity Framework, I could simply have a Reminder
class outside the model (because there is no model) and load reminders from the database into that and extract them without using Entity Framework. 如果我不使用Entity Framework,则可以在模型之外简单地设置一个
Reminder
类(因为没有模型),然后从数据库中将提醒加载到该提醒中并在不使用Entity Framework的情况下提取它们。
Here's an example of why I need to use the Reminder
class in my UI classes (this is just a small code sample of one UI class) 这是为什么我需要在UI类中使用
Reminder
类的示例(这只是一个UI类的一小段代码示例)
This code is inside a timer that ticks every 30 seconds 该代码位于一个计时器中,该计时器每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
will do get the reminders from the database and put them in reminder objects GetReminders
会从数据库中获取提醒并将其放入提醒对象中
using (RemindMeDbEntities db = new RemindMeDbEntities())
{
localReminders = (from g in db.Reminder select g).ToList();
db.Dispose();
}
If im not using the entity framework, i could simply have a reminder class outside the model
如果我不使用实体框架,那么我可以在模型外部简单地创建一个提醒类
You could create an interface instead of a class outside of the model in a shared assembly: 您可以在共享程序集中的模型外部创建接口,而不是创建类:
public interface IReminder
{
public long Id { get; }
public string Name { get; }
public string Date { get; }
public string RepeatType { get; }
public string Note { get; }
public long Enabled { get; }
public string SoundFilePath { get; }
public string PostponeDate { get; }
public Nullable<long> EveryXCustom { get; }
public string RepeatDays { get; }
public Nullable<long> DayOfMonth { get; }
}
Your Entity can than implement the interface: 您的实体可以实现以下接口:
public partial class Reminder : IReminder
{
//...
}
Maybe you want to make your Entities only internal visible and expose public service methods like IEnumerable<IReminder> GetReminders()
也许您想使实体仅在内部可见,并公开公共服务方法,例如
IEnumerable<IReminder> GetReminders()
You can create separate project called ie Shared
and put there all classes which are used in many projects. 您可以创建一个名为
Shared
单独项目,即“ Shared
,并在其中放置许多项目中使用的所有类。 Then you need to reference this project by UI project and data access project (and by others which use these classes). 然后,您需要按UI项目和数据访问项目(以及使用这些类的其他项目)引用该项目。
Both will have access to shared classes and UI won't be able to call data access layer directly. 两者都将有权访问共享类,而UI将无法直接调用数据访问层。
You can also create interface outside of data access layer but if your classes are DTOs (Data Transfer Object) first option will be better. 您也可以在数据访问层之外创建接口,但是如果您的类是DTO(数据传输对象),则第一个选择会更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.