小编inc*_*lly的帖子

实现死锁异常的重试逻辑

我已经实现了一个通用存储库,并想知道是否有一种智能方法可以在出现死锁异常的情况下实现重试逻辑?

所有存储库方法的方法都应该相同.那么无论如何我可以避免在每一种方法中使用retry-count再次编写'try/catch - call方法'吗?

任何建议都是受欢迎的.

我的存储库代码:

public class GenericRepository : IRepository
{
    private ObjectContext _context;

    public List<TEntity> ExecuteStoreQuery<TEntity>(string commandText, params object[] parameters) where TEntity : class
    {
        List<TEntity> myList = new List<TEntity>();

        var groupData = _context.ExecuteStoreQuery<TEntity>(commandText, parameters);

        return myList;
    }


    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {          
        var entityName = GetEntityName<TEntity>();
        return _context.CreateQuery<TEntity>(entityName);
    }

    public IEnumerable<TEntity> GetAll<TEntity>() where TEntity : class
    {
        return GetQuery<TEntity>().AsEnumerable();
    }
Run Code Online (Sandbox Code Playgroud)

编辑:

1.Solution:

chris.house.00解决方案略微修改

 public static T DeadlockRetryHelper<T>(Func<T> repositoryMethod, int maxRetries) …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework try-catch repository-pattern database-deadlocks

33
推荐指数
3
解决办法
2万
查看次数

复合字符串格式的String.Format变量

我想在String.Format中将变量放入复合格式.含义

String str = String.Format("{0:[what should I put here]}", mydate, myFormat};
Run Code Online (Sandbox Code Playgroud)

所以结果将取决于myFormat.

myFormat = "yyyy" => str = "2015"
myFormat = "hh:mm:ss" => str = "08:20:20"
Run Code Online (Sandbox Code Playgroud)

我没有成功

String.Format("{0:{1}}", mydate, myFormat}
Run Code Online (Sandbox Code Playgroud)

也不

String.Format("{0:{{1}}}", mydate, myFormat}
Run Code Online (Sandbox Code Playgroud)

也不

String.Format("{0:\{1\}}", mydate, myFormat}
Run Code Online (Sandbox Code Playgroud)

感谢大家.

c# string string.format datetime composite

3
推荐指数
1
解决办法
355
查看次数