管理数据提供者接口背后的 DBContext 状态

发布于 2024-12-22 16:11:33 字数 1111 浏览 1 评论 0原文

我有一个接口 IDataProvider,它仅公开(为了讨论)3 个操作:

public interface IDataProvider
{
    // get a list of projects (just metadata)
    List<Project> ListProjects();

    // load the Project by its GUID which we got from the metadata.
    Project LoadProject(Guid id);

    // save the project. underlying provider should determine to insert or update accordingly.
    void SaveProject(Project data);
}

我使用 DBContext 访问 SQL CE 作为底层数据访问层数据提供程序,我可以实现:

public DataProvider : SqlCeDbContext, IDataProvider { ... }

or

public DataProvider : IDataProvider
{
    List<Project> ListProjects()
    {
        using(var ctx = new SqlCeContext())
        {
            //... manage the life of the context for the API user.
        }
    }
    // ...
}

or

public DataProvider : IDataProvider
{
     SqlCeContext _mSqlCeContext = new SqlCeContext();

     List<Project> ListProjects() { .. }
     // ...
}

这三个实现的行为当然会非常不同关于连接和实体状态。由于接口“规则”不强制执行规则,哪个实现更好?或者万一我们应该强制执行其中之一,可以做到吗?

I have an interface IDataProvider which exposes (for sake of discussion) just 3 operations:

public interface IDataProvider
{
    // get a list of projects (just metadata)
    List<Project> ListProjects();

    // load the Project by its GUID which we got from the metadata.
    Project LoadProject(Guid id);

    // save the project. underlying provider should determine to insert or update accordingly.
    void SaveProject(Project data);
}

I am using DBContext accessing an SQL CE as the under lying data access layer data provider and I could implement:

public DataProvider : SqlCeDbContext, IDataProvider { ... }

or

public DataProvider : IDataProvider
{
    List<Project> ListProjects()
    {
        using(var ctx = new SqlCeContext())
        {
            //... manage the life of the context for the API user.
        }
    }
    // ...
}

or

public DataProvider : IDataProvider
{
     SqlCeContext _mSqlCeContext = new SqlCeContext();

     List<Project> ListProjects() { .. }
     // ...
}

The three implementations will of course behave very differently with respect to connection and entity states. Since the interface "rules" does not enforce rules on this, which implementation is better? Or in case we should enforce one or the other, can it be done?

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

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

发布评论

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

评论(1

惜醉颜 2024-12-29 16:11:33

假设这是在移动设备上(我相信由于 SqlCE 参考就是这种情况),我认为您可能会让您的生活比需要的稍微复杂一些。

您没有理由不能在应用程序启动时打开连接并在应用程序的整个生命周期中保持连接打开状态,因为不应该有任何其他应用程序使用该数据库。

多年来我们一直在使用这种方法生产 WinCE 应用程序,并且从来没有遇到过问题。

Assuming that this is on a mobile device (which I believe is the case due to the SqlCE references), I think that you may be making your life slightly more complicated than needed.

There is no reason that you can't open a connection at application startup and leave it open through the life of the application since there shouldn't be any other applications using the database.

We've had production WinCE apps for years that use this approach and have never had an issue with it.

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