如何在C#中继承System.Data.SQLite
我使用 System.Data.SQLite 和 C# 来访问 SQLite 数据库/表。出于懒惰和快速开发的原因,我创建了自己的类库,将一些 System.Data.SQLite 方法封装在一个方法中,并创建了许多常见的数据库例程(方法),让我在访问数据时减少工作量。
如果我继承 System.Data.SQLite 库而不是引用它将帮助我优化我的工作,这可能吗? ¿可以举个例子吗?
I use System.Data.SQLite and C# for accesing SQLite databases/tables. For lazy and fast development reasons, I created my own library of classes to encapsulate some of System.Data.SQLite methods in one method, and to create many common database routines (methods) that let me reduce my work when accessing to data.
If I would inherit System.Data.SQLite library instead of referencing it would help me to optimize my work, ¿is this possible? ¿may you give an example, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以从 SQLite 继承并对某些类进行添加,特别是 SQLiteConnection。但是,您将无法在任何地方使用自己的类,因为 SQLite 将在内部创建许多类,例如 SQLiteCommand 和 SQLiteParameter,并且您没有选项告诉 SQLite 使用您的自定义版本。有一个 SQLiteFactory,但它用于 ADO.NET 数据提供程序集成,并且不在 SQLite 内部使用。
将方法分开会更好。如果您希望它们感觉自己是库的一部分,可以使用 扩展方法
It's possible to inherit from SQLite and make additions to some of the classes, particularly SQLiteConnection. However, you won't be able to use your own classes everywhere as SQLite will internally create a lot of classes like SQLiteCommand and SQLiteParameter and you don't have an option to tell SQLite to use your custom versions. There is a SQLiteFactory, but this is used for ADO.NET data provider integration and is not used internally by SQLite.
You're much better off keeping your methods separate. If you want them to feel like they're part of the library you can use Extension Methods
这是一个很好的问题,七年后我没有找到太多答案!我只需要做一个简单的继承,发现它有点棘手(因为我并不完全熟悉约束泛型类型)。但这就是我最终得到的结果。
This is a great question and I didn't find much in the way of answers 7-years later! I just had to do a simple inherit and found it a little tricky (because I wasn't completely familiar with constraining a generic type). But here's what I ended up with that worked.