【问题标题】:Entity Framework and N-Tier实体框架和 N 层
【发布时间】:2012-12-17 06:24:35
【问题描述】:

存在架构问题。我正在尝试将逻辑层拆分为物理层并且遇到了一些麻烦。这是我想要的设置方式:

  • 实体 - 一个 DLL。我的 POCO 课程。没有依赖项。
  • DAL - 一个 DLL。包含模型和 DbContext。依赖实体。
  • BLL - 一个 DLL。包含 CRUD 函数。依赖 DAL 和实体
  • UI - 一个网站项目。与 BLL 对话。

我的问题是在 BLL 中,我做了类似的事情:

 ''' <summary>
''' The repository
''' </summary>
''' <remarks></remarks>
Private context As MyContext

''' <summary>
''' Instantiate the business layer
''' </summary>
''' <remarks></remarks>
Public Sub New()
    context = New MyContext()
End Sub

''' <summary>
''' Insert a general retrieve into the database
''' </summary>
''' <param name="myEntity">The entity to insert</param>
''' <returns>The id of the entity added</returns>
''' <remarks></remarks>
Public Function Create(ByVal myEntity As myEntity) As String
    Try
        context.myEntity.Add(myEntity )
        context.SaveChanges()
    Catch ex As Exception
        Throw ex
    End Try
    Return myEntity.id
End Function

但为了在我的实体上调用 Add,我需要对 EntityFramework.dll 的引用,因为我的上下文继承自 DbContext。如果不创建存储库,我不知道如何避免这种情况,这似乎是不必要的额外抽象层,而且我认为我不需要 ti 与 DbContext。

我在这里错过了什么?

【问题讨论】:

    标签: asp.net entity-framework webforms n-tier-architecture


    【解决方案1】:

    如果您对这个话题感到困惑,您应该看看 CodePlex 上提供的 N-Tier Entity Framework:http://ntieref.codeplex.com/

    【讨论】:

    • 我已经很清楚如何做到这一点,但肯定会看看那个项目来帮助清理。
    【解决方案2】:

    您的 DAL 可以为您的上下文定义派生上下文和工厂实现的接口。 BLL 将只使用接口和工厂。

    【讨论】:

    • 抱歉,我需要更多细节。我不确定我是否理解您所引用的所有概念。
    • 所以如果我的上下文叫myDbContext,它继承自DbContext,我应该有一个ImyDbContext,而实际的上下文继承自DbContext并实现了ImyDbContext。我必须在早上尝试实现它。
    • 我被困在这里了。我创建了一个接口,但是接口仍然依赖于IDbSet,这需要业务层的EntityFramework DLL。
    猜你喜欢
    • 1970-01-01
    • 2012-03-04
    • 2011-10-09
    • 2012-05-15
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 1970-01-01
    相关资源
    最近更新 更多