整个类的 C# IDisposable 对象

发布于 2024-09-26 11:43:04 字数 770 浏览 11 评论 0原文

我目前正在进行的项目在类中的每个方法中都使用 IDisposable 对象。在每个方法开始时重新输入 using 块已经开始变得乏味,并且想知道是否有一种方法可以指定在类的每个方法中使用的一次性变量?

public static class ResourceItemRepository
{
    public static ResourceItem GetById(int id)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
    public static List<ResourceItem> GetInCateogry(int catId)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
    public static ResourceItem.Type GetType(int id)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
}

The project that I'm working on at the moment uses an IDisposable object in every method in a class. It has started getting tedious re-typing the using block at the start of every method, and was wondering if there was a way to specify a disposable variable for use in every method of the class?

public static class ResourceItemRepository
{
    public static ResourceItem GetById(int id)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
    public static List<ResourceItem> GetInCateogry(int catId)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
    public static ResourceItem.Type GetType(int id)
    {
        using (var db = DataContextFactory.Create<TestDataContext>())
        {
            // Code goes here...
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

南烟 2024-10-03 11:43:04

不,没有什么特别适合这个的。您可以写:

public static ResourceItem GetById(int id)
{
    WithDataContext(db =>
    {
        // Code goes here...
    });
}

// Other methods here, all using WithDataContext

// Now the only method with a using statement:
private static T WithDataContext<T>(Func<TestDataContext, T> function)
{
    using (var db = DataContextFactory.Create<TestDataContext>())
    {
        return function(db);
    }
}

不过,我不确定这是否特别有益。

(请注意,我必须将其从原始版本中的 Action 更改为 Func 因为您希望能够从你的方法。)

No, there's nothing particularly geared towards this. You could write:

public static ResourceItem GetById(int id)
{
    WithDataContext(db =>
    {
        // Code goes here...
    });
}

// Other methods here, all using WithDataContext

// Now the only method with a using statement:
private static T WithDataContext<T>(Func<TestDataContext, T> function)
{
    using (var db = DataContextFactory.Create<TestDataContext>())
    {
        return function(db);
    }
}

I'm not sure that it would be particularly beneficial though.

(Note that I've had to change it from Action<TestDataContext> in my original version to Func<TestDataContext, T> as you want to be able to return values from your methods.)

毁梦 2024-10-03 11:43:04

坦率地说,我会保留冗长的代码,但使用片段而不是每次都键入它。
使用特殊工具创建自己的代码片段,或者使用文本替换工具,例如短信

Frankly, i'd keep the verbose code, but using a snippet instead of typing it all each time.
Either create your own snippet with a special tool or use text-replacement tools like Texter

半衾梦 2024-10-03 11:43:04

也许简单的重构是您可以做的最好的事情,而无需求助于 PostSharp 之类的东西:

public static class ResourceItemRepository {
  public static ResourceItem GetById(int id) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  public static List<ResourceItem> GetInCateogry(int catId) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  public static ResourceItem.Type GetType(int id) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  private static TestDataContext CreateDataContext() {
    return DataContextFactory.Create<TestDataContext>();
  }
}

Maybe a simple refactoring is the best that you can do without resorting to something like PostSharp:

public static class ResourceItemRepository {
  public static ResourceItem GetById(int id) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  public static List<ResourceItem> GetInCateogry(int catId) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  public static ResourceItem.Type GetType(int id) {
    using (var db = CreateDataContext()) {
      // Code goes here... 
    }
  }
  private static TestDataContext CreateDataContext() {
    return DataContextFactory.Create<TestDataContext>();
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文