gpt4 book ai didi

c# - 在不使用 ORM 的情况下,在 C# 中创建独立于供应商的 dal 的最佳方法是什么?

转载 作者:太空宇宙 更新时间:2023-11-03 16:29:14 24 4
gpt4 key购买 nike

目前我有一个涉及两个数据库提供商的项目:

  1. SQLClient
  2. OleDb

我需要创建一个处理这些提供程序的 DAL。我想创建一个这样的界面:

public interface IDataAccessLayer
{
public string GetSomeData();
}

并单独实现(虽然很简单这是错误的方式)

public class SqlClientDal : IDataAccessLayer
{
public string GetSomeData() { }
}

public class OleDbDal : IDataAccessLayer
{
public string GetSomeData() { }
}

我看过这样的帖子:

.Net: how to create vendor independent Dataset, Tableadapters, bindings (DB decided at runtime) (很好,但没有提供任何示例)

Obtaining a DbProviderFactory

Creating a Data Access Layer (C#) (非常好,但表适配器依赖于供应商)

我想创建一个具有此功能的 DAL:

  1. 使用类型化数据表(使用 visual studio designer)
  2. 使用供应商独立数据适配器(手动创建)
  3. 不是 ORM

如何创建具有这些功能的简单 DAL?

最佳答案

考虑从以下类派生您的类:

System.Data.Common.DbConnection BaseConnection;
System.Data.Common.DbDataAdapter BaseAdapter;

namespace DAL
{
public class DataAccess : DbConnection
{
protected override DbTransaction BeginDbTransaction(System.Data.IsolationLevel isolationLevel)
{
throw new Exception("The method or operation is not implemented.");
}

public override void ChangeDatabase(string databaseName)
{
throw new Exception("The method or operation is not implemented.");
}

public override void Close()
{
throw new Exception("The method or operation is not implemented.");
}

public override string ConnectionString
{
get
{
throw new Exception("The method or operation is not implemented.");
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}

protected override DbCommand CreateDbCommand()
{
throw new Exception("The method or operation is not implemented.");
}

public override string DataSource
{
get { throw new Exception("The method or operation is not implemented."); }
}

public override string Database
{
get { throw new Exception("The method or operation is not implemented."); }
}

public override void Open()
{
throw new Exception("The method or operation is not implemented.");
}

public override string ServerVersion
{
get { throw new Exception("The method or operation is not implemented."); }
}

public override System.Data.ConnectionState State
{
get { throw new Exception("The method or operation is not implemented."); }
}
}
}

弄乱这些函数和类中的每一个。它非常灵活。

关于c# - 在不使用 ORM 的情况下,在 C# 中创建独立于供应商的 dal 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11402259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com