SQL Server CE 架构

发布于 2024-10-12 20:28:22 字数 409 浏览 1 评论 0原文

我必须构建许多小型独立应用程序,这些应用程序可以复制到 USB 设备并从那里开箱即用。所以我正在考虑使用 WPF,它首先使用 EF 代码连接到 SQL Server CE 数据库。

我的问题是我应该使用什么架构。尽管这些应用程序是独立的,但我仍然希望将 UI 与域与数据分离,以实现层的清晰分离。但我也不想让它变得太复杂。

因此,我想要一个 UI 层 (WPF/MVVM),它使用底层域层(具有域逻辑的域对象)和存储库(首先使用 EF 代码)。

我的问题是:在这种情况下我应该使用什么模式来使 EF 工作?是否有一个示例演示如何在这种情况下实现 CRUD 操作?例如,我应该创建一个上下文并将其保持打开状态吗?或者我应该实现工作单元模式并将对象附加到其他上下文(如果需要)?

或者你会用完全不同的方式来做吗?

感谢您的建议!

I have to build a number of small independent applications, that can be copied to an USB device and run from there out of the box. So I was thinking to use WPF, that uses EF code first to connect to a SQL Server CE database.

My question is about what architecture I should use. Although the apps are standalone, I would still like to decouple UI from domain from data, to have a clean separation of layers. But I also don't want to make it too complex.

So, I want to have a UI layer (WPF/MVVM) that uses the underlying domain layer (domain objects with domain logic) and repositories (that use EF code first).

My question is: what pattern should I use to make EF work in this case? Is there somewhere an example that demonstrates how to implement CRUD operations in such scenario? For example, should I create one context and leave it open; or should I implement the unit of work pattern and attach objects to other context if needed?

Or would you do it in a totally different way?

Thanks for the advice!

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

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

发布评论

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

评论(1

梦在深巷 2024-10-19 20:28:22

EF 上下文的开放时间应尽可能短。最好在 using 语句中使用它。

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new. 
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the 
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context 
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state 
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}

有一种称为 Attach 的方法,它将实体附加到上下文:

private static void AttachRelatedObjects(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    // Attach the root detachedOrder object to the supplied context.
    currentContext.Attach(detachedOrder);

    // Attach each detachedItem to the context, and define each relationship
    // by attaching the attached SalesOrderDetail object to the EntityCollection on 
    // the SalesOrderDetail navigation property of the now attached detachedOrder.
    foreach (SalesOrderDetail item in detachedItems)
    {
        currentContext.Attach(item);
        detachedOrder.SalesOrderDetails.Attach(item);
    }
}

http://msdn .microsoft.com/en-us/library/bb896271.aspx

The EF context should be open for as short time as possible. Preferably use it within a using statement.

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new. 
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the 
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context 
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state 
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}

There is a method called Attach, which attachs entities to a context:

private static void AttachRelatedObjects(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    // Attach the root detachedOrder object to the supplied context.
    currentContext.Attach(detachedOrder);

    // Attach each detachedItem to the context, and define each relationship
    // by attaching the attached SalesOrderDetail object to the EntityCollection on 
    // the SalesOrderDetail navigation property of the now attached detachedOrder.
    foreach (SalesOrderDetail item in detachedItems)
    {
        currentContext.Attach(item);
        detachedOrder.SalesOrderDetails.Attach(item);
    }
}

http://msdn.microsoft.com/en-us/library/bb896271.aspx

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