我编写了一个代码,该代码从2个不同的数据库返回一个列表。这两个dbcontext之间的联合字段是accountid
和email
(两者具有相同的值)。由于有2个不同的数据库,因此我不能在实体框架中使用join。所以我对每个块使用了一个嵌套的using和。这是我的代码:
namespace AdminMvc.Components.BankDepositHistory
{
public class BankDepositHistoryHelper
{
public static List<BankDepositHistoryItemDto> GetChangeRequestsList(int skip, int take, out int total, string name, string email, AvailableBankDepositStates state)
{
using (var myketAdsDB = new MyketAdsEntities())
{
using (var myketDB = new MyketReadOnlyDb())
{
#region DefaultQuery
var bankDepositHistories = myketAdsDB.BankDepositHistories.AsQueryable();
#endregion
#region Filtering
if (!string.IsNullOrWhiteSpace(name))
{
var emails = myketDB.AppDevelopers
.Where(n => n.RealName.Contains(name))
.Select(e => e.Email).ToList();
// emails.Add(email);
if (emails.Count > 0)
{
bankDepositHistories = bankDepositHistories.Where(e => emails.Contains(e.AccountId));
}
}
if (!string.IsNullOrWhiteSpace(email))
{
bankDepositHistories = bankDepositHistories.Where(a => a.AccountId.Contains(email));
}
if (state != AvailableBankDepositStates.All)
{
bankDepositHistories = state == AvailableBankDepositStates.Success ?
bankDepositHistories.Where(x => x.State == AvailableBankDepositStates.Success.ToString()) :
bankDepositHistories.Where(x => x.State == AvailableBankDepositStates.Fail.ToString());
}
else
{
bankDepositHistories = bankDepositHistories.
Where(x => x.State != BankDepositState.Start.ToString());
}
#endregion
#region GetingTotalpages
total = bankDepositHistories.Count();
#endregion
#region Pagination
var pageResult = bankDepositHistories.OrderByDescending(ba => ba.CreationDate).Skip(skip).Take(take).ToList();
#endregion
#region FillingDomainObjects
var emailFilter = pageResult.Select(r => r.AccountId).ToList();
var developers = myketDB.AppDevelopers.Where(a => emailFilter.Contains(a.Email)).
Select(r => new { r.RealName, r.Email }).ToList();
var result = pageResult
.Select(b => new BankDepositHistoryItemDto()
{
Id = b.Id,
AccountId = b.AccountId,
Amount = b.Amount,
ClientIp = b.ClientIp,
State = (BankDepositState)Enum.Parse(typeof(BankDepositState), b.State, true),
ReturnUrl = b.ReturnUrl,
AdditionalData = b.AdditionalData,
Gateway = b.Gateway,
CreationDate = b.CreationDate,
PaymentRefNumber = b.PaymentRefNumber,
Uuid = b.Uuid,
}).ToList();
foreach (var bankDepositHistory in result)
{
foreach (var developer in developers)
{
if (bankDepositHistory.AccountId == developer.Email)
{
bankDepositHistory.RealName = developer.RealName;
}
}
}
return result;
#endregion
}
}
}
我想知道是否有可能避免使用嵌套的use并为每个数据库编写一个单独的using。
答案 0 :(得分:1)
You can do what you're asking. The list of emails from the inner using affects the bankDepositHistories
, which are from the outer using, but that outer query isn't executed until later. (Also, the original inner using doesn't depend on anything in the outer, so can be moved outside of it).
So, get the email list first, using myketDB
:
List<Email> emails = new List<Email>();
using (var myketDB = new MyketReadOnlyDb())
{
if (!string.IsNullOrWhiteSpace(name))
{
emails = myketDB.AppDevelopers
.Where(n => n.RealName.Contains(name))
.Select(e => e.Email).ToList();
}
}
// original outer using is now after the above
Then do all the other logic by moving the outer using of myketAdsDB
in your original code below the using above. This is now one after the other, not nested.
If what you are doing does not have to be transactional, accessing the contexts sequentially is preferred because you don't have to extend the lifetime of the outer context for no reason. Running an inner inside of an outer extends the life of the outer.
答案 1 :(得分:1)
您的代码非常复杂。这是我最好的分离和简化方法:
public static List<BankDepositHistoryItemDto> GetChangeRequestsList(int skip, int take, out int total, string name, string email, AvailableBankDepositStates state)
{
var statesFilter = new Dictionary<AvailableBankDepositStates, Func<IQueryable<BankDepositHistory>, IQueryable<BankDepositHistory>>>()
{
{ AvailableBankDepositStates.All, bdh => bdh.Where(x => x.State != BankDepositState.Start.ToString()) },
{ AvailableBankDepositStates.Success, bdh => bdh.Where(x => x.State == AvailableBankDepositStates.Success.ToString()) },
{ AvailableBankDepositStates.Fail, bdh => bdh.Where(x => x.State == AvailableBankDepositStates.Fail.ToString()) },
};
List<string> emails = new List<string>();
ILookup<string, string> developers = null;
using (var myketDB = new MyketReadOnlyDb())
{
if (!string.IsNullOrWhiteSpace(name))
{
emails = myketDB.AppDevelopers.Where(n => n.RealName.Contains(name)).Select(e => e.Email).ToList();
}
developers = myketDB.AppDevelopers.ToLookup(x => x.Email, x => x.RealName);
}
using (var myketAdsDB = new MyketAdsEntities())
{
var bankDepositHistories = myketAdsDB.BankDepositHistories.AsQueryable();
if (emails.Count() > 0)
{
bankDepositHistories = bankDepositHistories.Where(e => emails.Contains(e.AccountId));
}
if (!string.IsNullOrWhiteSpace(email))
{
bankDepositHistories = bankDepositHistories.Where(a => a.AccountId.Contains(email));
}
bankDepositHistories = statesFilter[state](bankDepositHistories);
total = bankDepositHistories.Count();
var result =
bankDepositHistories
.OrderByDescending(ba => ba.CreationDate)
.Skip(skip)
.Take(take)
.ToList()
.Select(b => new BankDepositHistoryItemDto()
{
Id = b.Id,
AccountId = b.AccountId,
Amount = b.Amount,
ClientIp = b.ClientIp,
State = (BankDepositState)Enum.Parse(typeof(BankDepositState), b.State, true),
ReturnUrl = b.ReturnUrl,
AdditionalData = b.AdditionalData,
Gateway = b.Gateway,
CreationDate = b.CreationDate,
PaymentRefNumber = b.PaymentRefNumber,
Uuid = b.Uuid,
RealName = developers[b.AccountId].LastOrDefault(),
}).ToList();
return result;
}
}
这是我必须编写以安全重构的代码:
public enum AvailableBankDepositStates
{
All, Success, Fail
}
public enum BankDepositState
{
Start
}
public class BankDepositHistoryItemDto
{
public string AccountId;
public BankDepositState State;
public DateTime CreationDate;
public string RealName;
}
public class MyketAdsEntities : IDisposable
{
public IEnumerable<BankDepositHistory> BankDepositHistories;
public void Dispose()
{
throw new NotImplementedException();
}
}
public class MyketReadOnlyDb : IDisposable
{
public IEnumerable<AppDeveloper> AppDevelopers;
public void Dispose()
{
throw new NotImplementedException();
}
}
public class BankDepositHistory
{
public string AccountId;
public string State;
public DateTime CreationDate;
}
public class AppDeveloper
{
public string RealName;
public string Email;
}