DataContext在哪一层创建?

发布于 2024-09-30 16:50:55 字数 381 浏览 3 评论 0原文

我正在使用 RIA 创建一个新的 Silverlight 应用程序。我正在使用业务层和数据访问层,它看起来像这样...

Silverlight->ASP.Net/RIA->C# BLL 类库->C# DAL 类库 我的实体框架模型可以在所有层中访问。

我不确定在哪里为我的实体框架请求创建 DataContext。我应该在 RIA (ASP.Net) 层上创建上下文并将其传递到 BLL,然后传递到 DAL,还是应该在 DAL 中创建它并将 LINQ 结果传递回来?

如果我在 DAL 中创建它并传递一个实体备份,延迟加载意味着数据还不存在。在我有机会实际从数据库查询数据之前,我的上下文是否可能会被垃圾收集?

如果我在 DAL 中创建上下文,是否会遇到线程问题?

I am creating a new Silverlight app using RIA. I am using a Business layer and a data access layer, it looks something like this...

Silverlight->ASP.Net/RIA->C# BLL class library->C# DAL class library
My entity framework model is accessible in all layers.

I'm not sure where to create the DataContext for my Entity Framework requests. Should I create the context on the RIA (ASP.Net) layer and pass it down to the BLL, then to the DAL, or should I create it in the DAL and pass the LINQ result back up?

If I create it in the DAL and pass an entity back up, lazy loading means that the data is not there yet. Is it possible that my context will be garbage collected before I get a chance to actually query my data from the database?

Will I run into threading problems if I create the context in my DAL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往昔成烟 2024-10-07 16:50:55

如果您无论如何都在使用 RIA 服务,您不妨使用 LinqToEntitiesDomainService<EfModelGoesHere>并让 RIA 服务保存单个 EF DataContext/模型。

我们使用自己的自定义 POCO 来实现业务逻辑,但 RIA 服务管理从客户端到我们各个数据层(包括直接到 EF)的所有连接。在需要的地方,我们传递单个 EF 模型(存储在域上下文的 ObjectContext 成员中)。

保留 EF 引用,而不是切换到普通的 DomainService,感觉是正确的,并且使向域服务添加快速(临时)添加变得非常容易,因为我们只需 LINQ 直接连接到 EF 模型。如果我们向下管理它,我们就会不断地将它拉回到 RIA 层来使用它。

If you are using RIA servcies anyway, you might as well make use of LinqToEntitiesDomainService<EfModelGoesHere> and let RIA services hold a single EF DataContext/model.

We use our own custom POCOs for business logic, but RIA services manages all connections from the client to our various data layers (including direct to EF). Where needed we pass down the single EF model (which is stored in the ObjectContext member of the Domain Context).

It felt right to retain the EF reference, rather than switch to a plain DomainService and has made adding quick (temporary) additions to the Domain Service really easy as we just LINQ direct to the EF model. If we manage it lower down, we then are constantly pulling it back up to the RIA layer to make use of it.

白芷 2024-10-07 16:50:55

您可以在应用程序的 Silverlight(客户端)部分中创建数据上下文。我通常将上下文保留在类中的变量中,以便可以重用它。在对该类的所有引用都消失之前,它不会被垃圾收集。这是一个简短的示例,从数据库异步加载一些对象(实体)(通过回调来处理返回的对象):

public class MyClass

    private _context as MyDomainContext
    private _employees as list(of Employees)

    public sub DoSomething()
        _context = new MyDomainContext
        _context.Load(_context.GetEmployeesQuery(), AddressOf EmployeesLoaded, Nothing)
    end sub

    public sub EmployeesLoaded(loadOp as LoadOperation(Of Employee))
        _employees = new list(of Employee)(loadOp.entities)
    end sub

end class

You create the data context in your Silverlight (client) part of the application. I normally leave my context in a variable in my class so I can reuse it. It won't get garbage collected until the all references to the class are gone. Here's a short example loading some objects (entities) from the database asynchronously (with a call back to process the returned objects):

public class MyClass

    private _context as MyDomainContext
    private _employees as list(of Employees)

    public sub DoSomething()
        _context = new MyDomainContext
        _context.Load(_context.GetEmployeesQuery(), AddressOf EmployeesLoaded, Nothing)
    end sub

    public sub EmployeesLoaded(loadOp as LoadOperation(Of Employee))
        _employees = new list(of Employee)(loadOp.entities)
    end sub

end class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文