gpt4 book ai didi

c# - 自定义 "using" block

转载 作者:可可西里 更新时间:2023-11-01 09:01:11 25 4
gpt4 key购买 nike

我正在使用一个数据库,并且有一种情况我想关闭其中的一个特性。关闭该功能看起来像这样......

DatabaseContext.Advanced.UseOptimisticConcurrency = false;

开启它同样简单。这功能很好。但我对某些东西很好奇,想探索它......

是否可以像处理 dispose 和 unsafe 那样将其包装在“using” block 中?例如……

using(DatabaseContext.Advanced.UseOptimisticConcurrency = false){
// do things!
}

// the feature is turned back on automatically here!

更新

在 StackOverflow 的优秀人员的帮助下,我现在已经使我想要的行为完美运行。再次感谢。这是我的工作代码。不要介意冗长的文档。我只是那种把脑子里的所有东西都输入出来的程序员。

using System;

namespace Raven.Client {
/// <summary>
/// Used to emulate a series of transactions without optimistic concurrency in RavenDB
/// </summary>
/// <remarks>
/// This has been flagged as an 'abuse' of the IDisposable interface, which is something I will learn more about
/// and try to find a better solution. The purpose of this class is to make absolutely sure that we never use
/// a document session without optimistic concurrency unless we are completely certain it is what we want. I
/// elected to wrap this in a disposable block because that is an easy convention to remember, and it makes the
/// intention very clear to the others who may see this code.
/// Topics[0]: http://stackoverflow.com/questions/19643266/custom-using-blocks
/// Topics[1]: http://stackoverflow.com/questions/2101524/is-it-abusive-to-use-idisposable-and-using-as-a-means-for-getting-scoped-beha/2103158#2103158
/// Topics[2]: http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface
/// </remarks>
public class RavenPessimistic : IDisposable {
private readonly IDocumentSession RavenSession;

/// <summary>
/// Disable optimistic concurrency for this exact session only.
/// </summary>
/// <param name="session"></param>
public RavenPessimistic(IDocumentSession session) {
RavenSession = session;
RavenSession.Advanced.UseOptimisticConcurrency = false;
}

/// <summary>
/// Enable the optimistic concurrency again, so that we do not
/// ever use it unintentionally
/// </summary>
public void Dispose() {
RavenSession.Advanced.UseOptimisticConcurrency = true;
}
}

/// <summary>
/// An extension method to make it more convenient to get to this. This is probably not necessary, but I have been
/// anxious to see how RavenDB handles extension methods.
/// </summary>
public static class RavenSessionExtensions {
public static RavenPessimistic OpenPessimisticSession(this IDocumentSession documentSession) {
return new RavenPessimistic(documentSession);
}
}
}

然后,在我的实际代码中...

    /// <summary>
/// Edit the given item prototype.
/// </summary>
/// <param name="model">
/// A unique prototype to edit in the database.
/// </param>
/// <returns></returns>
[HttpPost]
[Route("items/edit/prototype")]
public JsonResult Prototype(Models.Items.Prototype model) {
if (ModelState.IsValid) {
// go through the prototype and make sure to set all of the
// mutation names to it.
foreach (var mutation in model.Mutations) {
mutation.Name = model.Name;
}

// we are updating an entry, instead of creating a new one,
// so temporarily turn off optimistic concurrency since we
// are 100% sure we want to overwrite the existing document.
using (RavenSession.OpenPessimisticSession()) {
RavenSession.Store(model);
RavenSession.SaveChanges();
}

// if this was successful, then return the result to the client
return Json(true, JsonRequestBehavior.AllowGet);
}

return Json(false, JsonRequestBehavior.AllowGet);
}

最佳答案

只需将其包装在 IDisposable 类中,您就可以恢复 Dispose 函数中的功能。

public class SomeClass : IDisposable
{
public SomeClass()
{
DatabaseContext.Advanced.UseOptimisticConcurrency = false;
}

public void Dispose()
{
DatabaseContext.Advanced.UseOptimisticConcurrency = true;
}
}

以上代码只是示例,您需要根据自己的需要进行调整。

关于c# - 自定义 "using" block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19643266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com