简体   繁体   English

OOP:仅在数据访问层中使用实体框架

[英]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.

相关问题 关于使用实体框架的数据访问层的建议 - Suggestion For Data Access Layer with Entity Framework 在单独的数据访问和业务逻辑层中,我可以在业务层中使用Entity框架类吗? - In separate data access & business logic layer, can I use Entity framework classes in business layer? 如何在业务层和/或数据层中使用实体框架? - How to use entity framework in business layer and/or data layer? 在不使用实体框架的情况下使用存储库设计数据访问层 - Design of Data Access Layer using Repository without using Entity Framework 实体框架代码优先的数据访问层集成测试 - Integration Test for Data access Layer with Entity Framework code-first 实体框架数据访问层项目的连接字符串 - Entity framework Data Access Layer project's connection string 当使用实体框架作为数据访问层时,如何实现业务逻辑层? - How do you implement a business logic layer when using entity framework as data access layer? 实体框架数据访问 - Entity Framework data access 即使访问仅限于数据访问层,我也需要在UI中引用实体框架吗? - Do I need Entity Framework Referenced In My UI even if access is limited to my data access layer? 我应该在实体框架数据访问层中存储硬编码数据吗? - Should I store hard-coded data in my Entity Framework data access layer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM