??????????1?д???????????????????????????縴???Щ????????к??LINQ???????ж?????????????????????????mock???????????????????
??????????仯?????????????????????????仯??????????????——???????????????????????????????????????????????????
??????????????????? ????????????????????????????????????????????????????????????????????y????????????????????е????????????????????????????д????????????????????
???????????????.Where(x => x.Id == Id).FirstOrDefault(); ??????????? _refreshTokens ?? context.Set<RefreshToken>()???????????? List<RefreshToken>????????????? System.Data.Entity.DbSet ????2??????????????????
??????Visual Studio?а?F12?????????????????????1??????????????IQueryable——DbSet?????IQueryable????List????????IQueryable?????AsQueryable??????????????????????????????????????????????????2??RefreshTokenRepository???1??RefreshTokenRepository??
public class RefreshTokenRepository : IRefreshTokenRepository
{
private IQueryable<RefreshToken> _refreshTokens;
public RefreshTokenRepository()
{
}
public async Task<RefreshToken> FindById(string Id)
{
return _refreshTokens.Where(x => x.Id == Id).FirstOrDefault();
}
}
????????????????????——??2????????????????????????δ???????????θ???????????洢?????????????е?_refreshTokens??????????????????????_refreshTokens?????????????
??????????????????????????????????????????????——????Interface????
?????????????????????????_refreshTokens?????????????????????IUnitOfWork?ɡ?IUnitOfWork????????£?
????public interface IUnitOfWork : IDisposable
????{
????IQueryable<TEntity> Set<TEntity>() where TEntity : class;
????}
????????RefreshTokenRepository???????IUnitOfWork????_refreshTokens?????
public class RefreshTokenRepository : IRefreshTokenRepository
{
private IQueryable<RefreshToken> _refreshTokens;
public RefreshTokenRepository(IUnitOfWork unitOfWork)
{
_refreshTokens = unitOfWork.Set<RefreshToken>();
}
public async Task<RefreshToken> FindById(string Id)
{
return await _refreshTokens.Where(x => x.Id == Id).FirstOrDefaultAsync();
}
}
???????????????????洢????????????????FileStorageUnitOfWork??
publicclassFileStorageUnitOfWork:IUnitOfWork
{
publicIQueryable<TEntity>Set<TEntity>()whereTEntity:class
{
returnReadFromFile<TEntity>().AsQueryable<TEntity>();
}
privateIList<TEntity>ReadFromFile<TEntity>()
{
IList<TEntity>entities=null;
varjsonFilePath=HostingEnvironment.MapPath(string.Format("~/App_Data/{0}.json"??typeof(TEntity)));
if(File.Exists(jsonFilePath))
{
varjson=File.ReadAllText(jsonFilePath);
entities=JsonConvert.DeserializeObject<List<TEntity>>(json);
}
if(entities==null)entities=newList<TEntity>();
returnentities;
}
}
?????????????????洢??????????????Entity Framework??????EfUnitOfWork??EF???????????????
????public class EfUnitOfWork : DbContext?? IUnitOfWork
????{
????public new IQueryable<TEntity> Set<TEntity>() where TEntity : class
????{
????return base.Set<TEntity>();
????}
????}
???????????????????????IOC??????????Unity?????????UnitOfWork??
???????????洢?????FileStorageUnitOfWork??
????container.RegisterType<IUnitOfWork?? FileStorageUnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>());
????????????洢?????EfUnitOfWork??
????container.RegisterType<IUnitOfWork?? EfUnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>());
???????????????????????oauth refresh token??????????????洢?????????洢?????????洢????????洢???????????????????NoSQL?????????????????
????д??????????????????????????????ī???——IUnitOfWork??????????????仯??????£?????Repository????????????????IUnitOfWork??????Repositroy????IUnitOfWork????????????????IUnitOfWork??????У??????????????????????????????????????????????С????????????
??????????
?????仯?????????£?
????Presentation??-WebAPI??CNBlogsRefreshTokenProvider
????Application??-????IRefreshTokenService
????Application??-????RefreshTokenService
????Domain??-??壺RefreshToken
????Repository??-????IRefreshTokenRepository
????Repository??-????RefreshTokenRepository
????UnitOfWork??-????IUnitOfWork
????UnitOfWork??-????FileStorageUnitOfWork??EfUnitOfWork