C# 泛型继承数据访问
您好,我想创建一个基类来继承,但我遇到了一些问题。
我有两个类,它们执行几乎相同的工作,但从不同的数据库获取它们使用的数据,并使用不同的内部数据结构来操作数据。 我希望在基础中有虚拟 doSomething 方法,并且最好在基础中有虚拟 dataAccess 方法。
第二个问题可以通过使用泛型来解决,但我不能使用泛型来解决第一个问题,因为我使用的 DBMl 上下文的构造函数不是无参数的。
我是不是把这一切都搞错了?我试图保持干燥,但似乎正在反对继承。
下面的示例代码。
class Foo {
private _ctx DBML.Database1; // Inherits from System.Data.Linq.DataContext
public Foo(string constring) {
_ctx = new DBML.Database1(constring);
}
private DoSomeThing() {
FooDataObj = DataAccess(1);
}
private FooDataObj DataAccess(int ID)
{
var v = from t in _ctx
where t.Id = ID
select new FooDataObj(t);
return v
}
}
class Bar {
private _ctx DBML.Database2; // Inherits from System.Data.Linq.DataContext
public Bar(string constring)
{
_ctx = new DBML.Database2(constring);
}
private DoSomeThing() {
BarDataObj = DataAccess(1);
}
private BarDataObj DataAccess(int ID) {
var v = from t in _ctx
where t.Id = ID
select new BarDataObj(t);
return v
}
}
Hi I am wanting to create a base class to inherit from but I am having some problems.
I have two classes which do almost identical work but get the data which they work with from different databases and use different internal data structures to manipulate the data.
I want to have virtual doSomething method in the base and ideally virtual dataAccess method in the base also.
The second problem can be solved through the use of generics but I cant use generics to solve the first problem as the constructor of the DBMl context I use is not parameterless.
Am I going about this all wrong. I am trying to be DRY but seem to be working against inheritance.
Example code below.
class Foo {
private _ctx DBML.Database1; // Inherits from System.Data.Linq.DataContext
public Foo(string constring) {
_ctx = new DBML.Database1(constring);
}
private DoSomeThing() {
FooDataObj = DataAccess(1);
}
private FooDataObj DataAccess(int ID)
{
var v = from t in _ctx
where t.Id = ID
select new FooDataObj(t);
return v
}
}
class Bar {
private _ctx DBML.Database2; // Inherits from System.Data.Linq.DataContext
public Bar(string constring)
{
_ctx = new DBML.Database2(constring);
}
private DoSomeThing() {
BarDataObj = DataAccess(1);
}
private BarDataObj DataAccess(int ID) {
var v = from t in _ctx
where t.Id = ID
select new BarDataObj(t);
return v
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Foo
和Bar
不应自行调用数据库构造函数,数据库对象应该是构造函数的参数(而不是连接字符串)。这个原则称为依赖注入,它将解决您的大部分问题。然后应该很容易创建一个新的通用类DataObjFactory
作为 Foo 和 Bar 的替代品。Foo
andBar
should not call the database constructor by themselves, the database object should be a parameter of the constructor (instead of the connection string). This principle is called Dependency Injection and will solve most of your problems. Should be easy then to create a new generic classDataObjFactory<DataObjType>
as a replacement for Foo and Bar.您应该创建一个基类来封装您想要共享的功能。在这种情况下,如果您在实体对象上使用接口,则可以重用数据访问代码。像这样:
然后你可以将 IUniqueEntity 接口应用到你的 Foo/BarDataObj,并从 FooBarBase 继承你的 Foo/Bar 类:
You should make a base class that encapsulates the functionality you want to share. In this case, you could reuse the data access code if you use an interface on your entity object. Something like this:
Then you can apply the IUniqueEntity interface to your Foo/BarDataObj, and inherit your Foo/Bar classes from FooBarBase: