???????????????????贸???
?????Omar Al Zabir ???????[ 2016/9/7 14:12:51 ] ??????????????? ??????? ????????????
??????????????小??????????渭??????????????????????????????????睢�
????????????????写?????????????????小??位?????????????????????校??????????????????纾�????????????????????File???????????????DummyFile?????????????????????????????????????????????????????????class???????????/???????谢???????????????????谩???????????????IFile??IDatabase????????????File??Database??
??????????????????????????????????????????????????????榘�????????????????????????????????????屑??????????纾�???????CunsomerData??????????LINQ to SQL???????????????????校?????????????????????????????
??????????????????TDD?????????????????????????????写????????????写??????????纾�??写????????????CustomerData??????????????????CustomerData????CustomerData???????????????InsertCustomer ??DeleteCustomer ?? GetCustomer????????????????????些????Customer???????????????? ?????????????????????????????????写??CustomerData????????????????????????????????写????????????????????懈????????TDD?????????????????????????????小????械??????????????????纾珻ustomerData????????SqlConnection?????????ISqlConnection ?????械?????????CustomerData????
??????????????????????
?????????????????????????????榉�????????写????????????????????????????????????????????????????校???????????????????????????????????????????????????? ???纾�?????????Stack ?????????????????Pop??????????????????械????????????????????????????????魏味??????Pop????????????????????????Pop?????????????????????????????????????Pop ??
???????BDD???????
???????????????校?????????械?????????????????LINQ??SQL???????????????????????????妾�???纾�??????????????????????????????妫�????????????????妫�?????校???????????屑???????????????????????????PageRepository ?????????婕癙age?????????????????????????o??????CRUD????????????????????GetPageById ????????PageId???????Page????????小?
????public class PageRepository : IPageRepository
????{
????#region Fields
????private readonly IDropthingsDataContext _database;
????private readonly ICache _cacheResolver;
????#endregion Fields
????#region Constructors
????public PageRepository(IDropthingsDataContext database?? ICache cacheResolver)
????{
????this._database = database;
????this._cacheResolver = cacheResolver;
????}
????#endregion Constructors
????#region Methods
????public Page GetPageById(int pageId)
????{
????string cacheKey = CacheSetup.CacheKeys.PageId(pageId);
????object cachedPage = _cacheResolver.Get(cacheKey);
????if (null == cachedPage)
????{
????var page = _database.GetSingle<Page?? int>(
????DropthingsDataContext.SubsystemEnum.Page??
????pageId??
????LinqQueries.CompiledQuery_GetPageById);
????page.Detach();
????_cacheResolver.Add(cacheKey?? page);
????return page;
????}
????else
????{
????return cachedPage as Page;
????}
????}
????}
????PageRepository???IDropthingsDataContext ???????????LINQ to SQL???????位DataContext ?????????锟�?LINQ??SQL????????DataContext??????????????貌??????????DataContext?????????????????????ICache?????????婕�???????????????校???????????????EnterpriseLibraryCache???????ICache ??
?????????????????????????PageRepository??????????妫�?? GetPageById??????PageId ?????????榛�?妫�?????????校???????????械?????屑???????????????妾�
????public void GetPage_Should_Return_A_Page_from_database_when_cache_is_empty_and_then_caches_it()
????{
????var cache = new Mock();
????var database = new Mock();
????IPageRepository pageRepository = new PageRepository(database.Object?? cache.Object);
????const int pageId = 1;
????var page = default(Page);
????var samplePage = new Page() { ID = pageId?? Title = "Test Page"?? ...};
????database
????.Expect(d => d.GetSingle<Page?? int>(
????DropthingsDataContext.SubsystemEnum.Page??
????1?? LinqQueries.CompiledQuery_GetPageById))
????.Returns(samplePage);
????"Given PageRepository and empty cache".Context(() =>
????{
????// cache is empty
????cache.Expect(c => c.Get(It.IsAny())).Returns(default(object));
????// It will cache the Page object afte loading from database
????cache.Expect(c =>
????c.Add(It.Is(cacheKey =>
????cacheKey == CacheSetup.CacheKeys.PageId(pageId))??
????It.Is(cachePage =>
????object.ReferenceEquals(cachePage?? samplePage))))
????.AtMostOnce().Verifiable();
????});
????"when GetPageById is called".Do(() =>
????page = pageRepository.GetPageById(1));
????"it checks in the cache first and finds nothing and then caches it".Assert(() =>
????cache.VerifyAll());
????"it loads the page from database".Assert(() =>
????database.VerifyAll());
????"it returns the page as expected".Assert(() =>
????{
????Assert.Equal(pageId?? page.ID);
????});
????}
????????????????????
?????????写?????????????????????????????貌????????????????????械?????????????????????????谩?????????????校???????cache??database????????芯?????????????????????????????纾�??????????????AspectF?????????????PageRepository ??????????????????PageRepository??????????????????????????????????妫�????????浠�?????????榛�?妫�?????????????????????????????校???????????屑?????????????????GetPageById??????AspectF ???????????
????public Page GetPageById(int pageId)
????{
????return AspectF.Define
????.Cache(_cacheResolver?? CacheSetup.CacheKeys.PageId(pageId))
????.Return(() =>
????_database.GetSingle<Page?? int>(DropthingsDataContext.SubsystemEnum.Page??
????pageId?? LinqQueries.CompiledQuery_GetPageById).Detach());
????}
??????
???路???
??????????????????
2023/3/23 14:23:39???写?貌??????????
2023/3/22 16:17:39????????????????????些??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???路???????路
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11