C# 泛型继承数据访问

发布于 2024-12-09 07:24:13 字数 1067 浏览 0 评论 0原文

您好,我想创建一个基类来继承,但我遇到了一些问题。

我有两个类,它们执行几乎相同的工作,但从不同的数据库获取它们使用的数据,并使用不同的内部数据结构来操作数据。 我希望在基础中有虚拟 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 技术交流群。

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

发布评论

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

评论(2

寄居人 2024-12-16 07:24:13

FooBar 不应自行调用数据库构造函数,数据库对象应该是构造函数的参数(而不是连接字符串)。这个原则称为依赖注入,它将解决您的大部分问题。然后应该很容易创建一个新的通用类 DataObjFactory 作为 Foo 和 Bar 的替代品。

Foo and Bar 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 class DataObjFactory<DataObjType> as a replacement for Foo and Bar.

幻想少年梦 2024-12-16 07:24:13

您应该创建一个基类来封装您想要共享的功能。在这种情况下,如果您在实体对象上使用接口,则可以重用数据访问代码。像这样:

interface IUniqueEntity
{
  int ID { get; }
}

abstract class FooBarBase<TEntity>
  where TEntity : class, IUniqueEntity
{
  private DataContext _ctx;

  public Foo(DataContext context)    {
      _ctx = context;
  }

  protected abstract DoSomeThing();

  protected TEntity DataAccess(int ID)
  {
      return _ctx.GetTable<TEntity>()
        .First(e => object.Equals(e.ID, ID);
  }
}

然后你可以将 IUniqueEntity 接口应用到你的 Foo/BarDataObj,并从 FooBarBase 继承你的 Foo/Bar 类:

class Foo : FooBarBase<FooDataObj> {
  public Foo(DBML.Database1 context) : base(context)  {
  }

  protected override DoSomeThing()   {
      var myobj = DataAccess(1);
  }
}

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:

interface IUniqueEntity
{
  int ID { get; }
}

abstract class FooBarBase<TEntity>
  where TEntity : class, IUniqueEntity
{
  private DataContext _ctx;

  public Foo(DataContext context)    {
      _ctx = context;
  }

  protected abstract DoSomeThing();

  protected TEntity DataAccess(int ID)
  {
      return _ctx.GetTable<TEntity>()
        .First(e => object.Equals(e.ID, ID);
  }
}

Then you can apply the IUniqueEntity interface to your Foo/BarDataObj, and inherit your Foo/Bar classes from FooBarBase:

class Foo : FooBarBase<FooDataObj> {
  public Foo(DBML.Database1 context) : base(context)  {
  }

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