想了解如何将抽象的我的数据访问层我的、想了、如何将、抽象

2023-09-06 23:28:26 作者:最凉不过人心i

我正在写,用于目录文件和属性这些文件的元数据的应用程序。相册程序将是一个很好的比较,我想要做的事情。

I'm writing an application that is used to catalog files, and attribute those files with meta data. A photo album program would be a good comparison to what I'm trying to do.

我试图抽象我的程序和存储该文件的文件系统,并存储该元数据的文件中的数据库之间的接口。这是为了允许嘲笑和单元测试。此外,我希望能够建立我的工具,多个实现,可以利用不同的数据库格式,和或文件系统结构。

I'm trying to abstract the interface between my program and the file system which stores the files, and the database which stores the meta data for the files. This is to allow for mocking and unit testing. In addition, I'd like to be able to create multiple implementations of my tool that can leverage different database formats, and or file system structures.

例如我创建了一个接口, IFileSystemAdaptor ,这是用于读取和写入文件和相关的图元文件(缩略图和附件)的文件系统。 IFileSystemAdaptor的实际实施确定文件的存储位置,以何种结构。

For example I've created an interface, IFileSystemAdaptor, which is used to read and write files and associated metafiles (thumbnails and attachments) to a file system. Actual implementations of IFileSystemAdaptor decide where files are stored and in what structure.

public interface IFileSystemAdaptor
{
    void WriteFileData(string fileName, Stream data);
    Stream ReadFileData(string filename);
    void DeleteFileData(string filename);
    void ClearAllData();
    void WriteMetaFileData(string filename, string path, Stream data);
    Stream ReadMetaFileData(string filename, string path, Stream data);
    void DeleteMetaFileData(string filename, string path);
    void ClearMetaFilesData(string filename);
}

所以,现在我想要做的数据库连接类似的东西。我有一个的我想读取和写入,并从课程结构相当复杂,数据库。我希望能有实现,连接到SQL Server数据库,并使用无服务器的数据库,如SQL精简版另一个实现。

So now I'm trying to do something similar with the connection to the database. I have a fairly complex structure of classes which I want to read and write to and from a database. I'd like to be able to have implementations that connect to a SQL Server database, and another implementation that uses a serverless database such as SQL Lite.

我如何抽象我的课并在其中将支持多个数据库类型的方式数据库之间的数据访问层?此外,我怎么能允许在我的课继承关系反映在数据库呢?我想千元以下的类表继承模式p $ PFER数据库格式(见1我 previous问题)的。

How can I abstract the data access layer between my classes and the database in a way which will support multiple database types? Also how can I allow for inheritance relationships in my classes to be reflected in the database? I'd prefer a database format following the "class table inheritance" pattern (See one of my previous questions).

推荐答案

为什么要重新发明轮子?使用 NHibernate的

Why re-invent the wheel? Use nhibernate

一切都已经为你做了。你可以保持您的所有车型,并可以很容易地切换数据库引擎。

Everything is already done for you. You can keep all your models and it's easy to switch database engine.

我有我自己写的三种不同的DAL的/运筹学和管理学与用于SQL Server,MySQL和PostgreSQL和SQLite支持。但现在,我切换到NHibernate的替代。这不值得冒这个险完全按照你想要得到的一切。

I've myself written three different DAL's/ORMS with support for SQL Server, Mysql, postgresql and sqlite. But right now I'm switching to nhibernate instead. It's not worth the hassle to get everything exactly as you want it.

如果你还是想自己,你要记住,大多数数据库都增加了自己的智能功能,SQL,你必须处理这样做。你需要阅读上的SQL92标准,并坚持这些数据类型。

If you still want to do it yourself you have to remember that most databases have added their own "smart" features to SQL that you have to handle. You need to read up on the SQL92 standard and stick to those data types.

当插入你需要不同的方式处理该主键的retreival对于大多数的数据库引擎(有些发动机使用发电机来获得PK值插入前行,而另一些功能使用检索插入后的PK值)行

When inserting rows you need to handle retreival of the primary key differently for most database engines (some engines use generators to get a PK value BEFORE the insert the row while others have functions you use to retrieve the PK value AFTER the insert)

分页是每个数据库都有自己的执行了另一件事。 SQL Server中更像是一个黑客。

Paging is another thing that each db have their own implementation of. Sql servers is more like a hack.