This content was uploaded by our users and we assume good faith they have the permission to share this book. If you own the copyright to this book and it is wrongfully on our website, we offer a simple DMCA procedure to remove your content from our site. Start by pressing the button below!
QYTRcBcPcTT]cXch 4]cXchBcPcT<^SXUXTS* l l %VERY ENTITY GENERATED BY THE %& $ESIGNER INHERITS FROM THE CLASS 4]cXch>QYTRc SO THE FIRST STEP IS TO BE SURE IN THIS CASE THAT THE OBJECT PASSED TO THE <PaZ3Xach METHOD IS OF TYPE 4]cXch>QYTRc SO THAT YOU CAN CAST IT BACK AND PLAY WITH THE PROPERTY 4]cXchBcPcT 4HE CODE FIRST VERIFIES THAT THE OBJECT IS NOT NULL )F IT IS NULL AN EXCEPTION IS THROWN .EXT IT VERIFIES THAT THE OBJECT IS NOT ALREADY ATTACHED )F IT IS NOT ATTACHED THE CODE ATTACHES IT AND THEN CHANGES ITS STATE TO MODIFIED USING THE HELPER CLASS >QYTRcBcPcT
QYTRc2^]cTgc SESSION 5NFORTUNATELY THE %& DOESNT PROVIDE A METHOD FOR DISCOVERING IF AN ENTITY HAS BEEN DETACHED OR NOT FROM A SPECIFIC SESSION SO YOULL NEED TO EXTEND THE FRAMEWORK BY CREATING AN EXTENSION METHOD
■ .OTE %XTENSION METHODS ENABLE YOU TO ADD METHODS TO EXISTING TYPES WITHOUT CREATING A NEW DERIVED TYPE RECOMPILING OR OTHERWISE MODIFYING THE ORIGINAL TYPE %XTENSION METHODS ARE A SPECIAL KIND OF STATIC METHOD BUT THEY ARE CALLED AS IF THEY WERE INSTANCE METHODS ON THE EXTENDED TYPE &OR CLIENT CODE WRITTEN IN # AND 6ISUAL "ASIC THERE IS NO APPARENT DIFFERENCE BETWEEN CALLING AN EXTENSION METHOD AND THE METHODS THAT ARE ACTUALLY DEFINED IN A TYPE
)N ORDER TO ADD AN EXTENSION METHOD YOU NEED A STATIC CLASS SO ON THE $!, PROJECT ADD A NEW STATIC CLASS AND CALL IT 4]cXch5aP\Tf^aZ4gcT]bX^]b )N THIS CLASS YOU WILL ADD THE FIRST EXTENSION METHOD USING THE SIGNATURE SHOWN IN ,ISTING ,ISTING %NTITY &RAMEWORK %XTENSION -ETHOD _dQ[XR bcPcXR R[Pbb 4]cXch5aP\Tf^aZ4gcT]bX^]b j _dQ[XR bcPcXR Q^^[ 8b0ccPRWTSC^cWXb >QYTRc2^]cTgc R^]cTgc ^QYTRc T]cXch j >QYTRcBcPcT4]cah T]cah* Q^^[ Xb0ccPRWTS , UP[bT* XU R^]cTgc>QYTRcBcPcT
QYTRcBcPcT4]cah R^]cTgc2aTPcT4]cXch:ThT]cXch6TcCh_T=P\T b T]cXch ^dc T]cah j Xb0ccPRWTS , T]cahBcPcT ,, 4]cXchBcPcT3TcPRWTS* l QYTRc bT]STa 4eT]c0aVb T j C4eT]c0aVb aTP[0aVb , C4eT]c0aVbT* cWXbaTP[7P]STabT]STa aTP[0aVb* aTcda] cadT* l l l ,ISTING CREATES A NEW GENERIC FTPZ4eT]c;XbcT]Ta THAT CAN LISTEN FOR ANY EVENT THAT INHERITS FROM THE BhbcT\4eT]c0aVb CLASS WHICH IS THE BASE CLASS FOR ANY EVENT HANDLERS ARGUMENT OBJECT IN .%4 )T THEN MANUALLY IMPLEMENTS THE 8FTPZ4eT]c;XbcT]Ta INTERFACE WHICH EXPOSES A SINGLE METHOD ATRTXeTFTPZ4eT]c 4HIS METHOD SIMPLY INTERCEPTS THE EVENT AND FIRES THE CORRESPONDING HANDLER ]2P]4gTRdcT2WP]VTS* l RaTPcT P ]Tf R^]bcadRc^a cWPc fX[[ PSS P [XbcT]Ta _dQ[XR <ee\2^\\P]S 0RcX^]+^QYTRc- TgTRdcT 5d]R+^QYTRc Q^^[- RP]4gTRdcT 8=^cXUh?a^_Tach2WP]VTS b^daRT bcaX]V _a^_Tach=P\T ) QPbTTgTRdcT RP]4gTRdcT j ?a^_Tach2WP]VTS4eT]c a4\_ch>aXVX]P[CTgc* l 0SS;XbcT]Ta+5^a\PcEXTf<^ST[-cWXb g ,- g>aXVX]P[CTgc 0SS;XbcT]Ta+5^a\PcEXTf<^ST[-cWXb g ,- g5^a\PccTSCTgc* _TaP]S Pb
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
T[bT j Xb0ccPRWTS , cadT* l aTcda] Xb0ccPRWTS* l l 4HE SIGNATURE cWXb IN THE METHOD TELLS THE COMPILER THAT YOU ARE EXTENDING AN OBJECT OF TYPE >QYTRc2^]cTgc WITH A METHOD CALLED 8b0ccPRWTSC^ 7HAT THE CODE IS DOING IS CALLING THE METHOD 2aTPcT4]cXch:Th OF THE >QYTRc2^]cTgc TO INDICATE WHETHER THE ENTITY IS A NEW ENTITY OR AN EXISTING ONE )T DOES THIS BY READING THE )$ PROPERTY OF THE ENTITY WHICHIS EQUAL TO ONLY IF THE OBJECT IS BRAND NEW )N THIS CASE THERE WILL BE TWO TESTS /NE WILL VERIFY THAT IT CAN EXECUTE AN UPDATE AND THE SECOND ONE WILL VERIFY THAT IT CAN UPDATE A DETACHED ENTITY FROM ANOTHER UNIT OF WORK 4HE FIRST TEST SHOWN IN ,ISTING VERIFIES THAT IN THE SAME SESSION THE OBJECT IS SUCCESSFULLY UPDATED ,ISTING ! 4EST 4HAT 6ERIFIES 4HAT AN /BJECT (AS "EEN 5PDATED JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
ePa RWP]VTSA^[T , d^fBTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a8S ,, a^[T8S 5Xabc>a3TUPd[c* 0bbTac8b=^c=d[[Tg_TRcTSA^[T* 0bbTac0aT4`dP[RWP]VTSA^[T=P\T ?^fTaDbTab* l l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l 4HE CODE INSERTS A NEW A^[T ENTITY IN THE DATABASE THEN RETRIEVES THE ENTITY USING THE 8S PROPERTY WHICH SHOULD NOW NOT EQUAL THUS VERIFYING THAT THE ENTITY EXISTS .EXT THE CODE MODIFIES THE ENTITY BY CHANGING THE =P\T PROPERTY AND THEN VERIFIES THAT THE ENTITY HAS BEEN SUCCESSFULLY UPDATED BY EXECUTING ANOTHER QUERY AGAINST THE %& SESSION )F THE PROPERTY HAS BEEN CHANGED THE =P\T PROPERTY SHOULD NOW BE EQUAL TO ?^fTaDbTab 4HE SECOND TEST EXECUTES THE SAME STEPS USING TWO DIFFERENT UNIT OF WORK IN THIS WAY YOU CAN BE SURE THAT THE ENTITY IS DETACHED WHEN YOU TRY TO SAVE IT ,ISTING SHOWS CODE THAT UPDATES A DETACHED ENTITY ,ISTING 5PDATING A $ETACHED %NTITY JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
dbX]V 8D]Xc>UF^aZ d^f , ]Tf D]Xc>UF^aZ j RWP]VT Xc d_SPcTbA^[T=P\T , ?^fTaDbTab* d^fBcPacCaP]bPRcX^]* d^f<PaZ3Xachd_SPcTbA^[T* 0bbTac8bCadTd_SPcTbA^[T4]cXchBcPcT ,, 4]cXchBcPcT<^SXUXTS* d^f2^\\XcCaP]bPRcX^]* aTcaXTeT Xc PVPX] ePa RWP]VTSA^[T , d^fBTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a8S ,, ^aXVX]P[A^[T8S 5Xabc>a3TUPd[c* 0bbTac8b=^c=d[[d_SPcTbA^[T* 0bbTac0aT4`dP[RWP]VTSA^[T=P\T ?^fTaDbTab* l l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l
Final Consideration for the Unit of Work 9OU SAW HOW TO CREATE A UNIT OF WORK FOR THE %NTITY &RAMEWORK USING THE 4$$ APPROACH THAT CONSISTS OF CONSTANTLY TESTING THE CODE YOU ARE WRITING BY VERIFYING THE CODE AGAINST THE APPLICATION REQUIREMENTS 7E CREATED THESE TESTS USING THE A^[T ENTITY JUST BECAUSE ITS THE SIMPLEST ENTITY IN THE DOMAIN MODEL AND BECAUSE WE NEED A REAL 4]cXch>QYTRc TO TEST THE EFFICIENCY OF THE UNIT OF WORK /F COURSE EVERY TIME WE RUN A TEST WE POLLUTE THE DATABASE WITH A MOCKUP A^[T ENTITY THAT IS PROBABLY NOT NECESSARY &OR THIS REASON ) SUGGEST YOU CREATE BOTH AN )NITIALIZE AND A #LEAN5P METHOD IN EVERY TEST CLASS BY MARKING THEM WITH THE ATTRIBUTES JCTbc8]XcXP[XiTL AND JCTbc2[TP]d_L SO THEY WILL BE EXECUTE BEFORE AND AFTER ALL THE TESTS OF THAT TEST CLASS ARE RUN SEE ,ISTING )N THIS WAY YOU CAN CLEAR UP THE POLLUTION FROM THE DATABASE CAUSED BY THE TEST AND LEAVE A NEW BLANK DATABASE ,ISTING #LEAN5P 4EST -ETHODS JCTbc8]XcXP[XiTL _dQ[XR e^XS CTbc8]XcXP[XiT j dbX]V ePa d^f , ]Tf CX\TCaPRZTa<^ST[2^]cPX]Ta j aT\^eT P[[ cWT \^RZd_b a^[Tb Ua^\ cWT SPcPQPbT U^aTPRW A^[T a^[T X] d^fA^[Tb j d^f3T[TcT>QYTRca^[T* l d^fBPeT2WP]VTbBPeT>_cX^]b0RRT_c0[[2WP]VTb0UcTaBPeT* l l
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
JCTbc2[TP]d_L _dQ[XR e^XS CTbc2[TP]d_ j l
4HE 2EPOSITORY !T THE BEGINNING OF THIS CHAPTER ) MENTIONED THAT THE REPOSITORY PATTERN IS USED TO ADD AN ADDITIONAL ABSTRACTION LAYER TO THE $!, SO THAT THE OTHER LAYERS WONT HAVE DIRECT ACCESS TO THE %& SESSION BUT WILL BE FORCED TO USE THE REPOSITORY CLASSES EXPOSED BY THE $!, ! REPOSITORY CLASS SHOULD PROVIDE AT LEAST A SET OF METHODS THAT ALLOW YOU TO EXECUTE THE BASIC #25$ OPERATIONS PLUS A BASIC SET OF QUERIES LIKE THE INTERFACE IN ,ISTING ,ISTING 4HE 8AT_^bXc^ah )NTERFACE dbX]V BhbcT\2^[[TRcX^]b6T]TaXR* ]P\Tb_PRT 0?A4BBCX\TCaPRZTa3PcP;PhTa j _dQ[XR X]cTaUPRT 8AT_^bXc^ah +C- fWTaT C ) 4]cXch>QYTRc j e^XS 8]bTacC T]cXch* e^XS D_SPcTC T]cXch* e^XS 3T[TcTC T]cXch* 8;Xbc+C- 6Tc0[[* C 6Tc1h8SX]c XS* l l )N THIS CASE YOU ARE GOING TO IMPLEMENT THE CODE IN AN ABSTRACT AT_^bXc^ah1PbT CLASS THAT WILL BE USED AS A BASE CLASS FOR EACH CONCRETE REPOSITORY SEE ,ISTING 9OU WANT TO MAKE THE BASE CLASS ABSTRACT BECAUSE YOU DONT WANT IT TO BE USED DIRECTLY 5NFORTUNATELY BY DOING THAT YOU CANT TEST THE IMPLEMENTATION OF THE BASE REPOSITORY OUTSIDE THE SCOPE OF A CONCRETE REPOSITORY BECAUSE AN ABSTRACT CLASS CANT BE TESTED ,ISTING 'ENERIC 2EPOSITORY #LASS _dQ[XR PQbcaPRc R[Pbb AT_^bXc^ah1PbT+C- ) 8AT_^bXc^ah+C- fWTaT C ) 4]cXch>QYTRc j _aXePcT aTPS^][h 8D]Xc>UF^aZ d]Xc>UF^aZ* _dQ[XR AT_^bXc^ah1PbT8D]Xc>UF^aZ d]Xc>UF^aZ j cWXbd]Xc>UF^aZ , d]Xc>UF^aZ* l l
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
4HIS CODE USES THE $EPENDENCY )NJECTION PATTERN TO INJECT A CURRENT UNIT OF WORK INTO THE REPOSITORY .OW YOU NEED TO IMPLEMENT THE METHODS EXPOSED BY THE 8AT_^bXc^ah INTERFACE AND TEST THEM "ECAUSE YOU CANT TEST AN ABSTRACT CLASS YOU WILL HAVE TO CREATE AN ADDITIONAL CLASSA^[TAT_^bXc^ah WHICH OF COURSE IS A REPOSITORY FOR THE A^[T ENTITY SEE ,ISTING ,ISTING 2OLE 2EPOSITORY _dQ[XR bTP[TS R[Pbb A^[TAT_^bXc^ah ) AT_^bXc^ah1PbT+A^[Tj _dQ[XR A^[TAT_^bXc^ah8D]Xc>UF^aZ d]Xc>UF^aZ ) QPbTd]Xc>UF^aZ j l l 4HE FINAL OBJECT IS THE CTbc FIXTURE SO IN THE 4EST PROJECT YOU HAVE TO CREATE A NEW UNIT TEST CLASS AND CALL IT A^[TAT_^bXc^ahCTbc5XgcdaT THEN ADD INITIALIZE AND CLEANUP METHODS AS SHOWN IN ,ISTING ,ISTING A^[TAT_^bXc^ah 4EST &IXTURE JCTbc2[PbbL _dQ[XR R[Pbb A^[TAT_^bXc^ah5XgcdaT j _aXePcT 8D]Xc>UF^aZ d^f* JCTbc8]XcXP[XiTL _dQ[XR e^XS 8]XcXP[XiT j d^f , ]Tf D]Xc>UF^aZ* l JCTbc2[TP]d_L _dQ[XR e^XS 2[TP]D_ j R[TP] d_ cWT SPcPQPbT d^f3Xb_^bT* l l
Add a New Role 7HEN YOU WORK WITH THE REPOSITORY PATTERN YOU HAVE TO MANAGE THE OPERATION BY USING A UNIT OF WORK SO THAT IF SOMETHING GOES WRONG YOU STILL HAVE THE POWER TO ROLL BACK EVERYTHING OUTSIDE THE SCOPE OF THE REPOSITORY OBJECT 4HE CODE IN ,ISTING IS IN TWO PIECES THE FIRST IS IN THE AT_^bXc^ah1PbT OBJECT AND IT IS THE IMPLEMENTATION OF THE 8]bTac+C- METHOD THE SECOND IS IN THE A^[TAT_^bXc^ah5XgcdaT TEST CLASS AND IS USED TO TEST THE METHOD
u
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
,ISTING 4HE 8]bTac+C- -ETHOD 1PbT aT_^bXc^ah _Pac _dQ[XR e^XS 8]bTacC T]cXch j d]Xc>UF^aZ<PaZ=TfT]cXch* l C33 JCTbc
Delete an Existing Role 4HE DELETE METHOD IS PRETTY STRAIGHTFORWARD YOU DONT HAVE TO DO ANYTHING MORE THAN CALL THE UNIT OF WORK <PaZ3T[TcTS+C- METHOD INSIDE THE AT_^bXc^ah1PbT CONTEXT AS SHOWN IN ,ISTING ,ISTING AT_^bXc^ah1PbT 3T[TcT+C- -ETHOD AND 4$$ AT_^bXc^ah1PbT R^ST _dQ[XR e^XS 3T[TcTC T]cXch j d]Xc>UF^aZ<PaZ3T[TcTST]cXch* l C33 JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
cah j PSS cWT T]cXch d^fBcPacCaP]bPRcX^]* ePa aT_^bXc^ah , ]Tf A^[TAT_^bXc^ahd^f* ePa a^[T , ]Tf A^[T j=P\T , 0S\X]XbcaPc^ab 3TbRaX_cX^] , 0S\X]XbcaPc^a 6a^d_l* aT_^bXc^ah8]bTaca^[T* d^f2^\\XcCaP]bPRcX^]* 0bbTac8bCadTa^[T8S - * ST[TcT cWT T]cXch d^fBcPacCaP]bPRcX^]* aT_^bXc^ah3T[TcTa^[T* 0bbTac8bCadTa^[T4]cXchBcPcT ,, 4]cXchBcPcT3T[TcTS* d^f2^\\XcCaP]bPRcX^]* cah VTc Xc ePa Tg_TRcTSA^[T , d^fBTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a8S ,, a^[T8S 5Xabc>a3TUPd[c* 0bbTac8b=d[[Tg_TRcTSA^[T* l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l ,ISTING CREATES A NEW A^[T AND SAVES IT IN THE DATABASE )T THEN DELETES THE A^[T USING THE 2EPOSITORYS METHOD AND COMMITS THE TRANSACTION &INALLY IT TRIES TO RETRIEVE THE A^[T TO MAKE SURE IT DOESNT EXIST ANYMORE IN THE DATABASE
Update an Existing Role 4HE D_SPcT METHOD FOLLOWS THE SAME PATTERN AS THE 8]bTac AND THE 3T[TcT METHODS )T CALLS THE UNIT OF WORK METHODS AND WAITS FOR A TRANSACTION TO BE COMMITTED IN ORDER TO PERSIST THE CHANGES TO THE DATABASE AS SHOWN IN ,ISTING ,ISTING 4HE D_SPcT %NTITY -ETHOD AND 4$$ AT_^bXc^ah1PbT \TcW^S _dQ[XR e^XS D_SPcTC T]cXch j d]Xc>UF^aZ<PaZ3XachT]cXch* l C33 JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
cah j PSS cWT T]cXch d^fBcPacCaP]bPRcX^]* ePa aT_^bXc^ah , ]Tf A^[TAT_^bXc^ahd^f* ePa a^[T , ]Tf A^[T j =P\T , 0S\X]XbcaPc^ab 3TbRaX_cX^] , 0S\X]XbcaPc^a 6a^d_ l* aT_^bXc^ah8]bTaca^[T* d^f2^\\XcCaP]bPRcX^]* 0bbTac8bCadTa^[T8S - * \^SXUh cWT T]cXch d^fBcPacCaP]bPRcX^]* a^[T=P\T , ?^fTaDbTab* aT_^bXc^ahD_SPcTa^[T* d^f2^\\XcCaP]bPRcX^]* cah VTc Xc ePa Tg_TRcTSA^[T , d^fBTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a8S ,, a^[T8S 5Xabc>a3TUPd[c* 0bbTac0aT=^c4`dP[Tg_TRcTSA^[T=P\T 0S\X]XbcaPc^ab* 0bbTac0aT4`dP[Tg_TRcTSA^[T=P\T ?^fTaDbTab* l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l 4HE PRECEDING CODE CREATES A NEW A^[T AND COMMITS IT TO THE DATABASE )T THEN UPDATES THE ROLES =P\T PROPERTY AND VERIFIES THAT THE CORRESPONDING RECORD IN THE DATABASE HAS THE UPDATED VALUE
GetAll Roles 4HE 6Tc0[[ METHOD IS A LITTLE DIFFERENT FROM THE PREVIOUS METHODS )T REQUIRES AN >QYTRcBTc+CCOMPONENT IN ORDER TO CREATE A NEW ,).1 QUERY THAT WILL RETURN ALL THE AVAILABLE ENTITIES OF THAT TYPE FROM THE DATABASE SEE ,ISTING ,ISTING 4HE 6Tc0[[ -ETHOD AT_^bXc^ah1PbT _dQ[XR 8;Xbc+C- 6Tc0[[ j aTcda] d]Xc>UF^aZBTbbX^]2aTPcT>QYTRcBTc+C-C^;Xbc* l C33 JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
cah j ePa R^d]c , d^fBTbbX^]2aTPcT>QYTRcBTc+A^[T-2^d]c* ePa aT_^bXc^ah , ]Tf A^[TAT_^bXc^ahd^f* ePa Tg_TRcTS2^d]c , aT_^bXc^ah6Tc0[[2^d]c* 0bbTac0aT4`dP[R^d]c Tg_TRcTS2^d]c* l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l 7HATS NEW HERE 9OU ALREADY SAW THE 2aTPcT>QYTRcBTc+C- METHOD AND NOTED THAT IT RETURNS A NEW INSTANCE OF AN >QYTRcBTc+C- OBJECT THAT PROVIDES A SET OF ,).1 METHODS TO QUERY THE OBJECTS IN THE %& CONTEXT ,).1 IS AN INTEGRATED QUERY LANGUAGE TRANSLATED IN # 7HEN USED AGAINST %& IT IS ABLE TO TRANSLATE THIS 31, LIKE SYNTAX INTO A REAL 31, STATEMENT CALL TO THE DATABASE 4HE CODE HERE USES THE ;8=@ c^ 4]cXcXTb ,).1 FOR %& EXTENSION METHOD CALLED 2^d]c THAT CAN BE TRANSLATED TO B4;42C 2>D=C 5A>< A>;4B 4HE TASK TO ACCOMPLISH HERE IS PRETTY SIMPLE 9OU WANT TO BE SURE THAT THE NUMBER OF ENTITIES RETURNED BY THE REPOSITORY METHOD 6Tc0[[ IS THE SAME AS WHATS PROVIDED BY THE DEFAULT >QYTRcBTc+A^[TAVAILABLE THROUGH THE 4IME4RACKER >QYTRc2^]cTgc COMPONENT
Get a Role by Id &INALLY THE 6Tc1h8S METHOD RETURNS ONE AND ONLY ONE ENTITY OF THE TYPE SPECIFIED IN THE GENERIC SIGNATURE BY USING THE 8S PROPERTY THAT IS UNIQUE IN THE DATABASE TABLE 4HE CODE IN ,ISTING IMPLEMENTS A NEW WAY OF QUERYING THE OBJECTS IN THE %& THAT USES THE 4]cXch:Th OBJECT ,ISTING 4HE 6Tc1h8S -ETHOD AT_^bXc^ah1PbT _dQ[XR C 6Tc1h8SX]c XS j ePa Ud[[@dP[XUXTS=P\T , bcaX]V5^a\Pcjlj lb d]Xc>UF^aZBTbbX^]3TUPd[c2^]cPX]Ta=P\T ch_T^U C=P\T* ePa ZTh , ]Tf 4]cXch:ThUd[[@dP[XUXTS=P\T 8S XS* aTcda] C d]Xc>UF^aZBTbbX^]6Tc>QYTRc1h:ThZTh* l C33 JCTbc
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
ePa a^[T , ]Tf A^[T j =P\T , 0S\X]XbcaPc^ab 3TbRaX_cX^] , 0S\X]XbcaPc^a 6a^d_ l* aT_^bXc^ah8]bTaca^[T* d^f2^\\XcCaP]bPRcX^]* 0bbTac8bCadTa^[T8S - * cah VTc Xc ePa Tg_TRcTSA^[T , aT_^bXc^ah6Tc1h8Sa^[T8S* 0bbTac8b=^c=d[[Tg_TRcTSA^[T* 0bbTac0aT4`dP[Tg_TRcTSA^[T8S a^[T8S* l RPcRW 4gRT_cX^] TgRT_cX^] j 0bbTac5PX[TgRT_cX^]C^BcaX]V* l l 4HE FIRST PART OF THE CODE IN ,ISTING USES A NEW OBJECT CALLED 4]cXch:Th 4HE 4]cXch:Th CLASS REPRESENTS THE KEY OF AN ENTITY OBJECT 4HIS OBJECT NEEDS THREE PIECES OF INFORMATION IN ITS CONSTRUCTOR METHOD THE FIRST IS THE FULLY QUALIFIED NAME OF THE >QYTRc2^]cTgc WHICH IN OUR CASE IS CX\TCaPRZTa<^ST[2^]cPX]TaA^[Tb 4HE OTHER TWO PARAMETERS ARE THE KEY NAME AND THE KEY VALUE 4HE TEST SIMPLY VERIFIES THAT THE REPOSITORY IS ABLE TO RETURN THE SAME A^[T ENTITY THAT WAS SAVED USING THE >QYTRc2^]cTgc BY QUERYING IT USING THE 8S PROPERTY
Additional Methods 9OU SAW HOW TO CREATE A REPOSITORY AND HOW TO IMPLEMENT THE BASIC #25$ METHODS BUT YOU HAVENT YET SEEN HOW TO IMPLEMENT CUSTOM QUERIES !S NOTED PREVIOUSLY THE AIM OF THIS PATTERN IS TO HIDE THE /2METHODS FROM THE OTHER LAYERS SO IT WOULD BE GOOD TO PROVIDE A CUSTOM QUERY FOR EACH CUSTOM METHOD OF EACH REPOSITORY &OR EXAMPLE SUPPOSE YOU WANT TO BE ABLE TO QUERY THE ROLES IN THE DATABASE USING A ;8:4 CLAUSE TO RETURN ALL THE ROLES THAT MATCH THE STRING YOU ARE PASSING 4O DO THAT YOU SHOULD CREATE A CUSTOM METHOD IN THE A^[TAT_^bXc^ah LIKE THE ONE IN ,ISTING ,ISTING 4HE 6Tc1h=P\T -ETHOD OF A^[TAT_^bXc^ah _dQ[XR 8;Xbc+A^[T- 6Tc1h=P\TbcaX]V ]P\T j aTcda] d]Xc>UF^aZBTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a=P\T2^]cPX]b]P\T C^;Xbc* l )N ORDER TO MAKE THE 5NIT/F7ORK FIELD VISIBLE FROM A CONCRETE REPOSITORY YOU NEED TO CHANGE THE FIELD IN THE AT_^bXc^ah1PbT CLASS FROM PRIVATE TO PROTECTED ,ISTING USES THE 2^]cPX]b METHOD OF THE STRING CLASS THAT IS TRANSLATED BY ,).1 TO %NTITIES TO B4;42C 5A>< A>;4B F74A4 =0<4 ;8:4 ]P\T ,).1 TO %NTITIES EXPOSES A LOT OF METHODS TO QUERY YOUR %& OBJECTS AND WE CANT DISCUSS ALL OF THEM IN THIS BOOK AS IT WOULD PROBABLY TAKE AN ENTIRE BOOK FOR JUST THAT TOPIC $URING THE BUILDING OF THE 4IME4RACKER APPLICATION WE WILL MODIFY THE BASE REPOSITORIES CLASSES OF THE PROJECT AND YOU WILL SEE
#(!04%2 ■ 4(% %.4)49 &2!-%7/2+
SEVERAL ADDITIONAL METHODS ) SUGGEST YOU VISIT THE -3$. SECTION ON ,).1 TO %NTITIES AT Wcc_)\bS]\XRa^b^UcR^\T]db[XQaPahQQ"'%(%#Pb_g TO LEARN HOW POWERFUL THIS TOOL IS ) LEAVE TO YOU THE CREATION OF THE OTHER REPOSITORIES WHICH WOULD BE VERY SIMILAR TO WHAT YOU DID FOR THE A^[TAT_^bXc^ah OBJECT
Summary $OMAIN $RIVEN $ESIGN WAS INTRODUCED IN THE EARLY S IN SOFTWARE ARCHITECT COMMUNITIES TO SOLVE SPECIFIC BUSINESS REQUIREMENTS USING THE MOST ADVANCED TECHNIQUES OF OBJECT ORIENTED PROGRAMMING $$$ CAN BE ACCOMPLISHED IN DIFFERENT WAYS STARTING FROM THE SIMPLEST !CTIVE 2ECORD PATTERN THROUGH THE MORE COMPLEX AND FLEXIBLE $OMAIN $RIVEN APPROACH 9OU CAN ACHIEVE THE DESIGN BY CREATING THE CODE BEFORE THE DATABASE EXISTS #ODE &IRST OR BY CREATING A DOMAIN THAT WILL FOLLOW THE DESIGN OF AN EXISTING LEGACY DATABASE $ATABASE &IRST 4HE 4IME4RACKER APPLICATION USES THE $OMAIN $RIVEN $ESIGN TECHNIQUE WITH THE #ODE &IRST APPROACH SO THE DOMAIN MODEL WILL DRIVE THE WAY WE DESIGN THE APPLICATION LOGIC AND THE DATABASE STRUCTURE )N ORDER TO STEER THE APPLICATION FLOW IN THE PROPER WAY THE DOMAIN NEEDS AN ADDITIONAL ABSTRACTION LAYER FOR CONTROLLING HOW THE ENTITIES IN THE DATABASE WILL BE PERSISTED 4HIS REQUIREMENT IS ACCOMPLISHED VIA A DATA LAYER THAT INCLUDES ALL THE COMPONENTS USED TO PERSIST THE DATA UNIT OF WORK REPOSITORIES PLUS THE /2- USED BY THE APPLICATION TO GENERATE THE 4 31, STATEMENTS 9OU ALSO SAW HOW EASY AND SMOOTH THE CREATION OF SUCH A COMPLEX ARCHITECTURE CAN BE IF YOU INCLUDE IN YOUR CODE A FULL SET OF UNIT TESTS
CHAPTER ■■■
Data Binding 4HE PRIMARY PURPOSE OF 70& IS TO PROVIDE A DYNAMIC POWERFUL WAY TO BUILD STANDALONE CLIENT USER INTERFACES USING 8!-, MARKUP ! USER INTERFACE CONSISTS OF DIFFERENT TYPES OF CONTROLS SUCH AS BUTTONS TEXTBOXES AND DATAGRIDS %ACH CONTROL EXPOSES DATA AND BEHAVIORS THE DATA WILL BE DISPLAYED ONSCREEN AND EDITED BY THE USER WHILE THE BEHAVIORS WILL MAKE THE 5) INTERACTIVE 4YPICALLY THE MOST CHALLENGING TASKS WHEN DEVELOPING A 5) ARE BINDING THE DATA TO THE 6IEW AND KEEPING THE DATA SYNCHRONIZATION BETWEEN THE 6IEW AND THE -ODEL UP TO DATE AT ALL TIMES &OR EXAMPLE IF THE USER MODIFIES THE DATA IN THE 6IEW YOU MUST UPDATE THE CORRESPONDING -ODEL AND IF THE -ODEL CHANGES THE CORRESPONDING 6IEW NEEDS TO BE UPDATED 70& EXPOSES A POWERFUL MECHANISM FOR KEEPING THE DATA IN THE -ODEL AND THE 6IEW UP TO DATE 4HIS MECHANISM IS KNOWN AS DATA BINDING AND IT PROVIDES A WAY TO SYNCHRONIZE THE DATA SHARED BY THE 6IEW AND THE -ODEL !S WITH ANY 5) TECHNOLOGY YOU MAY WANT TO UPDATE THE DATA ONLY ON ONE SIDE OF THE RELATIONSHIP THIS IS KNOWN AS UNIDIRECTIONAL DATA BINDING &OR EXAMPLE YOU MAY WANT TO UPDATE THE -ODEL IF SOMETHING CHANGES IN THE 6IEW BUT YOU MAY NOT WANT TO UPDATE THE 6IEW IF THE -ODEL CHANGES )F YOU WANT TO KEEP THE DATA UPDATED ON BOTH SIDES OF THE RELATIONSHIP YOU USE BIDIRECTIONAL DATA BINDING 4HE MOST POWERFUL FEATURE OF THIS TECHNOLOGY IS THAT THE DATA BINDING TO A 70& CONTROL CAN BE STYLED SO THAT THE LAYOUT OF THE CONTROL IS CHANGES COMPLETELY DEPENDING ON THE DATA YOU ARE BINDING TO IT 7ITH 70& YOU CAN BIND THE DATA TO JUST ABOUT ANY 8!-, ELEMENT OR DEPENDENCY PROPERTY SO THAT YOUR 6IEW CAN BE TOTALLY DYNAMIC
WPF Data Binding Engine $ATA BINDING IN 70& IS ALWAYS COMPRISES TWO OBJECTS THE BINDING SOURCE AND THE BINDING TARGET WHERE THE SOURCE REPRESENTS THE OBJECT THAT PROVIDES THE DATA AND THE TARGET REPRESENTS THE 5) ELEMENT OR PROPERTY THAT EXPOSE THE DATA VALUE &IGURE SHOWS A BASIC REPRESENTATION OF THIS MECHANISM
#(!04%2 ■ $!4! ").$).'
?a^_Tach2WP]VTS4eT]c7P]S[Ta WP]S[Ta , ?a^_Tach2WP]VTS* XU WP]S[Ta , ]d[[ j ?a^_Tach2WP]VTScWXb ]Tf ?a^_Tach2WP]VTS4eT]c0aVb_a^_Tach=P\T* l l cWTaT PaT \^aT _a^_TacXTb Qdc cWTh PaT ]^c bW^f] U^a bT\_[XRXch l l _a^_Tach X]bXST cWT ?Tab^] R[Pbb _aXePcT bcaX]V UXabc=P\T* _dQ[XR bcaX]V 5Xabc=P\T j VTc j aTcda] UXabc=P\T* l bTc j XU eP[dT ,, UXabc=P\T j aTcda]* l UXabc=P\T , eP[dT* >]?a^_Tach2WP]VTS5Xabc=P\T* l l 4HE CODE FIRST SHOWS A FAKE ?Tab^] CLASS THAT IMPLEMENTS THE 8=^cXUh?a^_Tach2WP]VTS INTERFACE 4HE CODE PROVIDED BY THE INTERFACE CONSISTS OF A ?a^_Tach2WP]VTS EVENT THAT REQUIRES A ?a^_Tach2WP]VTS4eT]c0aVb ARGUMENTA STRING PARAMETER THAT IS THE NAME OF THE PROPERTY CHANGED .EXT IS THE METHOD USED TO RAISE THE ?a^_Tach2WP]VTS EVENT 4HE ONLY THING TO NOTE HERE IS THE CHECK THATS DONE BEFORE RAISING THE EVENT )F THE ?a^_Tach2WP]VTS EVENT IS NULL IT MEANS NOBODY IS LISTENING TO THE EVENT SO IT DOESNT NEED TO BE RAISED )F IT IS RAISED THE .%4 &RAMEWORK THROWS AN UGLY =d[[ATUTaT]RT4gRT_cX^] ERROR 4HE SECOND PART OF THE CODE SHOWS A PROPERTY WITH A PRIVATE FIELD ACCESSOR THAT IMPLEMENTS THE NOTIFY EVENT WHEN YOU SET THE VALUE OF THE PROPERTY )F THE VALUE HAS CHANGED FROM THE PREVIOUS ONE YOU CALL THE METHOD >]?a^_Tach2WP]VTS
■ .OTE 9OU MAY WONDER h7HY DO ) HAVE TO IMPLEMENT THIS INTERFACE ON AN OBJECT THAT IS BOUND TO A 70& CONTROLv 4HE ANSWER IS PRETTY SIMPLE 70& NEEDS TO DETECT THE CHANGES IN ORDER TO TRIGGER THE BINDING ENGINE AND UPDATE THE TARGET DEPENDENCY PROPERTY ANDOR THE SOURCE )F YOU IMPLEMENT THIS INTERFACE IN YOUR BIND OBJECT 70& HAS AN IN PLACE MECHANISM TO DETECT THE CHANGES AND YOU DONT HAVE TO WORRY OR TO IMPLEMENT ANY ADDITIONAL MECHANISM
#(!04%2 ■ $!4! ").$).'
"INDING $IRECTION 4HE BINDING FLOW IN 70& REPRESENTS THE DIRECTION OF DATA BINDING BETWEEN TWO OBJECTS )N 70& YOU CAN USE DIFFERENT TYPES OF BINDING AS EXPLAINED IN THE FOLLOWING LIST 4HE LIST IS AN ENUMERATION AVAILABLE FROM BhbcT\FX]S^fb3PcP1X]SX]V<^ST v
/NE7AY 4HIS IS THE CLASSIC BINDING JUST ONE DIRECTION FROM THE SOURCE -ODEL TO THE TARGET 6IEW 4HE -ODEL UPDATES THE 6IEW BUT THE 6IEW DOESNT UPDATE THE -ODEL 4HIS IS ALSO KNOWN AS READ ONLY BINDING BECAUSE A CHANGE IN THE 6IEW DOESNT AFFECT THE UNDERLYING -ODEL
v
/NE4IME !S THE NAME INDICATES THIS BINDING MECHANISM BINDS THE DATA JUST ONCE THE FIRST TIME AND WILL NOT KEEP EITHER THE SOURCE OR THE TARGET UPDATED
v
4WO7AY 7ITH TWO WAY BINDING A CHANGE IN THE -ODEL AFFECTS THE 6IEW AND A CHANGE IN THE 6IEW AFFECTS THE -ODEL
v
/NE7AY4O3OURCE )N THIS CASE THE BINDING GOES IN THE OPPOSITE DIRECTION FROM ONE WAY BINDING SO ITS THE 6IEW THAT UPDATES THE -ODEL BUT NOT VICE VERSA 4HIS TYPE OF BINDING IS HARDLY USED
!S PREVIOUSLY NOTED WHEN BINDING THE -ODEL TO THE 6IEW YOU NEED TO IMPLEMENT THE ACTUAL CHANGE NOTIFICATION MECHANISM IN ORDER TO BE ABLE TO USE THE NOTIFY MECHANISM PROVIDED BY THE 70& BINDING ENGINE )N ORDER TO BE NOTIFIED THAT SOMETHING HAS CHANGED IN THE SOURCE OR IN THE TARGET OF THE BINDING EXPRESSION YOU NEED TO SPECIFY IN 70& THE BEHAVIOR THAT WILL SET OFF THE CHANGE THIS BEHAVIOR IS REPRESENTED IN 70& BY THE PROPERTY D_SPcTB^daRTCaXVVTa WHICH HAS THREE DIFFERENT VALUES AVAILABLE FROM THE ENUMERATION BhbcT\FX]S^fb3PcPD_SPcTB^daRTCaXVVTa v
,OST&OCUS THIS OCCURS WHEN THE AFFECTED CONTROL LOSES THE FOCUS 4HIS USUALLY HAPPENS WHEN ANOTHER CONTROL IN THE 5) GETS THE FOCUS 4HIS IS THE DEFAULT VALUE FOR THE CTgc PROPERTY OF ANY CONTROL
v
0ROPERTY#HANGED THIS HAPPENS AFTER THE PROPERTY VALUE IS CHANGED AND IT IS FIRED EVERY TIME THE VALUE CHANGES
v
%XPLICIT THIS HAPPENS ONLY ON DEMAND WHEN THE CODE ASKS THE BINDING SOURCE TO UPDATE THE VALUE
)TS WORTH NOTING THAT SOME CONTROLS LIKE CTgc1^gES NEED TO BIND USING THE ;^bc5^Rdb MECHANISM DUE TO THE HIGH NUMBER OF CHANGES THEIR CTgc PROPERTIES CAN RECEIVE AND THAT MAY REDUCE THE 5) PERFORMANCE OVERALL
4HE )6ALUE#ONVERTER )NTERFACE 7HEN YOU BIND A PROPERTY OF AN OBJECT TO A DEPENDENCY PROPERTY OF A 8!-, ELEMENT YOU ALWAYS USE A LITERAL EXPRESSION LIKE THOSE SHOWN IN ,ISTING ,ISTING 3AMPLE "INDING %XPRESSIONS IN 8!-, +CTgc1^g CTgc,|j1X]SX]V ?PcW,?Tab^]5Xabc=P\Tl| +CTgc1^g CTgc,|j1X]SX]V 5Xabc=P\T D_SPcTB^daRTCaXVVTa , ;^bc5^Rdbl| -
#(!04%2 ■ $!4! ").$).'
"Y DEFAULT THE 70& ENGINE IS ABLE TO CONVERT ALMOST ALL THE PRIMITIVE VALUES BASED ON THE VALUE TYPE OF THE PROPERTY YOU ARE BINDING 3O FOR INSTANCE IN ,ISTING THE 5Xabc=P\T PROPERTY IS CONVERTED USING A STRING CONVERTER WITH THE DEFAULT Ch_T2^]eTacTa COMPONENT AS BOTH THE 5Xabc=P\T AND THE CTgc PROPERTIES ARE OF TYPE BhbcT\BcaX]V !NOTHER EXAMPLE IS SHOWN IN ,ISTING ,OOK AT THE PROPERTY VALUES USED TO CUSTOMIZE THE ;PQT[ CONTROLTHERE USING FOUR PROPERTIES WITH THREE DIFFERENT VALUE TYPES AN INTEGER FOR THE WIDTH A STRING FOR THE 5^]c5P\X[h AND A COLOR FOR THE 5^aTVa^d]S PROPERTY ,ISTING "ACKGROUND #OLOR 6ALUE #ONVERTER +;PQT[ FXScW, 7TXVWc,!$ 5^]c5P\X[h,BTV^T D8 5^aTVa^d]S,BcTT[1[dT-BP\_[T ;PQT[+;PQT[5NFORTUNATELY SOMETIMES YOU NEED TO FORMAT IE CONVERT THE DATA BINDING TO THE 5) USING A SPECIFIC APPROACH A FORMAT EXPRESSION OR SOME MORE COMPLICATED PRESENTATION LOGIC 9OU MAY ALSO NEED TO RECYCLE THE CODE USED FOR THE CONVERSION ALL OVER THE 70& APPLICATION 4HE .%4 &RAMEWORK PROVIDES AN INTERFACE CALLED 8EP[dT2^]eTacTa THAT FORCES YOU TO IMPLEMENT A TWO WAY CONVERSION OF A SPECIFIC VALUE TYPE 2^]eTac AND 2^]eTac1PRZ ,ISTING SHOWS A SAMPLE CONVERTER THAT CONVERTS A 3PcTCX\T OBJECT TO A SPECIFIC VALUE IN A STRING FORMAT ,ISTING $ATE4IME #ONVERTER FOR 70& dbX]V dbX]V dbX]V dbX]V
BhbcT\* BhbcT\6[^QP[XiPcX^]* BhbcT\FX]S^fb* BhbcT\FX]S^fb3PcP*
]P\Tb_PRT EP[dT2^]eTacTa j JEP[dT2^]eTabX^]ch_T^U 3PcTCX\T ch_T^U BcaX]VL _dQ[XR bTP[TS R[Pbb 3PcTCX\T2^]eTacTa ) 8EP[dT2^]eTacTa j aTVX^] 8\_[T\T]cPcX^] ^U 8EP[dT2^]eTacTa _dQ[XR ^QYTRc 2^]eTac^QYTRc eP[dT Ch_T cPaVTcCh_T ^QYTRc _PaP\TcTa 2d[cdaT8]U^« Rd[cdaT j ePa SPcT , 3PcTCX\T eP[dT* aTcda] SPcTC^BW^ac3PcTBcaX]V* l
#(!04%2 ■ $!4! ").$).'
_dQ[XR ^QYTRc 2^]eTac1PRZ^QYTRc eP[dT Ch_T cPaVTcCh_T ^QYTRc _PaP\TcTa« 2d[cdaT8]U^ Rd[cdaT j ePa bcaEP[dT , bcaX]VeP[dT* 3PcTCX\T aTbd[c3PcTCX\T* XU 3PcTCX\TCah?PabTbcaEP[dT ^dc aTbd[c3PcTCX\T j aTcda] aTbd[c3PcTCX\T* l aTcda] 3T_T]ST]Rh?a^_TachD]bTcEP[dT* l T]SaTVX^] l l 4HIS CONVERTER IMPLEMENTS THE INTERFACE 8EP[dT2^]eTacTa WHICH PROVIDES THE METHODS SIGNATURE TO TRANSLATE THE TYPES FROM THE ORIGINAL TO THE FINAL 4HE FIRST THING TO NOTICE IS THE ATTRIBUTE EP[dT2^]eTabX^] ON THE CONVERTER CLASS WHICH INFORMS THE DEVELOPMENT TOOLS SUCH 6ISUAL 3TUDIO OR "LEND ABOUT THE TYPE OF CONVERSION THAT WILL OCCUR USING THIS CONVERTER 4HE SECOND THING IS THE 8EP[dT2^]eTacTa IMPLEMENTATION THAT CONSISTS OF TWO METHODS v
2^]eTac THIS IS THE METHOD USED TO CONVERT THE ORIGINAL VALUE INTO THE FINAL VALUE DISPLAYED IN THE 5)
v
2^]eTac1PRZ THIS METHOD IS CALLED WHEN THE SOURCE TRIES TO GET THE VALUE MODIFIED VIA THE 5) SO THE FINAL VALUE NEEDS TO BE CONVERTED BACK TO THE ORIGINAL FORMAT
4HE FIRST PART OF THE CONVERTER SIMPLY USES ONE OF THE 3PcTCX\T METHODS TO CONVERT THE DATE VALUE INTO A SHORT FORMAT MMDDYYYY 4HE SECOND METHOD BECAUSE IT RECEIVES A STRING AS AN INPUT NEEDS FIRST TO VERIFY THAT THE VALUE CAN BE CONVERTED BACK TO A 3PcTCX\T )F IT CANT THE 3T_T]ST]Rh?a^_TachD]bTcEP[dT OBJECT RETURNS A DEFAULT NULLABLE VALUE .OW YOU CAN ADD A REFERENCE IN THE 8!-, 6IEW AND DECLARE THE CONVERTER BY SIGNING IT WITH AN g)ZTh ATTRIBUTE AS SHOWN IN ,ISTING ,ISTING 6ALUE #ONVERTER 8!-, $ECLARATION +FX]S^f g)2[Pbb,EP[dT2^]eTacTa
#(!04%2 ■ $!4! ").$).'
/NCE YOUVE ATTACHED A NEW CONVERTER INSTANCE TO THE WINDOW RESOURCES YOU CAN USE IT IN THE DATA BIND BY USING THE KEY 3PcT2^]e WHICH IDENTIFIES THAT SPECIFIC CONVERTER &IRST OF ALL YOU TRY TO BIND THE ?Tab^] CLASS BUILT PREVIOUSLY IN THE WINDOWS 3PcP2^]cTgc BY WORKING IN THE CODE BEHIND FILE AS SHOWN IN ,ISTING ,ISTING !TTACHING AN /BJECT OF 4YPE 8=^cXUh?a^_Tach2WP]VTS TO THE 3PcP2^]cTgc OF A 7INDOW _dQ[XR _PacXP[ R[Pbb
&INALLY YOU ADD THREE CONTROLS TO THE WINDOWS 6aXS ELEMENT WHICH WILL EXPOSE THE THREE PROPERTIES OF THE DATA BIND OBJECT SEE ,ISTING ,ISTING "INDING THE 3PcP2^]cTgc TO THE 8!-, #ONTROLS +6aXS+;PQT[ 2^]cT]c, 5Xabc =P\T ) +;PQT[ 2^]cT]c,;Pbc =P\T ) +;PQT[ 2^]cT]c,5d[[ =P\T ) +CTgc1^g CTgc,j1X]SX]V ?PcW,5Xabc=P\Tl +CTgc1^g CTgc,j1X]SX]V ?PcW,;Pbc=P\Tl +CTgc1^g CTgc,j1X]SX]V ?PcW,3PcT>U1XacW 2^]eTacTa,jBcPcXRATb^daRT 3PcT2^]ell +6aXS4HE RESULT WILL BE A WINDOW WITH THREE LABELS AND THREE TEXTBOXES 4HE ONE BOUND TO THE 3PcT>U1XacW PROPERTY WILL EXPOSE A DATETIME VALUE USING A SHORT FORMAT AS SHOWN IN &IGURE
&IGURE #USTOM VALUE CONVERTER FINAL RESULT
#(!04%2 ■ $!4! ").$).'
$ATA4EMPLATE /NE OF THE MOST POWERFUL CHARACTERISTICS OF 70& IS THE WAY IT ALLOWS YOU CUSTOMIZE THE LAYOUT OF THE 5) SO THAT A SIMPLE ;XbcQ^g CONTROL CAN LOOK LIKE A GRID JUST BY CUSTOMIZING THE WAY THE CONTROL WILL RENDER THE DATA "Y USING A DATA TEMPLATE IN 70& YOU CAN CUSTOMIZE THE LAYOUT OF A CONTROL BASED ON THE DATA YOU ARE BINDING TO IT &IGURE SHOWS THE SAME CONTROL A ;XbcQ^g BOUND TO A LIST OF ?Tab^] CLASSES BUT RENDERED WITH TWO DIFFERENT 3PcPCT\_[PcTS !S YOU CAN SEE THE CONTROL AND THE DATA ARE THE SAME BUT THE RESULT IS TOTALLY DIFFERENT
&IGURE ! ;XbcQ^g WITH DIFFERENT 3PcPCT\_[PcTS APPLIED !S &IGURE SHOWS A COLLECTION OF TYPE ;Xbc+?Tab^]- IS BOUND TO THESE TWO LISTBOXES !S YOU CAN SEE THE FIRST LIST USES THE DEFAULT DATA TEMPLATE PROVIDED BY THE ;XbcQ^g CONTROL )N THIS CASE THE DATA TEMPLATE RENDERS THE ;Xbc1^g8cT\ AS A CTgc1[^RZ FILLED BY THE RESULT OF THE C^BcaX]V METHOD OF THE BIND CONTROL "ECAUSE WE DIDNT IMPLEMENT A CUSTOM C^BcaX]V METHOD IN THE ?Tab^] CLASS THE LIST DISPLAYS THE 5d[[=P\T OF THE OBJECT TYPE ?Tab^] USING REFLECTION )N THE SECOND LIST A CUSTOM DATA TEMPLATE HAS BEEN DEFINED FOR AN ITEM OF THE ;XbcQ^g WHICH IS CONSISTS OF A SET OF CTgc1[^RZ AND OTHER 5) ELEMENTS THAT ARE BOUND TO THE PROPERTIES OF THE ?Tab^] CLASS 4HE ;XbcQ^g IS A CONTROL THAT IS BOUND TO A COLLECTION OF ITEMS SO IT EXPOSES A DEPENDENCY PROPERTY OF TYPE 8cT\CT\_[PcT WHICH EXPOSES THE DATA TEMPLATE ELEMENT FOR EACH ITEM IN THE LIST 7ITHIN THIS DATA TEMPLATE YOU CAN DEFINE THE LAYOUT OF THE ;XbcQ^g ITEMS AS IN ,ISTING ,ISTING $EFINING A $ATA 4EMPLATE FOR A ;XbcQ^g +X] cWT G0<; QT[^f fT PaT bZX__X]V cWT D8 bch[T R^ST X] ^aSTa c^ \PZT cWT G0<; \^aT« aTPSPQ[T +;Xbc1^g 8cT\bB^daRT,j1X]SX]Vl+;Xbc1^g8cT\CT\_[PcT+3PcPCT\_[PcT+BcPRZ?P]T[+CTgc1[^RZ CTgc,j1X]SX]V ?PcW,5Xabc=P\Tl -
#(!04%2 ■ $!4! ").$).'
+CTgc1[^RZ CTgc,j1X]SX]V ?PcW,;Pbc=P\Tl +CTgc1[^RZ CTgc,j1X]SX]V ?PcW,3PcT>U1XacWl +BcPRZ?P]T[+3PcPCT\_[PcT+;Xbc1^g8cT\CT\_[PcT+;Xbc1^g4HE CODE IN ,ISTING TELLS THE ;XbcQ^g THAT THE SOURCE OF THE DATA IS PROVIDED BY THE 3PcP2^]cTgc OF THE PARENT CONTAINER YOU BIND A ;Xbc+?Tab^]- TO THE 3PcP2^]cTgc OF THE WINDOW )T THEN SPECIFIES THAT THE 8cT\CT\_[PcT WILL BE COMPOSED OF A BcPRZ?P]T[ WITH THREE CTgc1[^RZS INSIDE EACH ONE BOUND TO A SPECIFIC PROPERTY OF THE ?Tab^] CLASS 4HIS EXAMPLE IS THE FIRST TYPE OF APPROACH TO BIND A SPECIFIC CLASS TO A SPECIFIC DATA TEMPLATE 5NFORTUNATELY IN THIS CASE YOU CANT RECYCLE THIS CODE SO EVERY TIME YOU NEED TO BIND A CLASS OF TYPE ?Tab^] TO A CONTROL YOU WILL NEED TO SPECIFY A DATA TEMPLATE FOR THE CONTROL (OWEVER IF YOU PUT THE DATA TEMPLATE IN THE WINDOWS RESOURCES OR IN A ATb^daRT3XRcX^]Pah YOULL BE ABLE TO REFERENCE THE RESOURCE IN THE CONTROL WITHOUT HAVING TO REWRITE THE DATA TEMPLATE SCHEMA EVERY TIME 3EE ,ISTING ,ISTING $ATA4EMPLATE FOR THE ?Tab^] #LASS AS A 2ESOURCE +ATb^daRTb ^U P FX]S^f +FX]S^fATb^daRTb+EP[dT2^]eTacTa)3PcTCX\T2^]eTacTa g):Th,3PcT2^]e +3PcPCT\_[PcT 3PcPCh_T,jg)Ch_T EP[dT2^]eTacTa)?Tab^]l g):Th,?Tab^]CT\_[PcT+BcPRZ?P]T[+6aXS+ ^\XccTS VaXS X\_[T\T]cPcX^] +CTgc1[^RZ 6aXSA^f, 6aXS2^[d\], CTgc,5Xabc =P\T ) 5^]cFTXVWc,1^[S+CTgc1[^RZ 6aXSA^f, 6aXS2^[d\], CTgc,j1X]SX]V ?PcW,5Xabc=P\Tl +CTgc1[^RZ 6aXSA^f, 6aXS2^[d\], CTgc,;Pbc =P\T ) 5^]cFTXVWc,1^[S+CTgc1[^RZ 6aXSA^f, 6aXS2^[d\], CTgc,j1X]SX]V ?PcW,;Pbc=P\Tl +CTgc1[^RZ 6aXSA^f,! 6aXS2^[d\], CTgc,1XacW 3PcT ) 5^]cFTXVWc,1^[S+CTgc1[^RZ 6aXSA^f,! 6aXS2^[d\], CTgc,j1X]SX]V ?PcW,3PcT>U1XacW 2^]eTacTa,jBcPcXRATb^daRT 3PcT2^]ell +6aXS-
#(!04%2 ■ $!4! ").$).'
+BcPRZ?P]T[+3PcPCT\_[PcT+FX]S^fATb^daRTb+ ;XbcQ^g dbX]V cWT BcPcXRATb^daRT +;Xbc1^g 8cT\bB^daRT,j1X]SX]Vl 8cT\CT\_[PcT,jBcPcXRATb^daRT ?Tab^]CT\_[PcTl 7^aXi^]cP[2^]cT]c0[XV]\T]c,BcaTcRW!S YOU CAN SEE THE CODE USED TO BIND A ?Tab^] CLASS TO AN 8cT\CT\_[PcT IS PRETTY SIMPLE AND SPECIFIC TO THE ?Tab^] CLASS AND IT CAN BE REUSED BY THE WHOLE APPLICATION THOUGH YOU MAY NEED TO ADD IT IN A SHARED APPLICATION RESOURCE FILE OR IN A DICTIONARY #REATING A DATA TEMPLATE FOR A SPECIFIC OBJECT IS AN ADDITIONAL STEP IN THE DEVELOPMENT PROCESS BUT IT ALLOWS YOU TO v
0ROVIDE A STANDARD LAYOUT FOR A SPECIFIC OBJECT SO THE OBJECT WILL APPEAR IN THE SAME WAY THROUGHOUT THE APPLICATION
v
2ECYCLE THE 8!-, MARKUP TO STYLE A SPECIFIC ITEM OF A CONTROL
4HE DATA TEMPLATE CAN BE USED TO DISPLAY THE ITEMS OF AN 8cT\b2^]ca^[ LIKE A ;XbcQ^g OR A 2^\Q^1^g BUT IT CAN ALSO BE USED TO DISPLAY THE CONTENT OF A 2^]cT]c2^]ca^[ LIKE A ?P]T[ 9OU CAN ALSO CREATE PERSONALIZED BEHAVIORS SO THAT THE 8cT\CT\_[PcT WILL CHOOSE A SPECIFIC DATA TEMPLATE DEPENDING ON A SPECIFIC VALUE IN THE BIND OBJECT OR ON THE STATUS OF THE 8cT\CT\_[PcT SELECTED OR NOT SELECTED 9OULL FIND MORE DETAILS ABOUT THIS TECHNOLOGY AT \bS]\XRa^b^UcR^\[XQaPah\b!$! Pb_g ,ATER IN THIS CHAPTER AND OTHERS YOULL SEE HOW TO CREATE CONDITIONAL DATA TEMPLATES
5) 6ALIDATION 70& INTRODUCES A MECHANISM FOR VALIDATING THE DATA BOUND TO A SPECIFIC CONTROL AND THIS IN COMBINATION WITH THE DATA TEMPLATE AND THE STYLE MECHANISMS LETS YOU CUSTOMIZE THE 5) SO THAT IT WILL INFORM THE USER ABOUT BAD INPUT OR MISSING INFORMATION 9OU CAN USE DIFFERENT APPROACHES TO VALIDATE AN OBJECT IN .%4 9OU CAN WRITE A CUSTOM VALIDATION CLASS THAT INHERITS FROM THE EP[XSPcX^]Ad[T CLASS AND IMPLEMENTS THE VALIDATION BUT THE DOWNSIDE OF THIS APPROACH IS THAT YOU END UP WITH A HUGE NUMBER OF CUSTOM VALIDATION CLASSES 9OU CAN ALSO USE THE BhbcT\2^\_^]T]c<^ST[3PcP0]]^cPcX^]b ATTRIBUTES WHICH PROVIDE AN IN PLACE MECHANISM FOR DATA VALIDATION 4HE VALIDATION MECHANISM FOR 70& IS STRUCTURED AS FOLLOWS v
!SSOCIATE ONE OR MORE VALIDATION RULES TO A BINDING OBJECT THE VALIDATION RULES IMPLEMENT ATTRIBUTES TO CHECK IF A SPECIFIC OBJECT OR PROPERTY IS VALID
v
! VALIDATION RULE CAN BE OVERRIDEN BY CREATING A NEW CLASS THAT INHERITS FROM THE EP[XSPcX^]Ad[T CLASS
v
#REATE A CUSTOM 2^]ca^[CT\_[PcT FOR THE ATTACHED PROPERTY EP[XSPcX^]7Pb4aa^a IN ORDER TO DISPLAY A SPECIFIC LAYOUT IF THE BIND OBJECT HAS ERRORS
,ISTING SHOWS A SIMPLE VALIDATION TEMPLATE FOR A CTgc1^g
#(!04%2 ■ $!4! ").$).'
,ISTING ! 3IMPLE 6ALIDATION 4EMPLATE FOR A 4EXT"OX +CTgc1^g =P\T,5Xabc=P\T+CTgc1^gCTgc+1X]SX]V ?PcW,5Xabc=P\T D_SPcTB^daRTCaXVVTa,?a^_Tach2WP]VTS+1X]SX]VEP[XSPcX^]Ad[Tb+4gRT_cX^]EP[XSPcX^]Ad[T +1X]SX]VEP[XSPcX^]Ad[Tb+1X]SX]V+CTgc1^gCTgc+CTgc1^g)N THIS CASE THE CODE SAYS THAT IF THE PROPERTY CHANGE RAISES AN EXCEPTION LETS SAY THAT IF 5Xabc=P\T IS NULL THE ?Tab^] CLASS RAISES AN EXCEPTION THE ERROR WILL RENDER IN THE CTgc1^g
■ .OTE )N THE 70&S VALIDATION MECHANISM IF THE SOURCE OBJECT 6IEW-ODEL BOUND TO THE 6IEW IMPLEMENTS THE 83PcP4aa^a8]U^ INTERFACE IT IS ABLE TO PROVIDE VALIDATION FEEDBACK FOR EACH PROPERTY BOUND TO THE 6IEW 4HE BUILT IN 3PcP4aa^aEP[XSPcX^]Ad[T COMPONENT CAN BE USED TO CHECK THE ERRORS POTENTIALLY RAISED BY THE BOUND OBJECT 6IEW-ODEL !N ALTERNATIVE WAY IS TO EXPLICITLY SET EP[XSPcTb>]3PcP4aa^a TO TRUE IN THE BOUND PROPERTY 9OULL FIND MORE INFORMATION ON THIS TOPIC IN #HAPTER WHEN WE DISCUSS THE -ODEL 6IEW 6IEW-ODEL IN DEPTH
"Y DEFAULT 70& RENDERS A VALIDATION ERROR USING A RED BORDER AROUND A CONTROL BUT WITH FEW SIMPLE STEPS YOU CAN COME UP WITH A COOL RESULT AS SHOWN IN &IGURE
&IGURE 5SING A CUSTOM ERROR TEMPLATE 4HE VALIDATION OF A BOUND OBJECTS PROPERTY CAN HAPPEN AT DIFFERENT TIMES &OR EXAMPLE YOU CAN VALIDATE THE DATA ENTERED INTO A CTgcQ^g ONLY WHEN THE USER LEAVES THE CONTROL ;^bc5^Rdb OR EVERY TIME THE PROPERTY VALUE CHANGES ?a^_Tach2WP]VTS 9OU MAY THEN DECIDE TO VALIDATE THE DATA USING A VALIDATION RULE OR USING THE EXCEPTION APPROACH WHICH ) DONT SUGGEST )N THE 6IEW-ODEL SECTION WELL TAKE A LOOK AT AN ALTERNATIVE SOLUTIONHOW TO CREATE A CUSTOM VALIDATOR CLASS THAT WILL VALIDATE THE DATA ENTERED IN THE 6IEW USING THE 3PcP0]]^cPcX^]b ATTRIBUTES
#(!04%2 ■ $!4! ").$).'
"INDING 3YNTAX 4HE DATA BINDING PROCESS CONSISTS OF A SYNTAX THAT LETS YOU SPECIFY THE ITEM YOU ARE BINDING THE PATH TO REACH THE ITEM IN THE OBJECT TREE AND THE TYPE OF BINDING YOU ARE USING 4ABLE IS A SUMMARY OF THE POTENTIAL BINDING PATH EXPRESSIONS YOU CAN USE WHEN YOU BIND A PROPERTY OR AN OBJECT TO A 8!-, ELEMENT 4ABLE $ATA "INDING #HEAT 3HEET
Binding Syntax
Description
["INDING]
"IND TO CURRENT $ATA#ONTEXT
["INDING .AME]
"IND TO THE .AME PROPERTY OF THE CURRENT $ATA#ONTEXT
["INDING .AME,ENGTH]
"IND TO THE ,ENGTH PROPERTY OF THE OBJECT IN THE .AME PROPERTY OF THE CURRENT $ATA#ONTEXT
["INDING %LEMENT.AME3OME4EXT"OX 0ATH4EXT]
"IND TO THE 4EXT PROPERTY OF THE ELEMENT 8!-, ELEMENT WITH NAME3OME4EXT"OX OR X.AME3OME4EXT"OX
4HIS TABLE SHOWS THE CLASSIC BINDING PATH TO USE WHEN YOU CREATE SIMPLE BINDING ASSOCIATIONS 4ABLE SHOWS SOME LESS COMMON MORE COMPLEX BINDING PATHS THAT YOU MAY NEED TO USE WHEN YOU HAVE TO BIND A DIFFERENT ELEMENT THAN THE CLASSIC OBJECT PROPERTY 4ABLE 2ELATIVE 3OURCE "INDING #HEAT 3HEET
Binding Path
Description
["INDING 2ELATIVE3OURCE[2ELATIVE3OURCE 3ELF]]
"IND TO THE TARGET ELEMENT
["INDING 2ELATIVE3OURCE[2ELATIVE3OURCE 3ELF] 0ATH.AME]
"IND TO THE .AME PROPERTY OF THE TARGET ELEMENT
["INDING 2ELATIVE3OURCE[2ELATIVE3OURCE &IND!NCESTOR !NCESTOR4YPE[X4YPE 7INDOW]] 0ATH4ITLE]
"IND TO THE TITLE OF THE PARENT WINDOW USING THE !NCESTOR4YPE TO FIND THE CORRECT TYPE
["INDING 2ELATIVE3OURCE[2ELATIVE3OURCE &IND!NCESTOR !NCESTOR4YPE[X4YPE )TEMS#ONTROL] !NCESTOR,EVEL] 0ATH.AME]
"IND THE NAME OF THE ND PARENT OF TYPE )TEMS#ONTROL 4HE !NCESTOR,EVEL TALKS ABOUT HOW FAR YOU BACK YOU GO ON THE TREE
#(!04%2 ■ $!4! ").$).'
Binding Path
Description
["INDING 2ELATIVE3OURCE[2ELATIVE3OURCE 4EMPLATED0ARENT] 0ATH.AME]
)NSIDE A CONTROL TEMPLATE BIND TO THE NAME PROPERTY OF THE ELEMENT THE TEMPLATE IS APPLIED TO
[4EMPLATE"INDING .AME]
3HORTCUT FOR THE PREVIOUS EXAMPLE
4ABLES AND ARE ADAPTED FROM fff]QScTRWR^\5aTTF_U1X]SX]V_SU +EEP THEM HANDY FOR WHEN YOU WILL HAVE TO WRITE BINDING EXPRESSIONS
ViewModels 4HE 6IEW-ODEL IS AN OBJECT INTRODUCED BY THE -ODEL 6IEW 6IEW-ODEL -66- PATTERN A SPECIFIC PRESENTATION PATTERN DESIGNED FOR 70& AND 3ILVERLIGHT AND RIGHT NOW USED ALSO IN 7INDOWS 0HONE 4HE CORE OF THIS PATTERN IS THE -ODEL FOR 6IEW IE THE 6IEW-ODEL 7E WILL ANALYZE THIS AND OTHER CONCEPTS IN MORE DETAIL IN THE CHAPTER DEDICATED TO 5) PATTERNS FOR 70& BUT RIGHT NOW LETS JUST IDENTIFY THIS OBJECT AS THE 6IEW-ODEL AND CREATE ONE FOR EACH 6IEW OF THE 4IME4RACKER APPLICATION 3O WHAT IS THE MAIN PURPOSE OF A 6IEW-ODEL OBJECT AND WHAT SHOULD IT IMPLEMENT 4HE 6IEW-ODEL SHOULD IMPLEMENT THE 8=^cXUh?a^_Tach2WP]VTS INTERFACE SO IT CAN BE BOUND DYNAMICALLY TO A 6IEW )T SHOULD ALSO PROVIDE A MECHANISM FOR VALIDATION AND IT SHOULD EXPOSE THE COMMANDS AND THE BEHAVIORS EXECUTED BY THE 6IEW )N #HAPTER YOU CREATED THE BASE 6IEW-ODELS FOR THE DETAILS AND THE LIST VIEWS NOW LETS ADD VALIDATION TO THE 6IEW-ODELS AND BIND THEM TO THE ENTITY OBJECTS CREATED IN #HAPTER 7E WILL ALSO MODIFY HOW THE 6IEW-ODELS ACT AS NOW WE HAVE AN ADDITIONAL COMPONENT FOR THE APPLICATIONTHE DOMAIN MODEL THAT WE WILL BIND TO THE 6IEW THE DATA LAYER THAT WILL RETRIEVE THE DATA FROM THE DATABASE AND WE KNOW HOW TO USE DATA BINDING 4HE MAIN REASON TO HAVE A EXTf<^ST[ OBJECT INSTEAD OF BINDING THE BUSINESS ENTITY DIRECTLY TO THE 6IEW LIKE THE 4\_[^hTT ENTITY FOR THE 4\_[^hTT3TcPX[bEXTf IS THAT v
9OU WANT TO KEEP THE PRESENTATION LOGIC LOOSELY COUPLED FROM THE BUSINESS LOGIC
v
9OU WANT TO KEEP 5) OBJECTS LIKE COMMANDS BEHAVIORS AND EVENTS LOOSELY COUPLED FROM THE BUSINESS ENTITY
v
9OU WANT TO MAKE THE PRESENTATION LOGIC TESTABLE
! 'ENERIC 6IEW-ODEL 6ALIDATOR 7HEN WE CREATED THE 1PbT3TcPX[bEXTf<^ST[+C- IN #HAPTER WE DIDNT INCLUDE ANY INFORMATION ABOUT HOW TO VALIDATE THE DATA ENTERED BY A USER BECAUSE WE HADNT YET LOOKED AT THE 70& VALIDATION RULE MECHANISM .OW WEVE SEEN THAT THE BhbcT\2^\_^]T]c<^ST[3PcP0]]^cPcX^]b ASSEMBLY PROVIDES A SIMPLE USEFUL WAY OF VALIDATING AN OBJECT BY DECORATING IT WITH DATA ANNOTATIONS ATTRIBUTES 7HAT WE ARE GOING TO DO IS TO DECORATE THE 6IEW-ODELAND NOT THE BUSINESS ENTITYWITH THE VALIDATION RULES THAT WILL BE DISPLAYED ON THE CORRESPONDING 6IEW 7HY ARE WE VALIDATING THE 6IEW-ODEL AND NOT THE DOMAIN ENTITY OBJECT DIRECTLY 4YPICALLY WITH AN N TIER APPLICATION YOU RECYCLE THE DOMAIN OBJECTS SO THE VALIDATION RULES MAY BE DIFFERENT FROM APPLICATION TO APPLICATION -OREOVER THE SAME DOMAIN ENTITY SHOULD BE VALIDATED IN DIFFERENT WAYS FROM ONE 6IEW TO ANOTHER 9OU MAY ALSO FIND A DIFFERENT POINT OF
#(!04%2 ■ $!4! ").$).'
VIEW WHERE THE DOMAIN MODEL DRIVES THE VALIDATION RULES SO YOU DONT NEED TO ADD THEM IN THE PRESENTATION LOGIC
■ .OTE 4HE DEBATE ABOUT WHETHER TO PLACE THE VALIDATION ON THE BUSINESS ENTITIES OR ON THE 6IEW MODELS IS STILL OPEN IN THE SOFTWARE ARCHITECTURE COMMUNITY NOT ONLY FOR 70& TECHNOLOGY BUT FOR ALL THE TECHNOLOGIES THAT INVOLVE A 5) LAYER AND ONE OR MORE BUSINESS AND PRESENTATION LAYERS 4HE VALIDATION RULES WE ARE USING IN THIS APPLICATION ARE RELATED MORE TO THE DATA ENTRY DONE BY THE END USER AND THIS IS ANOTHER REASON FOR SAVING THE VALIDATION IN THE 6IEW-ODEL 9OU CAN MOVE THE VALIDATION TO THE BUSINESS ENTITY IF YOU BELIEVE IT IS MORE CORRECT AND VALIDATE THE -ODEL INSTEAD OF THE 6IEW-ODEL BUT ) BELIEVE THAT THIS APPROACH WILL END UP POLLUTING THE DOMAIN WITH PRESENTATION VALIDATION RULES
,ETS OPEN THE 1PbTEXTf<^ST[+C- CLASS AND REFACTOR IT TO PROVIDE DIRECT SUPPORT TO THE 83PcP4aa^a8]U^ INTERFACE WE WILL USE REFLECTION TO READ cWT3PcP0]]^cPcX^] ATTRIBUTES AND VALIDATE THE EXTf<^ST[ PROPERTIES )N ORDER TO USE IT YOU HAVE TO ADD A NEW ASSEMBLY REFERENCE TO THE PROJECT THE BhbcT\ 2^\_^]T]c<^ST[3PcP0]]^cPcX^]bS[[ 3EE ,ISTING ,ISTING $ATA 6ALIDATION 5SING 3PcP0]]^cPcX^]b AND 83PcP4aa^a8]U^ dbX]V dbX]V dbX]V dbX]V dbX]V dbX]V
BhbcT\* BhbcT\2^[[TRcX^]b6T]TaXR* BhbcT\2^\_^]T]c<^ST[* BhbcT\2^\_^]T]c<^ST[3PcP0]]^cPcX^]b* BhbcT\;X]`* BhbcT\ATU[TRcX^]*
]P\Tb_PRT 0?A4BBCX\TCaPRZX]VEXTf<^ST[b j _dQ[XR PQbcaPRc R[Pbb 1PbTEXTf<^ST[+C- ) >QbTaePQ[T>QYTRc+C- 83PcP4aa^a8]U^ j _aXePcT bcaX]V eXTfCXc[T* _dQ[XR bcaX]V EXTfCXc[T j VTc j aTcda] eXTfCXc[T* l bTc j XU eXTfCXc[T ,, eP[dT j aTcda]* l eXTfCXc[T , eP[dT* >]?a^_Tach2WP]VTSe\ ,- EXTfCXc[T* l l
#(!04%2 ■ $!4! ").$).'
aTVX^] 8\_[T\T]cPcX^] ^U 83PcP4aa^a8]U^ _dQ[XR bcaX]V cWXbJbcaX]V R^[d\]=P\TL j VTc j ?a^_Tach8]U^ _a^_ , 6TcCh_T6Tc?a^_TachR^[d\]=P\T* 84]d\TaPQ[T+EP[XSPcX^]0ccaXQdcT- eP[XSPcX^]
2ETRIEVE THE ?a^_Tach8]U^ OBJECT CORRESPONDING TO THE CURRENT VALIDATED PROPERTY USING THE REFLECTION
2.
2ETRIEVE A LIST OF EP[XSPcX^]0ccaXQdcTS OF THE CURRENT PROPERTY
3.
&OR EACH ATTRIBUTE USED TO DECORATE THE PROPERTY VALIDATE THE PROPERTY AND IF THERE ARE ERRORS RETURN THEM
.OW EVERY PROPERTY OF A ;XbcEXTf<^ST[ OR A 3TcPX[bEXTf<^ST[ CAN VALIDATED
%MPLOYEE 6IEW-ODELS 4HE FIRST ENTITY TO TACKLE IS THE 4\_[^hTT DOMAIN ENTITY WHICH IS DISPLAYED BY THE DIFFERENT VIEWS 4HE MAIN 6IEW PRESENTS A LIST OF THE FIRST EMPLOYEES DISPLAYED IN A ;XbcQ^g CONTROL 4HE 4\_[^hTTb;XbcEXTf
#(!04%2 ■ $!4! ").$).'
DISPLAYS ALL THE EMPLOYEES USING A 3PcP6aXS CONTROL AND A COMPOSITE 6IEW THE %MPLOYEE$ETAILS6IEW SHOWS DETAILED INFORMATION ABOUT A PARTICULAR EMPLOYEE AS SHOWN IN &IGURE
&IGURE %MPLOYEE MASTER DETAILS VIEWS 4HE BACKGROUND SCREEN IN &IGURE IS A SIMPLE FX]S^f OBJECT CONSISTING OF A CTgc1^g CONTROL AND A 1dcc^] FOR SEARCHING THE EMPLOYEES AS WELL AS A 3PcP6aXS WITH A LIST OF RESULTS AND THREE 1dcc^]b FOR ADDING REMOVING OR EDITING AN ITEM 7HEN THE USER PRESSES THE NEW OR EDIT 1dcc^] A DETAILS 6IEW OPENS THAT IS COMPOSED OF A SET OF CPQ?PVTb IN A CPQ2^]ca^[ WHERE EACH ONE CONTAINS A SPECIFIC DbTa2^]ca^[ THAT BINDS TO A SPECIFIC PART OF THE EMPLOYEE ENTITY %ACH 6IEW IS BOUND TO A 6IEW-ODEL THAT IS THE MODEL FOR THE 6IEW )F YOURE WORKING WITH A DETAILS 6IEW THE 6IEW-ODEL WILL BE A DETAILS 6IEW-ODEL IF IT IS A LIST 6IEW THE 6IEW-ODEL WILL BE A LIST 6IEW-ODEL 7HEN THE 6IEW CONSISTS OF DIFFERENT PARTS LIKE THE EMPLOYEE DETAILS 6IEW IT WILL BE COMPOSED OF ADDITIONAL 6IEW-ODELS DETAILS OR LIST ONE FOR EACH COMPONENT OF THE 5) 4HE 6IEW-ODEL SHOULD EXPOSE THE PROPERTIES THAT ARE DISPLAYED IN THE CORRESPONDING 6IEW BY HIDING THE PROPERTIES EXPOSED DIRECTLY FROM THE -ODEL THIS APPROACH ALLOWS YOU TO ADD AN ADDITIONAL ABSTRACTION FROM THE 6IEW AND THE -ODEL AND IT ALSO ADDS ANOTHER LAYER OF SECURITY TO THE -ODEL THAT OTHERWISE WOULD BE DIRECTLY EXPOSED IN THE 6IEW
#(!04%2 ■ $!4! ").$).'
Employee Details 4HE FIRST 6IEW-ODEL YOULL CREATE IS THE 4\_[^hTT3TcPX[bEXTf<^ST[ SEE ,ISTING WHICH WILL EXPOSE THE FOLLOWING OBJECTS v
!LL THE PROPERTIES EXPOSED BY THE 4\_[^hTT CLASS WRAPPED AROUND
v
4WO 82^\\P]S COMMANDS ONE FOR THE 3AVE COMMAND AND ONE FOR THE #ANCEL COMMAND
2EMEMBER IF YOU DIDNT YET TO ADD A REFERENCE IN THE 70& PROJECT THAT POINTS TO THE "USINESS,AYER PROJECT IN ORDER TO HAVE THE DOMAIN MODEL VISIBLE IN THE 6IEW-ODEL OBJECTS ,ISTING 4\_[^hTT3TcPX[b 6IEW-ODEL dbX]V dbX]V dbX]V dbX]V dbX]V dbX]V dbX]V
BhbcT\* BhbcT\FX]S^fb8]_dc* BhbcT\2^[[TRcX^]b6T]TaXR* BhbcT\2^\_^]T]c<^ST[3PcP0]]^cPcX^]b* BhbcT\3PcP>QYTRcb3PcP2[PbbTb* BhbcT\;X]`* 0?A4BBCX\TCaPRZX]V1dbX]Tbb;PhTa*
]P\Tb_PRT 0?A4BBCX\TCaPRZX]VEXTf<^ST[b j _dQ[XR bTP[TS R[Pbb 4\_[^hTT3TcPX[bEXTf<^ST[ ) 1PbT3TcPX[bEXTf<^ST[+4\_[^hTTj _dQ[XR 4\_[^hTT3TcPX[bEXTf<^ST[4\_[^hTT RdaaT]c4]cXch ) QPbTRdaaT]c4]cXch j l JAT`dXaTS0[[^f4\_chBcaX]Vb , UP[bT 4aa^a
#(!04%2 ■ $!4! ").$).'
bTc j 2daaT]c4]cXch5d[[=P\T;Pbc=P\T , eP[dT* >]?a^_Tach2WP]VTSe\ ,- ;Pbc=P\T* l l JAT`dXaTS4aa^a
#(!04%2 ■ $!4! ").$).'
bTc j 2daaT]c4]cXch?Pbbf^aS , eP[dT* >]?a^_Tach2WP]VTSe\ ,- ?Pbbf^aS* l l _dQ[XR 82^\\P]S BPeT2^\\P]S j VTc* bTc* l _dQ[XR 82^\\P]S 2P]RT[2^\\P]S j VTc* bTc* l l l ,ISTING CREATES THE FIRST PART OF THE DETAILS 6IEW-ODEL THAT YOULL USE TO DISPLAY THE EMPLOYEE INFORMATION %ACH PROPERTY OF THE EMPLOYEE DOMAIN ENTITY HAS BEEN WRAPPED AROUND A NEW PROPERTY THE ONE THAT YOU WILL BIND IN THE 70& DbTa2^]ca^[ USED TO DISPLAY THE DATA %ACH PROPERTY HAS ALSO BEEN DECORATED WITH THE 3PcP0]]^cPcX^]b ATTRIBUTES SO THAT THE 6IEW WILL VALIDATE THE INFORMATION ENTERED BY THE USER &OR MORE INFORMATION ON THE 3PcP0]]^cPcX^]b RULES TAKE A LOOK AT \bS]\XRa^b^UcR^\ [XQaPahbhbcT\R^\_^]T]c\^ST[SPcPP]]^cPcX^]bPb_g 4HE 82^\\P]S INTERFACE EXPOSES METHODS AND PROPERTIES USED TO IMPLEMENT THE COMMAND PATTERN THATS ALSO USED BY THE 70& BINDING ENGINE WELL LOOK AT THIS PATTERN AND AT OTHER OBJECTS LIKE THE A^dcTS2^\\P]S IN MORE DEPTH IN THE NEXT CHAPTER &OR NOW JUST CREATE A PROPERTY IN THE 6IEW-ODEL FOR EACH COMMAND AND BIND IT !S YOUVE SEEN THERE ARE THREE ADDITIONAL TABS IN THE 4\_[^hTT3TcPX[bEXTf BUT WELL CREATE THESE PROPERTIES LATER AS WE NEED THE CORRESPONDING 6IEW-ODELS BEFORE DOING SO 4HE NEXT STEP NOW IS TO EDIT THE 4\_[^hTT3TcPX[bDbTa2^]ca^[gP\[ OBJECT THAT CONTAINS ONLY THE INFORMATION EXPOSED BY THE 4\_[^hTT DOMAIN ENTITY AND THE LIST OF A^[Tb SEE ,ISTING 4HE TAB CONTROL IN THE DETAILS 6IEW CONTAINS ONLY THE REFERENCE TO THE SPECIFIC USER CONTROL EXPOSED BY THE 6IEW BUT THE BINDING WILL HAPPEN IN THE DbTa2^]ca^[ DIRECTLY )N THIS WAY THE 4\_[^hTT3TcPX[bEXTf WILL REMAIN CLEAN WITHOUT ANY REFERENCE TO THE SPECIFIC BINDING OBJECT ,ISTING 4HE 4\_[^hTT3TcPX[bEXTf +FX]S^f g\[]b)\h,R[a]P\Tb_PRT)0?A4BBCX\TCaPRZX]VEXTfb g)2[Pbb,0?A4BBCX\TCaPRZX]VEXTfb4\_[^hTT3TcPX[bEXTf g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ g\[]b)2^]ca^[b,R[a]P\Tb_PRT)0?A4BBCX\TCaPRZX]V2^]ca^[b CXc[T,4\_[^hTT3TcPX[bEXTf 7TXVWc,"## FXScW,$$& ATbXiT<^ST,2P]ATbXiTFXcW6aX_ FX]S^fBch[T,CWaTT31^aSTaFX]S^f FX]S^fBcPacd_;^RPcX^],2T]cTaBRaTT]+3^RZ?P]T[ ;Pbc2WX[S5X[[,CadT+CTgc1[^RZ 3^RZ?P]T[3^RZ,C^_ Bch[T,jBcPcXRATb^daRT CXc[T1[^RZl-CX\TCaPRZTa« 4\_[^hTT 3TcPX[b+CTgc1[^RZ-
#(!04%2 ■ $!4! ").$).'
+BcPRZ?P]T[ 3^RZ?P]T[3^RZ,1^cc^\
#(!04%2 ■ $!4! ").$).'
+2^[d\]3TUX]XcX^] FXScW, $ +2^[d\]3TUX]XcX^] FXScW,Pdc^ +2^[d\]3TUX]XcX^] FXScW,$ +2^[d\]3TUX]XcX^] FXScW, +2^[d\]3TUX]XcX^] FXScW,$ +2^[d\]3TUX]XcX^] FXScW,Pdc^ +2^[d\]3TUX]XcX^] FXScW,$ +2^[d\]3TUX]XcX^] FXScW, +2^[d\]3TUX]XcX^] FXScW, $ +6aXS2^[d\]3TUX]XcX^]b+;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,CXc[T ) 6aXS2^[d\], +CTgc1^g CTgc,j1X]SX]V ?PcW,CXc[T <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXS2^[d\]," +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,1XacW SPcT ) 6aXS2^[d\],$+3PcT?XRZTa CTgc,j1X]SX]V ?PcW,3PcT>U1XacW <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXS2^[d\],& +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,;Pbc =P\T ) 6aXSA^f, « 6aXS2^[d\], +CTgc1^g CTgc,j1X]SX]V ?PcW,;Pbc=P\T <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXSA^f, « 6aXS2^[d\]," +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,5Xabc =P\T ) 6aXS2^[d\],$« 6aXSA^f, +CTgc1^g CTgc,j1X]SX]V ?PcW,5Xabc=P\T <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXS2^[d\],&« 6aXSA^f, +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,DbTa]P\T ) 6aXS2^[d\], « 6aXSA^f,! +CTgc1^g CTgc,j1X]SX]V ?PcW,DbTa]P\T <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXS2^[d\],"« 6aXSA^f,! +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,?Pbbf^aS ) 6aXS2^[d\],$« 6aXSA^f,! +CTgc1^g
#(!04%2 ■ $!4! ").$).'
CTgc,j1X]SX]V ?PcW,?Pbbf^aS <^ST,Cf^FPh D_SPcTB^daRTCaXVVTa,;^bc5^Rdb EP[XSPcTb>]3PcP4aa^ab,CadT EP[XSPcTb>]4gRT_cX^]b,CadTl 6aXS2^[d\],&« 6aXSA^f,! +;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l 2^]cT]c,DbTa A^[Tb ) 6aXS2^[d\]B_P],"« 6aXS2^[d\], 6aXSA^f," +;Xbc1^g 8cT\bB^daRT,j1X]SX]V ?PcW,2daaT]c4]cXchA^[Tbl 3Xb_[Ph
Employee List 4HE NEXT 6IEW-ODEL TO CREATE IS THE 4\_[^hTT;XbcEXTf<^ST[ SEE ,ISTING WHICH WILL EXPOSE THE FOLLOWING OBJECTS
v
! SEARCH STRING THAT WILL CONTAIN THE SEARCH TEXT ENTERED BY THE USER
v
&OUR 82^\\P]S OBJECTS THAT WILL EXPOSE THE FOUR COMMANDS AVAILABLE IN THIS 6IEW
v
!N 8D]Xch2^]cPX]Ta INTERFACE THAT WILL BE USED TO RESOLVE THE DEPENDENCIES OF THE 6IEW-ODEL
#(!04%2 ■ $!4! ").$).'
,ISTING 4\_[^hTT;XbcEXTf<^ST[ &IRST 0ART dbX]V dbX]V dbX]V dbX]V dbX]V dbX]V dbX]V
BhbcT\2^[[TRcX^]b6T]TaXR* BhbcT\;X]`* BhbcT\FX]S^fb8]_dc* 0?A4BBCX\TCaPRZX]V1dbX]Tbb;PhTa* 0?A4BBCX\TCaPRZX]V3PcP;PhTa* 0?A4BBCX\TCaPRZX]VBTaeXRTb* <XRa^b^Uc?aPRcXRTbD]Xch*
]P\Tb_PRT 0?A4BBCX\TCaPRZX]VEXTf<^ST[b j _dQ[XR R[Pbb 4\_[^hTT;XbcEXTf<^ST[ ) 1PbT;XbcEXTf<^ST[+4\_[^hTTj ST_T]ST]RXTb aTb^[eTS fXcW cWT 8^2 R^]cPX]Ta _aXePcT aTPS^][h 8D]Xch2^]cPX]Ta R^]cPX]Ta* _aXePcT aTPS^][h 83XP[^VBTaeXRT SXP[^VBTaeXRT* _aXePcT 8=PeXVPcX^]BTaeXRT ]PeXVPcX^]BTaeXRT* _aXePcT 4\_[^hTTAT_^bXc^ah aT_^bXc^ah* _aXePcT bcaX]V bTPaRW2aXcTaXP , bcaX]V4\_ch* _dQ[XR 4\_[^hTT;XbcEXTf<^ST[8D]Xch2^]cPX]Ta R^]cPX]Ta ) QPbT]Tf ;Xbc+1PbT3TcPX[bEXTf<^ST[+4\_[^hTT-- j cWXbR^]cPX]Ta , R^]cPX]Ta* ATb^[eT3T_T]ST]RXTb* 8]XcXP[XiT2^\\P]Sb* l _dQ[XR bcaX]V BTPaRW2aXcTaXP j VTc j aTcda] bTPaRW2aXcTaXP* l bTc j bTPaRW2aXcTaXP , eP[dT* >]?a^_Tach2WP]VTSe\ ,- BTPaRW2aXcTaXP* l l _dQ[XR 82^\\P]S BTPaRW2^\\P]S j VTc* _aXePcT bTc* l _dQ[XR 82^\\P]S =Tf2^\\P]S j VTc* _aXePcT bTc* l _dQ[XR 82^\\P]S 4SXc2^\\P]S j VTc* _aXePcT bTc* l _dQ[XR 82^\\P]S 3T[TcT2^\\P]S j VTc* _aXePcT bTc* l
#(!04%2 ■ $!4! ").$).'
_aXePcT e^XS ATb^[eT3T_T]ST]RXTb j aT_^bXc^ah , R^]cPX]TaATb^[eT+4\_[^hTTAT_^bXc^ah-* ]PeXVPcX^]BTaeXRT , R^]cPX]TaATb^[eT+8=PeXVPcX^]BTaeXRT-* l _aXePcT e^XS 8]XcXP[XiT2^\\P]Sb j X] cWT ]Tgc RWP_cTa fT fX[[ X\_[T\T]c cWT R^ST c^ RaTPcT cWT R^\\P]Sb WTaT l l l 4HE 4\_[^hTT;XbcEXTf<^ST[ HAS THREE DEPENDENCIES THE 8=PeXVPcX^]BTaeXRT SERVICE THE 83XP[^VBTaeXRT SERVICE AND THE 4\_[^hTTAT_^bXc^ah CLASS THEY CAN BE ALL RESOLVED USING THE UNITY CONTAINER THATS ALREADY SET UP IN THE APPLICATIONS P__gP\[Rb FILE "ECAUSE THE 6IEW-ODEL IS CREATED BY THE )/# CONTAINER TOO YOU CAN SIMPLY INJECT IN ITS CONSTRUCTOR AN INSTANCE OF THE INTERFACE 8d]Xch2^]cPX]Ta WHICH WILL BE AUTOMATICALLY INJECTED AND RESOLVED AT RUNTIME 7HEN THE CONSTRUCTOR RECEIVES THE )/# CONTAINER INSTANCE IT TRIES TO RESOLVE THE DEPENDENCIES AND ASSIGN A VALUE TO THE PRIVATE FIELDS THAT EXPOSE THE SERVICES AND THE REPOSITORY 4HE OTHER FIVE PROPERTIES ARE A STRING THAT REPRESENTS THE SEARCH CRITERIA WHICH WILL BE BOUND TO THE CTgc1^g EXPOSED IN THE 6IEW AND FOUR 82^\\P]S OBJECTS WHICH WE WILL EXPLORE IN THE NEXT CHAPTER &OR NOW YOU JUST NEED TO KNOW THAT THEY REPRESENT THE COMMANDS EXECUTED IN THE 5) 4HE FINAL STEP IS TO MOVE BACK TO THE P__gP\[Rb FILE AND MODIFY THE CODE FROM ,ISTING 4HE REASON FOR DOING THIS IS TO TEST THE CHANGESWE WANT TO MAKE THE 4\_[^hTT;XbcEXTf THE STARTUP 6IEW AND TO CHANGE THE WAY THE 6IEW-ODEL IS INJECTED IN THE 6IEW SEE ,ISTING ,ISTING 0__gP\[Rb #HANGES TO "OOTSTRAP THE 4\_[^hTT;XbcEXTfgP\[ 0__gP\[Rb _aXePcT e^XS 2^]UXVdaT2^]cPX]Ta j ]Tf aTVXbcaPcX^]b R^]cPX]TaATVXbcTaCh_T+8D]Xc>UF^aZ D]Xc>UF^aZ-* R^]cPX]TaATVXbcTaCh_T+4\_[^hTTAT_^bXc^ah-* R^]cPX]TaATVXbcTaCh_T+4\_[^hTTb;XbcEXTf-* ]PeXVPcX^] , R^]cPX]TaATb^[eT+8=PeXVPcX^]BTaeXRT-* l _a^cTRcTS ^eTaaXST e^XS >]BcPacd_BcPacd_4eT]c0aVb T j QPbT>]BcPacd_T* X]Xc cWT =PeXVPcX^]BTaeXRT R[Pbb FX]S^f T\_[^hTTbEXTf , ]PeXVPcX^]BW^f4\_[^hTTb;XbcEXTf* l
#(!04%2 ■ $!4! ").$).'
=PeXVPcX^]BTaeXRTRb _dQ[XR FX]S^f BW^f4\_[^hTTb;XbcEXTf j ePa fX]S^f , 2^]cPX]TaATb^[eT+4\_[^hTTb;XbcEXTf-* ePa EXTf<^ST[ , 2^]cPX]TaATb^[eT+4\_[^hTT;XbcEXTf<^ST[-* fX]S^f3PcP2^]cTgc , EXTf<^ST[* fX]S^fBW^f* aTcda] fX]S^f* l .OW IF YOU PRESS & THE APPLICATION WILL START UP SHOWING YOU THE 4\_[^hTT;XbcEXTf WINDOW WITH AN EMPTY 3PcPVaXS AND A SEARCH CTgc1^g "UT FIRST LETS BIND THE 6IEW-ODEL PROPERTIES TO THE 6IEW SEE ,ISTING ,ISTING "INDING THE 4\_[^hTT;XbcEXTf +FX]S^f g)2[Pbb,0?A4BBCX\TCaPRZX]VEXTfb4\_[^hTTb;XbcEXTf g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ g\[]b)2^]ca^[b,R[a« ]P\Tb_PRT)0?A4BBCX\TCaPRZX]V2^]ca^[b CXc[T,4\_[^hTTb;XbcEXTf 7TXVWc,"! FXScW,$$+3^RZ?P]T[+CTgc1[^RZ 3^RZ?P]T[3^RZ,C^_ Bch[T,jBcPcXRATb^daRT CXc[T1[^RZl-CX\TCaPRZTa« 4\_[^hTTb ;Xbc+CTgc1[^RZ+BcPRZ?P]T[ <PaVX],$ <X]7TXVWc," 7^aXi^]cP[0[XV]\T]c,2T]cTa« >aXT]cPcX^],7^aXi^]cP[ 3^RZ?P]T[3^RZ,C^_+;PQT[ Bch[T,jBcPcXRATb^daRT 2^]ca^[;PQT[l-BTPaRW 4\_[^hTTb) +;PQT[+CTgc1^g <PaVX],"-+CTgc1^g+2^]ca^[b)8\PVT1dcc^] 7^aXi^]cP[0[XV]\T]c,AXVWc ?PSSX]V," <PaVX]," CTgc,BTPaRW 2[XRZ2^\\P]S,j1X]SX]V ?PcW,BTPaRW2^\\P]Sl 8\PVT,j3h]P\XRATb^daRT BTPaRWB\P[[l +BcPRZ?P]T[+BcPRZ?P]T[ <PaVX],$ <X]7TXVWc," 7^aXi^]cP[0[XV]\T]c,AXVWc« >aXT]cPcX^],7^aXi^]cP[ 3^RZ?P]T[3^RZ,1^cc^\+2^]ca^[b)8\PVT1dcc^] ?PSSX]V," CTgc,=Tf 8\PVT,j3h]P\XRATb^daRT =TfB\P[[l+2^]ca^[b)8\PVT1dcc^] ?PSSX]V," 2[XRZ2^\\P]S,>_T] CTgc,4SXc 8\PVT,j3h]P\XRATb^daRT 4SXcB\P[[l +2^]ca^[b)8\PVT1dcc^] ?PSSX]V," CTgc,3T[TcT 8\PVT,j3h]P\XRATb^daRT 2[^bTB\P[[l +BcPRZ?P]T[-
#(!04%2 ■ $!4! ").$).'
+3PcP6aXS <PaVX],$ 3^RZ?P]T[3^RZ,1^cc^\ 8cT\bB^daRT,j1X]SX]V ?PcW,2^[[TRcX^]l BT[TRcTS8cT\,j1X]SX]V ?PcW,BT[TRcTSl| 0dc^6T]TaPcT2^[d\]b,5P[bT+3PcP6aXS2^[d\]b+3PcP6aXSCTgc2^[d\] 1X]SX]V,j1X]SX]V ?PcW,CXc[Tl 7TPSTa,CXc[T FXScW,$ +3PcP6aXSCTgc2^[d\] 1X]SX]V,j1X]SX]V ?PcW,5Xabc=P\Tl 7TPSTa,5Xabc =P\T FXScW, ! +3PcP6aXSCTgc2^[d\] 1X]SX]V,j1X]SX]V ?PcW,;Pbc=P\Tl 7TPSTa,;Pbc =P\T FXScW, ! +3PcP6aXSCTgc2^[d\] 1X]SX]V,j1X]SX]V ?PcW,3PcT>U1XacW BcaX]V5^a\Pc,jlj)<<<SShhhhll 7TPSTa,3PcT ^U 1XacW FXScW, ! +3PcP6aXS2^[d\]b+3PcP6aXS+3^RZ?P]T[+FX]S^f! 1PbT;XbcEXTf<^ST[ EXPOSES ONLY TWO PROPERTIES THE COLLECTION OF 3TcPX[bEXTf<^ST[+C- THATS REPRESENTED BY THE PROPERTY 2^[[TRcX^] AND THE CURRENT ITEM THATS EXPOSED BY THE PROPERTY BT[TRcTS YOU BIND BOTH PROPERTIES TO THE 3PcP6aXS AND EVERY TIME YOU SELECT A NEW ROW IN THE GRID THE 4\_[^hTT;XbcEXTf<^ST[ RECEIVES A NOTIFICATION FROM THE 6IEW AND UPDATES THE CURRENT SELECTED ITEM
!DDITIONAL 6IEW-ODELS AND 0ROPERTIES 4HE 4\_[^hTT3TcPX[bEXTf REQUIRES OF COURSE ADDITIONAL EXTf<^ST[b &OR EXAMPLE YOU NEED A ;XbcEXTf<^ST[ FOR THE LIST OF CONTACTS AND ONE FOR THE LIST OF ADDRESSES AND YOU ALSO NEED TO SHOW A LIST OF APPOINTMENTS %ACH 1PbT;XbcEXTf<^ST[+C- ALSO REQUIRES A CORRESPONDING DETAIL 1PbT3TcPX[bEXTf<^ST[ 9OUVE SEEN HOW TO CREATE DETAILS AND LIST 6IEW-ODEL CLASSES 5NFORTUNATELY FOR REASONS OF SPACE WE CANT SHOW THE CODE FOR CREATING EACH 6IEW-ODEL OBJECT IN THIS BOOK (OWEVER IF YOU UNDERSTAND THE PROCESS USED TO CREATE THE COMBINATION DETAILSLIST 6IEW-ODEL YOU SHOULD BE ABLE TO CREATE THE REMAINING 6IEW-ODELS BY YOURSELF (ERE ARE THE ADDITIONAL 6IEW-ODELS YOULL NEED TO CREATE v
v
#ONTACT ENTITY v
#ONTACT$ETAILS6IEW-ODEL
v
#ONTACT,IST6IEW-ODEL
!DDRESS ENTITY v
!DDRESS$ETAILS6IEW-ODEL
v
!DDRESS,IST6IEW-ODEL
#(!04%2 ■ $!4! ").$).'
,ISTING !GGREGATE &UNCTION 5SED TO 4RANSFORM A #OLLECTION OF /BJECTS _dQ[XR 0SSaTbb;XbcEXTf<^ST[ 0SSaTbbTb j VTc j aTcda] ]Tf 0SSaTbb;XbcEXTf<^ST[ 2daaT]c4]cXch0SSaTbbTb0VVaTVPcT ]Tf ;Xbc+1PbT3TcPX[bEXTf<^ST[+0SSaTbb-- [Xbc XcT\ ,j [Xbc0SS]Tf 0SSaTbb3TcPX[bEXTf<^ST[XcT\* aTcda] [Xbc* l* l bTc j 2daaT]c4]cXch0SSaTbbTb , eP[dT2^[[TRcX^]0VVaTVPcT ]Tf 4]cXch2^[[TRcX^]+0SSaTbb- [Xbc XcT\ ,j [Xbc0SSXcT\2daaT]c4]cXch* aTcda] [Xbc* l* >]?a^_Tach2WP]VTSe\ ,- 0SSaTbbTb* l l 4HIS CODE USES THE 0VVaTVPcT EXTENSION METHOD TO TRANSFORM A LIST OF 4]cXch2^[[TRcX^]+0SSaTbb- TO AN 0SSaTbb;XbcEXTf<^ST[ THE 0VVaTVPcT FUNCTION WORKS BY USING A DELEGATE THAT ITERATES OVER EACH ITEM OF THE COLLECTION AND RETURNS A NEW TYPE OF COLLECTION WHICH MUST BE SPECIFIED IN THE 0VVaTVPcT METHOD )N THE SET PART OF THE PROPERTY YOU DO THE INVERSE OPERATION )N THIS CASE YOU SIMPLY ASSOCIATE THE LIST OF EXTf<^ST[S BACK TO THE 0SSaTbb COLLECTION USING THE 0VVaTVPcT FUNCTION WITH A DIFFERENT SIGNATURE ! SAMPLE RESULT IS SHOWN IN &IGURE .OW YOU CAN CREATE A DbTa2^]ca^[ FOR EACH OF THESE COLLECTION PROPERTIES IN THE 4\_[^hTT3TcPX[bEXTf BIND THE COLLECTION TO A ;XbcQ^g FOR EXAMPLE AND CREATE A CUSTOM 3PcPCT\_[PcT 2EMEMBER ALSO THAT IN ,ISTING FOR EXAMPLE YOU ARE ASSOCIATING A ;XbcEXTf<^ST[ OF CHILD OBJECTS TO THE EXTf<^ST[ SO YOU CAN BIND NOT ONLY THE COLLECTION OF CHILD OBJECTS BUT ALSO THE CURRENTLY SELECTED CHILD ITEM
#(!04%2 ■ $!4! ").$).'
&IGURE $ATA "IND 2ESULT FOR THE 0SSaTbb #OLLECTION
Summary 70& AND 3ILVERLIGHT EXPOSES A NEW WAY OF DISPLAYING THE DATA AND BEHAVIORS OF OBJECTS TO THE 5) BY USING A TECHNOLOGY KNOWN AS DATA BINDING 7ITH THIS TECHNOLOGY YOU CAN BIND COMPLEX OBJECTS TO THE 5) AND SPECIFY TEMPLATES AND BEHAVIORS IN THE 5) USING RULES AND STYLES )N ORDER TO MAKE AN OBJECT BINDABLE TO A 70& 5) YOU NEED TO IMPLEMENT THE 8=^cXUh?a^_Tach2WP]VTS INTERFACE WHICH EXPOSES AN EVENT ?a^_Tach2WP]VTS THAT IS ABLE TO INFORM AND UPDATE THE 70& 6IEW WHEN SOMETHING HAPPEN BETWEEN THE 6IEW AND THE OBJECT BOUND TO IT 4HE 70& ENGINE IS ABLE TO TRANSLATE A VALUE USING DIFFERENT DATA TYPES BASED ON THE VALUE CONVERTER APPLIED TO THE BOUND OBJECT YOU CAN ALSO SPECIFY A CUSTOM VALUE CONVERTER IN THE BINDING EXPRESSION AND CREATE A CUSTOM ONE BY IMPLEMENTING THE INTERFACE 8EP[dT2^]eTacTa 4O USE ALL THE POWER OF THE DATA BINDING EXPOSED BY 70& YOU HAVE TO IMPLEMENT 3PcPCT\_[PcT OBJECTS WITH THESE OBJECTS YOU CAN RADICALLY CHANGE THE WAY A SPECIFIC 5) ITEM WILL APPEAR AND BEHAVE IN THE APPLICATION DEPENDING ON THE OBJECT AND THE VALUES BOUND TO IT )N THE SECOND PART OF THIS CHAPTER YOU SAW HOW TO CREATE A SPECIFIC EXTf<^ST[ HIERARCHY AND HOW TO MAKE EXTf<^ST[S INTERACTIVE WITH OTHER RELATED EXTf<^ST[S 9OU SAW HOW TO DO THAT SPECIFICALLY FOR THE 4\_[^hTT VIEWS YOU CAN TRY TO DO THE SAME WITH THE 2dbc^\Ta VIEWS AND TEST THE RESULTS BY TWEAKING THE CODE IN THE 0__gP\[Rb FILE
CHAPTER ■■■
Command Handling and Event Routing )F YOUVE EVER WORKED WITH A CLIENT TECHNOLOGY LIKE 7INDOWS &ORMS OR *AVA &ORMS YOU MAY ALREADY KNOW HOW THE MECHANISM OF REACTING TO A USER REQUEST IS ACHIEVED YOU CREATE A CONTROL IN THE 5) THE CONTROL EXPOSES SEVERAL 4eT]cb AND YOU LISTEN THESE EVENTS BY CREATING AN EVENT HANDLER WHICH IS NOTHING MORE THAN A METHOD WITH SOME CODE INSIDE TO HANDLE INPUT TO THE PROGRAM 4HE DOWNSIDE OF USING THIS APPROACH IS THAT YOU CANT RECYCLE AND TEST THE CODE EXECUTED IN AN EVENT HANDLER AND YOU CANT MAKE THE RELATIONSHIP BETWEEN THE 5) ELEMENT AND THE CORRESPONDING EVENT HANDLER METHOD DYNAMIC 70& EXPOSES A NUMBER OF PREDEFINED EVENTS SO FOR EVERY 5) ELEMENT YOU CAN USE A SET OF EVENTS AVAILABLE THROUGH THE %VENTS TAB OF THE 0ROPERTIES PANEL IN 6ISUAL 3TUDIO AS SHOWN IN &IGURE
&IGURE %VENTS TAB ON 6ISUAL 3TUDIO
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
)N THIS CASE YOU CAN ASSOCIATE AN EVENT HANDLER TO A 1dcc^] CONTROL FOR WHEN THE USER PRESSES THE BUTTON )N THE METHOD YOU THEN EXECUTE SOME SPECIFIC CODE /F COURSE THE SEPARATION OF CONCERNS IN THIS CASE IS NOT SATISFIED BECAUSE THE CODE THAT INCLUDES THE EXECUTION LOGIC OF THE EVENT IS STORED IN THE CODE BEHIND FILE OF THE 70& 8!-, FILE AND THE OBJECT THAT INVOKES THE METHOD THROUGH AN EVENT CANT BE SEPARATED FROM THE EVENT ITSELF 4HIS EXAMPLE FOR INSTANCE FORCES 70& TO ASSOCIATE THE 2[XRZ EVENT OF THE 1dcc^] CONTROL TO A 2[XRZN1dcc^] METHOD
Introduction to Commands 4HE #OMMAND PATTERN WAS NOT INTRODUCED WITH 70& OR 3ILVERLIGHT )NSTEAD THE COMMAND HANDLING TECHNIQUE IS A BEHAVIORAL PATTERN FIRST PRESENTED IN THE h'ANG OF &OURv BOOK $ESIGN 0ATTERNS %LEMENTS OF 2EUSABLE /BJECT /RIENTED 3OFTWARE BY %RICH 'AMMA 2ICHARD (ELM 2ALPH *OHNSON AND *OHN 6LISSIDES !DDISON 7ESLEY )T DESCRIBES HOW THE LOGIC OF A REQUEST SHOULD BE ENCAPSULATED IN A SEPARATE OBJECT 4HE GOAL IS TO SEPARATE THE SEMANTICS AND OBJECT THAT INVOKE A COMMAND FROM THE LOGIC THAT EXECUTES THE COMMAND SO THAT THE SAME COMMAND CAN BE USED OVER DIFFERENT 5) ELEMENTS WITHOUT HAVING TO REWRITE THE EXECUTION LOGIC &OR EXAMPLE THE CLASSIC COMMANDS YOU MAY FIND IN A WORD PROCESSING APPLICATION ARE 2^_h 2dc AND ?PbcT )N -ICROSOFT /FFICE 7ORD YOU CAN CALL THESE COMMANDS IN THREE DIFFERENT WAYS v
&ROM THE 4SXc MENU
v
&ROM THE 2^]cTgc MENU
v
5SING KEYBOARD COMBINATIONS SUCH #TRL8 #TRL6 AND SO ON
7HAT IT IS IMPORTANT HERE IS THAT YOU WOULD NOT WANT TO HAVE TO WRITE THE LOGIC OF EXECUTING A #UT COMMAND MORE THAN ONCE SO ALL OF THESE METHODS ACTUALLY EXECUTE THE SAME CODE !N OBJECT OF TYPE 2^\\P]S CAN TELL YOU WHEN IT CAN BE EXECUTED AND WHEN IT CANT &OR INSTANCE IN -ICROSOFT /FFICE 7ORD THE COMMAND 2dc IS DISABLED UNTIL YOU SELECT SOME TEXT BUT AS SOON AS YOU SELECT SOME TEXT THE APPLICATION TRIGGERS THE COMMAND SAYING SOMETHING LIKE h(EY #UT COMMAND LOOK .OW THERES SOME TEXT SELECTED SO YOU CAN BE EXECUTED !LSO YOU 5) ELEMENT YOU CAN BE ENABLEDv /F COURSE THIS IS A FUNNY INTERPRETATION OF HOW 7ORD WORKS BEHIND THE SCENE BUT IN THE END THIS IS WHAT IS HAPPENING
4HE #OMMAND 0ATTERN 4HE #OMMAND PATTERN IS A BEHAVIORAL DESIGN PATTERN THIS MEANS ITS PURPOSE IS TO DESCRIBE HOW TO SOLVE A SPECIFIC BEHAVIORAL PROBLEM ASSOCIATED WITH AN OBJECTIN THIS CASE HOW TO SEPARATE A REQUEST BY CREATING A SPECIFIC OBJECT THAT REPRESENTS THE REQUEST AND THE INFORMATION NEEDED BY THE REQUEST 4HE COMMAND PATTERN IS TYPICALLY USED WHEN THE REQUEST CAN BE INVOKED BY A DIFFERENT CLIENT AND RECEIVED BY A DIFFERENT RECEIVER &IGURE SHOWS THE CLASS DIAGRAM OF A COMMAND DESIGN PATTERN IMPLEMENTED USING 6ISUAL 3TUDIO AND #
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
&IGURE #LASS DIAGRAM OF A #OMMAND PATTERN IMPLEMENTATION 4HE 2^\\P]S IS INVOKED BY AN 8]e^ZTa THROUGH A STANDARDIZED METHOD 4gTRdcT2^\\P]S )NSIDE THE METHOD THE 8]e^ZTa VERIFIES THAT THE COMMAND CAN BE EXECUTED BY CALLING THE 2P]4gTRdcT METHOD OF THE COMMAND OBJECT )F THE METHOD RETURNS TRUE THE 8]e^ZTa THEN CALL THE 4gTRdcT METHOD OF THE 2^\\P]S 4HE 2^\\P]S INSIDE THE 4gTRdcT METHOD IMPLEMENTATION CALLS THE ATRTXeTa4gTRdcT0RcX^] METHOD SO THAT THE RECEIVER METHOD IS NOT STANDARDIZED AS A GLOBAL COMMAND LOOSELY COUPLED FROM THE RECEIVER OBJECT )N THE ORIGINAL IMPLEMENTATION OF THE #OMMAND PATTERN THE 2P]4gTRdcT METHOD WAS NOT INTRODUCED AND THE EXECUTION LOGIC WAS INSIDE THE 2^\\P]S OBJECT ITSELF )N 70& THE #OMMAND PATTERN HAS BEEN REINTERPRETED TO SATISFY THE TECHNOLOGY REQUIREMENTS OF HAVING A COMMAND OBJECT THAT IS ABLE TO NOTIFY ABOUT ITS ABILITY TO BE EXECUTED IN A SPECIFIC CONTEXT
#OMMAND IN 70& )F YOU ARE NOT USING ANY SPECIFIC DESIGN PATTERNS APPLIED TO 70& SUCH AS -ODEL 6IEW 6IEW-ODEL -66- OR -ODEL 6IEW 0RESENTER -60 YOU CAN STILL USE THE CORE STRUCTURE OF 70& TO IMPLEMENT A 70& COMMAND ALSO KNOWN AS A A^dcTS2^\\P]S 4HE A^dcTS2^\\P]S IS A CUSTOM COMMAND CLASS AVAILABLE IN THE BhbcT\FX]S^fb8]_dc NAMESPACE AND IT DEFINES A COMMAND OBJECT THAT IMPLEMENTS THE 82^\\P]S INTERFACE AND THAT IS ROUTED THROUGH THE ENTIRE 70& ELEMENT TREE 82^\\P]S IS A .%4 INTERFACE THAT EMULATES THE COMMAND PATTERN BY EXPOSING METHODS SUCH AS 4gTRdcT AND 2P]4gTRdcT AND BY PROVIDING A 2P]4gTRdcT2WP]VTS EVENT THAT IS TRIGGERED TO RE EVALUATE THE EXECUTION OF THE COMMAND ,ISTING SHOWS THE DECOMPILED # CODE FOR THE 82^\\P]S INTERFACE
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
,ISTING 82^\\P]S AND A^dcTS2^\\P]S /BJECTS _dQ[XR X]cTaUPRT 82^\\P]S j 4eT]cb TeT]c 4eT]c7P]S[Ta 2P]4gTRdcT2WP]VTS*
&IGURE 2OUTED #OMMAND 5) FLOW
The RoutedCommand Class 70& CAN HELP YOU USE THE COMMANDS BECAUSE IT PROVIDES A SET OF PREDEFINED COMMANDS EXPOSED BY THE 0__[XRPcX^]2^\\P]Sb CLASS %ACH OF THESE COMMANDS IS A A^dcTS2^\\P]S TYPE AND CAN BE USED IN ANY 5) ELEMENT THAT SUPPORTS THE 82^\\P]S INTERFACE ! A^dcTS2^\\P]S OBJECT EXPOSES THE 4gTRdcT AND 2P]4gTRdcT METHODS WHICH DONT CONTAIN THE APPLICATION LOGIC FOR THE COMMAND !S YOU WOULD DO WITH AN 82^\\P]S OBJECT IMPLEMENTATION THESE METHODS RAISE EVENTS ABLE TO TRAVERSE THE ELEMENT TREE LOOKING FOR AN OBJECT WITH A 2^\\P]S1X]SX]V IN ORDER TO FIND THE CORRESPONDING BINDING )N THE BINDING YOU DEFINE THE IMPLEMENTATIONS FOR 4gTRdcT AND
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
2P]4gTRdcT 4HE 4gTRdcT METHOD RAISES THE ?aTeXTf4gTRdcTS AND 4gTRdcTS EVENTS 4HE 2P]4gTRdcT METHOD RAISES THE ?aTeXTf2P]4gTRdcT AND 2P]4gTRdcT EVENTS 9OU CAN DEFINE A A^dcTS2^\\P]S FOR A 70& ELEMENT IN TWO DIFFERENT WAYS )N THE FIRST WAY YOU HAVE TO EXPLICITLY DECLARE AND BIND THE 4gTRdcT AND 2P]4gTRdcT IMPLEMENTATIONS OF THE COMMAND IN THE 8!-, AND IN THE CODE BEHIND AS IN ,ISTING ,ISTING %XPLICIT )MPLEMENTATION OF 4gTRdcT AND 2P]4gTRdcT
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
+1dcc^] 2^\\P]S,jg)BcPcXR \h)
■ .OTE 7ITH THE ANONYMOUS DELEGATE TECHNIQUE INTRODUCED IN .%4 YOU CAN PASS A PIECE OF CODE TO AN OBJECT AS A DELEGATE PARAMETER 7ITH THIS TECHNIQUE YOU CAN CREATE ON THE FLY A DELEGATE METHOD WITHOUT NEEDING TO DECLARE THE DELEGATE METHOD ITSELF 4HE FOLLOWING CODE SHOWS TWO DIFFERENT WAYS OF CREATING A DELEGATE METHOD WITH AND WITHOUT THE ANONYMOUS DELEGATE TECHNIQUE fXcW^dc cWT P]^]h\^db ST[TVPcT Qdcc^]2[XRZ , 2[XRZN3T[TVPcT* _dQ[XR e^XS 2[XRZN3T[TVPcT^QYTRc bT]STa 4eT]c0aVb PaVb j Rdbc^\ R^ST l fXcW cWT P]^]h\^db ST[TVPcT Qdcc^]2[XRZ , bT]STa PaVb ,- j Rdbc^\ R^ST l*
)N THIS CASE LETS USE ONE OF THE AVAILABLE 0__[XRPcX^]2^\\P]Sb A^dcTS2^\\P]S SO YOU DONT NEED TO DECLARE A A^dcTS2^\\P]S OBJECT IN THE CODE BEHIND FILE SEE ,ISTING 4HIS DOESNT MEAN THAT THE CUSTOM IMPLEMENTATION OF THE 82^\\P]S INTERFACE IS USELESS /N THE CONTRARY THE CUSTOM IMPLEMENTATION OF THIS INTERFACE LETS YOU HAVE MORE LOOSELY COUPLED CONTROL OVER THE COMMAND IMPLEMENTATION RATHER THAN BEING FORCED TO USE THE A^dcTS2^\\P]S CLASS
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
,ISTING "UTTON 5SING 0__[XRPcX^]2^\\P]Sb +1dcc^] 2^\\P]S,|0__[XRPcX^]2^\\P]Sb2^_h| 2^]cT]c,|2^_h cTgc| 4HEN YOU CREATE A NEW 2^\\P]S1X]SX]V IN THE CODE BEHIND FILE BY USING THE ANONYMOUS DELEGATE IN THE CONSTRUCTOR OF THE WINDOW OBJECT AS SHOWN IN ,ISTING ,ISTING 2^\\P]S1X]SX]V WITH !NONYMOUS $ELEGATE _dQ[XR
Implement the ICommand Interface 7HILE A GOOD APPROACH TO USING A COMMAND IN A 70& APPLICATION IS TO RECYCLE THE EXISTING A^dcTS2^\\P]Sb OBJECTS EXPOSED BY THE 0__[XRPcX^]2^\\P]Sb CLASS OR TO CREATE A NEW INSTANCE OF A A^dcTS2^\\P]S SOMETIMES YOU MAY NEED TO CREATE A MORE GENERIC AND FLEXIBLE COMMAND OBJECT THAT CAN EXECUTE AND INCLUDE BUSINESS LOGIC CODE NOT AVAILABLE FROM A COMMON A^dcTS2^\\P]S OBJECT &IGURE SHOWS HOW THE 82^\\P]S CUSTOM OBJECT WILL BE INTEGRATED IN THE 70& COMMAND MECHANISM
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
&IRST YOU NEED TO IMPLEMENT THE CUSTOM LOGIC TO EVALUATE AND EXECUTE A COMMAND )N THE <ee\2^\\P]S YOU NEED TO PROVIDE THE METHOD TO HANDLE THE 2P]4gTRdcT2WP]VTS EVENT AND THE IMPLEMENTATION OF 4gTRdcT AND 2P]4gTRdcT BY USING GENERIC AND LAMBDA EXPRESSIONS SO THAT THE <ee\2^\\P]S IMPLEMENTATION CAN REMAIN GENERIC 3EE ,ISTING ,ISTING <ee\2^\\P]S #ONCRETE )MPLEMENTATION _aXePcT 0RcX^]+^QYTRc- TgTRdcT , ]d[[* _aXePcT 5d]R+^QYTRc Q^^[- RP]4gTRdcT , ]d[[* _dQ[XR <ee\2^\\P]S0RcX^]+^QYTRc- TgTRdcT 5d]R+^QYTRc Q^^[- RP]4gTRdcT j cWXbTgTRdcT , TgTRdcT* cWXbRP]4gTRdcT , RP]4gTRdcT* l _dQ[XR e^XS >]2P]4gTRdcT2WP]VTS j 4eT]c7P]S[Ta WP]S[Ta , 2P]4gTRdcT2WP]VTS* XU WP]S[Ta , ]d[[ j 2P]4gTRdcT2WP]VTScWXb 4eT]c0aVb4\_ch* l l 4HE 0RcX^]+C- REPRESENTS A GENERIC DELEGATE THAT WILL CONTAINS THE CODE TO EXECUTE THE 5d]R+C Q^^[- REPRESENTS A "OOLEAN FUNCTION THAT CONTAINS THE CODE TO ENABLEDISABLE THE COMMAND AND THE ASSOCIATED 5) ELEMENT >]2P]4gTRdcT2WP]VTS IS THE METHOD USED TO RAISE THE 2P]4gTRdcT2WP]VTS EVENT 4HE LAST STEP IS TO ASSOCIATE THE 0RcX^]+C- AND THE 5d]R+C Q^^[- TO THE CORRESPONDING 82^\\P]S METHODS AS SHOWN IN ,ISTING ,ISTING !SSOCIATING 0RcX^]+C- AND 5d]R+C Q^^[- TO 82^\\P]S _dQ[XR e^XS 4gTRdcT^QYTRc _PaP\TcTa j XU cWXbTgTRdcT , ]d[[ j cWXbTgTRdcT_PaP\TcTa* l l _dQ[XR Q^^[ 2P]4gTRdcT^QYTRc _PaP\TcTa j XU cWXbRP]4gTRdcT ,, ]d[[ j aTcda] cadT* l aTcda] cWXbRP]4gTRdcT_PaP\TcTa* l
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
4HE FIRST PART OF ,ISTING SIMPLY CALLS 0RcX^]+C- BY PASSING THE PARAMETER IF AVAILABLE OF THE COMMAND AND EXECUTING THE METHOD 4HE SECOND PART OF THE CODE VERIFIES WHETHER 5d]R+C Q^^[- IS NULL AND IF IT IS NULL SIMPLY ENABLES THE COMMAND OR VERIFIES THAT THE EXPRESSION IS SATISFIED ,ISTING SHOWS HOW YOU CAN CREATE THE <ee\2^\\P]S COMMAND IN A 8!-, WINDOW ,ISTING <ee\2^\\P]S #ODE BEHIND )MPLEMENTATION R^STQTWX]S UX[T _dQ[XR _PacXP[ R[Pbb
)NTERACTIVE #OMMAND ,ISTEN FOR #HANGES 7HAT YOUVE DONE SO FAR IS PRETTY COOL FLEXIBLE AND TESTABLEYOU CREATED A GENERIC <ee\2^\\P]S EXPOSED IT AS AN 82^\\P]S INTERFACE OBJECT AND DECLARED THE CODE TO EXECUTE AND TO EVALUATE THE EXECUTION USING THE ANONYMOUS DELEGATE WHICH IS ALSO FANCY AND PRETTY READABLE !T THIS POINT YOU CAN PROBABLY TRY TO CREATE A BASIC 6IEW-ODEL OBJECT THAT EXPOSES A COUPLE OF PROPERTIES AND AN 82^\\P]S PROPERTY LIKE THE ONE IN ,ISTING AND BIND THESE PROPERTIES TO A 6IEW 7HAT YOU WANT TO DO IS TO ENABLE THE 5^a\Pc2^\\P]S PROPERTY ONLY IF THE TEXT IN THE CTgc1^g IS NOT NULL ,ISTING SHOWS THE 6IEW-ODEL CREATED FOR THIS EXAMPLE 9OU KNOW ALREADY WHAT AN >QbTaePQ[T>QYTRc+C- IS AND WHAT AN <ee\2^\\P]S IS ,ISTING 5^a\PcEXTf<^ST[ /BJECT _dQ[XR bTP[TS R[Pbb 5^a\PcEXTf<^ST[ ) >QbTaePQ[T>QYTRc+5^a\PcEXTf<^ST[j _aXePcT bcaX]V U^a\PccTSCTgc* _aXePcT bcaX]V ^aXVX]P[CTgc*
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
_dQ[XR 5^a\PcEXTf<^ST[ j 5^a\Pc2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,- j 5^a\PcCTgc* l _PaP\TcTa ,- j aTcda] bcaX]V8b=d[[>a4\_ch>aXVX]P[CTgc* l* l _dQ[XR 82^\\P]S 5^a\Pc2^\\P]S j VTc* _aXePcT bTc* l _dQ[XR bcaX]V >aXVX]P[CTgc j VTc j aTcda] ^aXVX]P[CTgc* l bTc j ^aXVX]P[CTgc , eP[dT* >]?a^_Tach2WP]VTSe\ ,- e\>aXVX]P[CTgc* l l _dQ[XR bcaX]V 5^a\PccTSCTgc j VTc j aTcda] U^a\PccTSCTgc* l bTc j U^a\PccTSCTgc , eP[dT* >]?a^_Tach2WP]VTSe\ ,- e\5^a\PccTSCTgc* l l _aXePcT e^XS 5^a\PcCTgc j 5^a\PccTSCTgc , bcaX]V5^a\Pc5^a\PccTS) jl >aXVX]P[CTgc* l l 4HIS EXTf<^ST[ OBJECT IS PRETTY SIMPLE )T PROVIDES TWO STRING PROPERTIESORIGINAL TEXT AND RESULT TEXT FORMATTED 4HE 82^\\P]S IS ENABLED ONLY IF THE >aXVX]P[CTgc PROPERTY IS NOT NULL OR EMPTY ,ISTING IS THE CORRESPONDING 8!-, MARKUP THAT BINDS THE 5) ELEMENTS TO THE EXTf<^ST[ PROPERTIES ,ISTING 5^a\PcEXTfgP\[ AND 5^a\PcEXTfgP\[Rb + 5^a\PcCTgcgP\[ +FX]S^f g)2[Pbb,2^\\P]S7P]S[X]V5^a\PcCTgc g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ CXc[T,5^a\PcCTgc 7TXVWc,!!% FXScW,!&!+BcPRZ?P]T[+;PQT[ <PaVX],$ 2^]cT]c,>aXVX]P[ CTgc ) -
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
+CTgc1^g <PaVX],$ FXScW,! CTgc,j1X]SX]V >aXVX]P[CTgc D_SPcTB^daRTCaXVVTa,?a^_Tach2WP]VTSl +;PQT[ <PaVX],$ 2^]cT]c,5^a\PccTS CTgc ) +CTgc1^g <PaVX],$ FXScW,! CTgc,j1X]SX]V 5^a\PccTSCTgc D_SPcTB^daRTCaXVVTa,?a^_Tach2WP]VTSl +1dcc^] <PaVX],$ FXScW, 2^\\P]S,j1X]SX]V 5^a\Pc2^\\P]Sl 2^]cT]c,5^a\Pc cWT CTgc +BcPRZ?P]T[+FX]S^f 5^a\PcCTgcgP\[Rb dbX]V BhbcT\FX]S^fb* dbX]V 2^\\P]S7P]S[X]V<ee\* ]P\Tb_PRT 2^\\P]S7P]S[X]V j +bd\\Pah 8]cTaPRcX^] [^VXR U^a 5^a\PcCTgcgP\[ +bd\\Pah_dQ[XR _PacXP[ R[Pbb 5^a\PcCTgc ) FX]S^f j _dQ[XR 5^a\PcCTgc j 8]XcXP[XiT2^\_^]T]c* 3PcP2^]cTgc , ]Tf 5^a\PcEXTf<^ST[* l l l )F YOU PRESS & THE APPLICATION STARTS AND THE FORMAT COMMAND IS DISABLED AS EXPECTED !FTER YOU INSERT SOME TEXT IN THE FIRST CTgc1^g YOU DISCOVER THAT <ee\2^\\P]S HAS A BUGIT STAYS DISABLED AS IT DOES AS WELL IF THERES TEXT IN THE >aXVX]P[CTgc PROPERTY 7HY DOES THIS HAPPEN 4HE 2P]4gTRdcT METHOD OF AN 82^\\P]S OBJECT IS EXECUTED ONLY WHEN THE DATA BINDING ENGINE CREATES THE BINDING BETWEEN THE 5) ELEMENT AND THE COMMAND THEN THE COMMAND EXECUTION IS RE EVALUATED ONLY IF SOMETHING CHANGES AND THE 2^\\P]S
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
&IRST LETS LOOK AT HOW THE EVENT MODEL WORKS IN # AND MORE GENERALLY IN .%4 7HEN USING A NORMAL EVENT REGISTERING AN EVENT HANDLER CREATES A STRONG REFERENCE FROM THE EVENT SOURCE TO THE LISTENING OBJECT AND THIS STRONG REFERENCE CAN CAUSE A MEMORY LEAKESPECIALLY WHEN THE SOURCE OBJECT HAS A LONGER LIFETIME THAN THE LISTENER 4HE ONLY WAY TO AVOID THIS BEHAVIOR IS TO MANUALLY UNSUBSCRIBE THE LISTENER FROM THE SOURCE OBJECT SO THAT THE RESOURCES ARE FREE )N # YOU CAN DO THIS BY USING THE , SYNTAX "UT THERES ANOTHER INTERESTING SOLUTION ONE THAT USES THE FTPZ4eT]c A SPECIAL TYPE OF EVENT MANAGER THAT IS ABLE TO AUTOMATICALLY FREE UNUSED RESOURCES 4HE CODE IN ,ISTING USES A FTPZ4eT]c
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
METHOD ASSOCIATED WITH THE EVENT 4HE INTERFACE HAS TO BE MANUALLY IMPLEMENTED IN ORDER TO BE USED PROPERLY IN 70& WITH THE FTPZ4eT]c
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
X]bXST cWT <ee\2^\\P]S fT aTcda] cWT RdaaT]c R^\\P]S ^QYTRc Qh S^X]V cWXb fT RP] RWPX] \d[cX_[T RP[[b c^ 0SS;XbcT]Ta c^VTcWTa _dQ[XR <ee\2^\\P]S 0SS;XbcT]Ta+C4]cXch-8=^cXUh?a^_Tach2WP]VTS b^daRT« 4g_aTbbX^]+5d]R+C4]cXch ^QYTRc-- _a^_Tach j bcaX]V _a^_Tach=P\T , 6Tc?a^_Tach=P\T_a^_Tach* ?a^_Tach2WP]VTS4eT]c
TimeTracker Commands )N THIS CHAPTER YOUVE SEEN HOW THE A^dcTS2^\\P]S WORKS IN 70& AND WHAT 70& PROVIDES OUT OF THE BOX WITHOUT ANY NEED TO IMPLEMENT A CUSTOM CLASS 5NFORTUNATELY THE A^dcTS2^\\P]S IS NOT FLEXIBLE ENOUGH TO BE USED IN AN -66- APPLICATION LIKE 4IME4RACKER WHERE THE PRESENTATION LOGIC IS DRIVEN AROUND THE 6IEW-ODEL OBJECT )N THE PREVIOUS CHAPTER YOU STARTED TO CREATE THE 6IEW-ODELS FOR EACH VIEW AND ADDED AN 82^\\P]S PROPERTY FOR EACH COMMAND AVAILABLE IN THE 6IEW 7ITH THAT FOUNDATION YOU SHOULD NOW BE ABLE TO COMPLETE THE %MPLOYEE,IST%MPLOYEE$ETAILS COMBINATION AND MAKE THIS FIRST PART OF THE APPLICATION FULLY FUNCTIONAL )T WILL BE THEN UP TO YOU TO CONTINUE THE APPLICATION BY IMPLEMENTING THE SAME LOGIC FOR THE #USTOMER ,IST$ETAILS COMBINATION OR YOU CAN SIMPLY TAKE A LOOK AT THE SOURCE CODE TO VIEW THE FULLY FUNCTIONAL PROJECT )N #HAPTER WE WILL LOOK AT THE EXTf<^ST[ CLASS IN MORE DEPTH AT HOW IT WORKS AND HOW IT SHOULD BE IMPLEMENTED &OR NOW LETS JUST FOCUS ON THE 82^\\P]S OBJECT THAT WILL BE EXPOSED BY A EXTf<^ST[
4HE %MPLOYEE,IST6IEW &IRST OF ALL YOU NEED TO REPLICATE THE 82^\\P]S IMPLEMENTATION FROM THE FIRST PART OF THIS CHAPTER IN THE 4IME4RACKER APPLICATION )F YOU CREATED A CUSTOM APPLICATION FOLLOWING THE FIRST PART OF THE CHAPTER AND YOU DONT WANT TO REWRITE THE TWO CLASSES INVOLVED <ee\2^\\P]S AND THE GENERIC
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
6T]TaXRFTPZ4eT]c
&IGURE <ee\2^\\P]S IMPLEMENTATION WITH 6T]TaXRFTPZ4eT]c
Search for an Employee 4HE 4\_[^hTT;XbcEXTf<^ST[ OBJECT HAS A METHOD CALLED 8]XcXP[XiT2^\\P]Sb WHERE YOU WILL INITIALIZE THE FOUR COMMANDS 4HE FIRST ONE IS THE BTPaRW2^\\P]S THAT SHOULD BE ENABLED ONLY IF THERE IS SOME TEXT IN THE SEARCH CTgc1^g SO THAT THE USER WILL NOT ACCIDENTALLY FIRE A FULL SEARCH ON THE ENTIRE DATABASE 3EE ,ISTING ,ISTING 3EARCH#OMMAND AND 3EARCH-ETHOD BTPaRW2^\\P]S X]XcXP[XiPcX^] X]bXST cWT 8]XcXP[XiT2^\\P]Sb \TcW^S BTPaRW2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,j cWXbBTPaRW4\_[^hTTb* l _PaP\TcTa ,j aTcda] bcaX]V8b=d[[>a4\_chcWXbBTPaRW2aXcTaXP* l 0SS;XbcT]Ta+4\_[^hTT;XbcEXTf<^ST[-cWXb g ,- gBTPaRW2aXcTaXP* BTPaRW4\_[^hTTb \TcW^S _aXePcT e^XS BTPaRW4\_[^hTTb j ;Xbc+4\_[^hTT- aTbd[c , aT_^bXc^ahBTPaRW4\_[^hTTbBTPaRW2aXcTaXP*
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
XU aTbd[c ,, ]d[[ kk aTbd[c2^d]c ,, j SXP[^VBTaeXRTBW^f0[Tac=^ 4\_[^hTTb U^d]S CWTaT PaT ]^ 4\_[^hTTb dbX]V« cWT U^[[^fX]V RaXcTaXP* aTcda]* l 2^[[TRcX^]2[TPa* U^aTPRW ePa T\_[^hTT X] aTbd[c j 2^[[TRcX^]0SS]Tf 4\_[^hTT3TcPX[bEXTf<^ST[T\_[^hTT* l l 4HE IMPLEMENTATION OF THE SEARCH METHOD IS PRETTY SIMPLE YOU ATTACH A FTPZ4eT]c TO THE BTPaRW2^\\P]S SO THAT IF THE CTgc1^g IS NOT EMPTY THE BTPaRW1dcc^] WILL BE ENABLED 4HE SEARCH METHOD IS CALLING THE 4\_[^hTTAT_^bXc^ah OBJECT TO SEARCH FOR ANY EMPLOYEE THAT MATCHES THE SEARCH CRITERIA )F THERE ARE NO SUCH EMPLOYEES YOU ADVISE THE USER BY POPPING UP A
Select and Edit an Employee 4HE NEXT STEP IS TO SELECT AN EMPLOYEE FROM THE GRID AND EDIT THE ENTRY USING THE 4\_[^hTT3TcPX[bEXTf )N THIS CASE THE 4SXc2^\\P]S SHOULD BE ENABLED ONLY WHEN A ROW IN THE GRID IS SELECTED )F THE GRID DOESNT HAVE ANY ROW THE 4SXc2^\\P]S SHOULD BE DISABLED AS IN ,ISTING ,ISTING 4SXc4\_[^hTT #OMMAND )MPLEMENTATION 8]XcXP[XiT2^\\P]Sb 4SXc2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,j cWXb4SXc2daaT]c4\_[^hTT* l _PaP\TcTa ,j aTcda] cWXbBT[TRcTS , ]d[[* l 0SS;XbcT]Ta+4\_[^hTT;XbcEXTf<^ST[-cWXb g ,- gBT[TRcTS , ]d[[* 4SXc2daaT]c4\_[^hTT \TcW^S _aXePcT e^XS 4SXc2daaT]c4\_[^hTT j ]PeXVPcX^]BTaeXRTBW^f4\_[^hTT3TcPX[bFX]S^fBT[TRcTS* l )N THIS CASE YOU ARE CHECKING THAT NO EMPLOYEE IS SELECTED IN THE GRID )F THATS THE CASE YOU SHOULD DISABLE THE EDITING PROCESS AS THE 4\_[^hTT3TcPX[bEXTf WILL RECEIVE A NULL EXTf<^ST[ 4HE TRIGGER FOR THE COMMAND IS THE BT[TRcTS PROPERTY THAT IS BOUND TO THE BT[TRcTS8cT\ PROPERTY OF THE 3PcPVaXS
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
Delete an Employee 4HE LAST STEP IS TO DELETE AN EMPLOYEE IN THE GRID IF ONE IS SELECTED AND IF THE USER ALSO PRESSES YES IN THE CONFIRMATION MESSAGE HE RECEIVES SEE ,ISTING ,ISTING $ELETE A 3ELECTED %MPLOYEE 8]XcXP[XiT2^\\P]Sb 3T[TcT2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,- j 3T[TcT2daaT]c4\_[^hTT* l _PaP\TcTa ,- j aTcda] BT[TRcTS , ]d[[* l 0SS;XbcT]Ta+4\_[^hTT;XbcEXTf<^ST[-cWXb g ,- gBT[TRcTS* 3T[TcT fXcW 2^]UXa\PcX^] _aXePcT e^XS 3T[TcT2daaT]c4\_[^hTT j XU BT[TRcTS ,, ]d[[ j SXP[^VBTaeXRTBW^f0[Tac=^ 4\_[^hTT bT[TRcTS CWTaT PaT ]^ T\_[^hTTb bT[TRcTS* aTcda]* l Q^^[ RP]3T[TcT , SXP[^VBTaeXRT0bZ2^]UXa\PcX^]3T[TcT 4\_[^hTT 3^ h^d fP]c c^ ST[TcT cWT RdaaT]c« T\_[^hTT.* XU RP]3T[TcT j cah j d]Xc>UF^aZBcPacCaP]bPRcX^]* aT_^bXc^ah3T[TcTBT[TRcTS2daaT]c4]cXch* d]Xc>UF^aZ2^\\XcCaP]bPRcX^]* BTPaRW4\_[^hTTb* l RPcRW 4gRT_cX^] TgRT_cX^] j d]Xc>UF^aZA^[[QPRZCaP]bPRcX^]* SXP[^VBTaeXRTBW^f4aa^a3T[TcT Taa^a 0] Taa^a ^RRdaTS SdaX]V cWT 3T[TcT« R^\\P]S* l l (ERE THE CODE IS A LITTLE BIT MORE COMPLEX &IRST YOU NEED TO VERIFY THAT AN EMPLOYEE HAS BEEN SELECTED THEN YOU NEED TO ASK THE USER TO CONFIRM THE DELETION AS YOU WANT TO BE SURE HE WANTS TO DROP THE CURRENT EMPLOYEE RECORD )F YOU GET A CONFIRMATION MESSAGE FROM THE USER YOU NEED TO START A TRANSACTION WITH THE UNIT OF WORK AND DELETE THE RECORD WITH THE 4\_[^hTTAT_^bXc^ah OBJECT THAT IS CREATED USING THE CURRENT UNIT OF WORK )F THE OPERATION IS SUCCESSFUL YOU CAN COMMIT THE TRANSACTION BUT IF SOMETHING GOES WRONG YOU NEED TO ROLL BACK THE OPERATION AND ADVISE THE USER
#(!04%2 ■ #/--!.$ (!.$,).' !.$ %6%.4 2/54).'
Suggested Steps 9OU SHOULD NOW BE PRETTY CONFIDENT ABOUT CREATING A EXTf<^ST[ AND AN 82^\\P]S OBJECT THAT HAS ONE OR MORE TRIGGERS (ERES WHAT YOU NEED TO ACCOMPLISH TO COMPLETE THE #25$ STEPS OF THE %MPLOYEE ,IST$ETAILS 6IEWS v
#REATE TWO COMMANDS FOR THE 3TcPX[bEXTf ONE FOR CONFIRMATION AND ONE FOR CANCELLATION "ASED ON THE COMMAND SELECTED BY THE USER YOU PERSIST THE CHANGES OR CANCEL THEM
v
7HEN THE USER SELECTS THE =Tf2^\\P]S IN THE ;XbcEXTf YOU NEED TO CREATE A NEW 4\_[^hTT ENTITY ATTACH IT TO THE REPOSITORY AND PASS IT TO THE 3TcPX[bEXTf
v
&OLLOWING THE SAME LOGIC YOU NEED TO PROVIDE THE 6IEWS FOR CREATING OR MODIFYING EXISTING 2^]cPRc 0SSaTbb AND 0__^X]c\T]c AND OF COURSE DO THE SAME FOR THE 2dbc^\Ta
Summary )N THIS CHAPTER YOU SAW HOW THE EVENT HANDLING APPROACH HAS BEEN CHANGED IN 70& WITH THE INTRODUCTION OF A^dcTS2^\\P]SS AND FTPZ4eT]c
CHAPTER ■■■
Testing with TDD /NE OF THE MAIN REASONS FOR WRITING A LOOSELY COUPLED APPLICATION THAT USES THE LATEST DESIGN PATTERNS AND LAYERING TECHNIQUE IS ARE TO INCREASE AS MUCH POSSIBLE THE TESTABILITY SURFACE OF THE APPLICATION TO MAKE THE VARIOUS COMPONENTS OF THE APPLICATION SEPARATE AND INDEPENDENT AND SO MORE TESTABLE 6ISUAL 3TUDIO PROVIDES OUT OF THE BOX A SET OF TOOLS AND ASSEMBLIES THAT ALLOW YOU TO WRITE TESTABLE APPLICATIONS WHICH MEANS WRITING APPLICATIONS THAT CAN BE TESTED EVEN OUTSIDE THE SCOPE OF THE 5) THAT THE USER WILL INTERACT WITH 4O GET STARTED YOU NEED TO LOOK AT 4EST $RIVEN $EVELOPMENT 4$$ PART OF THE !GILE APPROACH WHICH SUPPORTS THE hTEST FIRSTv PRINCIPLE OF %XTREME 0ROGRAMMING 80 (OWEVER 4$$ CAN BE USED INDEPENDENTLY OF THE OTHER !GILE 80 APPROACHES )N THE COMPANY WHERE ) WORK FOR EXAMPLE WE APPLY 4$$ TO EVERY PROJECT BUT WE DONT USE OTHER !GILE 80 TECHNIQUES 6ISUAL 3TUDIO AND -34EST ARENT THE ONLY UNIT TEST FRAMEWORKS FOR .%4 /N THE 7EB YOULL FIND MANY OTHERS MOST OF THEM OPEN SOURCE THAT ARE ALSO SUITABLE FOR WRITING UNIT TESTS LIKE THE FOLLOWING v
N5NIT fff]d]Xc^aV
v
X5NIT.%4 Wcc_)gd]XcR^ST_[TgR^\
v
MB5NIT fff\Qd]XcR^\
"EFORE USING THE 4$$ APPROACH THE ONLY WAY OF TESTING A SPECIFIC SET OF CODE COMMANDS WAS TO BUILD AND EXECUTE THE APPLICATION AND DEBUG INSIDE THE APPLICATION CODE "Y DEBUG ) MEAN THAT PROCESS USED TO FIND AND FIX THE NUMBER OF BUGS IN A SPECIFIC APPLICATION 7ITH THIS APPROACH YOU HAVE TO FIRST IDENTIFY THE BUG THAT IS THE CAUSE OF THE PROBLEM AND AFTER IT IS IDENTIFIED YOU STEP THROUGH THE CODE THAT IS CAUSING THE BUG AND YOU TRY TO FIX IT 4HEN YOU RE EXECUTE THAT PIECE OF CODE IN ORDER TO BE SURE THAT THE BUG IS FIXED AND THAT THE CHANGE YOU MADE DID NOT AFFECT OTHER PARTS OF THE APPLICATION /F COURSE THE TWO APPROACHES ARE DRASTICALLY DIFFERENT BUT THEY CAN EASILY COEXIST IN THE SAME APPLICATION )T IS VERY GOOD TO WRITE YOUR APPLICATION FOLLOWING 4$$ PRINCIPLES BUT IT IS STILL NECESSARY SOMETIMES TO STEP INTO THE CODE AND IDENTIFY THE ROOT CAUSE OF THE PROBLEM )N THIS CHAPTER WE WILL EXPLORE HOW TO WRITE THE 4IME4RACKER APPLICATION FOLLOWING THE PRINCIPLES OF 4$$ AND HOW 6ISUAL 3TUDIO CAN HELP APPLY THESE PRINCIPLES TO EVERY LAYER OF THE APPLICATION 7E WILL THEN LOOK AT THE BASIC CODE COVERAGE PRINCIPLES THAT COME FROM 4$$ AND FINALLY WE WILL SEE HOW TO TEST THE VARIOUS LAYERS OF THE 4IME4RACKER APPLICATION
Test Driven Development )F YOU LOOK AS A PURIST AT THE 4$$ PRINCIPLES YOUD SAY THAT 4$$ IS A TECHNIQUE FOR DEVELOPING SOFTWARE BASED ON A SET OF SMALL DEVELOPMENT CYCLES WHERE EACH CYCLE CONSISTS OF A TEST CASE THAT DEFINES THE CODE
#(!04%2 ■ 4%34).' 7)4( 4$$
REQUIREMENTS AND THE CODE THAT WILL SATISFY THESE REQUIREMENTS 4$$ IS ALSO KNOWN AS RED GREEN REFACTOR BECAUSE THE LIFECYCLE OF A TEST INCLUDES THREE SIMPLE STEPS v
7RITE THE TEST AND THE ASSERTIONS AND FAIL THEM RED
v
)MPLEMENT THE CODE TO PASS THE ASSERTIONS GREEN
v
2EFACTOR THE CODE AND TEST IT AGAIN
4HIS TYPE OF APPROACH MAY LOOK STRANGE TO YOU AT THE BEGINNING BUT YOU WILL REALIZE THAT IF YOU WRITE THE CODE THAT IDENTIFIES AN APPLICATION REQUIREMENT BEFORE THE REQUIREMENT HAS BEEN WRITTEN IN CODE THE TEST SHOULDNT PASS AT THAT POINT THE CODE IMPLEMENTATION SHOULD JUST SATISFY THE REQUIREMENT NOTHING MORE 4HE FINAL REFACTORING STEP IS ACCOMPLISHED TO CLEAN UP THE CODE WRITTEN TO PASS THE ASSERTIONS 4$$ IS NOT A PATTERN BUT A DEVELOPMENT METHODOLOGY SO YOU MAY FIND SOME PEOPLE POSITIVE ABOUT THIS APPROACH AND OTHERS THAT DONT LIKE TO DEVELOP USING 4$$ (ERE ARE SOME PROS OF USING THE 4$$ TECHNIQUE AS WE WILL FOR THE 4IME4RACKER APPLICATION v
)T FORCES YOU DEVELOP ACCORDING TO THE REQUIREMENT AND TO REFINE THE CODE TO RESPECT AND HONOR THE APPLICATION REQUIREMENTS
v
)T FORCES YOU TO WRITE CODE THAT IS LOOSELY COUPLED AS THE CODE MUST BE QUICK AND EASY TO TEST
v
)T ALLOWS YOU TO TEST THE CODE YOU WRITE IN AN INCREMENTAL PROCESS WITHOUT HAVING A GRAPHICAL LAYER TO EXECUTE THE CORE CODE
v
)T FORCES YOU TO WRITE A LOT OF SINGLE TESTS IN ORDER TO COVER AT LEAST OF THE CODE WITH TESTS AND THIS APPROACH HELPS MAKE YOU AND OTHER DEVELOPERS CONFIDENT ABOUT THE QUALITY OF THE CODE AND THE NUMBER OF DEFECTS COVERED BY TESTS
4$$ CANT BE USED EVERYWHERE ESPECIALLY IF THE REQUIREMENTS ARISE FROM CODE THAT CANT EASILY BE TESTED WITH AUTOMATION LIKE A 70& 6IEW )N THE CASE OF A '5) OBJECT YOU SHOULD INTRODUCE IN YOUR TESTING ENVIRONMENT A MORE COMPLEX MECHANISM OF 5) AUTOMATION AND A 5) MACRO THAT WILL GUARANTEE THE CORRECTNESS OF THE TEST EXECUTION 4$$ MAY ALSO NOT FIT PROPERLY WHEN THE REQUIREMENTS HAVE SOME DEPENDENCIES THAT ARE CANT BE EMULATED IN THE TEST ENVIRONMENT FOR EXAMPLE TESTING A DATA LAYER AGAINST A BACKUP COPY OF A PRODUCTION DATABASE /NE GOOD WAY TO MEASURE THE AMOUNT OF CODE COVERED BY THE TEST IS THE #ODE #OVERAGE APPROACH ,ETS TAKE A LOOK AT THAT NOW
#ODE #OVERAGE /NE OF THE METRICS USED IN 4$$ IS CALLED #ODE #OVERAGE BECAUSE IT REPRESENTS THE AMOUNT OF CODE COVERED BY A SPECIFIC TEST OR SET OF TESTS IE THE AMOUNT OF CODE EXECUTED BY THE TESTS WHILE THE TESTS ARE RUNNING ,ISTING SHOWS A SIMPLE CLASS THAT EXPOSES A METHOD TO EXECUTE A CALCULATION BETWEEN TWO INTEGERS 4HE CODE USES SOME 6ISUAL 3TUDIO FEATURES THAT WE WILL ANALYZE IN THE NEXT SECTION FOR NOW LETS JUST FOCUS ON THE CODE COVERAGE CONCEPT
#(!04%2 ■ 4%34).' 7)4( 4$$
,ISTING #ODE #OVERAGE %XAMPLE dbX]V BhbcT\* ]P\Tb_PRT 2P[Rd[Pc^a j _dQ[XR bTP[TS R[Pbb 4]VX]T j _dQ[XR X]c Bd\X]c P X]c Q j XU P ,, kk Q ,, j cWa^f ]Tf 0aVd\T]c=d[[4gRT_cX^] CWT PaVd\T]cb P P]S Q \dbc QT VaTPcTa cWP] * l aTcda] P Q* l l l .OW LETS WRITE A TEST FOR THIS CLASS SO THAT WE CAN TEST THAT A SUM OF TWO INTEGERS BOTH GREATER THAN WILL COVER THE CLASS 4]VX]T SEE ,ISTING ,ISTING 5NIT 4EST FOR THE Bd\ -ETHOD dbX]V <XRa^b^UcEXbdP[BcdSX^CTbcC^^[bD]XcCTbcX]V* ]P\Tb_PRT 2P[Rd[Pc^aCTbc j JCTbc2[PbbL _dQ[XR R[Pbb 4]VX]TCTbc j JCTbc
u
#(!04%2 ■ 4%34).' 7)4( 4$$
&IGURE #ODE COVERAGE WINDOW IN 6ISUAL 3TUDIO "ECAUSE ,ISTING DIDNT COVER THE OCCURRENCE WHERE ONE OF THE PARAMETER CAN BE EQUAL OR LESS THAN -34EST THE 4$$ FRAMEWORK INTEGRATED INTO 6ISUAL 3TUDIO TELLS YOU THAT YOU DIDNT COVER ALL THE CODE 4HE NEXT TEST SHOWN IN ,ISTING WILL COVER THAT PART OF THE Bd\ METHOD ,ISTING #OVERING AN %XCEPTION JCTbc
&IGURE #ODE COVERAGE FOR THE Bd\ METHOD )N THIS CASE FOR A SINGLE METHOD WE WROTE TWO TEST METHODS SO THAT THE AMOUNT OF CODE USED TO TEST THE REQUIREMENT WAS MORE THAN THE AMOUNT OF CODE USED TO SATISFY THE REQUIREMENT #ODE COVERAGE DOESNT ALWAYS INDICATE QUALITY OF THE 4$$ PROCESS ESPECIALLY WHEN THE CODE AND THE TESTS ARE WRITTEN BY THE SAME DEVELOPER )N ANY CASE SETTING YOUR MINIMUM CODE COVERAGE REQUIREMENT TO IS A GOOD STARTING POINT AND SHOULD GUARANTEE A CERTAIN CODE QUALITY LEVEL 4HE MORE COVERAGE THE BETTER
4$$ WITH -34EST 7HEN YOU DEVELOP AN APPLICATION USING THE 4$$ APPROACH YOU NEED AN ENVIRONMENT THAT LETS YOU RUN THE TESTS IN A CONTEXT DIFFERENT FROM THE ONE REPRESENTED BY THE APPLICATION )N 6ISUAL 3TUDIO YOU CAN DO THIS BY CREATING IN THE SAME SOLUTION YOU ARE TESTING A NEW PROJECT OF TYPE h4ESTv THAT WILL CONTAIN THE METHODS AND TESTS FOR YOUR TESTING
#(!04%2 ■ 4%34).' 7)4( 4$$
■ .OTE 5NFORTUNATELY THE %XPRESS EDITIONS OF 6ISUAL # OR 6ISUAL 6".%4 DONT ALLOW YOU TO INSTALL OR RUN PLUG INS AND SINCE THE -34EST ENGINE AVAILABLE IN 6ISUAL 3TUDIO ANY VERSION EXCEPT %XPRESS IS A SORT OF PLUG IN IN ORDER TO FOLLOW THE EXAMPLES PROVIDED IN THIS CHAPTER YOU NEED A DEMO VERSION OF 6ISUAL 3TUDIO 0ROFESSIONAL 0REMIUM OR 5LTIMATE AVAILABLE FOR DOWNLOAD AT fff\XRa^b^UcR^\eXbdP[bcdSX^T]dbS^f][^PS
,ETS EXPLORE THE -34EST ENGINE BY CREATING A SIMPLE PROJECT THE PROJECT YOU SAW IN THE h#ODE #OVERAGEv SECTION WHICH IS COMPOSED OF A CLASS LIBRARY THAT CONTAINS A SIMPLE #ALCULATOR ENGINE THAT CAN EXECUTE SUMS ONLY OF INTEGERS GREATER THAN
Calculator Example /PEN A NEW INSTANCE OF 6ISUAL 3TUDIO AND CREATE A NEW 0ROJECT UNDER 6ISUAL # ➤ 7INDOWS ➤ #LASS ,IBRARY 'IVE THE 3OLUTION THE NAME 2P[Rd[Pc^a AND PRESS /+ .OW YOU SHOULD HAVE A BLANK CLASS LIBRARY PROJECT THAT CONTAINS ONLY A FILE CALLED 2[Pbb Rb $ELETE THIS FILE AND CREATE A NEW CLASS WITH THE NAME OF 4]VX]T 4HIS CLASS WILL CONTAIN THE SAME CODE USED IN ,ISTING .OW THAT YOUVE CREATED THE CLASS TO TEST LETS WRITE THE TWO REQUIREMENTS FOR THE TEST v
!SSERT THAT 4]VX]T CAN CALCULATE THE SUM OF TWO INTEGER GREATER THAN
v
!SSERT THAT 4]VX]T WILL THROW AN EXCEPTION IF ONE OF THE NUMBERS IS EQUAL TO
4O CREATE A NEW TEST YOU CAN EITHER CREATE A TEST PROJECT IN 6ISUAL 3TUDIO AND REFERENCE THE #ALCULATOR PROJECT OR YOU CAN RIGHT CLICK IN THE 4]VX]TRb CLASS FILE CONTENT ANY PLACE IN THE CODE EDITOR AND CHOOSE THE COMMAND #REATE 5NIT TESTSx4HIS COMMAND POPS UP A NEW DIALOG LIKE THE ONE IN &IGURE
&IGURE #REATE UNIT TEST DIALOG
#(!04%2 ■ 4%34).' 7)4( 4$$
4HE DIALOG LETS YOU SELECT THE METHODS AND PROPERTIES YOU WANT TO TEST SO SELECT THE CONSTRUCTOR AND THE Bd\ METHOD OF THE CLASS 4]VX]T 4HE NEXT WINDOW ASKS FOR A NAME FOR THE NEW # PROJECT #ALL IT 2P[Rd[Pc^aCTbc SO YOU KNOW THAT IT REPRESENTS THE TEST CTbc FOR THE 2P[Rd[Pc^a ASSEMBLY !T THIS POINT 6ISUAL 3TUDIO WILL CREATE TWO DIFFERENT METHODS AS SHOWN IN ,ISTING ,ISTING #ONSTRUCTOR 4EST JCTbc
&IGURE 4EST RESULTS WINDOW IN 6ISUAL 3TUDIO
#(!04%2 ■ 4%34).' 7)4( 4$$
4HE WINDOW IN THE &IGURE SHOWS THAT YOU CAN ACCESS ALL THE FEATURES OF THE -34EST ENGINE USING THE 4EST 2ESULTS PANEL 9OU CAN LOOK AT THE TEST RESULTS THE TEST EXECUTION STATISTICS AND MORE 9OU CAN ALSO SELECT ANY TEST AND RERUN IT OR RUN THEM WITH THE DEBUG OPTION 4HE OTHER TWO TESTS WERE SHOWN ALREADY IN ,ISTING AND ,ISTING ,ISTING VERIFIES THAT 4]VX]T CAN EXECUTE A NORMAL OPERATION WITH TWO INTEGER NUMBERS GREATER THAN WHILE ,ISTING IS USED FOR VERIFYING THE EXCEPTION /F COURSE THE TWO TESTS HAVE BEEN CHANGED FROM THE ORIGINAL ONES THAT 6ISUAL 3TUDIO PROVIDES 4HE METHOD IN ,ISTING USES THE ATTRIBUTE J4g_TRcTS4gRT_cX^]L AVAILABLE FROM THE <XRa^b^UcEXbdP[BcdSX^CTbcC^^[b NAMESPACE 4HE ATTRIBUTE IS USED TO TEST WHETHER AN EXPECTED EXCEPTION IS THROWN 4HE TEST METHOD WILL PASS IF THE EXPECTED EXCEPTION IS THROWN OF COURSE FOR IT TO PASS THE TEST YOU CANT INCLUDE IN YOUR TEST METHOD A TRYCATCH BLOCK THAT WILL CATCH THE EXCEPTION !NOTHER OPTION FOR CREATING A NEW 4EST IN 6ISUAL 3TUDIO IS TO RIGHT CLICK IN 3OLUTION %XPLORER ON A TEST PROJECT AND SELECT !DD .EW 4EST 4HIS BRINGS UP IN A DIALOG ALL THE TESTS THAT 63 PROVIDES YOULL SEE THE SAME LIST IN THE CONTEXT MENU WHEN YOU RIGHT CLICK ON A 4EST PROJECT SEE &IGURE 4HE NEXT SECTION GIVES IS A SHORT OVERVIEW OF WHAT EACH OF THESE TEST OBJECTS IS USED FOR
&IGURE !DD .EW 4EST DIALOG
#(!04%2 ■ 4%34).' 7)4( 4$$
Available Tests v
"ASIC 5NIT 4EST ! CLASS MARKED WITH THE ATTRIBUTE JCTbcL WHICH CAN INCLUDE VARIOUS TEST METHODS
v
#ODED 5) 4EST ! SPECIAL TEST THAT CAN RECORD USER ACTIONS IN A 5) TO TEST A GRAPHICAL COMPONENT SUCH AS A FX]S^f ?PVT OR DbTa2^]ca^[
v
$ATABASE 5NIT 4EST ! SPECIAL TEST USED TO VERIFY OBJECTS IN A DATABASE SUCH AS STORED PROCEDURES TABLES AND MORE
v
'ENERIC 4EST ! GENERIC PROGRAM OR S[[ WRAPPED TO WORK AND ACT AS A TEST
v
,OAD 4EST !N AGGREGATION OF TESTS THAT CAN RUN TOGETHER
v
/RDERED 4EST ! WAY TO EXECUTE A SET OF TESTS IN A SPECIFIED ORDER
v
5NIT 4EST 4HE CLASSIC UNIT TEST LIKE THE ONE CREATED IN THIS SECTION
v
5NIT 4EST 7IZARD ! WAY TO BRING UP THE !DD.EW 4EST WIZARD SHOWN IN &IGURE
v
7EB 0ERFORMANCES ! WAY TO TEST THE PERFORMANCE OF 7EB APPLICATIONS SUCH !30.%4
)N THIS BOOK YOULL LEARN HOW TO CREATE A UNIT TEST AND THEN SEE HOW MUCH CODE WAS COVERED WITH THE TEST )F YOU PLAN TO COVER YOUR APPLICATION BY TESTING ALL THE LAYERS AND COMPONENTS AND TO DEVELOP USING THE 4$$ APPROACH YOU WILL PROBABLY ALSO USE THE ,OAD AND THE /RDERED TESTS SO YOU CAN TEST AN ENTIRE COMPONENT OR AN ENTIRE LAYER BY SEQUENTIALLY RUNNING A SET OF UNIT TESTS THAT WILL COVER ALL YOUR APPLICATION REQUIREMENTS 4HE ONLY TEST THAT YOU WILL NEVER USE FOR A 70& APPLICATION IS THE 0ERFORMANCES TEST FOR A 7EB APPLICATION
#ODE #OVERAGE WITH 6ISUAL 3TUDIO )N THE PREVIOUS SECTION YOU SAW THAT AN IMPORTANT MEASURE OF THE CORRECTNESS OF YOUR TESTS IS THE #ODE #OVERAGE METRIC THAT IS A CORE PART OF THE 4$$ METHODOLOGY BUT YOU HAVENT SEEN YET HOW TO USE THIS TECHNIQUE IN 6ISUAL 3TUDIO )N 6ISUAL 3TUDIO THE CODE COVERAGE FEATURE WAS PRECONFIGURED SO THAT AFTER YOU RAN A UNIT TEST YOU COULD IMMEDIATELY VERIFY THE TESTS COVERAGE )N THE VERSION THE CODE COVERAGE IS NOT ENABLED BY DEFAULT AND TO ENABLE IT YOU NEED TO UNDERSTAND HOW 6ISUAL 3TUDIO CONFIGURES THE TEST ENVIRONMENT &IGURE SHOWS THE DEFAULT LAYOUT OF THE #ALCULATOR PROJECT AFTER THE FIRST SET OF UNIT TESTS WAS CREATED
#(!04%2 ■ 4%34).' 7)4( 4$$
&IGURE 3OLUTION %XPLORER VIEW OF THE #ALCULATOR PROJECT 6ISUAL 3TUDIO CREATED TWO ADDITIONAL FILES AND ONE FOLDER IN THE ROOT OF THE APPLICATION 4HE FOLDER IS CALLED B^[dcX^] 8cT\b AND THE FILES ARE 2P[Rd[Pc^aeb\SX AND ;^RP[cTbcbTccX]Vb 4HE FILE WITH THE EXTENSION eb\SX IS USED BY 6ISUAL 3TUDIO )F YOU DOUBLE CLICK IT YOULL SEE A PANEL SHOWING ALL THE AVAILABLE TESTS IN THE SOLUTION 4HE FILE WITH THE cTbcbTccX]Vb EXTENSION INCLUDES ALL THE INFORMATION THE TESTS REQUIRE TO RUN 9OU CAN HAVE MORE THAN ONE cTbcbTccX]Vb SO YOU CAN SPECIFY DIFFERENT REQUIREMENTS FOR YOUR TESTS ,ETS SAY THAT IN YOUR TESTS YOU REFERENCE AN ASSEMBLY LOCATED IN 2)K
#(!04%2 ■ 4%34).' 7)4( 4$$
&IGURE 6ISUAL 3TUDIO TEST SETTINGS 9OU CAN ACCESS EACH SECTION IN THE DIALOG USING THE LIST ON THE LEFT THE OPTION YOU NEED TO MODIFY IS THE ONE LABELED $ATA AND $IAGNOSTICS WHICH CONTAINS INFORMATION ABOUT THE TOOLS YOU WANT TO ADD TO YOUR TEST .OTE THOUGH THAT THIS SECTION IS AVAILABLE ONLY IN THE 5LTIMATE AND 0REMIUM VERSIONS OF 6ISUAL 3TUDIO SO IF YOU HAVE A LOWER VERSION LIKE 0ROFESSIONAL YOU WONT BE ABLE TO ACCESS THIS SECTION OF THE TEST CONFIGURATION 3ELECT THE #ODE #OVERAGE TOOL AND THEN CLICK THE #ONFIGURE BUTTON AT THE TOP OF THE $ATA AND $IAGNOSTICS LIST 9OULL THEN SEE ANOTHER WINDOW WITH A LIST OF ASSEMBLIES YOU CAN INSTRUMENT )N THIS WINDOW YOU SELECT THE !SSEMBLY YOU WANT TO TEST SO CHOOSE 2P[Rd[Pc^aS[[ 7HAT DID YOU DO IN THIS STEP 6ISUAL 3TUDIO NEEDS TO KNOW WHAT CODE YOU ARE EXECUTING AND COVERING IN YOUR TESTS SO IT CAN MODIFY THE CORRESPONDING ASSEMBLY CODE IN ORDER TO INSTRUMENT IT SO THAT WHEN YOU RUN THE CODE -34EST WILL BE ABLE TO MONITOR IT AND GIVE YOU BACK A CODE COVERAGE RESULT .OW IF YOU RUN YOUR TESTS AGAIN THE 6ISUAL 3TUDIO TEST ENGINE SHOULD ALSO CREATE CODE COVERAGE STATISTICS THAT YOU CAN EXPLORE AFTER ALL THE TESTS ARE EXECUTED 4HE CODE COVERAGE IS CALCULATED ONLY AFTER A SPECIFIC TEST IS EXECUTED AND ITS ASSERTS ARE ALL SUCCESSFUL )F THE TESTS FAIL 63 WILL NOT CALCULATE THE CODE COVERAGE 9OU CAN VIEW THE CODE COVERAGE WINDOW IN DIFFERENT WAYS v
&ROM THE MENU SELECT 4EST ➤7INDOWS ➤ #ODE #OVERAGE 2ESULTS
v
2IGHT CLICK A TEST RESULT IN THE 4EST 2ESULTS WINDOWS AND SELECT 6IEW #ODE #OVERAGE
,OOK BACK AT &IGURE WHICH SHOWS THE #ODE #OVERAGE PANEL IN ACTION FOR EACH METHOD OR PROPERTY EXPOSED BY THE INSTRUMENTED ASSEMBLY CODE COVERAGE SHOWS YOU THE NUMBER OF BLOCKS OF CODE THAT CAN BE COVERED BY TESTS THE NUMBER ALREADY COVERED AND A PERCENTAGE
#(!04%2 ■ 4%34).' 7)4( 4$$
&ROM THIS WINDOW YOU CAN DOUBLE CLICK ONE OF THE TESTED METHODS OR PROPERTIES AND VERIFY THE AMOUNT OF CODE COVERED "UT BEFORE DOING THAT YOU SHOULD ENABLE THE CODE COLORING FEATURE #ODE COLORING CAN BE SWITCHED ON AND OFF BY CLICKING ON THE FOURTH ICON IN THE TOOLBAR OF THE #ODE #OVERAGE PANEL !FTER YOU ENABLE CODE COLORING AND YOU CLICK FOR EXAMPLE ON THE Bd\ METHOD OF THE 4]VX]T CLASS IT WILL APPEAR AS SHOWN IN &IGURE
&IGURE #ODE COLORING 9OU CAN CONFIGURE THE COLORS IN 6ISUAL 3TUDIO BY OPENING THE COLOR PREFERENCES AVAILABLE THROUGH THE MENU 4OOLS ➤ /PTIONS ➤ &ONTS AND #OLORS ) ASSIGNED RED TO THE NONCOVERED REGIONS GREEN TO THE COVERED REGIONS AND ORANGE TO THE PARTIALLY COVERED REGIONS &IGURE SHOWS THE Bd\ METHOD MODIFIED FROM ITS ORIGINAL IMPLEMENTATION JUST TO SHOW YOU HOW THE CODE COVERAGE COLORING WORKS IN 6ISUAL 3TUDIO
TDD: the TimeTracker Application .OW THAT YOUVE SEEN HOW THE 4$$ METHODOLOGY CAN BE APPLIED TO A PROJECT AND THE TOOLS THAT 6ISUAL 3TUDIO PROVIDES TO IMPLEMENT 4$$ LETS TAKE A LOOK AT THE 4IME4RACKER APPLICATION AND SEE HOW TO TEST THE VARIOUS LAYERS OF THE APPLICATION 9OU MAY ALREADY UNDERSTAND FROM READING THIS CHAPTER THAT EVEN THOUGH THE 4$$ TECHNIQUE IS VERY HELPFUL IT IS ALSO VERBOSE AND TIME CONSUMING IN FACT THE COMMON RULE OF 4$$ IS THAT YOU WILL END UP WRITING CODE MORE IN THE TESTS THAN IN THE PRODUCTION CODE 3O FOR INSTANCE IF YOUR PRODUCTION CODE CONSISTS OF LINES YOU WILL PROBABLY END UP WITH LINES LINES OF PRODUCTION CODE LINES OF TESTING CODE /F COURSE THIS EXAMPLE IS PURELY THEORETICAL )N ANY CASE LETS TAKE A LOOK AT HOW TO TEST EACH OF THE EXISTING LAYERS $!, ",, 5) OF THE APPLICATION AND HOW TO DO THIS USING THE COMMON 4$$ BEST PRACTICES )T IS NOT THE GOAL OF THIS SECTION TO SHOW YOU THE FULL TESTING CODE OF THE ENTIRE APPLICATION 7HEN YOU WRITE A LAYERED APPLICATION LIKE THE 4IME4RACKER 70& APPLICATION YOU USUALLY CREATE AN ADDITIONAL TEST PROJECT FOR EACH LAYER SO YOU CAN KEEP THE TESTS OF ONE LAYER SEPARATE AND LOOSELY COUPLED TO THE TESTS OF ANOTHER LAYER
#(!04%2 ■ 4%34).' 7)4( 4$$
4HE ORDER IN WHICH YOU TEST YOUR LAYERS IS USUALLY DEVELOPMENT DRIVEN SO YOU SHOULD EXPECT TO CREATE A TEST PROJECT FOR THE DOMAIN LAYER THEN CREATE THE DOMAIN REQUIREMENTS IN THE TEST AND ONE BY ONE CREATE THE CORRESPONDING CODE IN THE DOMAIN ASSEMBLY 7HEN THE DOMAIN IS READY YOU SHOULD CREATE THE DATA LAYER PART AND THEN THE PRESENTATION LAYER 6IEW-ODELS 6IEWS
4EST THE "USINESS $OMAIN ,AYER "ECAUSE WE CREATED ALL THE 4IME4RACKER CODE BEFORE WRITING THE TESTS IN THE FOLLOWING EXAMPLE WE ARE NOT REALLY WRITING 4$$ BUT SIMPLY WRITING TESTS FOR THE EXISTING CODE 3INCE THE PURPOSE OF THIS BOOK IS TO CREATE A LAYERED 70& APPLICATION AND NOT TO LEARN 4$$ THE IDEA IS JUST TO SHOW YOU HOW TO IMPLEMENT 4$$ )N THE REAL WORLD OF COURSE THE APPROACH SHOULD BE OPPOSITE TO THE ONE USED IN THIS BOOKYOU SHOULD CREATE THE TESTS FIRST AND THEN THE CORRESPONDING CODE )N 3OLUTION %XPLORER RIGHT CLICK THE 4IME4RACKER SOLUTION NODE AND ADD A NEW SOLUTION FOLDER CALLED 4ESTS 4HIS FOLDER WILL KEEP THE TEST PROJECTS SEPARATE FROM THE REST OF THE APPLICATION 9OU MAY HAVE ALREADY CREATED THIS IN THE PREVIOUS CHAPTERS WHEN YOU CREATED THE UNIT TESTS FOR THE DATA LAYER )M REPEATING IT HERE JUST TO BE SURE YOU HAVE EVERYTHING IN PLACE )N THE 4$$ SYNTAX A TEST IS CALLED &IXTURE BUT 6ISUAL 3TUDIO CALLS THEM 4EST )N THIS BOOK ) USE THE 4EST NAMING CONVENTION JUST TO STAY IN LINE WITH THE 6ISUAL 3TUDIO SYNTAX BUT IT MAY HAPPEN THAT YOU SEE A PROJECT WHERE THE TESTS ARE CALLED USING THE PREFIXSUFFIX &IXTURE INSTEAD OF THE 4EST ONE .OW RIGHT CLICK ON THE 4ESTS FOLDER AND CREATE A NEW PROJECT OF TYPE 4EST $OCUMENT ➤ 4EST 0ROJECT .AME IT 0?A4BBCX\TCaPRZX]V1dbX]Tbb;PhTaCTbc AND PRESS /+ 6ISUAL 3TUDIO WILL CREATE A NEW TEST PROJECT WITH A DEFAULT UNIT TEST CALLED D]XcCTbc Rb THAT YOU HAVE TO DELETE .OW CHOOSE TO ADD A NEW TEST TO THE PROJECT AND SELECT THE 5NIT 4EST FILE TYPE 6ISUAL 3TUDIO OPENS A DIALOG SHOWING ALL THE AVAILABLE PROJECTS IN THE APPLICATION NAVIGATE THROUGH THE "USINESS ,AYER PROJECT AND SELECT THE A^[T DOMAIN ENTITY WITH ALL ITS PROPERTIES AND METHODS AND PRESS /+ 6ISUAL 3TUDIO WILL NOW ADD ALL THE REQUIRED REFERENCES AND IT WILL ALSO CREATE A NEW FILE CALLED A^[TCTbcRb CONTAINING ALL THE REQUIRED TEST 4ESTS FOR THE A^[T OBJECT 9OU CAN SIMPLY RIGHT CLICK ON THE A^[TCTbc JCTbc2[PbbL ATTRIBUTE AND SELECT THE 2UN TEST COMMAND AND EXECUTE ALL THE RELATED TESTS 9OU WILL RECEIVE A FAILURE FOR SOME TESTS AND A QUESTION MARK FOR OTHERS THIS IS DUE TO THE FACT THAT 63 ADDS AT THE END OF EACH TEST THE 0bbTac8]R^]R[dbXeT METHOD WHICH INFORMS 63 THAT YOUR TESTS ARE INCONCLUSIVE AND THAT YOU HAVE TO VERIFY THE FINAL RESULT 7HAT YOU CAN DO NOW IS TO REMOVE ALL THE 0bbTac8]R^]R[dbXeT METHODS AND RERUN ALL THE TESTS 4HEN READ THE #ODE #OVERAGE TO UNDERSTAND HOW MUCH AMOUNT OF CODE THE 6ISUAL 3TUDIO WIZARD HAS ALREADY COVERED FOR YOU 9OU SHOULD GET COVERAGE AS THE CODE IMPLEMENTED BY 6ISUAL 3TUDIO IN YOUR TESTS IS JUST THE STARTING POINT &OR A DOMAIN ENTITY CREATED BY %NTITY &RAMEWORK YOU SHOULD TEST THE FOLLOWING COMPONENTS AND METHODS v
4EST THE CONSTRUCTOR AND VERIFY THAT THE PROPERTIES ARE INITIALIZED CORRECTLY
v
7RITE A TEST FOR EACH PROPERTY BY ADDING A MOCKUP VALUE TO THE PROPERTY AND VERIFY THAT THE VALUE IS PROPERLY INJECTED
v
7RITE A TEST FOR EACH METHOD EXPOSED BY THE ENTITY AND A CORRESPONDING TEST TO ALSO VERIFY THE WELL KNOWN EXCEPTIONS IT MAY THROW
&OR THE DOMAIN MODEL YOU SHOULD COVER ALMOST AS IT DOESNT HAVE ANY DEPENDENCIES AND IT REPRESENTS THE CORE OF THE APPLICATION
#(!04%2 ■ 4%34).' 7)4( 4$$
4ESTING THE $ATA ,AYER 4ESTING THE $!, OF AN APPLICATION CAN BE A MORE CHALLENGING TASK BECAUSE IT HAS A DIRECT DEPENDENCY ON THE DATABASE 7HEN YOU WORK WITH AN /2- THE TESTING YOU DO USING THE DATA LAYER IS TO BE SURE THAT THE DATA CAN BE PERSISTED AND RETRIEVED AS EXPECTED USING THE /2- AND THE DATA LAYER THAT IS WRAPPING IT AROUND 3O HOW CAN YOU TEST A DATA LAYER PROPERLY &IRST CREATE A NEW TEST PROJECT BUT THIS TIME NAME THE PROJECT 0?A4BBCX\TCaPRZX]V3PcP;PhTaCTbc AS IT WILL INCLUDE TESTS FOR THE $!, OF THE 4IME4RACKER APPLICATION 4HE FIRST COMPONENT TO TEST IS THE D]Xc>UF^aZNOT THE 8D]Xc>UF^aZAS IN THE 4$$ METHODOLOGY YOU TEST THE IMPLEMENTATION AND NOT THE CONTRACT 3O AS YOU DID FOR THE A^[T ENTITY IN THE PREVIOUS SECTION USING THE 4EST CREATION WIZARD CREATE A NEW TEST PROJECT THIS TIME FOR THE D]Xc>UF^aZ COMPONENT OF THE $!, "UT DONT SELECT ANY METHOD THIS TIME SO THAT THE TEST WILL BE CREATED WITH ALL THE CORRESPONDING REFERENCES BUT WITHOUT ANY METHOD INSIDE 4HE SECOND STEP IS TO ADD A NEW FILE OF TYPE h#ONFIGURATION FILE APPCONFIG v IN THE TEST PROJECT AND PROVIDE A CONNECTION STRING LIKE YOU DID IN THE 70& APPLICATION SO THAT THE %NTITY &RAMEWORK WILL BE ABLE TO READ THE CONFIGURATION SECTION AND RUN AGAINST A DATABASE 3EE ,ISTING 7EVE ACTUALLY DONE THIS ALREADY IN #HAPTER BUT NOW WERE ALSO TAKING A CLOSER LOOK THE 4$$ TECHNIQUE ,ISTING #ONFIGURATION &ILE FOR THE $!, 4EST 0ROJECT +.g\[ eTabX^], T]R^SX]V,dcU'.+R^]UXVdaPcX^]+R^]]TRcX^]BcaX]Vb+PSS ]P\T,CX\TCaPRZTa<^ST[2^]cPX]Ta R^]]TRcX^]BcaX]V,\TcPSPcP,aTb)CX\TCaPRZTa<^ST[RbS[k aTb)CX\TCaPRZTa<^ST[bbS[k aTb)CX\TCaPRZTa<^ST[\b[* _a^eXSTa,BhbcT\3PcPB`[2[XT]c* _a^eXSTa R^]]TRcX^] bcaX]V,`d^c* 3PcP B^daRT,KB@;4G?A4BB* 8]XcXP[ 2PcP[^V,CX\TCaPRZTa3PcPQPbT* 8]cTVaPcTS BTRdaXch,CadT*
#(!04%2 ■ 4%34).' 7)4( 4$$
,ISTING 5NIT 4EST FOR THE #REATION OF A D]Xc >U F^aZ JCTbc
#(!04%2 ■ 4%34).' 7)4( 4$$
aT_^bXc^ah3T[TcTa^[T* 0bbTac8bCadTTg_TRcTSA^[T4]cXchBcPcT ,, 4]cXchBcPcT3T[TcTS* d^f2^\\XcCaP]bPRcX^]* eTaXUh cWT ^QYTRc S^Tb]c TgXbc A^[T UX]P[4g_TRcTSA^[T , d^f BTbbX^] 2aTPcT>QYTRcBTc+A^[T- FWTaTa ,- a8S ,, a^[T8S 5Xabc>a3TUPd[c* 0bbTac8b=d[[UX]P[4g_TRcTSA^[T* l l 4HE PURPOSE OF THIS CODE IS TO VERIFY THAT THE 5O7 IS ABLE TO DELETE AN EXISTING OBJECT "ECAUSE THE 4$$ REQUIRES THAT THE TESTING ENVIRONMENT BE ROLLED BACK TO ITS INITIAL STATE WHEN THE TEST IS DONE YOU SHOULD ALWAYS LEAVE THE TEST DATABASE EMPTY AND PREPARE IT BASED ON THE TEST YOU ARE RUNNING )N THIS CASE YOU NEED AN ENTITY IN THE DATABASE BEFORE YOU CAN DELETE ONE SO THE FIRST THING TO DO IS TO CREATE A NEW ENTITY AND VERIFY THAT IS HAS BEEN CREATED PROPERLY IN THE DATABASE 4HEN YOU DELETE IT USING THE REPOSITORY AND FINALLY RETRIEVE IT AGAIN USING THE 5O7 4HE CREATION OF A NEW ENTITY SHOULD BE A TEST ON ITS OWN AS WELL BUT TO KEEP THINGS SIMPLE IN THIS BOOK WE JUST ASSEMBLED THE TWO TESTS TOGETHER )F YOU ARE WONDERING WHY WE ARE NOT USING THE REPOSITORY TO INSERT AND RETRIEVE THE ENTITY THE ANSWER IS VERY SIMPLE )TS BECAUSE THIS TEST CLASS IS USED TO TEST THE REPOSITORY ITSELF AND THIS METHOD IS USED SPECIFICALLY TO TEST THE DELETE METHOD !T THIS POINT YOU STILL DONT KNOW IF THE INSERT METHOD IS WORKING SO YOU SHOULDNT ADD ADDITIONAL DEPENDENCIES TO TEST 4HE SAME PROCESS SHOULD BE DONE FOR THE REMAINING REPOSITORIES OF THE DATA LAYER
4ESTING THE 5) 4HE PROCESS OF TESTING THE 5) CAN BE ACCOMPLISHED WITH TWO DIFFERENT STEPS &OR EXAMPLE THE 0?A4BBCX\TCaPRZTa 70& PROJECT CONSISTS OF A SET OF SERVICES AND 6IEW-ODEL OBJECTS THAT CAN BE TESTED USING THE NORMAL APPROACH OF CREATING A NEW UNIT TEST AND ASSERTING THE CORRESPONDING REQUIREMENTS )N CONTRAST SOMETHING LIKE THE 4\_[^hTT;XbcEXTfgP\[ OBJECT CAN BE TESTED ONLY BY HAVING A USER THAT INTERACTS WITH THE VIEW ,ETS CREATE A NEW TEST PROJECT AND CALL IT 0?A4BBCX\TCaPRZTaCTbc )T WILL INCLUDE TESTS FOR THE SERVICES TESTS FOR THE 6IEW-ODELS AND TESTS FOR THE 5) 9OU CAN USE THE 7IZARD TEST TO TEST ALL THE 6IEW-ODELS THE 3ERVICES AND THE COMMAND OBJECTS IN THE SAME WAY AS WITH THE DOMAIN ENTITY 3O FOR INSTANCE IF A 6IEW-ODEL OBJECT HAS SOME PROPERTIES AND METHODS YOU NEED TO VERIFY THAT THEY ARE ALL CREATED AND INSTANTIATED AS EXPECTED )N THIS CASE WITH A 6IEW-ODEL YOU WANT TO VERIFY THAT THE VALUE EXPOSED BY THE 6IEW-ODEL IS THE SAME AS THAT OF THE CORRESPONDING PROPERTY IN THE CORRESPONDING MODEL AS IN ,ISTING
#(!04%2 ■ 4%34).' 7)4( 4$$
,ISTING 4ESTING THE 0SSaTbb3TcPX[bEXTf<^ST[ /BJECT JCTbc
&IGURE 2ECORDING ACTIONS IN 6ISUAL 3TUDIO 5) TESTING )N THIS CASE WE INSERT A hSEARCH TEXTv STRING IN A CTgc1^g SO THE CORRESPONDING TEST SHOULD VERIFY THAT THE 6IEW-ODEL PROPERTY HAS THAT SAME VALUE 7RITING TESTS FOR THE 5) IS NOT AS EASY AS WRITING TESTS FOR NON 5) OBJECTS AS YOU NEED TO CONSIDER ADDITIONAL FACTORS LIKE ALL THE POSSIBLE OPTIONS OFFERED BY THE 5) ELEMENTS THE USER INTERACTIONS AND MORE &INALLY WITH THE 5) TESTING YOU CANT GET REAL CODE COVERAGE AS YOU ARENT TESTING THE CODE COVERAGE OF THE APPLICATION BUT THE 5) BEHAVIOR REQUIREMENTS
#(!04%2 ■ 4%34).' 7)4( 4$$
Summary 4EST $RIVEN $EVELOPMENT IS AN AGILE METHODOLOGY USED TO INVERT THE ORDER IN WHICH YOU DEVELOP AND TEST AN APPLICATION )NSTEAD OF WRITING THE CODE AND THEN DEBUGGING IT WITH 4$$ YOU WRITE THE TEST TO VERIFY THE APPLICATION REQUIREMENTS AND THEN YOU WRITE THE CODE TO SATISFY THESE TESTS AND CONSEQUENTLY ALSO THE APPLICATION REQUIREMENTS 7RITING A 4$$ APPLICATION MAY REQUIRE TIME AND WHEN IT IS YOUR FIRST 4$$ APPLICATION IT MAY REQUIRE DOUBLE THE TIME YOU NORMALLY NEED TO WRITE AN APPLICATION 4HE ADVANTAGE OF USING 4$$ IS REALLY DISCOVERED IN THE FUTURE WHEN YOU MOVE FROM THE DEVELOPMENT PHASE OF THE APPLICATION TO THE MAINTENANCE PHASE )N THIS PHASE YOU TYPICALLY SPEND A LOT OF TIME TESTING DEBUGGING AND TRYING TO FIX AND SORT OUT BUGS DISCOVERED BY YOUR 1! DEPARTMENT )F YOUVE ADOPTED 4$$ METHODOLOGY THIS PHASE WILL BE MUCH EASIER BECAUSE YOU WILL ALREADY HAVE A SET OF TESTS THAT CAN VERIFY WHETHER THE APPLICATION IS STILL STABLE AND COVERED AND IF A NEW FEATURE MIGHT COMPROMISE THE EXISTING STRUCTURE OF THE SOFTWARE 7HEN YOU TEST A LAYERED APPLICATION LIKE 4IME4RACKER YOU NEED TO PROVIDE A TEST PROJECT FOR EACH LAYER AND USE A DIFFERENT APPROACH BASED ON THE TYPE OF CODE YOU ARE TESTING &OR INSTANCE THE TESTING CODE YOU WRITE FOR A DOMAIN ENTITY IS TOTALLY DIFFERENT FROM THE CODE YOU WRITE TO TEST A USER INTERFACE LIKE A 70& 6IEW 4$$ CAN BE MEASURED USING CODE COVERAGETHE AMOUNT OF CODE COVERED BY A SPECIFIC SET OF UNIT TESTS #ODE COVERAGE IS ONE METRIC FOR 4$$ BUT IT SHOULD NOT BE CONSIDERED THE ONLY METRIC BECAUSE SOMETIMES IT DOESNT REALLY PROVIDE AN ABSOLUTE MEASURE OF CODE QUALITY 6ISUAL 3TUDIO PROVIDES A SET OF PANES TOOLS AND ASSEMBLIES THAT CAN MAKE THE CREATION OF A 4$$ APPLICATION FAST AND EASY 7ITHIN THE 6ISUAL 3TUDIO )$% YOU CAN ACCESS ALL THE ASPECTS OF THE 4$$ AND CONTROL CODE QUALITY AND CODE COVERAGE 5NFORTUNATELY ALL THESE NICE FEATURES ARE NOT AVAILABLE IN THE %XPRESS EDITION OF 6ISUAL 3TUDIO
CHAPTER ■■■
Reports with Microsoft Reporting Services 'IVING THE USER A WAY TO PRINT OUT THE DATA HES WORKING WITH IS A VERY COMMON APPLICATION REQUIREMENT ESPECIALLY WHEN THIS DATA IS USED FOR BUSINESS PURPOSES )N THE 4IME4RACKER APPLICATION ONE OF THE TASKS THE USER CAN ACCOMPLISH IS TO ORGANIZE APPOINTMENTS BETWEEN AN EMPLOYEE AND A CUSTOMER AND THIS REQUIRES A SET OF REPORTS THAT SHOULD BE DISPLAYED AND PRINTED WITHIN THE 5) USING A REPORT VIEWER COMPONENT ! REPORT IS A DIGITAL DOCUMENT THAT DISPLAYS INFORMATION RETRIEVED FROM A DATABASE IN THE FORM OF ORDERED DATA TYPICALLY AS A GRID OR MATRIX OR GRAPHICALLY AS IN A CHART 4HE REPORT BECOMES MORE USEFUL TO THE USER WHEN IT CAN BE PRINTED AND WHEN IT CAN BE EXPORTED FROM THE APPLICATION AND USED WITH DIFFERENT SOFTWARE FOR EXAMPLE IF IT CAN BE EXPORTED AS A 0ORTABLE $IGITAL &ORMAT 0$& AND SAVED TO THE LOCAL FILE SYSTEM OR SENT WITH AN E MAIL 6ISUAL 3TUDIO OFFERS TWO DIFFERENT TYPES OF REPORTING YOU CAN USE IN YOUR APPLICATIONS /NE IS THE -ICROSOFT 31, 3ERVER 2EPORTING 3ERVICES SOFTWARE A SUBSET OF THE REPORTING TECHNOLOGY IN -ICROSOFT 31, 3ERVER 2 THE SECOND IS A THIRD PARTY TECHNOLOGY THAT IS NOW PROVIDED FOR FREE WITHIN THE 6ISUAL 3TUDIO ENVIRONMENTTHE FAMOUS #RYSTAL 2EPORTS #2 4HE -ICROSOFT REPORTING TECHNOLOGY CAN BE HOSTED USING YOUR 31, 3ERVER 2 7INDOWS 3HARE0OINT 3ERVICES IF YOU HAVE THAT OR DIRECTLY WITHIN YOUR 70& APPLICATION USING THE 7INDOWS &ORMS CONTROL 2EPORT6IEWER THAT 63 PROVIDES )T PRODUCES A FILE WITH EXTENSION aS[R WHICH CAN BE EDITED IN THE 6ISUAL 3TUDIO )$% )T LETS YOU HAVE A PREVIEW OF THE FINAL RESULT BY SIMPLY QUERYING THE DATA AVAILABLE IN THE DATABASE AND BY SHOWING YOU A PREVIEW OF THE FINAL RESULT 3!0 #RYSTAL 2EPORTS FOR 6ISUAL 3TUDIO IS A SUBSET OF THE 3!0 #RYSTAL 2EPORTS ENGINE AVAILABLE FROM 3!0 WHICH BOUGHT THE #RYSTAL 2EPORTS COMPANY A FEW YEARS AGO 4HOUGH #2 WAS INTEGRATED IN 6ISUAL 3TUDIO IT WAS NOT FREE YOU HAD TO PURCHASE A DEVELOPMENT LICENSE -OREOVER THE INTEGRATION WITH 7INDOWS &ORMS WAS NOT EASY AT ALL .OW HOWEVER 3!0 #RYSTAL 2EPORTS HAS RELEASED A VERSION FOR 6ISUAL 3TUDIO THAT IS COMPLETELY FREE AND EASY TO REDISTRIBUTE 9OU JUST NEED TO REGISTER YOUR VERSION AND DOWNLOAD THE RUNTIMES FROM THIS WEB SITE IN ORDER TO HAVE A FULL WORKING VERSION fffbP_R^\ b^[dcX^]bbP_RahbcP[b^[dcX^]b`dTahaT_^acX]VP]P[hbXbbP_RahbcP[aT_^acb eXbdP[bcdSX^X]STgT_g )N THIS CHAPTER WE WILL CONFIGURE THE -ICROSOFT REPORTING TECHNOLOGY AND BUILD SOME REPORTS SO YOULL GET A NICE OVERVIEW OF THE PRODUCT 7E WILL HOST THE REPORTS IN OUR 70& APPLICATION OR ON A 31, 3ERVER 2EPORTING 3ERVICE WEB SITE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
■ .OTE )N ORDER TO FOLLOW THE NEXT TUTORIALS YOUR DATABASE SHOULD BE POPULATED WITH SOME DUMMY DATA SO THAT YOU WILL BE ABLE TO VIEW THE DATA DISPLAYED IN THE REPORTS AND VERIFY THAT ITS CORRECT )N CASE YOU HAVENT COMPLETED THE 5) OF THE 4IME4RACKER APPLICATION YOU CAN EDIT THE DATA IN THE 4IME4RACKER DATABASE BY OPENING -ICROSOFT 31, 3ERVER -ANAGEMENT 3TUDIO RIGHT CLICKING ON A TABLE AND SELECTING THE COMMAND h%DIT ALL ROWSv 4HIS OPENS THE TABLE IN EDIT MODE AND YOU WILL BE ABLE TO ADD REMOVE OR MODIFY THE DATA
SQL Server Reporting Services 6ISUAL 3TUDIO PROVIDES A TOOL FOR CREATING aS[R REPORTS WHICH CAN BE HOSTED IN A 7INDOWS &ORMS OR 70& APPLICATION THE SECOND OPTION USES A SIMPLE APPROVED WAY OF EMBEDDING THE 7INDOWS &ORMS 2EPORT6IEWER CONTROL IN 70& OR BY DEPLOYING THEM TO A 3HARE0OINT 31, 2EPORT 3ERVER TO BE DISPLAYED USING A WEB BROWSER $URING THE WRITING OF THIS BOOK ) OMITTED SOME STEPS LIKE CREATING THE 2dbc^\Ta ENTITY DATA ENTRY VIEWS AS ) BELIEVE THAT AS PART OF THE LEARNING PROCESS THE READER SHOULD EMULATE THE STEPS DESCRIBED IN THE BOOK TO GENERATE NEW FEATURES IN THE APPLICATION &OR THIS REASON THE BOOK INCLUDES THE ENTIRE CODE TO SEARCH FOR AN 4\_[^hTT ENTITY AND TO EDIT CREATE OR DELETE AN 4\_[^hTT ENTITY )N THE SAME WAY WE WILL BUILD REPORTS FOR THE 4\_[^hTT ENTITY AND THEN YOU CAN FOLLOW THE STEPS TO BUILD REPORTS FOR THE 2dbc^\Ta ENTITY ON YOUR OWN 7E WANT TO PRODUCE THREE DIFFERENT REPORTS FOR AN 4\_[^hTT DOMAIN ENTITY 4HE FIRST ONE IS A SIMPLE LIST OF ALL THE AVAILABLE EMPLOYEES ORDERED BY FIRST NAME AND LAST NAME WITH A SUMMARY OVERVIEW OF THE EMPLOYEE INFORMATION 4HE SECOND ONE IS A MORE DETAILED REPORT THAT SHOWS ALL THE INFORMATION AND ALL THE ACTIVITIES OF A SINGLE EMPLOYEE AND IT WILL BE SHOWN FROM THE $ETAILS 6IEW 4HE THIRD REPORT IS A LIST OF APPOINTMENTS FOR EACH EMPLOYEE
#ONFIGURE 31, 3ERVER 2EPORTING 3ERVICES 7HEN YOU INSTALL 31, 3ERVER 2 WITH ADVANCED SERVICES AND YOU CHOOSE TO INSTALL THE 2EPORTING 3ERVICES FEATURE THE SETUP WILL CREATE AND INSTALL ALL THE REQUIRED COMPONENTS AND SERVICES FOR RUNNING 23 FROM YOUR DEVELOPMENT MACHINE OR FROM WHEREVER YOUVE INSTALLED SUCH SERVICE 5NFORTUNATELY THE SETUP DOESNT PREPARE THE SERVER SO YOULL HAVE TO CONFIGURE IT MANUALLY USING THE WIZARD AVAILABLE THROUGH 3TART ➤ !LL 0ROGRAMS ➤ -ICROSOFT 31, 3ERVER 2 ➤ #ONFIGURATION 4OOLS ➤ 2EPORTING 3ERVICES #ONFIGURATION -ANAGER !S SOON AS YOU OPEN THE WIZARD IT ASKS YOU TO IDENTIFY THE SERVER NAME IN THE FIRST CTgc1^g AND THE 31, 3ERVER INSTANCE IN THE SECOND ONE WHICH BY DEFAULT SHOULD BE B@;4G?A4BB 7HEN YOU HAVE LOCATED THE CORRECT SERVER PRESS /+ AND THE WIZARD WILL START SEE &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE 31, 3ERVER 2EPORTING 3ERVICES #ONFIGURATION -ANAGER 4HE CONFIGURATION WIZARD CONSISTS OF A SERIES OF STEPS THAT ARE INDEPENDENT OF EACH OTHER EACH STEP ALLOWS YOU TO CONFIGURE A SPECIFIC FUNCTIONALITY OF THE 31, 3ERVER 2EPORTING 3ERVICES 3323 !FTER YOU SET UP THE CONFIGURATION IN ONE STEP PRESS THE !PPLY BUTTON BEFORE MOVING ON TO A NEW ONE 4HE FIRST STEP IS SIMPLY VERIFYING THAT 3323 IS UP AND RUNNING 4HEN YOU FOLLOW THESE STEPS 1.
3ERVICE !CCOUNT )N THIS STEP YOU SELECT A SPECIFIC 7INDOWS ACCOUNT TO RUN THE 3323 SERVICE YOU CAN JUST LEAVE IT TO THE DEFAULT .ETWORK 3ERVICE SO YOU DONT NEED TO PRESS !PPLY
2.
7EB 3ERVICE 52, 4HIS STEP CONFIGURES THE WEB APPLICATION IN ))3 THAT WILL HOST THE 3323 WEB SITE 9OU CAN LEAVE THE SETTINGS AS PROPOSED BY THE WIZARD AND CLICK !PPLY THEN CLICK THE LINK IN THE MIDDLE OF THE DIALOG THAT POINTS TO THE CURRENT 3323 52, )F YOU ALREADY HAVE OTHER WEB SITES RUNNING ON ))3 PORT CONSIDER INSTALLING 3323 ON A DIFFERENT PORT
3.
$ATABASE )N THIS STEP YOU NEED TO TELL 3323 WHERE TO SAVE THE REPORTS YOULL CREATE 3ELECT #HANGE DATABASE AND FOLLOW THE WIZARD TO CREATE A NEW 3323 DATABASE )N THE $ATABASE SERVER SECTION BE SURE TO SUBSTITUTE THE DATABASE NAME WITH KB@;4G?A4BB IF YOU INSTALLED 3323 ON YOUR DEVELOPMENT MACHINE 4HEN SET UP CREDENTIALS AND AUTHENTICATION AND CREATE THE DATABASE FOLLOWING THE WIZARD 4HIS STEP CREATES TWO DATABASES ONE USING THE NAME YOU INSERTED AND ONE WITH THE SUFFIX 4EMP$B
4.
2EPORT -ANAGER 52, 4HIS STEP CREATES AN ADDITIONAL ))3 WEB APPLICATION THAT YOU WILL USE TO ADMINISTER THE REPORTS 0RESS !PPLY AND VERIFY THAT THE APPLICATION HAS BEEN CREATED
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
5.
% MAIL 3ETTINGS 4HIS IS USED TO CONFIGURE E MAIL SETTINGS IF YOU PLAN TO DEPLOY REPORTS THROUGH AN E MAIL SERVICE LIKE -ICROSOFT %XCHANGE 3ERVER 4HIS STEP IS NOT MANDATORY
6.
%XECUTION !CCOUNT )N THIS STEP YOU CAN CHANGE THE ACCOUNT THAT IS USED TO PROCESS THE REPORTS IF YOU NEED TO APPLY ADDITIONAL SECURITY
7.
%NCRYPTION +EYS 4HIS STEP ALLOWS YOU TO CREATE AND BACK UP AN ENCRYPTED KEY FOR KEEP YOUR DATA SAFE )F YOU PLAN TO USE THIS OPTION REMEMBER TO BACK UP THE ENCRYPTION KEY AND SAVE IT AS IT WILL BE THE ONLY WAY TO RESTORE OR CHANGE THE 3323 CONFIGURATION IN THE FUTURE
/NCE YOUR MACHINE IS SET UP YOU NEED TO RESTART IT TO ALLOW 3323 TO PROPERLY RECONFIGURE ))3 AND USER ACCESS CONTROL SO YOU CAN NAVIGATE THE 3323 PORTAL
■ .OTE )F YOU HAVE A MACHINE THAT IS RUNNING 7INDOWS OR 7INDOWS 6ISTA YOULL HAVE TO DISABLE 5!# IN ORDER TO ALLOW THROUGH THE BROWSER TO AUTHENTICATE IN THE 3323 PORTAL 9OU CAN FIND INFORMATION ABOUT HOW TO DISABLE 5!# FOR 7INDOWS 6ISTA AND 7INDOWS AT Wcc_)fX]S^fb\XRa^b^UcR^\T]DBfX]S^fbeXbcPCda]DbTa 0RR^d]c2^]ca^[^]^a^UU
.OW TRY TO ACCESS THE REPORT PORTAL WHICH SHOULD BE LOCATED AT Wcc_)Jh^dabTaeTaL AT_^acbNB@;4G?A4BBE THE BROWSER SHOULD DISPLAY A PROJECT PORTAL PAGE LIKE THE ONE IN &IGURE
&IGURE 3323 PORTAL PAGE WITH DEFAULT SETTINGS
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
.OW ON THE 3323 HOME PAGE ADD A NEW FOLDER AND CALL IT 4IME4RACKER 4HIS FOLDER IS WHERE YOULL CREATE THE REPORTS FOR THE APPLICATION
TimeTracker Reports !S NOTED EARLIER WE ARE GOING TO USE -ICROSOFT 31, 3ERVER 2EPORTING 3ERVICES 3323 IN THIS CHAPTER 5NFORTUNATELY IF YOU WANT TO CREATE A NEW REPORT FOR 3323 RIGHT NOW YOU CANT USE 6ISUAL 3TUDIO AS -ICROSOFT HASNT YET PROVIDED INTEGRATION BETWEEN THE TWO PRODUCTS 4HE ONLY WAY YOU CAN CREATE A NEW 3323 REPORT IS TO OPEN THE 6ISUAL 3TUDIO SHELL INSTALLED WITH 3323 AND CREATE A NEW REPORT PROJECT FROM THERE 4HEN YOU CAN PUBLISH YOUR REPORTS AND DISPLAY THEM WITHIN YOUR 70& APPLICATION
&IRST 2EPORT %MPLOYEE,IST2EPORT 4HE FIRST REPORT YOULL CREATE IS THE LIST OF EMPLOYEES THAT WILL BE USED TO PRINT A SUMMARY LIST OF THE EMPLOYEES IN THE DATABASE 4HEN YOULL APPLY SOME STYLE AND LAYOUT TO THE REPORT 4O START LOCATE THE 63 "USINESS )NTELLIGENCE SHORTCUT ON YOUR COMPUTER THAT THE 31, 3ERVER 2 %XPRESS %DITION SETUP INSTALLED )F YOU DIDNT INSTALL IT YOU CAN RUN THE SETUP AGAIN AND ADD MORE FEATURES TO THE CURRENT INSTALLATION /R YOU CAN GO TO 3TART ➤ !LL 0ROGRAMS ➤ -ICROSOFT 31, 3ERVER 2 ➤ 31, 3ERVER "USINESS )NTELLIGENCE $EVELOPMENT 3TUDIO !FTER YOU OPEN THIS YOULL SEE THE 63 )$% BUT WITH FEWER FEATURES AND PROJECT TEMPLATES AS THIS VERSION OF 63 JUST USES THE 63 SHELL TO HOST THE 2EPORT $ESIGNER AND OTHER FEATURES PROVIDED BY 31, 3ERVER .OW GO TO THE &ILE MENU AND CREATE A NEW PROJECT BY SELECTING "USINESS )NTELLIGENCE 0ROJECTS ➤ 2EPORT 3ERVER 0ROJECT .AME THE NEW PROJECT CX\TCaPRZTaAT_^acb AND CLICK /+ 63 CREATES A NEW EMPTY SOLUTION THAT WILL HOST REPORTS AND DATA SOURCES FOR 3323 &IGURE SHOWS THE DEFAULT STRUCTURE IN 3OLUTION %XPLORER AFTER YOU CREATE A NEW 2EPORT 3ERVER 0ROJECT "BY DEFAULT 63 ADDS THREE DIFFERENT FOLDERS IN YOUR PROJECTS v
3HARED $ATA 3OURCES )N THIS FOLDER YOULL CREATE THE DATA SOURCES SHARED BY THE REPORT PROJECT AND HOSTED IN 3323
v
3HARED $ATASET 4HIS FOLDER WILL HOLD THE DATASETS YOU CREATE THAT WILL PROVIDE THE DATA FOR YOUR REPORTS
v
2EPORTS 4HIS FOLDER SHOULD BE USED TO STORE YOUR REPORTS
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE $EFAULT LAYOUT OF A 2EPORT PROJECT IN 63 9OU CANT CHANGE THE NAMES AND THE STRUCTURE OF THIS LAYOUT BECAUSE IT IS USED BY 63 TO DEPLOY THE OBJECTS TO THE 3323 PORTAL AND ALSO TO PROVIDE A CONTEXT MENU FOR EACH FOLDER TYPE
Create a DataSource "EFORE EVEN STARTING TO CREATE THE FIRST REPORT YOU NEED TO SET UP THE DATA SOURCE FOR THE PROJECT AND HOST IT ON 3323 SO THAT THE REPORTS YOU PUBLISH CAN RETRIEVE THE DATA IN THE SAME WAY AS IN THE 63 )$% 4O CREATE A NEW DATASOURCE RIGHT CLICK ON THE CORRESPONDING FOLDER IN THE 3OLUTION %XPLORER TREE AND SELECT !DD .EW $ATASOURCE 6ISUAL 3TUDIO BRINGS UP A DIALOG THAT ASKS FOR A NAME AND CREDENTIALS FOR THE CONNECTION STRING AND OF COURSE THE CONNECTION STRING THAT POINTS TO THE 4IME4RACKER DATABASE YOU ARE USING .AME THE DATASOURCE CX\TCaPRZTa3PcPb^daRT PROVIDE A CONNECTION TO THE DATABASE BY PRESSING THE %DIT BUTTON AND VERIFY THAT THE CONNECTION IS WORKING 0RESS /+ AND SAVE THE NEW DATASOURCE 2EMEMBER TO ALSO SELECT A DATABASE IN THE CONNECTION WINDOW AND TO PRESS THE 4EST BUTTON TO VERIFY THAT THE CONNECTION IS PROPERLY CONFIGURED )F YOU WANT YOU CAN ALSO PROVIDE DIFFERENT CREDENTIALS IN THE DATASOURCE THAT WILL BE USED BY THE REPORT ENGINE FOR EXAMPLE TO LET AN ORDINARY USER ON THE NETWORK RUN THE REPORTS .OW YOU SHOULD SEE THE NEW DATASOURCE IN THE CORRESPONDING FOLDER OF 63 READY TO BE DEPLOYED 4HE NEXT STEP IS TO CONFIGURE THE 63 SOLUTION TO BE DEPLOYED IN THE 3323 PORTAL 4O DO THIS SELECT THE 3OLUTION NODE ROOT IN 3OLUTION %XPLORER AND THEN SELECT 0ROPERTIES FROM THE CONTEXT MENU ! PROPERTIES DIALOG WILL BE DISPLAYED AS SHOWN IN &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE 3323 0ROJECT CONFIGURATION &IGURE SHOWS THE DIALOG USED TO CONFIGURE 3323 PROJECT PROPERTIES 9OU SHOULD CHANGE THE INFORMATION TO MATCH WHATS IN &IGURE 0AY ATTENTION TO THE CPaVTcBTaeTaDA; WHICH IN YOUR CASE SHOULD BE Wcc_)Jh^daBBABLJaT_^acbTaeTada[L .OW ALL THE REPORTS DATASOURCES AND DATASETS WILL BE DEPLOYED TO THE CORRESPONDING FOLDERS IN THE 4IME4RACKER ROOT FOLDER OF THE 3323 WEB SITE 9OU CAN TRY THIS BY APPLYING THE CHANGES IN THE 0ROPERTIES DIALOG AND THEN RIGHT CLICKING THE REPORT SOLUTION AND CHOOSING THE $EPLOY COMMAND )F YOUVE PROPERLY CONFIGURED THE SOLUTION YOU SHOULD BE ABLE TO SEE IN THE 3323 ROOT A NEW FOLDER CALLED $ATA3OURCES AND IN IT THE SHARED DATASOURCE OBJECT
Create the Dataset 4HE NEXT STEP CONSISTS OF CREATING YOUR FIRST REPORT USING THE SHARED DATASOURCE 2IGHT CLICK ON THE REPORT FOLDER OF THE 63 3OLUTION AND CHOOSE !DD ➤ .EW )TEMx 3ELECT THE 2EPORT ITEM CALL IT 4\_[^hTT;XbcAT_^ac AND PRESS /+ )F YOU SELECT !DD .EW 2EPORT FROM THE FOLDER MENU INSTEAD OF !DD .EW )TEM 63 WILL BRING UP THE REPORT CREATION WIZARD WHICH WE WONT USE IN THIS PROJECT WE WANT TO CREATE A MORE COMPLEX REPORT 4HE NEXT STEP IS TO PROVIDE A DATASET FOR THE REPORT AND THIS CAN BE ACCOMPLISHED IN TWO DIFFERENT WAYS 9OU CAN CREATE A NEW DATASET IN THE SOLUTION FOLDER AND SHARE IT WITH OTHER REPORTS IN THE FUTURE OR YOU CAN USE THE 2EPORT $ATA PANEL ON YOUR LEFT AND ADD A NEW REPORT DATASET THAT IS VISIBLE ONLY TO THE CURRENT REPORT AND OF COURSE IS NOT RECYCLABLE ) PERSONALLY CREATE ONLY SHARED DATASETS SO ) CAN RECYCLE THEIR QUERIES IN THE FUTURE 5NFORTUNATELY THE %XPRESS VERSION OF 31, DOESNT ALLOW SHARING DATASETS SO LETS POINT TO THE 63 2EPORT $ATA PANEL AND ON THE .EWx MENU CLICK AND ADD A NEW DATASET 63 WILL SHOW A DIALOG FOR CREATING THE NEW DATASET &IGURE SHOWS THE 3HARED $ATASET 0ROPERTIES DIALOG WHERE YOU CAN CREATE A NEW DATASET 4HE DIALOG HAS A SET OF PANELS THAT YOU CAN NAVIGATE USING THE LIST ON THE LEFT )N THE FIRST PANEL YOU CAN SET UP THE QUERY THAT WILL RETRIEVE THE DATA YOU WANT TO DISPLAY IN THE REPORT 9OU CAN GENERATE THE QUERY BY SIMPLY WRITING IN THE 1UERY TEXT BOX THE 4 31, STATEMENT IF YOU HAVE IT ALREADY OR YOU CAN USE A DATASET
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
CREATED PREVIOUSLY IN THE REPORT OR AVAILABLE THROUGH THE SHARED DATASETS THIS OPTION ISNT AVAILABLE FOR 31, 3ERVER %XPRESS EDITION
&IGURE #REATE NEW DATASET DIALOG 7HAT WE ARE GOING TO DO IS TO GENERATE A NEW QUERY USING THE QUERY GENERATOR PROVIDED WITH THIS DIALOG WINDOW 4O DO THAT CLICK THE h5SE DATASET EMBEDDED IN THIS REPORTv OPTION AND THE LAYOUT OF THE DIALOG WILL CHANGE ALLOWING YOU TO TYPE IN SOME 4 31, CODE OR TO SELECT A SPECIFIC TABLE OR STORED PROCEDURE 9OU WILL BE TYPING IN THE 4 31, CODE SO SELECT THE FIRST OPTION AND PRESS THE .EW COMMAND ON THE DATASOURCE COMBOBOX 4HIS COMMAND LETS YOU SELECT A DATASOURCE FOR THE DATASET OR CREATE A NEW ONE 3ELECT THE SECOND OPTION h5SE SHARED DATASOURCE REFERENCEv AND CHOOSE THE DATASOURCE YOU JUST CREATED THEN PRESS /+ .OW YOU CAN CLICK THE 1UERY $ESIGNER COMMAND AND THE WIZARD WILL BRING UP A NEW DIALOG THAT YOU CAN USE TO BUILD THE QUERY &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE 4HE 1UERY $ESIGNER WINDOW 7ITHIN THE WINDOW SHOWN IN &IGURE YOU CAN CREATE A QUERY TO RETRIEVE THE DATA FOR THE REPORT 5SING THE COMMANDS AVAILABLE ON THE TOOLBAR YOU CAN EDIT THE QUERY USING THE CUSTOM QUERY DESIGNER OR BY TYPING 4 31, CODE 9OU CAN SELECT TABLES AND VIEWS CREATE JOINS BETWEEN THE TABLES SELECT FIELDS AND CRITERIA AND TEST THE RESULT BY SIMPLY RUNNING THE QUERY USING THE hv COMMAND FROM THE TOOLBAR 7HEN YOU ARE ALL SET CLICK /+ AND THE GENERATED 4 31, CODE WILL APPEAR IN THE PREVIOUS WINDOW &OR THIS REPORT YOU WANT TO SELECT THE 4\_[^hTT THE CORRESPONDING A^[T AND THE 0__^X]c\T]cS HE HAS SCHEDULED 4O BE ABLE TO DO THAT BY PRESSING THE COMMAND YOU NEED TO ADD THE FOUR TABLES SHOWN IN &IGURE 4HE QUERY DESIGNER WILL RENDER THE JOINS USING THE FOREIGN KEYS CREATED BY THE %NTITY &RAMEWORK WHEN YOU CREATED THIS DATABASE .OW SELECT THE FIELDS FOR THE 4\_[^hTT EXCEPT THE 8S AND THE ?Pbbf^aSYOU DONT WANT TO SHOW A PASSWORD IN A REPORT AND SELECT THE .AME AND $ESCRIPTION FIELDS FROM THE #OLUMN LIST 4HE FINAL RESULT SHOULD ALSO USE BD< AND 2>D=C FUNCTIONS TO AGGREGATE THE TOTAL NUMBER OF APPOINTMENTS FOR EACH EMPLOYEE DUE TO SPACE LIMITATIONS ONLY THE 4 31, CODE IS POSTED IN ,ISTING ,ISTING ,IST %MPLOYEES 0LUS #OUNT !PPOINTMENTS B4;42C 4\_[^hTTbCXc[T 4\_[^hTTb5d[[=P\TN5Xabc=P\T 4\_[^hTTb5d[[=P\TN;Pbc=P\T« 4\_[^hTTb3PcT>U1XacW 4\_[^hTTbDbTa]P\T 2>D=C0__^X]c\T]cb8S 0B 0__^X]c\T]cb A^[Tb=P\T A^[Tb3TbRaX_cX^] 5A>< A^[Tb A867C >DC4A 9>8= A^[T4\_[^hTT >= A^[Tb8S , A^[T4\_[^hTTA^[TbN8S A867C >DC4A 9>8= 4\_[^hTTb >= A^[T4\_[^hTT4\_[^hTTbN8S , 4\_[^hTTb8S ;45C >DC4A 9>8= 0__^X]c\T]cb >= 4\_[^hTTb8S , 0__^X]c\T]cb4\_[^hTTN8S
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
6A>D? 1H 4\_[^hTTbCXc[T 4\_[^hTTb5d[[=P\TN5Xabc=P\T 4\_[^hTTb5d[[=P\TN;Pbc=P\T« 4\_[^hTTb3PcT>U1XacW 4\_[^hTTbDbTa]P\T A^[Tb=P\T A^[Tb3TbRaX_cX^] )F YOU NEVER WORKED WITH THE 6A>D? 1H AND 2>D=C FUNCTIONS OF 4 31, YOU CAN FIND A NICE DETAILED DESCRIPTION OF HOW THEY WORK AT \bS]\XRa^b^UcR^\[XQaPah\b &&%&"Pb_g &OR NOW JUST PRESS /+ IN THE QUERY DESIGNER AND YOU SHOULD HAVE THE 4 31, CODE AVAILABLE IN THE TEXT BOX )F YOU DONT HAVE THE SAME AS THE BOOK YOU CAN SIMPLY WRITE THE 4 31, IN THE CODE SPACE OF THE QUERY DESIGNER AND CLICK IN THE DESIGN SURFACE TO SEE THE FINAL RESULT #LICK /+ IN THE FIRST DIALOG AND 6ISUAL 3TUDIO WILL CREATE A DATASET NODE IN THE 2EPORT $ATA PANEL CONTAINING THE FIELDS YOU SELECTED FROM THE DATABASE
Design the Report "EFORE STARTING TO DRAG THE FIELDS FROM THE DATASET TO THE REPORT DESIGN SURFACE YOU NEED TO DEFINE A HEADER AND FOOTER FOR THE REPORT SO THAT EVERY PAGE OF THE REPORT WILL LOOK ALMOST THE SAME AND THE FINAL RESULT WILL LOOK MORE PROFESSIONAL 4O EDIT THE HEADER AND FOOTER OF A REPORT YOU NEED TO MAKE THEM VISIBLE 2IGHT CLICK ON THE REPORT DESIGN SURFACE INSIDE THE REPORT AREA AND FROM THE MENU SELECT )NSERT ➤ (EADER AND )NSERT ➤ &OOTER AND 6ISUAL 3TUDIO WILL ADD TWO NEW HORIZONTAL LINES TO THE REPORT AREA SHOWING YOU THAT THERES A HEADER THE CONTENT OF THE REPORT AND A FOOTER )N THE HEADER RIGHT CLICK AND SELECT A CTgc1^g FROM THE CONTEXT MENU AND INSERT THE CONTENT h%MPLOYEES ,IST REPORTv 4HEN FROM THE TOOLBAR SELECT THE FONT 3EGOE 5) WITH A SIZE AND A COLOR OF 3TEEL"LUE $RAG THE CTgc1^g TO THE TOP LEFT CORNER OF THE REPORT AND RESIZE IT SO THE ENTIRE TEXT IS VISIBLE AND IT LOOKS LIKE &IGURE .OW ITS TIME FOR THE FOOTER ,OCATE ON THE 2EPORT $ATA PANEL THE "UILT IN &IELDS FOLDER AND EXPAND IT SELECT THE 0AGE.UMBER FIELD AND DRAG IT TO THE BOTTOM LEFT CORNER $O THE SAME FOR THE %XECUTION TIME FIELD BUT MOVE IT TO THE OPPOSITE CORNER &OR BOTH SELECT THE 3EGOE 5) FONT AND RESIZE THEM 9OU SHOULD END UP WITH A RESULT LIKE THE ONE IN &IGURE NOTE THAT IN THE FIGURE ) ADDED FEW MORE CONTROLS LIKE THE LINE
&IGURE 0REVIEW OF THE 4\_[^hTT;XbcAT_^ac FIRST DRAFT
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
.OW YOU NEED TO CREATE A CPQ[T OBJECT IN THE REPORT THAT WILL DISPLAY THE DATA YOU EXTRACTED FROM THE TABLE USING THE DATASET )N THIS CASE LETS GROUP THE DATA INTO DIFFERENT GROUPS EACH ONE FOR A A^[T EXISTING IN THE DATASET #LICK INSIDE THE DESIGN AREA IN THE MIDDLE OF THE REPORT AND ADD A NEW CPQ[T OBJECT FROM THE )NSERT MENU AND RESIZE IT SO IT COVERS THE ENTIRE SURFACE IN WIDTH .OW FOR EACH COLUMN SELECT A CORRESPONDING FIELD BY CLICKING ON THE MENU AVAILABLE IN THE DETAILS CELL $OUBLE CLICK THE HEADER CONTENT THEN USE THE TOOLBAR TO SELECT THE CELLS AND CHANGE THE FONT TO 3EGOE 5) AND THE COLUMN HEADER TO "OLD 2IGHT CLICK ON THE $ATE OF "IRTH COLUMN THE CELL VALUE AND SELECT 4EXTBOX 0ROPERTIES AND THEN SELECT THE .UMBER PANEL THE $ATE SECTION AND FORMAT THE DATE IN THE SHORT DATETIME FORMAT DDMMYYYY 2ENAME THE COLUMN HEADERS USING THE NAMES SHOWN IN &IGURE .OW IF YOU SELECT THE 0REVIEW TAB YOU SHOULD BE ABLE TO SEE THE REPORT WITH A TABLE POPULATED WITH THE DATA YOUVE EXTRACTED 4HE LAST STEP IS TO GROUP THE DATA USING THE TABLE GROUP FUNCTION 4O DO THAT SELECT THE GRAY SQUARE AT THE BEGINNING OF THE TABLE ROW BEFORE THE FIRST CELL 2IGHT CLICK ON IT AND SELECT !DD GROUP ➤ 0ARENT GROUP THEN SELECT THE ;.AME= FIELD FROM THE DIALOG AND THEN !DD GROUP HEADER AND PRESS /+ 2ENAME THE .AME LABEL TO 'ROUP AND SWITCH AGAIN TO THE 0REVIEW TAB 9OU SHOULD SEE NOW THE ROW GROUPED BY THE 'ROUP NAME AS SHOWN IN &IGURE
&IGURE &INAL REPORT RESULT .OW LETS DESIGN THE DETAILED REPORT FOR A SELECTED EMPLOYEE 7E WILL CREATE A GENERIC DETAIL REPORT WITH A QUERY THAT WILL REQUIRE A PARAMETER THE %MPLOYEE )D &ROM 3OLUTION %XPLORER SELECT THE $EPLOY COMMAND ON THE CONTEXT MENU AND VERIFY THAT THE MENU IS UPLOADED PROPERLY INTO THE 3323 FOLDER ON THE REPORT PORTAL
%MPLOYEE$ETAILS2EPORT 4HE 4IME4RACKER APPLICATION HAS TWO VIEWS THAT ALLOW YOU TO INTERACT WITH THE REGISTERED EMPLOYEES 4HE FIRST ONE IS A LIST OF EMPLOYEES AND YOU JUST CREATED THE CORRESPONDING REPORT 4HE SECOND IS THE DETAILED VIEW FOR THE EMPLOYEE SO LETS CREATE THE REPORT FOR THAT VIEW )N 3OLUTION %XPLORER SELECT THE 2EPORTS FOLDER AND CLICK !DD ➤ .EW )TEM ➤ 2EPORT NAME IT 4\_[^hTT3TcPX[bAT_^ac AND PRESS /+ #REATE A HEADER AND A FOOTER USING THE SAME SETTINGS AS FOR THE 4\_[^hTT;XbcAT_^ac BUT CHANGE THE TITLE OF THE REPORT TO $ETAILS INSTEAD OF ,IST AND SAVE IT )T SHOULD LOOK LIKE THE REPORT IN THE &IGURE EXCEPT FOR THE TITLE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
.OW YOU NEED TO CREATE A DATASET FOR THIS REPORT SO IN THE 2EPORT $ATA PANEL SELECT .EW AND CREATE A NEW DATASET #HOOSE THE OPTION OF CREATING A NEW DATASET EMBEDDED IN THE REPORT AND OPEN THE QUERY DESIGNER AS YOU DID FOR THE PREVIOUS REPORT
Create a Query with a Parameter 4HIS TIME YOU WANT TO SELECT ALL THE TABLES THAT PROVIDE INFORMATION RELATED TO THE 4\_[^hTT ENTITY AND YOU DONT NEED TO RUN ANY AGGREGATE FUNCTION BECAUSE YOU JUST WANT TO DISPLAY ALL THE INFORMATION RELATED TO A SPECIFIC EMPLOYEE IN A FORM STYLE REPORT &IGURE SHOWS THE INFORMATION AND THE TABLES TO SELECT FROM THE DATABASE
&IGURE $ATABASE QUERY FOR THE %MPLOYEE$ETAILS2EPORT 4HE LAST STEP IS TO ADD AN ADDITIONAL LINE OF 4 31, AT THE END OF THE GENERATED 4 31, CODE LIKE THIS F74A4
4\_[^hTTb8S , /8S
0RESS /+ AND LOOK AT THE 0ARAMETERS SECTION OF THE #REATE $ATASET DIALOG 9OU SHOULD SEE THAT THE REPORT NOW HAS A PARAMETER THAT REQUIRES THE CURRENT %MPLOYEE )D IN ORDER TO DISPLAY THE EMPLOYEE INFORMATION !FTER YOU CONFIRM THIS THE 2EPORT DATA PANEL WILL HAVE TWO NEW OBJECTS THE NEW DATASET AND IN THE 0ARAMETERS FOLDER THE NEW PARAMETER .OW YOU NEED TO PROVIDE A 2^\Q^1^g IN THE REPORT SO WHETHER THE USER WANTS TO RUN THE REPORT FROM THE 3323 WEB SITE OR FROM 70& HE WILL BE ABLE TO SELECT THE EMPLOYEE FROM A 2^\Q^1^g AND RUN THE REPORT #REATE A NEW DATASET AND CALL IT 0ePX[PQ[T4\_[^hTTb 4HIS REPORT WILL SHOW THE LIST OF EMPLOYEES USING THE FOLLOWING QUERY
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
B4;42C 8S CXc[T 5d[[=P\TN5Xabc=P\T 5d[[=P\TN;Pbc=P\T 0B 5d[[=P\T 5A>< 4\_[^hTTb >A34A 1H 5d[[=P\T 0RESS /+ AND SAVE THE NEW DATASET )T WILL PROVIDE A LIST OF EMPLOYEE NAMES IN THIS FORM h-R &IRST.AME ,AST.AMEv AND THE CORRESPONDING )D )N THE 0ARAMETERS FOLDER OF THE 2EPORT $ATA PANEL LOCATE THE /8S PARAMETER AND DOUBLE CLICK IT TO OPEN A NEW DIALOG THAT ALLOWS YOU TO CONFIGURE THE PARAMETER VALUES 3ELECT THE !VAILABLE 6ALUES SECTION USING THE ,ISTBOX ON THE LEFT AND CLICK THE OPTION h'ET VALUES FROM A QUERYv .OW SELECT THE NEWLY CREATED DATASET AND SPECIFY THE 8S FOR THE VALUE AND THE 5d[[]P\T FIELD FOR THE LABEL 0RESS /+ AND SAVE THIS SETTING .OW YOU CAN TEST THE REPORT BY SWITCHING TO THE 0REVIEW TAB YOU SHOULD SEE A HEADER THAT WILL PROMPT YOU TO SELECT AN EMPLOYEE FROM THE LIST
Create the Details )N THE PREVIOUS REPORT YOU USED A COMPONENT CALLED CPQ[T THAT ALLOWS YOU TO CREATE A LIST OF ROWS USING FEATURES LIKE 'ROUP "Y 3UM AND MORE .OW LETS CREATE A SIMPLE DETAILS VIEW SO YOU DONT NEED TO EMBED THE DATA INTO A SPECIFIC CONTAINER 9OU CAN SIMPLY DRAG THE FIELDS FROM THE DATASET IN THE 2EPORT DATA PANEL DIRECTLY INTO THE REPORT 5NFORTUNATELY 3323 DOESNT HAVE A FEATURE THAT IF YOU DRAG A FIELD OF A DATASET TO A REPORT SURFACE IT WILL ADD A LABEL SO YOU NEED TO CREATE A LABEL FOR EACH PROPERTY OF THE 4\_[^hTT ENTITY LIKE 5Xabc=P\T ;Pbc=P\T AND SO ON 7HEN YOU ARE DONE WITH THE FIRST PART OF THE REPORT WHICH IS THE %MPLOYEE DETAILS SECTION YOU NEED TO CREATE THREE DISTINCT TABLES ONE FOR THE #ONTACTS ONE FOR THE !DDRESSES AND ONE FOR THE !PPOINTMENTS *UST RIGHT CLICK ON THE DESIGN SURFACE OF THE REPORT SELECT )NSERT ➤ 4ABLE AND THEN SPECIFY IN THE TABLE TO USE THE DATASET THAT PROVIDES THE DATA AND NOT THE DATASET THAT PROVIDES THE LIST OF EMPLOYEES 7HEN YOU ARE DONE YOUR RESULT SHOULD LOOK LIKE THE ONE IN &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE &INAL %MPLOYEE DETAILS REPORT !FTER YOU ADD THE TABLES TO THE REPORT THEY WILL BE DYNAMICALLY FILLED WITH ALL THE ROWS COMING FROM THE DATASET FILTERED BY %MPLOYEE )D 4HE LAST STEP IS TO 'ROUP "Y EVERY TABLE FOR THE SPECIFIC )D YOURE SHOWING &OR EXAMPLE THE TABLE THAT SHOWS ALL THE CONTACTS SHOULD BE GROUPED BY THE #ONTACTS )D FIELD 4HIS PROCESS HAS TO BE DONE BECAUSE THE B4;42C WE ARE USING IS RETURNING ADDITIONAL ROWS BECAUSE OF EXTRA 9>8=B BEING EXECUTED THIS TYPE OF B4;42C WILL RETURN DUPLICATE ROWS FOR THE JOINED TABLES !DDRESSES #ONTACTS AND !PPOINTMENTS 4O GROUP A TABLE WITHOUT SHOWING THE GROUP ON THE DESIGN SURFACE YOU HAVE TO EXECUTE THE FOLLOWING STEPS 1.
3ELECT A TABLE YOU WANT TO GROUP
2.
)N THE 2OW 'ROUPS PANEL POSITIONED BELOW THE REPORT DESIGN SURFACE AREA SELECT THE LITTLE ARROW ON THE RIGHT OF THE LINES CALLED $ETAILS
3.
3ELECT THE LAST MENU ITEM 'ROUP 0ROPERTIES
4.
)N THE 'ROUP "Y DIALOG SELECT !DD AND CHOOSE THE CORRESPONDING )D FIELD OF THE TABLE
.OW THE REPORT SHOULD BE FINE AND READY TO BE DEPLOYED TO THE REPORT SERVER FOLDER SO THAT IT CAN BE USED BY THE USERS AND BY THE 4IME4RACKER APPLICATION
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
!DDITIONAL 2EPORTS )N THE FIRST PART OF THIS CHAPTER YOU SAW HOW TO CREATE TWO DIFFERENT TYPES OF REPORTS A SIMPLE LIST OF INFORMATION RETRIEVED FROM THE DATABASE WITHOUT ANY FILTERING BUT USING A 'ROUP "Y FUNCTION TO AGGREGATE SOME INFORMATION SPECIFICALLY THE NUMBER OF APPOINTMENTS PER EMPLOYEE 4HE SECOND TYPE OF REPORT IS ONE THAT REQUIRES A PARAMETER WHICH MEANS THAT THE DATA DISPLAYED IN THE REPORT IS FILTERED BY A SPECIFIC CRITERION THAT CAN BE DECIDED ON THE FLY BY THE USER OR BY THE APPLICATION THAT IS RUNNING THE REPORT )N THE 4IME4RACKER APPLICATION YOULL PROBABLY NEED A LOT MORE REPORTS &OR EXAMPLE ALL THE REPORTS WE CREATED FOR THE 4\_[^hTT ENTITY SHOULD BE CREATED FOR THE 2dbc^\Ta ENTITY AND YOU SHOULD CREATE A REPORT FOR THE !PPOINTMENTS INCLUDING ITS DETAILS AND THE INVOICE THAT HAS TO BE SENT TO THE CUSTOMER &OR SPACE REASONS WE CANT CREATE ALL THESE REPORTS IN THIS CHAPTER BUT YOU CAN USE THE TWO PREVIOUS REPORTS AS A PATH TO FOLLOW TO CREATE NEW REPORTS FILTERED OR NOT FILTERED )F YOU WANT TO TAKE A MORE IN DEPTH LOOK AT 3323 AND LEARN MORE ABOUT THE MANY DIFFERENT TYPES OF REPORTS YOU CAN CREATE HERE ARE A FEW VERY USEFUL RESOURCES v
0RO 31, 3ERVER 2EPORTING 3ERVICES !PRESS
v
&OUNDATIONS OF 31, 3ERVER 2 "USINESS )NTELLIGENCE !PRESS
v
h31, 3ERVER 2EPORTING 3ERVICES v \bS]\XRa^b^UcR^\[XQaPah\b $( %Pb_g
+EEP IN MIND THAT THE BUSINESS INTELLIGENCE PART OF 31, 3ERVER 2 COMPRISES A VERY BIG AREA AND IN THIS CHAPTER YOU ARE SEEING HOW TO CREATE ONLY SOME VERY SIMPLE REPORTS AND HOW TO INTEGRATE THEM IN YOUR 70& APPLICATION USING THE -66- PATTERN AND THE 7INDOWS (OST CONTROL 4HIS IS ONLY A SMALL PART OF WHAT CAN BE ACHIEVED WITH THIS VERY POWERFUL TOOL
Hosting the Reports in a WPF Control 3323 PROVIDES A COMPONENT CALLED AT_^acEXTfTa THAT CAN BE USED TO HOST DISPLAY A REPORT INSIDE A 7INDOWS &ORMS CONTROL 5NFORTUNATELY THE CONTROL FOR 70& HASNT BEEN CREATED YET SO TO DISPLAY A REPORT IN 70& USING THE 7INDOWS &ORMS CONTROL YOU NEED TO RUN THE CONTROL IN A SPECIAL CONTAINER THAT CAN PROVIDE FORWARD COMPATIBILITY BETWEEN 7INDOWS &ORMS AND 70& )N 70& YOU CAN USE A CONTROL OF TYPE FX]S^fb5^a\b7^bc WHICH IS AVAILABLE FROM THE FX]S^fb5^a\b8]cTVaPcX^]S[[ ASSEMBLY PROVIDED WITH .%4 4HIS CONTROL IS A FULL RICH 70& CONTROL THAT PROVIDES TONS OF METHODS EVENTS AND PROPERTIES TO FULLY INTEGRATE AND HOST A 7INDOWS &ORMS CONTROL IN A 70& CONTROL 4HE CONTROL CAN BE USED IN YOUR 70& 8!-, MARKUP TO INSTANTIATE AND CREATE ADDITIONAL CONTROLS IN IT THAT DERIVE FROM THE BhbcT\FX]S^fb5^a\b NAMESPACE )N ORDER TO USE THE CONTROL YOU NEED TO ADD THE FOLLOWING REFERENCES TO THE 70& APPLICATION THAT WILL HOST THE CONTROL v
BhbcT\FX]S^fb5^a\b
v
FX]S^fb5^a\b8]cTVaPcX^]
4HEN YOU CAN INTEGRATE THE CONTROL IN YOUR 8!-, MARKUP AS IN THE EXAMPLE IN ,ISTING
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
,ISTING 3AMPLE TO 3HOW A 7INDOWS &ORMS #ONTROL IN A 70& 7INDOW +FX]S^f g)2[Pbb,0?A4BBCX\TCaPRZX]VEXTfbAT_^acEXTf g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ g\[]b)fU,R[a]P\Tb_PRT)BhbcT\FX]S^fb5^a\b*PbbT\Q[h,BhbcT\FX]S^fb5^a\b CXc[T,AT_^acEXTf 7TXVWc," FXScW,"+6aXS+FX]S^fb5^a\b7^bc+fU)?P]T[+fU)?P]T[2^]ca^[b+fU);PQT[ CTgc,
$ISPLAY THE 2EPORT IN A 70& 6IEW 4HE AT_^acEXTfTa IS A 7INDOWS &ORMS CONTROL USED TO DISPLAY LOCAL AND REMOTE REPORTS IN A 7INDOWS &ORMS APPLICATION 4HE CONTROL IS NOT AVAILABLE IN THE DEFAULT 6ISUAL 3TUDIO INSTALLATION SO YOU NEED TO DOWNLOAD IT SEPARATELY 9OU CAN FIND THE DOWNLOAD FOR THE -ICROSOFT 2EPORT 6IEWER 2EDISTRIBUTABLE 0ACKAGE AT fff\XRa^b^UcR^\S^f][^PSbT]STcPX[bPb_g.5P\X[h83,P(# R%Q!%#SS#S"(RP& # &PS %#US !FTER YOUVE INSTALLED IT ON YOUR DEVELOPMENT MACHINE YOULL FIND SOME NEW ASSEMBLIES IN THE 6ISUAL 3TUDIO !DD REFERENCE WINDOW v
-ICROSOFT2EPORT6IEWER#OMMON 6 AND 6
v
-ICROSOFT2EPORT6IEWER7EB&ORMS 6 AND 6
v
-ICROSOFT2EPORT6IEWER7IN&ORMS 6 AND 6
4O DISPLAY A REPORT IN THE 4IME4RACKER APPLICATION YOU HAVE A FEW TASKS TO ACCOMPLISH BUT NOTHING REALLY COMPLICATED
v
0REPARE A 6IEW-ODEL FOR THE REPORT VIEW THAT WILL SET UP THE REPORT VIEWER COMPONENT
v
0REPARE A SIMPLE 70& 7INDOWS THAT WILL HOST THE REPORT 6IEW-ODEL
v
-ODIFY THE 8=PeXVPcX^]BTaeXRT TO LOAD THE AT_^acEXTf
v
-ODIFY THE 4\_[^hTT;Xbc AND 4\_[^hTT3TcPX[b 6IEWS AND 6IEW-ODELS TO LOAD THE REPORTS
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
4HE FIRST STEP IS TO PREPARE IN THE 4IME4RACKER 70& PROJECT A NEW 6IEW-ODEL OBJECT SPECIFICALLY FOR THE REPORT VIEWER YOUVE INSTALLED ON YOUR MACHINE SEE ,ISTING ,ISTING AT_^acEXTf<^ST[ dbX]V BhbcT\* dbX]V BhbcT\FX]S^fb5^a\b8]cTVaPcX^]* dbX]V <XRa^b^UcAT_^acX]VFX]5^a\b* ]P\Tb_PRT 0?A4BBCX\TCaPRZX]VEXTf<^ST[b j _dQ[XR bTP[TS R[Pbb AT_^acEXTf<^ST[ ) 1PbTEXTf<^ST[+AT_^acEXTf<^ST[j _aXePcT FX]S^fb5^a\b7^bc eXTfTa* _dQ[XR AT_^acEXTf<^ST[bcaX]V aT_^ac?PcW j eXTfTa , ]Tf FX]S^fb5^a\b7^bc* ePa aT_^acEXTfTa , ]Tf AT_^acEXTfTa* aT_^acEXTfTa?a^RTbbX]V<^ST , ?a^RTbbX]V<^STAT\^cT* WTaT h^d WPeT c^ _^X]c c^ h^da \PRWX]T DA; aT_^acEXTfTaBTaeTaAT_^acAT_^acBTaeTaDa[ , ]Tf DaXWcc_)aPUUPT[TVPaS#AT_^acbTaeTaNB@;4G?A4BB* aT_^acEXTfTaBTaeTaAT_^acAT_^ac?PcW , aT_^ac?PcW* aT_^acEXTfTaATUaTbWAT_^ac* eXTfTa2WX[S , aT_^acEXTfTa* l _dQ[XR FX]S^fb5^a\b7^bc EXTfTa j VTc j aTcda] eXTfTa* l bTc j eXTfTa , eP[dT* >]?a^_Tach2WP]VTSg ,- gEXTfTa* l l l l 4HIS CODE EXPOSES A FX]S^fb5^a\b7^bc CONTROL AS A PROPERTY THAT YOU WILL BIND TO THE 8!-, VIEW 4HE REPORT PATH MUST BE IN THE CONSTRUCTOR IN THIS CASE IT WILL BE SOMETHING LIKE CX\TCaPRZTa\haT_^ac &INALLY THE CODE BINDS THE REPORT TO THE REPORT VIEWER REFRESHES AND GENERATES THE REPORT AND THEN BINDS THE REPORT VIEWER TO THE FX]S^fb5^a\b7^bc CONTROL .OW YOU HAVE THE EXTf<^ST[ WHICH WILL HOST THE REPORT VIEWER IT HAS TO BE CREATED IN THE 6IEWS FOLDER AND IT IS A 7INDOWS COMPONENT ,ISTING CREATES THE EXTf THAT WILL HOST THE REPORT VIEWER AND USES A 2^]cT]c?aTbT]cTa 70& CONTROL TO PUT A PLACEHOLDER IN THE VIEW 4HIS PLACEHOLDER WILL BE FILLED USING THE EXTfTa PROPERTY OF THE 3PcP2^]cTgc WHICH IN OUR CASE IS THE FX]S^fb5^a\b7^bc CONTROL EXPOSED BY THE EXTf<^ST[
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
,ISTING 2EPORT6IEW 70& 7INDOW +FX]S^f g)2[Pbb,0?A4BBCX\TCaPRZX]VEXTfbAT_^acEXTf g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ CXc[T,AT_^acEXTf 7TXVWc," FXScW,"+6aXS+2^]cT]c?aTbT]cTa 2^]cT]c,j1X]SX]V EXTfTa D_SPcTB^daRTCaXVVTa,?a^_Tach2WP]VTSl +6aXS+FX]S^f.OW LETS MODIFY THE NAVIGATION SERVICE AND THE CORRESPONDING INTERFACE AS SHOWN IN ,ISTING ,ISTING -ODIFICATION ON THE 8=PeXVPcX^]BTaeXRT )NTERFACE AND #ONCRETE #LASS 8=PeXVPcX^]BTaeXRT e^XS BW^fAT_^ac3XP[^VbcaX]V aT_^ac* =PeXVPcX^] BTaeXRT _dQ[XR e^XS BW^f?aX]c3XP[^VbcaX]V aT_^ac j ePa fX]S^f , 2^]cPX]TaATb^[eT+EXTfbAT_^acEXTf-* AT_^acEXTf<^ST[ e\ , ]Tf AT_^acEXTf<^ST[aT_^ac* fX]S^f3PcP2^]cTgc , e\* fX]S^fBW^f3XP[^V* l 4HE =PeXVPcX^]BTaeXRT CLASS CREATES A NEW AT_^acEXTf USING THE 5NITY )O# CONTAINER 9OU DONT NEED TO PREREGISTER THE AT_^acEXTf IN THE CONTAINER BECAUSE THE CONTAINER WILL RESOLVE IT USING THE DEFAULT CONSTRUCTOR OF THE VIEW .EXT THE CODE CREATES A NEW EXTf<^ST[ FOR THE REPORT BY INJECTING THE REPORT PATH RECEIVED FROM THE BW^fAT_^ac3XP[^V METHOD AND FINALLY IT BINDS THE EXTf<^ST[ TO THE EXTf AND THEN DISPLAYS THE EXTf AS A DIALOG SO THAT THE USER CANT DO ANYTHING UNTIL THE REPORT IS CLOSED
,OAD A ,IST OF %MPLOYEES 2EPORT 4HE FIRST VIEW TO MODIFY IS THE 4\_[^hTT;XbcEXTf 9OU NEED TO ADD A NEW 1dcc^] THAT WILL DISPLAY THE REPORT ASSOCIATE A ?aX]c2^\\P]S TO THE 1dcc^] AND CREATE THE DELEGATE THAT WILL BE USED TO CALL THE =PeXVPcX^]BTaeXRT FROM THE 4\_[^hTT;XbcEXTf<^ST[ 4O START OPEN THE 4\_[^hTT;XbcEXTf<^ST[ AND CREATE A NEW COMMAND AND THE CORRESPONDING IMPLEMENTATION AS SHOWN IN ,ISTING
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
,ISTING 4\_[^hTT;XbcEXTf<^ST[ PSS P ]Tf _a^_Tach _dQ[XR 82^\\P]S ?aX]c2^\\P]S j VTc* _aXePcT bTc* l \^SXUh cWT 8]XcXP[XiT2^\\P]Sb \TcW^S Qh PSSX]V cWXb ^]T ?aX]c2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,- j ?aX]c4\_[^hTTb* l _PaP\TcTa ,- j aTcda] cadT* l* \TcW^S c^ SXb_[Ph cWT aT_^ac _aXePcT e^XS ?aX]c4\_[^hTTb j cWXb]PeXVPcX^]BTaeXRTBW^f?aX]c3XP[^VCX\TCaPRZTa4\_[^hTT;XbcAT_^ac* l 4HE FIRST PART OF THIS LISTING CREATES A NEW COMMAND IN THE EXTf<^ST[ AND MODIFIES THE EXISTING 8]XcXP[XiT2^\\P]Sb METHOD BY ADDING AN ADDITIONAL INITIALIZATION THE ONE USED FOR THE ?aX]c2^\\P]S OBJECT .EXT IT ASSOCIATES A DELEGATE TO THE ?aX]c2^\\P]S THAT WILL SIMPLY CALL THE 8=PeXVPcX^]BTaeXRT METHOD TO DISPLAY THE REPORT BY PASSING THE REPORT PATH 4HE FINAL STEP IS TO CREATE THE 0RINT BUTTON IN THE 4\_[^hTT;XbcEXTf AND VERIFY THAT EVERYTHING WORKS AS EXPECTED 3EE ,ISTING ,ISTING -ODIFICATION OF THE 4\_[^hTT;XbcEXTf PSS P ]Tf 1dcc^] X] cWT c^^[QPa +2^]ca^[b)8\PVT1dcc^] ?PSSX]V," CTgc,?aX]c 2[XRZ2^\\P]S,j1X]SX]V ?PcW,?aX]c2^\\P]S D_SPcTB^daRTCaXVVTa,?a^_Tach2WP]VTSl 8\PVT,j3h]P\XRATb^daRT ?aX]cB\P[[l 4HE FINAL RESULT SHOULD LOOK LIKE THE ONE DISPLAYED IN &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE %MPLOYEE ,IST REPORT WITH NEW PRINT BUTTON
,OAD A $ETAILED 2EPORT 4HE CODE TO LOAD A SINGLE EMPLOYEE IS ALMOST THE SAME YOU NEED TO CREATE A NEW 1dcc^] IN THE 4\_[^hTT3TcPX[bEXTf AND ASSOCIATE A COMMAND IN THE CORRESPONDING EXTf<^ST[ THEN MODIFY THE =PeXVPcX^]BTaeXRT AND CREATE A NEW METHOD THAT WILL ASK ALSO FOR THE ?aX\Pah:Th OF THE RECORD YOU WANT TO PRINT OUT AS IN ,ISTING ,ISTING 4HE =PeXVPcX^]BTaeXRT -ETHOD TO 3HOW A $ETAILS 2EPORT _dQ[XR e^XS BW^f3TcPX[b?aX]c3XP[^VbcaX]V aT_^ac X]c XSe j ePa fX]S^f , 2^]cPX]TaATb^[eT+EXTfbAT_^acEXTf-* AT_^acEXTf<^ST[ e\ , ]Tf AT_^acEXTf<^ST[aT_^ac XS* fX]S^f3PcP2^]cTgc , e\* fX]S^fBW^f3XP[^V* l .OW YOU NEED TO MODIFY THE AT_^acEXTf<^ST[ BY CREATING A NEW CONSTRUCTOR THAT WILL REQUIRE THE 8S AS A PARAMETER WHICH YOULL INJECT INTO THE CORRESPONDING REPORT SEE ,ISTING 2EMEMBER TO ADAPT THE =PeXVPcX^]BTaeXRT CLASS FOR THESE NEW METHODS AND THE CORRESPONDING INTERFACE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
,ISTING .EW #ONSTRUCTOR OF THE AT_^acEXTf<^ST[ _dQ[XR AT_^acEXTf<^ST[bcaX]V aT_^ac?PcW X]c XS j eXTfTa , ]Tf FX]S^fb5^a\b7^bc* ePa aT_^acEXTfTa , ]Tf AT_^acEXTfTa* aT_^acEXTfTa?a^RTbbX]V<^ST , ?a^RTbbX]V<^STAT\^cT* aT\T\QTa c^ RWP]VT cWT PSSaTbb c^ cWT DA; h^d WPeT aT_^acEXTfTaBTaeTaAT_^acAT_^acBTaeTaDa[ , ]Tf« DaXWcc_)aPUUPT[TVPaS#AT_^acbTaeTaNB@;4G?A4BB* aT_^acEXTfTaBTaeTaAT_^acAT_^ac?PcW , aT_^ac?PcW* AT_^ac?PaP\TcTaJL _PaP\TcTab , ]Tf AT_^ac?PaP\TcTaJ L* _PaP\TcTabJL , ]Tf AT_^ac?PaP\TcTa8S XSC^BcaX]V* aT_^acEXTfTaBTaeTaAT_^acBTc?PaP\TcTab_PaP\TcTab* aT_^acEXTfTaATUaTbWAT_^ac* eXTfTa2WX[S , aT_^acEXTfTa* l 4HE ONLY DIFFERENCE HERE IS THE AT_^ac?PaP\TcTa ARRAY AN ARRAY OF PARAMETERS YOU CAN PASS TO THE REPORT SERVER TO FILTER THE DATA OF THE REPORT WITHOUT REQUIRING USER INTERACTION 3O IN THIS CASE THE USER WILL NOT NEED TO SELECT A SPECIFIC EMPLOYEE FROM THE REPORT COMBOBOX &INALLY YOU NEED TO DEFINE THE REPORT COMMAND FOR THE DETAILS VIEW AND OF COURSE YOU NEED TO ADD A NEW "UTTON ALSO IN THE $ETAILS VIEW AS YOU DID FOR THE LIST VIEW ,ISTING SHOWS THE ?aX]c2^\\P]S ,ISTING 0RINT#OMMAND FOR THE %MPLOYEE $ETAILS 6IEW-ODEL _dQ[XR 82^\\P]S ?aX]c2^\\P]S j VTc* bTc* l _aXePcT e^XS 8]XcXP[XiT2^\\P]Sb j ]PeXVPcX^]BTaeXRT , R^]cPX]TaATb^[eT+8=PeXVPcX^]BTaeXRT-* ?aX]c2^\\P]S , ]Tf <ee\2^\\P]S _PaP\TcTa ,- j ?aX]c4\_[^hTT* l _PaP\TcTa ,- j aTcda] cadT* l* l _aXePcT e^XS ?aX]c4\_[^hTT j cWXb]PeXVPcX^]BTaeXRT BW^f3TcPX[b?aX]c3XP[^V CX\TCaPRZTa4\_[^hTT3TcPX[bAT_^ac cWXb2daaT]c4]cXch8S* l &INALLY THE CODE CALLS THE NEW METHOD ON THE NAVIGATION SERVICE AND PASSES THE CURRENT ENTITY ID AND THE NAME OF THE REPORT 4HE FINAL RESULT LOOKS LIKE THE ONE IN &IGURE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
&IGURE %MPLOYEE $ETAILS 6IEW AND 2EPORT .OW YOU CAN REFACTOR THE CODE USED PREVIOUSLY AND PASS A 3XRcX^]Pah+bcaX]V bcaX]V- INSTEAD OF A SIMPLE X]c AS THE REPORT PARAMETER SO THAT YOU CAN USE THE METHOD TO DISPLAY ANY REPORT WITH OR WITHOUT PARAMETERS (ERE IS A POSSIBLE APPLICATION OF HOW YOU CAN CALL IT 2aTPcT cWT _PaP\TcTab 3XRcX^]Pah+bcaX]V bcaX]V- _PaP\TcTab , ]Tf 3XRcX^]Pah+bcaX]V bcaX]Vj 8S l cWXb]PeXVPcX^]BTaeXRT BW^f3TcPX[b?aX]c3XP[^V CX\TCaPRZTa4\_[^hTT3TcPX[bAT_^ac _PaP\TcTab*
Summary )N THIS CHAPTER YOU SAW HOW TO CONFIGURE 31, 3ERVER 2EPORTING 3ERVICES 3323 A POWERFUL BUSINESS INTELLIGENCE REPORTING SYSTEM PROVIDED WITH 31, 3ERVER 2 AND ALSO AVAILABLE WITH THE %XPRESS EDITION OF COURSE WITH SOME ARCHITECTURAL LIMITS 3323 WORKS BY CREATING A 7INDOWS 3HARE0OINT 3ERVER PORTAL THAT YOU CAN USE TO SHARE AND RUN THE REPORTS DEPLOYED ON IT )N ORDER TO CREATE A REPORT FOR 3323 YOU NEED TO OPEN THE SPECIAL VERSION OF 6ISUAL 3TUDIO INSTALLED WITH THE 3323 TOOLS WHICH IS CALLED 31, 3ERVER "USINESS )NTELLIGENCE $EVELOPMENT 3TUDIO 4HIS VERSION OF 63 LETS YOU CREATE RUN AND DEPLOY DATASOURCES DATASETS AND REPORTS OF ANY TYPE
#(!04%2 ■ 2%0/243 7)4( -)#2/3/&4 2%0/24).' 3%26)#%3
9OU SAW HOW TO CREATE A SIMPLE LIST OF RECORDS AND HOW YOU CAN CREATE A MORE COMPLEX REPORT OF TYPE MASTER DETAIL AND HOW TO FILTER THE DATA BY USING REPORT PARAMETERS 9OU CAN PROVIDE A SET OF VALUES FOR A REPORT PARAMETER SO THAT THE USER WILL NOT BE FORCED TO REMEMBER FOR EXAMPLE THE )D OF THE EMPLOYEE HE WANTS TO SHOW IN THE REPORT &INALLY YOU SAW HOW TO HOST A 7INDOWS &ORMS CONTROL IN 70& AND MORE PRECISELY HOW TO EXECUTE THE AT_^acEXTfTa COMPONENT TO LOAD AND RUN REMOTE REPORTS ALL USING THE -ODEL 6IEW 6IEW-ODEL PATTERN SO THAT THE 6IEW STAYS LOOSELY COUPLED AND UNAWARE OF THE AT_^acEXTfTa CONTROL
CHAPTER ■■■
Deploy the Application Using ClickOnce 7HEN THE APPLICATION IS COMPLETE AND READY TO BE DISTRIBUTED TO THE FINAL USER WHETHER IN YOUR ORGANIZATION OR AN EXTERNAL CUSTOMER YOU NEED TO PREPARE A PACKAGE OR A DEPLOYMENT SOLUTION THAT LETS THIS USER INSTALL AND RUN THE APPLICATION IN FEW EASY STEPS )N THIS PHASE OF THE DEPLOYMENT PROCESS ITS REALLY IMPORTANT THAT YOU FOCUS ON THE SIMPLICITY OF THE END RESULT 2EMEMBER THAT THE USER WILL PROBABLY BE COMPLETELY UNAWARE OF THE TECHNOLOGY REQUIREMENTS OF THE APPLICATION AND YOU HAVE TO BE ABLE TO GIVE HIM EVERYTHING HELL NEED TO INSTALL AND RUN THE APPLICATION &OR EXAMPLE IF THE USERS COMPUTER DOESNT HAVE THE .%4 &RAMEWORK RUNTIME FILES AND YOUR APPLICATION HAS BEEN DEVELOPED USING .%4 YOU HAVE TO PROVIDE AN EASY WAY TO INSTALL THESE MANDATORY FILES WITHOUT TOO MANY HASSLES 4HE .%4 &RAMEWORK PROVIDES DIFFERENT WAYS TO INSTALL AND DISTRIBUTE AN APPLICATION TO THE END USER 9OU CAN DISTRIBUTE A SETUP PACKAGE MSI THAT THE USER CAN DOWNLOAD AND INSTALL BUT WITH THIS SOLUTION THE APPLICATION WILL HAVE TO PROVIDE A MECHANISM TO BE hUPGRADABLEv ! GOOD ALTERNATIVE IS #LICK/NCE A TECHNOLOGY AVAILABLE IN THE .%4 &RAMEWORK FOR DISTRIBUTING PACKAGES ACROSS THE )NTERNET THAT ARE SAFE AND AUTO UPGRADABLE
ClickOnce Deployment 7HEN YOU DEPLOY AN APPLICATION YOU HAVE TO SATISFY THREE MAJOR REQUIREMENTS v
-AKE THE SETUP PROCESS SAFE ESPECIALLY IF IT IS DISTRIBUTED THROUGH THE )NTERNET
v
-AKE THE INSTALLATION PROCESS EASY THE USER SHOULD BE AWARE OF THE APPLICATION REQUIREMENTS AND PREREQUISITES AND SHOULD BE ABLE TO RUN A SETUP AND GET THE APPLICATION RUNNING
v
-AKE THE APPLICATION AUTO UPGRADABLE OR EASILY UPGRADED AND THERE SHOULD BE A WAY TO NOTIFY THE USER OF NEW UPDATES
4HE #LICK/NCE TECHNOLOGY IS DESIGNED TO BE DISTRIBUTED USING THE )NTERNET A FILE SHARE OR MEDIA SUCH AS A #$ 2/- OR 53" DRIVE 9OU SIMPLY SET UP THE DISTRIBUTION CONFIGURATION AND THEN DEPLOY THE APPLICATION AND #LICK /NCE WILL DO THE REST
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
7ITH #LICK/NCE YOU CAN MAKE THE APPLICATION SELF UPGRADABLE USING DIFFERENT OPTIONS THAT WE WILL DISCUSS IN THIS CHAPTER 9OU CAN ALSO SIGN AND CERTIFY THE APPLICATION AND YOU CAN INCLUDE PREREQUISITES LIKE THE .%4 &RAMEWORK OR OTHER REDISTRIBUTABLE PACKAGES 4HE WEB DISTRIBUTION OF #LICK/NCE RIGHT NOW WORKS WITH ANY VERSION OF )NTERNET %XPLORER AND -OZILLA &IREFOX IT ALSO WORKS WITH #HROME BUT THIS BROWSER IS NOT YET INCLUDED IN THE OFFICIAL LIST ON -3$. )N THE NEXT SECTIONS WE WILL TAKE A LOOK AT DISTRIBUTION SECURITY CERTIFICATES AND UPDATES THEN WE WILL APPLY THE KNOWLEDGES TO THE 4IME4RACKER APPLICATION
#LICK/NCE 3ECURITY #LICK/NCE PROVIDES A COMPLETE MECHANISM FOR SECURING YOUR DISTRIBUTABLE PACKAGE USING TECHNOLOGIES LIKE CERTIFICATES CODE ACCESS SECURITY POLICIES #!3 AND AUTHENTICATION )F YOU PLAN TO DISTRIBUTE YOUR APPLICATION USING #LICK/NCE THE FIRST STEP IS TO USE THE 7INDOWS !UTHENTICODE CERTIFICATE TO SIGN YOUR PACKAGE AND MAKE IT SAFE 7HEN THE USER DOWNLOADS THE PACKAGE HE WILL SEE THE CERTIFICATE AND BE ABLE TO IDENTIFY YOU AS THE AUTHOR OF THE PACKAGE
Certificates and Authenticode 4HERE ARE DIFFERENT TYPES OF CERTIFICATES YOU CAN USE IN 7INDOWS 9OU CAN USE CERTIFICATES TO SIGN THE CONTENTS OF AN E MAIL CERTIFICATES TO SIGN ACCESS TO A WEB SITE AND CERTIFICATES TO SIGN AN APPLICATIONS CODE 4HE LATTER ARE CLASSIFIED AS CODE SIGNING CERTIFICATES AND CAN BE OBTAINED IN THE FOLLOWING WAYS v
0URCHASE A CERTIFICATE ONLINE FROM ONE OF THE WELL KNOWN COMPANIES THAT SELL CERTIFICATES LIKE fffeTaXbXV]R^\R^STbXV]X]V
v
'ET A CERTIFICATE FROM YOU COMPANY OR YOUR COMPANYS NETWORK ADMINISTRATOR
v
#REATE A CERTIFICATE OF YOUR OWN USING THE
4HE TYPE OF CERTIFICATE YOU NEED DEPENDS ON THE TYPE OF APPLICATION YOURE PUBLISHING &OR EXAMPLE IF YOU ARE PUBLISHING THE 4IME4RACKER APPLICATION AS A SOFTWARE VENDOR AND YOU WANT TO DISTRIBUTE IT USING A PUBLIC WEB SITE YOUR AUTHORITY SHOULD BE CERTIFIED USING A TRUSTED PUBLISHER LIKE 6ERI3IGN /N THE OTHER HAND IF YOU ARE PUBLISHING THE APPLICATION TO YOUR COMPANY INTRANET USING THE
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
4O GENERATE A CERTIFICATE WITH THIS TOOL TO RUN IT USING OPTIONS LIKE THE FOLLOWING bX\_[T RTacXUXRPcT \PZTRTac cTbc2TacRTa RaTPcT P RTacXUXRPcT fXcW P ZTh \PZTRTac bZ GHI ] 2=,GHI 2^\_P]h cTbcGHIRTa
■ .OTE 4HE #LICK/NCE DEPLOYMENT PANEL IN 6ISUAL 3TUDIO IN THE PROJECT PROPERTIES UNDER THE 3IGNING PANEL LETS YOU CREATE A TEMPORARY CERTIFICATE THAT CAN BE USED TO DEPLOY YOUR #LICK/NCE APPLICATION WITHIN YOUR INTRANET )F YOU ARE WORKING FOR A COMPANY THAT HAS AN INTERNAL )4 INFRASTRUCTURE ASK YOUR NETWORK ADMINISTRATOR ABOUT CERTIFICATES (E SHOULD BE ABLE TO CREATE ONE USING THE #ERTIFICATE !UTHORITY SERVER ON YOUR NETWORK
Code Access Security #ODE !CCESS 3ECURITY IS A #LICK/NCE OPTION USED FOR LIMITING THE ACCESS OF THE #LICK/NCE APPLICATION TO SPECIFIC RESOURCES AND COMPONENTS OF THE CLIENT RUNNING THE APPLICATION &OR EXAMPLE YOU MAY WANT TO DISABLE ACCESS TO THE LOCAL FILE SYSTEM OR YOU MAY NEED TO INCREASE THE APPLICATIONS RIGHTS TO ACCESS A LOCAL FILE SYSTEM FOLDER ON THE CLIENT IN ORDER TO BE ABLE TO WRITE FOR EXAMPLE A CUSTOM LOG FILE "Y DEFAULT #LICK/NCE INSTALLS THE APPLICATION WITH FULL TRUST RIGHTS AND THIS TYPE OF CONFIGURATION CAN BE HARMFUL FOR THE CLIENT -OREOVER IF THE CLIENT COMPUTER HAS SOME RESTRICTED CONFIGURATION THE FULL TRUST PROVIDED BY #LICK/NCE MAY NOT WORK AND THE USER MAY BE FORCED TO GRANT ADDITIONAL PERMISSIONS TO THE APPLICATION 4HE ONLY WAY TO CONFIGURE THE SECURITY ACCESS IS TO USE THE SECURITY PAGE AVAILABLE IN 6ISUAL 3TUDIO &IGURE SHOWS AN EXAMPLE OF THE SECURITY PAGE OPTIONS
&IGURE 3ECURITY OPTIONS FOR #LICK/NCE
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
7HEN YOU SET UP THE SECURITY YOU NEED TO ALSO CONSIDER THE DISTRIBUTION TECHNIQUE YOULL USE FOR THE APPLICATION &OR INSTANCE IF YOU PLAN TO GRANT FULL TRUST AND DISTRIBUTE THE APPLICATION USING AN )NTERNET 52, #LICK/NCE WILL ASK THE USER TO GRANT ADDITIONAL PERMISSIONS BECAUSE AN )NTERNET SETUP DOESNT HAVE FULL TRUST PERMISSION BY DEFAULT
Publishing Options 7HEN YOU SET UP THE #LICK/NCE OPTIONS FOR THE APPLICATION YOU CAN SPECIFY ANY PREREQUISITES FOR THE APPLICATION HOW TO UPDATE THE APPLICATION OR ONE OF ITS COMPONENTS WHERE TO PUBLISH THE APPLICATION AND WHAT COMPONENTS WILL INCLUDED IN THE DEPLOYMENT PACKAGE 9OU CAN SPECIFY ALL THIS INFORMATION USING THE 0UBLISH OPTION PANEL AVAILABLE IN 6ISUAL 3TUDIO IN THE PROPERTIES WINDOW OF THE 70& PROJECT YOU ARE CONFIGURING
5PDATES /NE OF THE MOST INTERESTING FEATURES THAT MAKE #LICK/NCE A HANDY WAY OF DISTRIBUTING APPLICATIONS IS THE ABILITY TO PROVIDE A SELF CHECKING MECHANISM FOR UPDATES 9OU CAN CONFIGURE #LICK/NCE SO THAT THE APPLICATION CAN MAKE DECISIONS ABOUT UPDATES AND FORCE THE USER TO INSTALL THE LATEST VERSION OR TAKE DIFFERENT ACTIONS 4HE OPTIONS RELATED TO THE UPDATE STRATEGY USED BY YOUR #LICK/NCE DEPLOYMENT ARE AVAILABLE IN THE 0UBLISH PANEL OF THE 0ROJECT PROPERTIES WINDOW )N THE FIRST CTgc1^g OF THE 0UBLISH PANEL YOU CAN SET UP WHERE YOU WANT TO DEPLOY THE APPLICATION FILES WHICH CAN BE A 5.# PATH IE KK\h\PRWX]TK\hbWPaTSU^[STa A 52, OR AN &40 ADDRESS )F YOU PRESS THE BROWSE BUTTON NEXT TO THE CTgc1^g YOULL SEE A DIALOG THAT WILL HELP YOU CHOOSE THE DESTINATION FOLDER 9OU CAN USE THE DIALOG TO LOCATE A LOCAL ))3 WEB SITE A REMOTE &40 FOLDER OR A FILE SYSTEM LOCATION )N THE SECOND CTgc1^g YOU CAN CHOOSE A DIFFERENT PATH FOR THE INSTALLATION )T COULD FOR EXAMPLE BE A WEB SITE ADDRESS LIKE Wcc_)\hfTQbXcTR^\X]bcP[[Wc\[ !FTER YOUVE DECIDED WHERE TO DEPLOY THE APPLICATION FILES YOU NEED TO CONFIGURE THE UPDATE STRATEGY YOU PLAN TO USE AND YOU CAN DO THIS BY CHOOSING THE APPROPRIATE OPTIONS IN THE !PPLICATION 5PDATES DIALOG SHOWN IN &IGURE
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
&IGURE 4HE !PPLICATION 5PDATES DIALOG &OR EXAMPLE IF THE UPDATES YOU WILL RELEASE ARENT CRITICAL YOU CAN ALLOW THE USER TO RUN THE APPLICATION WITHOUT THE LATEST UPDATE AND CHECK FOR UPDATES WHEN THE APPLICATION IS SHUT DOWN /R YOU CAN FORCE THE APPLICATION TO LOOK FOR UPDATES AT STARTUP AND YOU CAN ALSO FORCE THE USER TO RUN A SPECIFIC VERSION NUMBER SO THAT ANY PREVIOUS VERSION WILL BE INVALID %VERY UPDATE OF THE APPLICATION CAN OVERRIDE THESE SETTINGS 4HESE SETTINGS CAN ALSO BE CHANGED USING THE CORRESPONDING DEPLOYMENT MANIFEST FILE WHICH CAN BE EDITED USING A TEXT EDITOR OR THE
0?A4BBCX\TCaPRZTa0__[XRPcX^] THE APPLICATION MANIFEST FILE USED TO SPECIFY HOW THE APPLICATION WILL BE DEPLOYED
v
0?A4BBCX\TCaPRZTaTgT\P]XUTbc THE MANIFEST FILE THAT DESCRIBES THE APPLICATION SETTINGS AND REQUIREMENTS
4HESE FILES CAN REPLACE THE ONE PUBLISHED BY THE #LICK/NCE WIZARD SO YOU CAN CONTROL THE DEPLOYMENT PROCESS WITH MORE GRANULAR SETTINGS
!PPLICATION &ILES AND 0REREQUISITES !NOTHER SECTION IN THE 0UBLISH TAB IS RELATED TO THE APPLICATIONS FILE DEPENDENCIES &OR EXAMPLE THE 4IME4RACKER APPLICATION USES SOME EXTERNAL COMPONENTS AND SOME .%4 ASSEMBLIES THAT MUST BE DEPLOYED WITH THE APPLICATION OTHERWISE THE CLIENT WHO RUNS THE INSTALLATION PACKAGE WONT BE ABLE TO EXECUTE THE SOFTWARE BECAUSE THE DEPENDENCIES ARENT AVAILABLE
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
)F YOU PRESS THE !PPLICATION &ILES BUTTON YOULL GET A DIALOG SHOWING YOU ALL THE DEPENDENCIES 6ISUAL 3TUDIO IDENTIFIES FOR THE APPLICATION 4HIS WINDOW IS DIVIDED IN FOUR COLUMNS THE FIRST ONE SHOWS YOU THE NAME OF THE FILE DLL NEEDED BY THE APPLICATION THE SECOND ONE SHOWS YOU THE CURRENT STATUS IN THE DEPLOYMENT PROCESS WHICH CAN BE INCLUDE PREREQUISITE ALREADY INSTALLED OR EXCLUDE 4HE THIRD COLUMN SHOWS YOU THE GROUP TO WHICH THE ITEM WILL BE PUBLISHED AND THE FINAL COLUMN SHOWS THE HASH RESULT OF THE ASSEMBLY AND ASKS YOU IF YOU WANT TO PUBLISH THIS AS WELL )N THE PREREQUISITES WINDOW ALSO ON THE 0UBLISH TAB YOU CAN USE THE CORRESPONDING BUTTON TO FORCE THE #LICK/NCE DEPLOYMENT TO CHECK FOR SOME SPECIFIC PREREQUISITES THAT THE CLIENT MUST HAVE INSTALLED IN ORDER TO RUN YOUR APPLICATION 9OU CAN THEN SPECIFY THAT THE SETUP WILL UPDATE THE MISSING COMPONENTS OR IT WILL FAIL THE INSTALLATION &OR EXAMPLE YOU MAY WANT TO CHECK THAT THE CLIENT HAS INSTALLED 31, 3ERVER 2 THE .%4 &RAMEWORK AND THE 6ISUAL 3TUDIO 2EPORT 6IEWER COMPONENTS )F THEY ARE NOT INSTALLED YOU CAN FORCE THE CLIENT TO DOWNLOAD THEM FROM A LOCATION OR YOU CAN INCLUDE THEM IN THE DEPLOYMENT PACKAGE
■ .OTE #LICK/NCE TECHNOLOGY HAS BEEN INTRODUCED TO HELP DEVELOPERS DISTRIBUTE AND UPDATE THEIR APPLICATIONS WITH LESS EFFORT AND PAIN )F YOU PLAN TO DEPLOY YOUR APPLICATION ON A NETWORK FOLDER OR WEB SITE AND TO DISTRIBUTE IT USING ONLY THIS TECHNIQUE YOU SHOULD BE CAREFUL ABOUT THE APPLICATION REQUIREMENTS &OR EXAMPLE THE .%4 &RAMEWORK PLUS THE 6ISUAL 3TUDIO 2EPORT 6IEWER PACKAGES CAN EASILY AMOUNT TO -" OF DATA THAT THE CLIENT WILL HAVE TO INSTALL THE FIRST TIME /N A SLOW NETWORK LINK A DOWNLOAD OF -" CAN TAKE QUITE A WHILE SO YOU SHOULD KEEP SUCH APPLICATION REQUIREMENTS IN MIND WHEN YOU DESIGN THE #LICK/NCE PART
!DDITIONAL /PTIONS 4HE FINAL SECTION OF THE 0UBLISH TAB CONTAINS SOME ADDITIONAL INFORMATION THAT YOU CAN PROVIDE TO THE INSTALLATION PACKAGE TO BETTER DESCRIBE YOUR COMPANY AND THE APPLICATION 4HE OPTIONS DIALOG WINDOW CONSISTS OF FOUR DIFFERENT SECTIONS v
$ESCRIPTION 4HIS SECTION ALLOWS YOU TO INSERT MORE INFORMATION ABOUT THE APPLICATION THE PUBLISHER AND THE LANGUAGE 9OU CAN ALSO PROVIDE A 52, FOR SUPPORT AND ANOTHER 52, IN CASE THE APPLICATION SETUP ENCOUNTERS AN ERROR
v
$EPLOYMENT )N THIS SECTION YOU CAN SPECIFY THE DEPLOYMENT WEB PAGE OR LEAVE IT BLANK SO THAT 63 WILL CREATE A DEFAULT ONE FOR YOU 9OU CAN OPEN THE DEPLOYMENT PAGE WHEN THE BUILD AND PUBLISH PROCESS IS COMPLETE AND YOU CAN ALSO CONFIGURE THE SETTINGS FOR THE #$ 2/- VERSION OF #LICK/NCE
v
-ANIFESTS )N THIS SECTION YOU CAN ADD MORE GRANULAR CONTROL OF THE SETUP PROCESS
v
&ILE !SSOCIATIONS 4HIS SECTION CAN BE USED TO ASSOCIATE A FILE EXTENSION OF A PARTICULAR TYPE TO THE APPLICATION 9OU CAN ALSO SPECIFY A UNIQUE )D AND AN ICON FILE THAT WILL BE USED BY 7INDOWS TO IDENTIFY THE FILE EXTENSION 4HE UNIQUE )D IS A ?a^V83 USED BY 7INDOWS
#LICK/NCE TECHNOLOGY PROVIDES ADDITIONAL SETTINGS AND SETUP CONFIGURATIONS FOR MORE ADVANCED USE THAT WE WONT ANALYZE HERE )F YOU PLAN TO TAKE ADVANTAGE OF #LICK/NCE FOR MORE ADVANCED USE ) SUGGEST YOU LOOK AT THE EXHAUSTIVE GUIDE TO #LICK/NCE AT \bS]\XRa^b^UcR^\[XQaPahc& P&""SPb_g
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
Deploy the TimeTracker Application )N THE FIRST PART OF THIS CHAPTER YOU SAW HOW TO SIGN A #LICK/NCE APPLICATION USING AN !UTHENTICODE CERTIFICATE HOW TO SET UP THE CODE ACCESS SECURITY #!3 FOR THE APPLICATION AND HOW TO PREPARE ))3 TO DISTRIBUTE THE APPLICATION .OW LETS APPLY ALL THESE CONCEPTS TO THE 4IME4RACKER APPLICATION SO YOU WILL HAVE A FULL WORKING EXAMPLE OF A REDISTRIBUTABLE APPLICATION THAT CAN BE HOSTED IN ))3 &IRST YOU NEED TO OPEN THE 4IME4RACKER APPLICATION 2IGHT CLICK ON THE 4IME4RACKER 70& PROJECT IN 3OLUTION %XPLORER AND SELECT ?a^_TacXTb THIS OPENS THE 70& PROJECT PROPERTIES WINDOW SHOWING A SET OF TABS THAT IDENTIFY ALL THE INFORMATION RELATED TO THE APPLICATION 4HE !PPLICATION TAB PROVIDES INFORMATION RELATED TO THE APPLICATION )N THE )CON AND -ANIFEST SECTIONS YOU NEED TO PROVIDE AN ICON THE SIZE DEPENDS ON YOUR OPERATING SYSTEM USUALLY A PX IS PRETTY GOOD FOR THE APPLICATION THAT WILL BE USED BY #LICK/NCE TO CREATE A CUSTOM SHORTCUT ON THE DESKTOP AND ON THE APPLICATION FOLDER ,EAVE THE SETTINGS AS IS FOR THE MANIFEST .EXT YOU NEED TO GET ALL THE INFORMATION NEEDED TO SIGN AND IDENTIFY THE 70& APPLICATION 9OU CAN ACCOMPLISH THIS BY CLICKING ON THE !SSEMBLY INFORMATION BUTTON WHICH WILL BRING UP A DIALOG THAT REQUIRES THE INFORMATION USED TO SIGN AND IDENTIFY THE ASSEMBLY &IGURE SHOWS THE INFORMATION FOR THE 4IME4RACKER APPLICATION
&IGURE !SSEMBLY PROPERTIES WINDOW .OW YOU CAN PRESS /+ SAVE THE FILE IN 6ISUAL 3TUDIO AND MOVE TO THE 3IGNING TAB WHICH WILL BE USED TO CREATE A CERTIFICATE FOR THE APPLICATION
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
#REATE A 4EMPORARY #ERTIFICATE )N THIS BOOK WE CANT USE A #ODE 3IGNING CERTIFICATE FOR THE 4IME4RACKER APPLICATION BECAUSE WE DIDNT PURCHASE ONE FROM AN AUTHORIZED COMPANY NOR HAVE WE A NETWORK ADMINISTRATOR WHO CAN PROVIDE A #ODE 3IGNING CERTIFICATE 4HIS MAY BE THE SAME SITUATION YOU HAVE ON YOUR DEVELOPMENT MACHINE BUT YOU NEED TO BE ABLE TO TEST AND DEPLOY THE #LICK/NCE APPLICATION ANYWAY SO WHAT CAN YOU DO )F YOU GO TO THE 6ISUAL 3TUDIO PROJECT PROPERTIES 3IGNING PANEL YOU WILL SEE A SET OF INFORMATION THATS GRAYED OUT BY DEFAULT 4O ENABLE THIS INFORMATION SELECT 3IGN THE #LICK/NCE MANIFESTS .OW YOU HAVE THREE OPTIONS TO PROVIDE A #LICK/NCE CERTIFICATE YOU CAN SELECT ONE AVAILABLE ON A CERTIFICATE STORE YOU CAN SELECT ONE FROM A FILE SYSTEM OR YOU CAN CREATE A TEST CERTIFICATE ,ETS USE THE THIRD OPTION 4HE ONLY INFORMATION REQUIRED BY THE DIALOG USED TO CREATE A TEMPORARY CERTIFICATE IS A PASSWORD FOR THE CERTIFICATE WHICH WILL BE USED BY !UTHENTICODE TO SIGN A TEMPORARY KEY FILE "ECAUSE THIS IS A TEMPORARY TEST CERTIFICATE YOU CAN LEAVE THE PASSWORD BLANK AND PRESS /+ 4HE RESULT IS A CUSTOM CERTIFICATE THAT #LICK/NCE WILL USE TO IDENTIFY YOU AS THE CREATOR OF THE APPLICATION AS &IGURE SHOWS 9OU CAN ALSO CLICK THE -ORE $ETAILS BUTTON TO VIEW THE STANDARD CERTIFICATE DIALOG SHOWING THAT THE CERTIFICATE IS UNTRUSTED
&IGURE 4EST CERTIFICATE CREATED USING 6ISUAL 3TUDIO 4HE CERTIFICATE HAS BEEN CREATED AS A FILE WITH THE EXTENSION _Ug AND YOU CAN NOW CHOOSE TO SIGN THE ASSEMBLY AND CERTIFY IT USING YOUR CUSTOM CERTIFICATE .OTE THAT AS SOON AS YOU MARK THE ASSEMBLY WITH THE CERTIFICATE THE APPLICATION WILL NOT BUILD ANYMORE BECAUSE THE 1dbX]Tbb;PhTa AND THE 3PcP;PhTa ASSEMBLIES ARE NOT SIGNED WITH THE SAME CERTIFICATE 4O FIX THIS PROBLEM YOU NEED TO OPEN THE PROPERTIES PANEL OF EACH 4IME4RACKER APPLICATION PROJECT AND SIGN THE ASSEMBLY USING THE SAME CERTIFICATE WHICH IS AVAILABLE FROM THE 3IGN THE !SSEMBLY COMBOBOX OPTION
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
3ECURITY 4HE SECURITY SETTINGS FOR THE 4IME4RACKER APPLICATION ARE PRETTY SIMPLE AS YOULL BE RUNNING THE APPLICATION FROM THE DEVELOPMENT MACHINE )F YOU PLAN TO DISTRIBUTE THE APPLICATION OVER YOUR COMPANY NETWORK YOU CAN EASILY CHANGE THESE SETTINGS LATER 'O TO THE 3ECURITY TAB OF THE 4IME4RACKER PROPERTIES PAGE AND SELECT THE OPTION h4HIS IS A &ULL TRUST APPLICATIONv AND THEN SAVE THE SETTINGS 4HIS CONFIGURATION WILL NOT CAUSE ANY SECURITY ISSUE WHILE YOU ARE TESTING YOUR DEPLOYMENT PACKAGE
0UBLISH 4IME4RACKER .OW ITS TIME TO PUBLISH THE 4IME4RACKER APPLICATION AND VERIFY THE INSTALLATION PROCESS SO CLICK ON THE 0UBLISH TAB &IRST YOU NEED TO DECIDE WHERE TO PUBLISH THE APPLICATION ,ETS USE THE LOCAL )NTERNET )NFORMATION 3ERVICE ))3 ALREADY CREATED BY THE 3323 SETUP IN THE PREVIOUS CHAPTER /N YOUR # DRIVE CREATE A NEW FOLDER AND CALL IT 4IME4RACKER$EPLOYMENT 4HIS FOLDER IS WHERE YOULL SAVE THE DEPLOYMENT PACKAGE )F YOU HAVE 7INDOWS OR 7INDOWS 6ISTA SIMPLY TYPE ))3 IN THE SEARCH CTgc1^g OF THE 3TART MENU AND SELECT )NTERNET )NFORMATION 3ERVICES )F YOU HAVE 7INDOWS 80 OR 7INDOWS 6ISTA OR 7INDOWS YOU SHOULD BE ABLE TO LOCATE ))3 IN #ONTROL 0ANEL ➤ !DMINISTRATIVE 4OOLS ➤ )NTERNET )NFORMATION 3ERVICES )N THE ))3 PANEL LOCATE THE 3ITES FOLDER AND EXPAND IT 9OU SHOULD HAVE A $EFAULT 7EB 3ITE NODE EXPAND IT RIGHT CLICK ON IT AND SELECT !DD !PPLICATION 5SE THE SETTINGS SHOWN IN THE DIALOG IN &IGURE
&IGURE ))3 WEB APPLICATION SETTINGS
#(!04%2 ■ $%0,/9 4(% !00,)#!4)/. 53).' #,)#+/.#%
)N THE PREVIOUS STEP YOU CREATED A NEW WEB APPLICATION CALLED 4IME4RACKER$EPLOYMENT WHICH WILL BE REACHABLE FROM YOUR DEVELOPMENT MACHINE AT Wcc_)[^RP[W^bcCX\TCaPRZTa3T_[^h\T]c WHERE YOULL PUT THE DEPLOYMENT FILES .OW GO BACK TO THE 0UBLISH TAB AND SET THE 0UBLISH FOLDER LOCATION TO 2)KCX\TCaPRZTa3T_[^h\T]cK AND THE )NSTALLATION FOLDER 52, TO Wcc_)[^RP[W^bcCX\TCaPRZTa3T_[^h\T]c AND SAVE THE SETTINGS 9OU CAN TRY TO PUBLISH THE APPLICATION AS IS NOW BY RIGHT CLICKING ON THE 70& APPLICATION PROJECT IN THE SOLUTION FOLDER AND CHOOSING 0UBLISH OR BY PRESSING THE 0UBLISH BUTTON ON THE 0UBLISH TAB 6ISUAL 3TUDIO WILL BUILD THE APPLICATION AND PUBLISH IT TO THE DESTINATION FOLDER )T WILL THEN OPEN YOUR DEFAULT BROWSER BY POINTING TO THE 52, Wcc_)[^RP[W^bcCX\TCaPRZTa3T_[^h\T]c_dQ[XbWWc\ 4HE _dQ[XbWWc\ PAGE IS A STATIC PAGE CREATED BY THE 6ISUAL 3TUDIO PUBLISHING TEMPLATE WHICH INCLUDES A BRIEF DESCRIPTION OF THE APPLICATION PACKAGE THAT CAN BE ENRICHED BY USING THE /PTIONS PAGE 4HAT PAGE INCLUDES AN )NSTALL BUTTON FOR INSTALLING THE APPLICATION ON YOUR MACHINE )T WILL ALSO CREATE A SHORTCUT ICON AND A 0ROGRAM MENU ITEM .OW BASED ON THE 5PDATE SETTINGS YOU HAVE CONFIGURED EVERY TIME YOU RUN THE 4IME4RACKER APPLICATION IT WILL OR WILL NOT CHECK FOR UPDATES AND ADDITIONAL VERSIONS OF THE APPLICATION
■ .OTE 4HE #LICK/NCE TECHNOLOGY IN 6ISUAL 3TUDIO HAS A WELL KNOWN BUG THAT SOMETIME HAPPENS WHEN YOU PRESS THE 0UBLISH BUTTON ON THE 0UBLISH TAB )T DISPLAYS A GENERIC ERROR SAYING THAT THE APPLICATION CANT BE DEPLOYED BECAUSE ONE OF THE PROJECTS CANT BE BUILT )F THIS HAPPENS YOU CAN FIX THE PROBLEM BY CALLING THE 0UBLISH COMMAND FROM THE CONTEXT MENU OF THE 70& PROJECT 4HIS SOLUTION HAS BEEN EXHAUSTIVELY DESCRIBED IN THE FOLLOWING -ICROSOFT #ONNECT THREAD Wcc_b)R^]]TRc\XRa^b^UcR^\EXbdP[BcdSX^UTTSQPRZSTcPX[b$$ % eb! ac\aTcda]bTaa^aRP]]^c_dQ[XbWQTRPdbTP_a^YTRcUPX[TSc^QdX[STeT]cW^dVW b^[dcX^]QdX[SbUX]T
Summary 7ITH #LICK/NCE YOU CAN DISTRIBUTE THE APPLICATION YOU ARE PRODUCING WITHOUT THE HASSLES OF CREATING COMPLICATED DISTRIBUTION PACKAGES AND WORRYING ABOUT APPLICATION UPDATES AND DEPENDENCIES #LICK/NCE IS A PROVEN TECHNOLOGY INTRODUCED WITH 6ISUAL 3TUDIO AND NOW TESTED AND EXTENDED TO SATISFY ALL THE MAJOR REQUIREMENTS AN APPLICATION SETUP PACKAGE MAY HAVE 9OU CAN CREATE A SHORTCUT ON THE CLIENT DESKTOP AN APPLICATION MENU ITEM IN 7INDOWS AND TAKE ADVANTAGE OF MANY OTHER FEATURES LIKE PROVIDING A HELP 52, OR AN ERROR 52, #LICK/NCE ALSO LETS YOU CERTIFY YOUR PACKAGES BY SIGNING THE CODE WITH THE !UTHENTICODE TECHNOLOGY USING A CUSTOM GENERATED CERTIFICATE OR A THIRD PARTY CERTIFICATE LIKE THE ONES PROVIDED BY 6ERI3IGN FOR EXAMPLE 7ITH #LICK/NCE YOU CAN CARRY WITH YOUR APPLICATION DEPLOYMENT PACKAGE ALL THE DEPENDENCIES REQUIRED BY THE APPLICATION AND IF YOU BELIEVE THE DEPENDENCIES ARE TOO HEAVY YOU CAN JUST FORCE THE CLIENT COMPUTER TO CHECK IF THE PREREQUISITES ARE SATISFIED OR PROVIDE AN ALTERNATIVE WAY OF OBTAINING THEM FOR EXAMPLE BY USING A DOWNLOAD 52, #LICK/NCE IS FLEXIBLE AND EASY TO USE AND WITH THE HELP OF ))3 IT LETS YOU CREATE AND DEPLOY AN APPLICATION SETUP FILE IN FEW MINUTES AND MAKE IT AVAILABLE TO YOUR ENTIRE COMPANY NETWORK JUST BY SHARING A STATIC ))3 WEB SITE
CHAPTER
2
■■■
Design Patterns in WPF 4HE MAIN PURPOSE OF CREATING A LAYERED APPLICATION AND LOOSELY COUPLING PRESENTATION LOGIC TO BUSINESS LOGIC IS TO HAVE A LARGER TESTABLE SURFACE AREA AND TO REMOVE AS FAR AS POSSIBLE CODE DEPENDENCIES SO THAT EACH LAYER OF THE APPLICATION CAN BE TESTABLE AND INDEPENDENT 0ROBABLY THIS CONCEPT CAN BE APPLIED FROM A PURIST POINT OF VIEW ONLY IN A PERFECT WORLD WHERE THE MODEL IS COMPLETELY INDEPENDENT OF ANYTHING ELSE AND THE 5) DOESNT KNOW ANYTHING OF THE DATA MODEL THAT IS RENDERING ON THE SCREEN 4HROUGHOUT THE ENTIRE BOOK WEVE APPLIED DESIGN PATTERNS TO EVERY COMPONENT OF THE 4IME4RACKER APPLICATION 7E CREATED A DOMAIN MODEL USING THE DOMAIN DRIVEN DESIGN TECHNIQUES WE CREATED A DATA LAYER USING THE 5NIT/F7ORK AND THE 2EPOSITORY PATTERNS AND WE APPLIED SOME 4EST $RIVEN $EVELOPMENT CONCEPTS TO THESE COMPONENTS 4HE ADVANTAGE OF USING THESE TECHNIQUES IS AMAZING 9OU CAN CHANGE SOMETHING IN THE DOMAIN MODEL AND BY TESTING UNDERSTAND RIGHT AWAY IF THE CHANGE WILL OR WILL NOT AFFECT THE REMAINING COMPONENTS OF THE APPLICATION 9OU CAN ALSO VERIFY THE IMPACT OF SUCH CHANGE AND UNDERSTAND IF THE APPLICATION CAN BE DEPLOYED WITH THE NEW CHANGES OR NOT /F COURSE THIS PROCESS IS COVERED BY A COMPLEX SET OF TESTS AS SOON AS YOU TACKLE ONE LAYER SAY THE DATA LAYER YOU SHOULD EXECUTE ALL THE UNIT AND INTEGRATION TESTS FOR ALL THE LAYERS TO MAKE SURE THAT THE CHANGES WILL NOT AFFECT INDIRECTLY A DIFFERENT LAYER THAT HAS NOT BEEN TESTED AGAIN 5NFORTUNATELY THOUGH WE ADDED ALL THIS FLEXIBILITY TO THE 4IME4RACKER APPLICATION WE DIDNT COMPLETELY ISOLATE AND LOOSELY COUPLED THE 5) LOGIC FROM THE REST OF THE APPLICATION LOGIC )N SHORT WE DIDNT APPLY A DESIGN PATTERN SPECIFICALLY FOR THE PRESENTATION LOGIC TO MAKE THE PRESENTATION PART MORE TESTABLE 4O BE HONEST ALTHOUGH YOUVE ALREADY SEEN A WHOLE LOT ABOUT THE -66- PATTERN WE HAVENT REALLY ANALYZED IT IN DEPTH OR LOOKED AT THE AVAILABLE ALTERNATIVES ,ETS DO THAT NOW
Patterns for the UI 4HE POINT OF APPLYING A DESIGN PATTERN TO THE GRAPHICAL LAYER OF AN APPLICATION IS TO LOOSELY COUPLE THE BUSINESS LOGIC TO THE PRESENTATION LOGIC SO THAT FOR EXAMPLE YOU CAN RECYCLE THE SAME COMPONENTS FOR A DIFFERENT 6IEW (OWEVER WITH PRESENTATION PATTERNS THE IDEA IS NOT TO APPLY THEM IN ORDER TO LOOSELY COUPLE THE -ODEL TO THE 6IEW )N THE FIRST PLACE ALL THE INFORMATION CARRIED FROM THE MODEL IS EXPOSED BY THE 6IEW AND THERES NO REASON TO HIDE IT -OREOVER THE AMOUNT OF EFFORT YOU HAVE TO PUT IN AND THE AMOUNT OF CODE YOU HAVE TO WRITE TO TOTALLY LOOSELY COUPLE THE VIEW TO THE MODEL IS TOO MUCH TO GAIN ANY OTHER ADVANTAGE
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
)F YOU LOOK AT ALL THE OTHER PRESENTATION PATTERNS NOT JUST FOR 70& THEY ALL INCLUDE THE WORD MODEL -ODEL 6IEW #ONTROLLER -6# -ODEL 6IEW 0RESENTER -60 0RESENTATION -ODEL 0- AND -ODEL 6IEW 6IEW-ODEL -66- SO IT SEEMS CLEAR THAT YOU SHOULDNT BE AFRAID OF EXPOSING THE MODEL TO THE VIEW TO DISPLAY THE INFORMATION YOU NEED TO DISPLAY 7HAT YOU SHOULD BE MORE AFRAID OF IS STICKING BUSINESS LOGIC IN THE VIEW AND MIXING LOGIC CODE FOR THE VIEW WITH LOGIC CODE FOR THE MODEL &OR EXAMPLE IF YOU HAVE A MODEL THAT IS NOT VALID THERES NO DATA YOU SHOULDNT WRITE THIS LOGIC IN THE VIEW JUST BECAUSE THE VIEW IS IN CHARGE OF NOTIFYING THE USER WITH A VALIDATION ERROR )NSTEAD YOU SHOULD USE A MIDDLE COMPONENT LIKE A PRESENTER TO ORCHESTRATE BETWEEN THE VIEW AND THE MODEL SO THAT THE VIEW REQUIREMENTS DONT AFFECT THE MODEL REQUIREMENTS )N THIS BOOK WE USE THE -66- PATTERN A DESIGN PATTERN DESIGNED FOR 70& AND SUBSEQUENTLY APPLIED AS WELL TO 3ILVERLIGHT AND 7INDOWS 0HONE (OWEVER -66- IS NOT THE ONLY DESIGN PATTERN APPLICABLE TO 70& SO LETS TAKE A SHORT TOUR OF THE DESIGN PATTERNS THAT CAN BE EASILY APPLIED TO 70&
■ .OTE 9OU MAY NOTICE THAT IN DISCUSSING DESIGN PATTERNS FOR 70& WE SKIP SOME OF THE MOST WELL KNOWN PRESENTATION PATTERNS LIKE -6# 4HIS IS QUITE DELIBERATE BECAUSE ) BELIEVE THAT THE -6# PATTERN IS NOT PRACTICALLY APPLICABLE TO 70&EVEN IF IT IS THEORETICALLY POSSIBLE TO BUILD AN -6# 70& APPLICATION &IRST OF ALL -6# REQUIRES AN ARCHITECTURE FRAMEWORK BEHIND THE SCENES THAT IS NOT EASY TO BUILD AND NOT FEASIBLE WITH CLIENT TECHNOLOGIES LIKE 70& OR 7INDOWS &ORMS 3ECOND AND MORE IMPORTANT WITH THE -6# PATTERN YOU CANT USE A NUMBER OF 70&S POWERFUL FEATURES LIKE DATA BINDING COMMANDS AND DELEGATES BECAUSE THE -6# PATTERN IS DRIVEN BY THE #ONTROLLER AND NOT BY THE 6IEW
-ODEL 6IEW 0RESENTER 4HE -ODEL 6IEW 0RESENTER -60 PATTERN HAS BEEN AROUND DEVELOPER COMMUNITIES SINCE WHEN IT WAS FIRST USED BY 4ALIGENT AND )"- )T WAS INTRODUCED BECAUSE THE PARENT -ODEL 6IEW #ONTROLLER -6# PATTERN LACKED A MIDDLE COMPONENT TO TAKE CARE OF THE PRESENTATION LOGIC 4HE -60 PATTERN HAS THREE COMPONENTS THE -ODEL THE 6IEW AND THE 0RESENTER THE LAST ONE IS IN CHARGE OF THE PRESENTATION LOGIC )N THIS PATTERN THE MAIN ROLE IS GIVEN TO THE 0RESENTER WHICH IS IN CHARGE OF LISTENING FOR THE 6IEWS EVENTS AND REACTING APPROPRIATELY TO THESE EVENTS 4HE 0RESENTER ALSO MAKES LOGICAL DECISIONS BASED ON THE 6IEW STATE 3INCE ITS DEVELOPMENT THIS PATTERN HAS BEEN EXAMINED ESPECIALLY BY -ARTIN &OWLER A SOFTWARE ARCHITECT WHO ANALYZED THIS AND OTHER PRESENTATION PATTERNS IN DEPTH )F YOU ARE INTERESTED IN THE TOPIC YOU MAY FIND HIS BOOK 0ATTERNS OF %NTERPRISE !PPLICATIONS !RCHITECTURE !DDISON 7ESLEY VERY USEFUL .OW LETS TAKE A LOOK AT THE -60 PATTERN ALSO KNOWN AS 0ASSIVE 6IEW AND AT A VARIANT CALLED 3UPERVISING #ONTROLLER
■ .OTE 4HE NEXT SECTIONS USE SOME SAMPLE CODE THAT CAN BE EASILY DOWNLOADED FROM THIS BOOKS PAGE ON THE !PRESS WEB SITE 4HE SAMPLE CODE DOES NOT REFER DIRECTLY TO THE 4IME4RACKER APPLICATION AND IS USED JUST TO EXPLAIN THE DESIGN PATTERN
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
X]c =d\QTa1 j VTc* bTc* l e^XS 2P[Rd[PcT^QYTRc bT]STa 4eT]c0aVb PaVb* l l 4HE VIEW HAS TWO PROPERTIES OF TYPE INTEGER =d\QTa0 AND =d\QTa1 AND A METHOD THAT WILL BE USED AS A DELEGATE FOR THE 2P[Rd[PcT BUTTON .OW WE NEED TO CREATE A 70& VIEW WITH TWO CTgc1^gES AND A 1dcc^] AND IMPLEMENT THE INTERFACE IN THE CODE BEHIND FILE AS IN ,ISTING ,ISTING )MPLEMENTATION OF THE 82P[Rd[PcTEXTf dbX]V BhbcT\* dbX]V BhbcT\FX]S^fb* ]P\Tb_PRT D8?PccTa]b?PbbXeTEXTf j +bd\\Pah 8]cTaPRcX^] [^VXR U^a ?PbbXeTEXTfgP\[ +bd\\Pah_dQ[XR _PacXP[ R[Pbb ?PbbXeTEXTf ) FX]S^f 82P[Rd[Pc^aEXTf j _dQ[XR ?PbbXeTEXTf j 8]XcXP[XiT2^\_^]T]c* Qc]2P[Rd[PcT2[XRZ , 2P[Rd[PcT* l aTVX^] 8\_[T\T]cPcX^] ^U 82P[Rd[Pc^aEXTf _dQ[XR X]c =d\QTa0 j VTc j aTcda] 2^]eTacC^8]c"!cgc0CTgc* l bTc j cgc0CTgc , eP[dTC^BcaX]V* l l _dQ[XR X]c =d\QTa1 j VTc j aTcda] 2^]eTacC^8]c"!cgc1CTgc* l bTc j cgc1CTgc , eP[dTC^BcaX]V* l l _dQ[XR e^XS 2P[Rd[PcT^QYTRc bT]STa 4eT]c0aVb PaVb j cWT X\_[T\T]cPcX^] fX[[ QT P]P[hiTS [PcTa X] cWXb bTRcX^] l T]SaTVX^] l l
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
!S YOU CAN SEE THE CODE IMPLEMENTS THE 6IEW BY MANUALLY BINDING THE PROPERTIES OF THE 82P[Rd[Pc^aEXTf TO THE OBJECTS OF THE 70& 6IEW AND ALSO ATTACHES THE 2P[Rd[PcT METHOD TO THE CORRESPONDING #LICK EVENT OF THE Qc]2P[Rd[PcT "UTTON .OW WE JUST NEED TO IMPLEMENT THE 0RESENTER AND BIND IT TO THE 6IEW AS SHOWN IN ,ISTING ,ISTING 4HE 0RESENTER AND )TS 2EFERENCE IN THE 6IEW ?aTbT]cTa R[Pbb ]P\Tb_PRT D8?PccTa]b?PbbXeTEXTf j _dQ[XR bTP[TS R[Pbb 2P[Rd[Pc^a?aTbT]cTa j _aXePcT aTPS^][h 82P[Rd[Pc^aEXTf eXTf* _dQ[XR 2P[Rd[Pc^a?aTbT]cTa82P[Rd[Pc^aEXTf eXTf j cWXbeXTf , eXTf* l _dQ[XR e^XS 2P[Rd[PcT j 2P[Rd[PcX^]<^ST[ \^ST[ , ]Tf 2P[Rd[PcX^]<^ST[eXTf=d\QTa0 eXTf=d\QTa1* \^ST[2P[Rd[PcT* l l l ATUTaT]RT X] cWT F?5 EXTf _dQ[XR _PacXP[ R[Pbb ?PbbXeTEXTf ) FX]S^f 82P[Rd[Pc^aEXTf j _aXePcT 2P[Rd[Pc^a?aTbT]cTa _aTbT]cTa* _dQ[XR ?PbbXeTEXTf j 8]XcXP[XiT2^\_^]T]c* cWXb_aTbT]cTa , ]Tf 2P[Rd[Pc^a?aTbT]cTacWXb* Qc]2P[Rd[PcT2[XRZ , 2P[Rd[PcT* l _dQ[XR e^XS 2P[Rd[PcT^QYTRc bT]STa 4eT]c0aVb PaVb j cWXb_aTbT]cTa2P[Rd[PcT* l ^\XccTS R^ST l )N THIS CASE THE 6IEW NOTIFIES THE 0RESENTER THAT THE USER HAS REQUESTED A CALCULATION THE 0RESENTER WILL PROCESS THE NUMBERS ENTERED IN THE 6IEW AND EXECUTE THE CALCULATION !S YOU CAN SEE THE 6IEW IS TOTALLY AGNOSTIC AND UNAWARE OF THE PRESENTATION LOGIC WHICH IS LEFT COMPLETELY TO THE 0RESENTER
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
!ND THE IMPLEMENTATION IN THE 70& 6IEW WILL USE THE BINDING ENGINE OF 70& TO KEEP THE -ODEL ALIVE SEE ,ISTING ,ISTING 70& )MPLEMENTATION OF THE 6IEW dbX]V BhbcT\* dbX]V BhbcT\FX]S^fb* ]P\Tb_PRT D8?PccTa]bBd_TaeXbX]V2^]ca^[[Ta j +bd\\Pah 8]cTaPRcX^] [^VXR U^a Bd_TaeXbX]V2^]ca^[[TagP\[ +bd\\Pah_dQ[XR _PacXP[ R[Pbb Bd_TaeXbX]V2^]ca^[[Ta ) FX]S^f 82P[Rd[Pc^aEXTf j _aXePcT aTPS^][h 2P[Rd[Pc^a?aTbT]cTa _aTbT]cTa* _dQ[XR Bd_TaeXbX]V2^]ca^[[Ta j 8]XcXP[XiT2^\_^]T]c* cWXb_aTbT]cTa , ]Tf 2P[Rd[Pc^a?aTbT]cTacWXb* l aTVX^] 82P[Rd[Pc^aEXTf
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
When to Use the MVP )F YOU ARE WORKING WITH 70& THERE SHOULDNT BE ANY PARTICULAR REASON TO USE -60 INSTEAD OF -66- BUT IF YOURE NOT BUILDING A BRAND NEW APPLICATION AND ARE SIMPLY MIGRATING AN EXISTING APPLICATION DESIGNED WITH THE -60 PATTERN TO 70& YOU MAY WONDER WHEN TO USE THE -60 0ASSIVE 6IEW AND WHEN THE 3UPERVISING #ONTROLLER VERSION ,ETS START WITH THE 0ASSIVE 6IEW IN THIS VERSION OF -60 YOU CAN TEST ALMOST THE ENTIRE SURFACE OF THE PRESENTATION LOGIC BECAUSE THE 6IEW IS A PURE ABSTRACT CONTAINER OF INFORMATION FOR THE 5) AND NOTHING MORE 4HE TESTABILITY OF THE APPLICATION IS VERY HIGH AND ALL THE PRESENTATION LOGIC CAN BE TESTED WITHOUT THE NEED OF MOCKING THE 5) PART BECAUSE ALL THE HARD WORK IS LEFT TO THE 0RESENTER 4HE DOWNSIDE OF USING THE 0ASSIVE 6IEW IS THAT IT REQUIRES A LOT OF ADDITIONAL CODE ESPECIALLY ON THE 6IEW SIDE BECAUSE YOU PROBABLY WONT EXPOSE THE -ODEL DIRECTLY INTO THE 6IEW 4HIS ADDITIONAL CODE CAN MAKE A HUGE DIFFERENCE IF THE 6IEW YOU ARE BUILDING IS COMPLEX AND RICH IN INFORMATION /N THE OTHER HAND THERE MAY BE SOME SITUATIONS WHERE THE TESTABILITY OF THE PRESENTATION LOGIC IS NOT THE ONLY CONCERN OR REQUIREMENT AND THE COMPLEXITY OF THE -ODEL YOU ARE EXPOSING TO THE 6IEW IS SO GREAT THAT IT MAY TURN OUT TO BE VERY DIFFICULT TO CREATE AN ABSTRACT 6IEW THAT REPRESENTS THE CORRESPONDING -ODEL )N THAT CASE YOU MIGHT CONSIDER USING THE 3UPERVISING #ONTROLLER -60 VERSION /F COURSE IF YOU PLAN TO USE THIS VERSION OF THE -60 YOU HAVE TO BEAR IN MIND THAT YOU ARE FORCING THE APPLICATION TO LOSE FLEXIBILITY BECAUSE THE 6IEW IS STRICTLY BOUND TO THE CORRESPONDING -ODEL AND THIS WILL PROBABLY REMOVE ABSTRACTION FROM THE 6IEW
-ODEL 6IEW 6IEW-ODEL )N WHEN RICH 5) TECHNOLOGIES LIKE 70& AND 3ILVERLIGHT WERE JUST IN BETA -ARTIN &OWLER INTRODUCED A NEW PRESENTATION PATTERN CALLED 0RESENTATION -ODEL 0- )N THIS PATTERN &OWLER PRESENTED SOME OF THE FEATURES THAT ARE NOW IN 70& BUT IN SIMPLER WAY 4HE 0- PATTERN CONSISTS OF TWO COMPONENTS THE 0RESENTATION-ODEL AND THE 6IEW THAT IS BOUND TO IT 4HE 0- IS IN CHARGE OF PROVIDING THE INFORMATION AND THE PRESENTATION LOGIC USED BY THE 6IEW AND THE 6IEW IS BOUND TO IT USING A BINDING ENGINE WHICH IN OUR CASE IS THE 70& BINDING ENGINE )N *OHN 'OSSMAN 70& SOFTWARE ARCHITECT AT -ICROSOFT REVISED THE 0- PATTERN A LITTLE BIT INTRODUCING A NEW SPECIALIZED VERSION MORE SPECIFIC TO 70& THE -ODEL 6IEW 6IEW-ODEL PATTERN 4HE MAIN DIFFERENCE BETWEEN THIS PRESENTATION PATTERN AND THE -60 IS THAT -66- LIKE 0- DOES NOT NEED A REFERENCE TO THE 6IEW WHILE IN -60 YOU HAVE TO REFERENCE THE 6IEW IN THE 0RESENTER 4HIS DIFFERENCE CAN HAVE A BIG ADVANTAGE IN TERMS OF FLEXIBILITY AS WITH THE -66- PATTERN YOU RECYCLE THE 6IEW-ODEL OVER DIFFERENT 6IEWS 4HE -66- PATTERN CONSISTS OF THREE MAJOR OBJECTS THE -ODEL THE 6IEW AND THE 6IEW-ODEL THE STRUCTURE OF THE -66- IS SHOWN IN &IGURE
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
,ISTING )MPLEMENTATION OF THE ).0# )NTERFACE dbX]V BhbcT\2^\_^]T]c<^ST[* ]P\Tb_PRT D8?PccTa]b<EE< j _dQ[XR bTP[TS R[Pbb 2P[Rd[Pc^aEXTf<^ST[ ) 8=^cXUh?a^_Tach2WP]VTS j aTVX^] 8\_[T\T]cPcX^] ^U 8=^cXUh?a^_Tach2WP]VTS _dQ[XR TeT]c ?a^_Tach2WP]VTS4eT]c7P]S[Ta ?a^_Tach2WP]VTS* T]SaTVX^] _dQ[XR e^XS >]?a^_Tach2WP]VTSbcaX]V ]P\T j ?a^_Tach2WP]VTS4eT]c7P]S[Ta WP]S[Ta , ?a^_Tach2WP]VTS* XU WP]S[Ta , ]d[[ j ?a^_Tach2WP]VTScWXb ]Tf ?a^_Tach2WP]VTS4eT]c0aVb]P\T* l l l l 4HE 70& BINDING ENGINE IS DESIGNED TO LISTEN FOR THE ?a^_Tach2WP]VTS EVENT EXPOSED BY THIS INTERFACE IF THE OBJECT DATABOUND TO THE 3PcP2^]cTgc OF THE 6IEW IS IMPLEMENTING THE 8=?2 INTERFACE &OR MORE INFORMATION SEE #HAPTER WHICH HAS AN ENTIRE SECTION DEDICATED TO THIS MECHANISM &OR THE 2P[Rd[Pc^a MODEL THE SECOND STEP IS TO INJECT THE -ODEL INTO THE 6IEW-ODEL AND REPLICATE THE -ODEL PROPERTIES IN THE 6IEW-ODEL THEN RAISE A ?a^_Tach2WP]VTS EVENT FOR EACH AS SHOWN IN ,ISTING ,ISTING )MPLEMENTATION OF THE -ODEL IN THE 6IEW-ODEL _dQ[XR bTP[TS R[Pbb 2P[Rd[Pc^aEXTf<^ST[ ) 8=^cXUh?a^_Tach2WP]VTS j _aXePcT 2P[Rd[Pc^a<^ST[ \^ST[* _dQ[XR 2P[Rd[Pc^aEXTf<^ST[2P[Rd[Pc^a<^ST[ \^ST[ j cWXb\^ST[ , \^ST[* l
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
_dQ[XR X]c =d\QTa0 j VTc j aTcda] cWXb\^ST[=d\QTa0* l bTc j 8U cWXb\^ST[=d\QTa0 , eP[dT j cWXb\^ST[=d\QTa0 , eP[dT* >]?a^_Tach2WP]VTS=d\QTa0* l l l 8=?2 X\_[T\T]cPcX^] ^\XccTS l )N THIS LISTING THE -ODEL IS NOT DIRECTLY EXPOSED IN THE 6IEW BUT IS WRAPPED AROUND A 6IEW-ODEL PROPERTY SO YOU DONT HAVE TO POLLUTE THE DOMAIN ENTITY WITH THE 8=?2 INTERFACE .OW YOU CAN EASILY DATA BIND THE 6IEW-ODEL IN THE 6IEW USING ONLY PURE 8!-, MARKUP AS SHOWN IN ,ISTING ,ISTING $ATA "IND THE 6IEW-ODEL TO THE 8!-, 6IEW +FX]S^f g)2[Pbb,D8?PccTa]b<EE<<^ST[EXTfEXTf<^ST[ g\[]b,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[_aTbT]cPcX^] g\[]b)g,Wcc_)bRWT\Pb\XRa^b^UcR^\fX]Ug!%gP\[ g\[]b)<EE<,R[a]P\Tb_PRT)D8?PccTa]b<EE< CXc[T,<^ST[EXTfEXTf<^ST[ 7TXVWc," FXScW,"+FX]S^f3PcP2^]cTgc+<EE<)2P[Rd[Pc^aEXTf<^ST[ +FX]S^f3PcP2^]cTgc+6aXS+CTgc1[^RZ-2P[Rd[Pc^a <EE<+CTgc1[^RZ+CTgc1[^RZ -2P[Rd[PcT cWT Bd\ QTcfTT] 0 P]S 1+CTgc1[^RZ+;PQT[-=d\QTa 0)+;PQT[+CTgc1^g CTgc,j1X]SX]V ?PcW,=d\QTa0l-+CTgc1^g+;PQT[-=d\QTa 1)+;PQT[+CTgc1^g CTgc,j1X]SX]V ?PcW,=d\QTa1l-+CTgc1^g+1dcc^]-2P[Rd[PcT+1dcc^]+6aXS+FX]S^f-
■ .OTE 4HE PRECEDING EXAMPLE SHOWS ONE POSSIBLE APPROACH TO BINDING A -ODEL TO A 6IEW-ODEL 9OU CAN ALSO CONSIDER EXPOSING THE -ODEL DIRECTLY IN THE 6IEW-ODEL OR CREATING A 6IEW-ODEL THAT REFLECTS THE -ODEL PROPERTIES AND DATA BIND THE -ODEL TO THE 6IEW-ODEL BACKWARDS AND FORWARD 9OULL FIND MUCH DEBATE ABOUT WHICH TECHNIQUE IS THE BEST AND WHICH IS THE WORST ) PERSONALLY BELIEVE THAT IT ALWAYS DEPENDS ON WHAT YOU ARE DOING AND ON THE COMPLEXITY OF THE -ODEL6IEW-ODEL STRUCTURE
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
Exposing the Commands )N #HAPTER YOU LOOKED AT HOW COMMANDS WORK IN 70& AND HOW THIS IS HANDLED BY THE DATA BINDING ENGINE 70& ITSELF EXPOSES THE A^dcTS2^\\P]S OBJECT WHICH IS NOTHING MORE THAN A CUSTOM IMPLEMENTATION OF THE 82^\\P]S INTERFACE DESIGNED FOR THE 70& ENGINE )N -66- YOU EXPOSE THE COMMANDS AVAILABLE FOR THE 5) AS SIMPLE READ ONLY PROPERTIES OF TYPE 82^\\P]S THAT ARE IMPLEMENTED INSIDE THE 6IEW-ODEL OBJECT AS IN ,ISTING 0LEASE REFER TO #HAPTER TO SEE HOW TO CREATE AN <ee\2^\\P]S OBJECT ,ISTING %XPOSING AN 82^\\P]S IN A 6IEW-ODEL _dQ[XR bTP[TS R[Pbb 2P[Rd[Pc^aEXTf<^ST[ ) 8=^cXUh?a^_Tach2WP]VTS j _dQ[XR 82^\\P]S 2P[Rd[PcT2^\\P]S j VTc* _aXePcT bTc* l _aXePcT 2P[Rd[Pc^a<^ST[ \^ST[* _dQ[XR 2P[Rd[Pc^aEXTf<^ST[ j cWXb\^ST[ , ]Tf 2P[Rd[Pc^a<^ST[* cWXb2P[Rd[PcT2^\\P]S , ]Tf <ee\2^\\P]S _PaP\ ,- \^ST[2P[Rd[PcT _PaP\ ,- =d\QTa0 , =d\QTa1 , 0SS;XbcT]Ta+2P[Rd[Pc^aEXTf<^ST[-cWXb g ,- g=d\QTa0* l l 4HIS LISTING CREATES A NEW 82^\\P]S THAT IS RE EVALUATED EVERY TIME THE PROPERTY =d\QTa0 OF THE 6IEW-ODEL IS CHANGED AND THIS EXECUTES THE 2P[Rd[PcT METHOD OF THE 6IEW-ODEL MODEL PROPERTY 4HE REASON FOR RE EVALUATING THE COMMAND EXECUTION IS THAT 70& BINDS IT TO THE 2^\\P]S DEPENDENCY PROPERTY OF THE 1dcc^] AND THIS RE EVALUATION PROCESS WILL ENABLEDISABLE THE BUTTON DEPENDING ON THE "OOLEAN RESULT RETURNED BY THE 2P]4gTRdcT METHOD OF THE 82^\\P]S IMPLEMENTATION 4HE MAIN CONCEPT HERE IS HOW TO USE THE 82^\\P]S INTERFACE IN 70& DISCUSSED EXTENSIVELY IN #HAPTER THEN IN THE 6IEW-ODEL YOU JUST HAVE TO EXPOSE THE 82^\\P]S INTERFACE AND DATA BIND IT TO THE CORRESPONDING 8!-, ELEMENT OF THE 6IEW LIKE A BUTTON AS SHOWN IN ,ISTINGS AND ,ISTING "INDING TO A "UTTON bP\_[T EXTf<^ST[ _dQ[XR R[Pbb BP\_[TEXTf<^ST[ ) 8=^cXUh?a^_Tach2WP]VTS j 8=^cXUh?a^_Tach2WP]VTS X]cTaUPRT X\_[T\T]cPcX^] WPb QTT] bZX__TS 82^\\P]S _aXePcT 82^\\P]S \h2^\\P]S* _dQ[XR 82^\\P]S
#(!04%2 ■ $%3)'. 0!44%2.3 ). 70&
bTc j 8U \h2^\\P]S , eP[dT j \h2^\\P]S , eP[dT* ]^cXUh l l l l ,ISTING "INDING THE "UTTON OF ,ISTING +1dcc^] 2^\\P]S,|j1X]SX]V ?PcW,
Summary ) WANT TO CLARIFY A COMMON MISUNDERSTANDING )VE SEEN IN THE .%4 COMMUNITIES IN THE PAST YEARS ABOUT USING AND WORKING WITH THE PRESENTATION PATTERNS IN GENERAL ! PRESENTATION PATTERN IS ADOPTED IN ORDER TO LOOSELY COUPLE THE 5) LOGIC FROM THE PRESENTATION LOGIC AND TO MAKE THE PRESENTATION PART OF THE APPLICATION MORE TESTABLE AND FLEXIBLE )T IS NOT USED TO SEPARATE THE DOMAIN FROM THE 5) THIS IS NOT THE PRIMARY REASON FOR ADOPTING PRESENTATION PATTERNS 0RESENTATIONS PATTERN SUCH -60 AND -66- ARE ONLY PART OF THE 5) THEY DONT REPRESENT THE OVERALL SOLUTION FOR 5) ARCHITECTURE 7ITH A PRESENTATION PATTERN YOU SHOULD SOLVE SOME SPECIFIC ASPECTS OF THE 5) LAYER BUT THIS DOESNT MEAN THAT YOU NEED A THIRD PARTY FRAMEWORK OR A PERSONAL SET OF FACILITIES THAT WILL ALLOW YOU TO BOOTSTRAP THE 5) AND ORCHESTRATE THE NAVIGATION AND THE COMMUNICATION BETWEEN THE VIEWS LIKE WE DID IN THE 4IME4RACKER APPLICATION USING 5NITY AND THE NAVIGATION PATTERN 4HERE IS NO SINGLE CORRECT PATTERN TO USE EACH ONE HAS PROS AND CONS AND BEFORE CHOOSING ONE INSTEAD OF ANOTHER YOU SHOULD EVALUATE THEM AND ASK YOURSELF QUESTIONS LIKE h$OES MY ARCHITECTURE NEED -60v OR h$O ) NEED TO FULLY TEST THE PRESENTATION SURFACE OF THE APPLICATIONv AND SO ON !LSO CONSIDER THAT IT IS NOT TRUE THAT ONE PRESENTATION PATTERN IS MORE COMPLICATED THAN ANOTHER THEY ARE SIMPLY DIFFERENT AND AS WITH ANY OTHER PATTERN YOU NEED TO TRY THEM OUT TO UNDERSTAND AND MASTER THEM 3PEND SOME TIME LEARNING THESE PATTERNS BEFORE MAKING A FINAL CHOICE
CHAPTER ■■■
WPF and Multithreading 7ITH THE COMPUTER HARDWARE CURRENTLY ON THE MARKET AND THE POWERFUL CONFIGURATIONS YOU CAN BUY FOR AN ACCEPTABLE PRICE YOU CAN HAVE IN YOUR HANDS A ROBUST MACHINE THAT IS ABLE TO EXECUTE MILLIONS OF CALCULATIONS PER SECOND 5NFORTUNATELY HAVING THIS KIND OF POWERFUL CALCULATOR DOESNT MAKE AN APPLICATION FASTER BY DEFAULT NOR DOES IT MAKE AN APPLICATIONS USER INTERFACE FASTER OR MORE RESPONSIVE )F YOU PLAN TO DESIGN AN APPLICATION TO BE RESPONSIVE AND TO EXECUTE TONS OF OPERATIONS IN PARALLEL YOU NEED TO WRITE CODE THAT IS ABLE TO DO THAT INSTEAD OF RELYING ON EXPENSIVE HARDWARE .OWADAYS THERE ARE MACHINES WITH MULTI CORE PROCESSORS #05S THAT CAN EXECUTE PARALLEL PROCESSES ALL AT THE SAME TIME )T USED TO BE THE CASE THAT PROCESSORS ONLY HAD A SINGLE CORE WHICH MEANT THEY COULD ONLY EXECUTE A SEQUENCE OF STEPS THEY WERE NOT ABLE TO EXECUTE PARALLEL OPERATIONS )F YOU HAD TO EXECUTE PARALLEL PROCESSES YOU HAD TO WRITE MULTITHREADED CODE "Y MULTITHREADING ) MEAN A SYSTEM PROGRAM OR COMPONENT THAT EXECUTES MORE THAN ONE PROCESS IN PARALLEL 4HE ADVANTAGE OF THIS IS THAT SOFTWARE CAN EXECUTE INSTRUCTIONS IN PARALLEL AND OFTEN DECREASE THE TOTAL TIME REQUIRED TO EXECUTE THOSE INSTRUCTIONS /F COURSE MULTI CORE AND MULTITHREADING ARE TWO TERMS THAT GO TOGETHER IN COMPUTER SCIENCE IF YOU PROGRAM YOUR SOFTWARE USING MULTITHREADING BUT YOU HAVE A MACHINE WITH A SINGLE CORE YOUR CODE WILL SHARE THE RESOURCE OF THE CORE AMONG ALL THE PARALLEL THREADS )F YOU HAVE A MULTI CORE PROCESSOR THOUGH THE PERFORMANCE OF YOUR hPARALLELv CODE SHOULD BE FASTER 4O APPLY THESE CONCEPTS TO 70& APPLICATIONS ONE OF THE PRIMARY ADVANTAGE OF USING A MULTITHREADING PROGRAMMING APPROACH IS TO HAVE A VERY RESPONSIVE USER INTERFACE BECAUSE ANY OPERATION CALLED BY THE 5) WILL BE EXECUTED IN THE BACKGROUND WHICH MEANS ON ANOTHER THREAD BY THE COMPUTER SO THAT THE THREAD INVOLVED IN KEEPING THE 5) RESPONSIVE THE 5) THREAD WILL NOT BE OCCUPIED BY THE EXECUTION OF THE BACKGROUND CALL 4HE .%4 &RAMEWORK OFFERS DIFFERENT WAYS OF RUNNING PARALLEL CODE AND THERE ARE ALSO DIFFERENT PATTERNS TO SATISFY THE REQUIREMENTS OF A PARALLEL PROGRAMMING APPROACH 70& PROVIDES A POWERFUL AND VERY EASY MECHANISM FOR RUNNING PARALLEL CODE THAT WILL KEEP THE 5) RESPONSIVE WITHOUT TOO MUCH EFFORT ON THE PROGRAMMING SIDE )N THIS CHAPTER WELL TAKE A LOOK AT SOME TECHNIQUES AND COMPONENTS THAT CAN BE USED WITH .%4 TO WRITE PARALLEL CODE AND ASYNCHRONOUS OPERATIONS (OWEVER THIS IS A HUGE TOPIC AND IF YOU PLAN TO LEARN ABOUT AND PROGRAM USING PARALLEL TECHNIQUES ) RECOMMEND YOU FOLLOW UP WITH A BOOK THAT FOCUSES MORE SPECIFICALLY ON THIS TOPIC LIKE !DAM &REEMANS 0RO .%4 0ARALLEL 0ROGRAMMING WITH # !PRESS ) ALSO SUGGEST YOU EXAMINE THE DEDICATED SECTION OF -3$. AT \bS]\XRa^b^UcR^\[XQaPah \b &" &'Pb_g
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
Writing Multithreading Code in .NET )N THE FIRST PART OF THIS CHAPTER WELL LOOK WHAT SOLUTIONS .%4 PROVIDES FOR WRITING AND EXECUTING MULTITHREADED OPERATIONS 4O EXECUTE THE CODE WE WILL CREATE A DEMO 70& PROJECT WITH 6ISUAL 3TUDIO CALLED h70&-ULTI4HREADING v WHICH WELL USE TO EXECUTE THE VARIOUS EXAMPLES 9OU CAN DOWNLOAD THE CODE FROM THE !PRESS WEB SITE ALONG WITH THE CODE RELATED TO THE 4IME4RACKER APPLICATION
4HE 4HREAD /BJECT 7HEN YOU TURN ON YOUR 0# AND START TO WORK IN YOUR OPERATING SYSTEM /3 YOU START TO EXECUTE PARALLEL PROCESSESAPPLICATIONSAND EACH ONE IS ON A DIFFERENT THREAD &OR EXAMPLE IF YOU HAVE 6ISUAL 3TUDIO AND .OTEPAD OPEN YOU ARE EXECUTING TWO DIFFERENT PROCESSES IN YOUR /3 EACH ON A DIFFERENT THREAD
■ .OTE 7INDOWS AND OPERATING SYSTEMS IN GENERAL ARE ABLE TO MULTITASK MEANING THEY ARE ABLE TO EXECUTE DIFFERENT PROCESSES AT THE SAME TIME 4HIS ABILITY GIVES USERS THE IMPRESSION THAT THE /3 IS RUNNING DIFFERENT APPLICATIONS IN PARALLEL WHICH IS NOT REALLY TRUE 4HE /3 HAS AN INTERNAL SCHEDULER THAT IS ABLE TO SPLIT THE TIME NEEDED BY EACH PROCESS TO BE EXECUTED BY GIVING PRIORITY TO ONE PROCESS INSTEAD OF ANOTHER )N THIS WAY THE /3 CAN SWITCH BETWEEN EXECUTING A PROCESS TO EXECUTING ANOTHER ONE BY GIVING A SPECIFIC LIMIT TO THE EXECUTION TIME
7HEN YOU WRITE # CODE USUALLY YOU EXECUTE ALL THE CODE WITHIN ONE SINGLE THREAD SO IF YOU PLAN TO EXECUTE THREE SEQUENTIAL STEPS EACH STEP HAS TO WAIT UNTIL THE PREVIOUS ONE IS COMPLETED 4HIS SEQUENTIAL EXECUTION CAN EASILY LOCK DOWN THE RESPONSIVENESS OF YOUR 5) WHEN THE SAME THREAD THAT IS RENDERING THE GRAPHIC INTERFACE IS ALSO IN CHARGE OF EXECUTING COMMANDS IN BACKGROUND )N SUCH SITUATIONS IT IS REALLY IMPORTANT TO KEEP THE 5) RESPONSIVE AND TO INFORM THE USER THAT A LONG RUNNING TASK IS EXECUTING AND THAT THE PROGRAM HAS NOT SIMPLY CRASHED 7HEN YOU WRITE AN APPLICATION WITH 70& YOU MAY ENCOUNTER THREE TYPES OF SITUATIONS WHERE YOU HAVE TO USE MULTITHREADING v
9OU WANT THE 5) TO REMAIN RESPONSIVE WHILE ANOTHER THREAD IS SENDING DATA TO THE 5)
v
9OU HAVE TO EXECUTE A LONG RUNNING TASK IN THE BACKGROUND AND NOTIFY ANOTHER THREAD WHEN THE PROCESS IS COMPLETED
v
9OU ARE WORKING WITH REMOTE COMPONENTS LIKE A 7#& SERVICE OR A .%4 2EMOTING OBJECT THAT MAY LOCK THE CURRENT THREAD WHILE EXECUTING DUE TO SOME LATENCY IN THE RESPONSE
)N THESE CASES YOU MIGHT CONSIDER MOVING THE CODE THAT IS LOCKING DOWN THE APPLICATION TO A DIFFERENT THREAD AND EXECUTE IT IN PARALLEL /UR DEMO 70& APPLICATION CREATES A SIMPLE 8!-, FX]S^f THAT CONTAINS A CTgc1^g AND A 1dcc^] THE BUTTON #LICK EVENT IS HANDLED IN THE CODE BEHIND FILE ,ISTING SHOWS THE 8!-, MARKUP USED TO CREATE IT
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
,ISTING 70& 7INDOW 4HAT 2UNS -ULTIPLE 4HREADS +FX]S^f g)2[Pbb,CWaTPS2[Pbb
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
_aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPSB[TT_"* cWXbcgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l l l 4HE ONLY CODE THATS NEW HERE IS THE B[TT_ METHOD EXPOSED BY THE CWaTPS CLASS THIS METHOD IS USED TO PAUSE THE CURRENTLY EXECUTING THREAD "Y EXECUTING THIS CODE YOU ARE TELLING THE OPERATING SYSTEM THAT YOU WANT TO PAUSE THE CURRENT THREAD WHICH IS ALSO THE THREAD THAT IS RENDERING THE USER INTERFACE &OR THIS REASON THE 5) BECOMES UNRESPONSIVE 9OU CAN HOWEVER CREATE A LONG RUNNING TASK AND EXECUTE IT WITHOUT LOCKING DOWN THE 5) THIS IS ACCOMPLISHED BY CREATING A NEW CWaTPS INSTANCE AND ASSOCIATING THE CODE OF THE LONG RUNNING TASK TO THIS THREAD OBJECT AND THEN UPDATING THE 5) WHEN THE OPERATION IS DONE SEE ,ISTING ,ISTING ,ONG 2UNNING 4ASK %XECUTED IN A $IFFERENT 4HREAD _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPS cWaTPS , ]Tf CWaTPS ,j CWaTPSB[TT_"* cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* cWaTPSBcPac* l /NE OF THE WAYS OF CREATING A NEW THREAD CLASS IS BY PASSING IN THE CONSTRUCTOR A DELEGATE THAT REPRESENTS THE BUTTON 4HE APPLICATION BECOMES RESPONSIVE RIGHT AWAY BUT AFTER SECONDS YOU GET AN ERROR SAYING THE THREAD THAT IS TRYING TO ACCESS THE CTgc1^g OBJECT IS NOT THE SAME THREAD USED BY 70& TO CREATE THE CTgc1^g AS &IGURE SHOWS
&IGURE %XCEPTION THROWN WHEN THE 5) THREAD IS NOT IN SYNC
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
4HE FIRST EASY WAY TO FIX THIS PROBLEM IS TO CALL THE 3Xb_PcRWTa CLASS EXPOSED BY THE 70& CTgc1^g 4HIS OBJECT AS YOU WILL SEE IN THE SECOND PART OF THE CHAPTER IS IN CHARGE OF DEALING WITH CONCURRENCY AND THREADING 4HE 70& DISPATCHER IS ABLE TO HANDLE MESSAGES BY AFFINITY AND IDENTIFY A MESSAGE THAT IS NOT COMING FROM THE SAME THREAD AS THE 5) AND DISPATCH IT TO THE 5) THREAD SO THAT IT CAN BE HANDLED PROPERLY ,ISTING MODIFIES THE WAY THE THREAD IS INTERACTING WITH THE CTgc1^g SO THE 5) WILL REMAIN RESPONSIVE WHILE RUNNING THE CODE IN A DIFFERENT THREAD ,ISTING 3AMPLE 5SING THE 70& 3Xb_PcRWTa /BJECT _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPS cWaTPS , ]Tf CWaTPS ,j CWaTPSB[TT_"* cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT]Tf 0RcX^] ,- cgc2daaT]cEP[dTCTgc ,« 7T[[^ F^a[S* l* cWaTPSBcPac* l 4HE 3Xb_PcRWTa OBJECT EXPOSED BY THE CTgc1^g CLASS AND ALSO BY ANY OTHER 70& COMPONENT HAS AN 8]e^ZT METHOD THAT CAN BE USED TO DISPATCH A MESSAGE A DELEGATE TO THE CORRECT 5) THREAD THE ONE USED BY THE CTgc1^g THAT IS CALLING THE 8]e^ZT METHOD -OST OBJECTS IN 70& DERIVE FROM 3Xb_PcRWTa>QYTRc WHICH PROVIDES THE BASIC CONSTRUCTS FOR DEALING WITH CONCURRENCY AND THREADING .OW IF YOU RUN THE PREVIOUS CODE YOU SHOULD HAVE A RESPONSIVE 5) AND AT THE SAME TIME YOU SHOULD BE ABLE TO PRINT THE h(ELLO 7ORLDv STRING IN THE CTgc1^g AFTER SECONDS
Interacting with the Thread class 4HE CWaTPS CLASS IS A COMPLEX OBJECT EXPOSED BY A SPECIFIC NAMESPACE THE BhbcT\CWaTPSX]V NAMESPACE WHICH PROVIDES FACILITIES AND COMPONENTS TO WORK WITH MULTITHREADING 7HEN YOU WORK WITH A MULTITHREADED APPLICATION THE MOST CHALLENGING TASKS ARE WHEN YOU NEED TO SYNCHRONIZE THE VARIOUS THREADS WAIT FOR A SPECIFIC THREAD OR JUST NEED TO KILL A RUNNING THREAD !S YOU SAW IN ,ISTING YOU CAN CREATE AND RUN A NEW THREAD SIMPLY BY CREATING A NEW INSTANCE OF THE CWaTPS CLASS AND THEN CALLING THE BcPac METHOD 4HE ONLY WAY TO STOP A RUNNING THREAD IS TO CALL THE 0Q^ac METHOD AS SHOWN IN ,ISTING ,ISTING 3TARTING AND 3TOPPING A 2UNNING 4HREAD _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPS cWaTPS , ]Tf CWaTPS ,- cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,-
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
j CWaTPSB[TT_"* cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* cWaTPSBcPac* cWaTPS0Q^ac* l )N THIS CASE AFTER THE CODE STARTS THE THREAD IT CALLS THE 0Q^ac METHOD WHICH WILL KILL THE EXECUTION AND THE THREAD SO THE cgc2daaT]cEP[dT 4EXT"OX WILL NOT BE SET USING THE ACTION DEFINED IN THE THREAD DECLARATION )N OTHER CASES YOU MIGHT NEED TO PAUSE THE THREAD FOR A WHILE AND THEN CONTINUE EXECUTION LIKE THE 0AUSE AND 0LAY COMMANDS ON A #$ PLAYER )N ORDER TO PAUSE AND CONTINUE THREAD EXECUTION THE CWaTPS CLASS EXPOSES TWO METHODS Bdb_T]S AND ATbd\T WHICH CAN BE USED AS SHOWN IN ,ISTING ,ISTING 0AUSE AND #ONTINUE A 4HREAD _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPS cWaTPS , ]Tf CWaTPS ,- cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j CWaTPSB[TT_"* cgc2daaT]cEP[dTCTgc , KaK]* l* cWaTPSBcPac* cWaTPSBdb_T]S* cWaTPSATbd\T* l 2EMEMBER THAT USING METHODS LIKE Bdb_T]S AND ATbd\T WILL PAUSE AND CONTINUE THE CALLING THREAD AND MAKE THE 5) LESS RESPONSIVE !LSO THESE TWO METHODS HAVE BEEN DEPRECATED SINCE .%4 AND ARE USED HERE JUST TO INTRODUCE THREADING CONCEPTS )F YOU PLAN TO WORK WITH THE CWaTPS CLASS YOU SHOULD CONSIDER HOW YOU WILL KEEP MULTIPLE THREADS SYNCHRONIZED HOW YOU WILL MANAGE THREAD AFFINITIES AND HOW YOU WILL SHARE RESOURCES AMONG DIFFERENT THREADS
!SYNCHRONOUS /PERATIONS 4HE CWaTPS OBJECT EXPOSED BY THE BhbcT\CWaTPSX]V NAMESPACE IS NOT THE ONLY OBJECT AVAILABLE IN 70& TO WRITE CODE THAT KEEPS THE 5) RESPONSIVE WHILE IT IS EXECUTED IN THE BACKGROUND )N THE PREVIOUS SECTION YOU SAW THAT ONE SOLUTION FOR KEEPING THE 5) RESPONSIVE IS TO EXECUTE THE REQUIRED CODE IN A SEPARATE PROCESS BY CREATING A NEW CWaTPS OBJECT 9OU ALSO SAW THAT THE USE OF THE CWaTPS OBJECT MAY BRING ADDITIONAL HASSLES LIKE THE NEED TO ARCHITECT A SYNCHRONIZATION MECHANISM AND ADDITIONAL THOUGHT ABOUT KEEPING THE 5) THREAD SYNCHRONIZED WITH THE BACKGROUND THREAD 9OU MAY ALSO HAVE ADDITIONAL THREADS RUNNING IN THE BACKGROUND AND EACH ONE MAY HAVE THE SAME ISSUE OF NEEDING TO BE SYNCHRONIZED WITH THE MAIN THREAD
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
!LSO IF YOU HAVE TO EXECUTE NOT ONE BACKGROUND PROCESS BUT LETS SAY OF THEM AND YOU PLAN TO EXECUTE EACH ONE IN PARALLEL BY CREATING AN ADDITIONAL THREAD YOU CAN EASILY END UP OVERTAXING THE #05 WHICH CANT HANDLE SO MANY PARALLEL PROCESSES AT THE SAME TIME AND HAVING THE ENTIRE SYSTEM UNRESPONSIVE !N ALTERNATIVE IS TO EXECUTE THE LONG RUNNING TASK OR THE CODE THAT IS BLOCKING THE 5) ASYNCHRONOUSLY 4HIS MEANS RUNNING THE CODE WITHOUT BLOCKING THE PRINCIPLE THREAD AND ADVISING THE MAIN THREAD OF THE COMPLETED EXECUTION USING A CALLBACK METHOD 4HE .%4 &RAMEWORK BY DESIGN ALLOWS YOU TO CALL ANY METHOD CREATED USING # OR 6".%4 ASYNCHRONOUSLY BUT IN ORDER TO DO THAT YOU NEED TO PROVIDE A DELEGATE TO DO IT 3UPPOSE THAT THE 70& FX]S^f CREATED IN ,ISTING IS EXECUTING FROM THE 1dcc^]2[XRZ EVENT A METHOD THAT WAITS FOR SECONDS AND THEN WRITES SOMETHING IN THE CTgc1^g AS ,ISTING SHOWS ,ISTING -ETHOD 4HAT 5PDATES THE CTgc1^g #ONTENT _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j cWXbD_SPcTCTgc1^g* l _aXePcT e^XS D_SPcTCTgc1^g j CWaTPSB[TT_"* cWXbcgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l )F YOU RUN THE DEMO APPLICATION USING THIS IMPLEMENTATION THE 5) WILL BE LOCKED DOWN UNTIL THE CTgc1^g CTgc PROPERTY IS SET TO h(ELLO 7ORLDv 7HAT YOU HAVE HERE IS THE SAME CODE INTRODUCED AT THE BEGINNING OF THE CHAPTER BUT REFACTORED USING A DIFFERENT APPROACH .OW WHAT WE NEED TO DO IN ORDER TO KEEP THE 5) RESPONSIVE WHILE THE D_SPcTCTgc1^g IS EXECUTING THE REMAINING CODE IS TO CALL IT ASYNCHRONOUSLY 4HE ASYNCHRONOUS PATTERN IN # REQUIRES TWO METHODS ONE CALLED 1TVX]8]e^ZT THAT IS USED TO INITIALIZE THE ASYNC PROCESS AND ONE CALLED 4]S8]e^ZT THAT IS CALLED WHEN THE ASYNC PROCESS IS COMPLETED 4HE 1TVX]8]e^ZT METHOD HAS TO RETURN AN 80bh]RATbd[c INTERFACE OBJECT THAT CAN BE USED TO MONITOR THE EXECUTION OF THE CORRESPONDING ASYNCHRONOUS METHOD ,ISTING SHOWS THE IMPLEMENTATION OF THIS PATTERN USING THE ,ISTING SAMPLE CODE ,ISTING !SYNCHRONOUS )MPLEMENTATION 0ATTERN IN # bcT_ RaTPcT cWT Pbh]R bXV]PcdaT _aXePcT ST[TVPcT bcaX]V 0bh]RD_SPcTCTgc1^g* _aXePcT 0bh]RD_SPcTCTgc1^g Pbh]R* bcT_ ! RaTPcT cWT RP[[QPRZ P]S cWT \TcW^S _aXePcT e^XS D_SPcT2P[[QPRZ80bh]RATbd[c aTbd[c j cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^],-
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
j cgc2daaT]cEP[dTCTgc , Pbh]R4]S8]e^ZTaTbd[c* l* l _aXePcT bcaX]V D_SPcTCTgc1^g j CWaTPSB[TT_"* aTcda] 7T[[^ F^a[S* l bcT_ " WP]S[T TeTahcWX]V _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j Pbh]R , ]Tf 0bh]RD_SPcTCTgc1^gD_SPcTCTgc1^g* 0bh]R2P[[QPRZ RP[[QPRZ , D_SPcT2P[[QPRZ* Pbh]R1TVX]8]e^ZTRP[[QPRZ]d[[* l 4HIS LISTING PERFORMS FOUR STEPS TO MAKE THE METHOD D_SPcTCTgc1^g ASYNCHRONOUS 1.
)T DECLARES A DELEGATE AND AN INSTANCE OF THIS DELEGATE WHICH NEEDS TO BE GLOBAL IN ORDER TO BE USED IN THE 4]S8]e^ZT METHOD TO HANDLE THE ASYNC PATTERN IMPLEMENTATION OFFERED BY THE DELEGATE OBJECT OF .%4 )N THIS CASE THE METHOD TAKES NO PARAMETERS AND RETURNS A STRING RESULT
2.
)T CREATES A DELEGATE CALLBACK THAT WILL EXECUTE ASYNCHRONOUSLY WHEN THE DELEGATE FROM STEP HAS COMPLETED AND THIS WILL POINT TO THE D_SPcTCTgc1^g METHOD
3.
)T ORCHESTRATES EVERYTHING IN THE 1dcc^]2[XRZ EVENT
4.
!T THE END 4]S8]e^ZT IS EXECUTED WHICH RETURNS THE VALUE FROM THE DELEGATE WHEN YOU PASS IN THE CORRECT 80bh]R OBJECT
The ThreadPool Component /NE OF MAJOR PROBLEMS OF IMPLEMENTING THE ASYNCHRONOUS PATTERN IN # IS THAT YOU MUST PROVIDE THE IMPLEMENTATION OF THE 1TVX]8]e^ZT AND 4]S8]e^ZT METHODS THIS FORCES YOU TO MAKE THE CALLBACK PUBLIC OR AT LEAST INTERNAL BECAUSE IT WILL BE PROBABLY SHARED ACROSS DIFFERENT CLASSES THE CALLING AND THE CALLED 9OU ALSO HAVE TO REMEMBER TO CALL THE 4]S8]e^ZT METHOD IN ORDER TO CLOSE THE ASYNCHRONOUS PROCESS 9OU CAN REDUCE THE CODE USED IN THE PREVIOUS LISTING BY USING THE ASYNCHRONOUS PATTERN IMPLEMENTATION OFFERED BY THE CWaTPS?^^[ OBJECT SHOWN IN ,ISTING THIS OBJECT SENDS THE CALLBACK IN THE BACKGROUND AND FORGETS IT x ,ISTING 4HREAD0OOL /BJECT _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CWaTPS?^^[@dTdTDbTaF^aZ8cT\RP[[QPRZ ,- D_SPcTCTgc1^g* l
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
_aXePcT e^XS D_SPcTCTgc1^g j CWaTPSB[TT_"* cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* l 4HE CWaTPS?^^[ COMPONENT OFFERS AN ALTERNATIVE APPROACH WHICH CAN EXECUTE ASYNCHRONOUS CODE IN A hCALL AND FORGETv MANNER )T MEANS YOU CAN USE THIS COMPONENT TO FIRE A METHOD AND THEN FORGET ABOUT IT 9OU ARE GUARANTEED THAT CWaTPS?^^[ WILL POOL THE METHOD AND EXECUTE IT AS SOON AS IT HAS A FREE THREAD AVAILABLE TO HANDLE IT 7HEN THE METHOD IS DONE THE 4]S8]e^ZT METHOD WILL BE CALLED AND EXECUTED BY THE CWaTPS?^^[ OBJECT GIVING YOU THE POWER OF FIRING AND FORGETTING THE ASYNCHRONOUS CODE
■ .OTE 9OU MAY WONDER AFTER READING THIS SECTION WHAT THE DIFFERENCE IS BETWEEN CREATING YOUR OWN THREAD AND RUNNING ASYNCHRONOUS CALLS IN A NEW THREAD USING THE CWaTPS?^^[ 4HE CWaTPS?^^[ IS ABLE TO HANDLE THE RESOURCES OF YOUR MACHINE AND DOES NOT OVERLOAD THE #05 AS YOU MIGHT DO BY SIMPLY CREATING AND FIRING PARALLEL THREADS USING THE CWaTPS OBJECT /N THE OTHER HAND IF YOU HAVE A LONG RUNNING TASK THAT SHOULD BE CONTROLLED AND MONITORED PAUSE KILL MONITOR OR YOU HAVE TO CREATE AN ASYNCHRONOUS THREAD THAT WILL LOCK THE PROGRAM EXECUTION NOT THE 5) BUT THE LOGICAL EXECUTION YOU MAY WELL FIND A BETTER SOLUTION IN USING THE CWaTPS OBJECT WHICH OFFERS A GREATER DEGREE OF CONTROL
4HE 4ASK /BJECT 4HE LAST .%4 COMPONENT FOR RUNNING MULTITHREADED CODE IS PROVIDED BY THE BhbcT\CWaTPSX]VCPbZb NAMESPACE A NEW SECTION OF THE BhbcT\CWaTPSX]V NAMESPACE INTRODUCED BY .%4 !T FIRST THE 4ASK 0ARALLEL ,IBRARY WAS AN EXPERIMENTAL PROJECT CONDUCTED BY -ICROSOFT 2ESEARCH A SPECIAL DEPARTMENT OF -ICROSOFT THAT EXPERIMENTS WITH NEW TECHNOLOGIES AND PATTERNS 7ITH THE RELEASE OF .%4 -ICROSOFT DECIDED TO INCLUDE THE 4ASK 0ARALLEL ,IBRARY IN THE CORE OF THE .%4 4HE MAIN DIFFERENCE BETWEEN USING THE CWaTPS OBJECT AND THE CPbZ OBJECT AVAILABLE IN THE BhbcT\CWaTPSX]VCPbZb NAMESPACE IS THAT THE CPbZ OBJECT IS CONTROLLED BY THE CWaTPS?^^[ OBJECT SO THE CONCURRENCY AND THE OVERALL USE OF RESOURCES RESULT IN A BETTER PRODUCT 4HE CPbZ OBJECT ALSO ALLOWS YOU TO v
3CHEDULE A TASKS EXECUTION
v
#HAIN TOGETHER CHILD AND PARENT TASKS RESULTING IN A SORT OF PARALLEL WORKFLOW
v
(ANDLE EXCEPTIONS AND PROPAGATE THEM TO THE PARENT AND CHILD TASKS
)F YOU WANT TO CREATE A TASK AND RUN IT YOU CAN USE THE STATIC OBJECT CPbZ5PRc^ah EXPOSED IN THE BhbcT\CWaTPSX]VCPbZb NAMESPACE OR YOU CAN CREATE A NEW INSTANCE OF THE CPbZ OBJECT AND THEN START IT AS SHOWN IN ,ISTING
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
,ISTING #REATING A .EW 4ASK AND 3TARTING )T _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CPbZ cPbZ , ]Tf CPbZD_SPcTCTgc1^g* cPbZBcPac* l _aXePcT e^XS D_SPcTCTgc1^g j CWaTPSB[TT_"* cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* l "ECAUSE THE CPbZ CONSTRUCTOR REQUIRES AN ACTION OBJECT INSTEAD OF CREATING A SEPARATE DELEGATE FOR THE ACTION AND THEN ASSOCIATING IT TO THE PARAMETER OF THE CPbZ CONSTRUCTOR YOU CAN EASILY CREATE A DELEGATE ON THE FLY BY USING A LAMBDA EXPRESSION SEE ,ISTING ,ISTING CPbZ /BJECT 5SING ,AMBA AND AN !NONYMOUS $ELEGATE _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j CPbZ cPbZ , ]Tf CPbZ ,j CWaTPSB[TT_"* cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* l* cPbZBcPac* l 4HE CPbZ OBJECT AND THE 4ASK 0ARALLEL ,IBRARY OFFER A NUMBER OF ADDITIONAL METHODS AND FACILITIES THAT YOU CAN USE TO BETTER CONTROL YOUR ASYNCHRONOUS CODE AS SHOWN IN ,ISTING &OR EXAMPLE YOU CAN EXECUTE ADDITIONAL CODE WHEN THE ASYNCHRONOUS CODE HAS COMPLETED AND YOU CAN ALSO SPECIFY CODE TO BE EXECUTED IF AN EXCEPTION IS RAISED USING THE FLUENT INTERFACE LANGUAGE APPROACH ,ISTING !DDITIONAL -ETHODS !VAILABLE ON THE CPbZ /BJECT _aXePcT e^XS 1dcc^]2[XRZ^QYTRc bT]STa A^dcTS4eT]c0aVb T j ]Tf CPbZ ,j CWaTPSB[TT_"*
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , 7T[[^ F^a[S* l* l 2^]cX]dTFXcW_Pa\ ,j cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , KaK]2^\_[TcTS* l* l BcPac* l 4HIS LISTING CREATES A NEW CPbZ THEN SPECIFIES THE CODE TO BE EXECUTED ASYNCHRONOUSLY AND THE CODE TO BE EXECUTED AT THE END USING THE 2^]cX]dTFXcW METHOD AND FINALLY IT STARTS THE CODE 4HE 2^]cX]dTFXcW METHOD ALLOWS YOU TO PASS IN THE PREVIOUS CPbZ SO THAT YOU CAN WORK WITH IT IF YOU NEED TO 4HE 4ASK 0ARALLEL ,IBRARY IS A NEW COMPONENT OF .%4 AND THOUGH IT IS VERY POWERFUL IT IS EASY TO USE )F YOU WANT TO LEARN MORE ABOUT IT -3$. HAS A 0ARALLEL #OMPUTING SECTION AVAILABLE AT \bS]\XRa^b^UcR^\T]dbR^]RdaaT]Rh &OR A DEEPER LOOK AT THE CPbZ OBJECT AND THE 0ARALLEL &RAMEWORK SEE \bS]\XRa^b^UcR^\T]db[XQaPahSS#%%("Pb_g
4HE "ACKGROUND 7ORKER #OMPONENT 4HE .%4 &RAMEWORK SINCE VERSION EXPOSES A COMPONENT CALLED 1PRZVa^d]SF^aZTa IN THE BhbcT\2^\_^]T]c<^ST[ NAMESPACE THAT IS IN CHARGE OF RUNNING AND CONTROLLING ASYNCHRONOUS CODE WITHOUT LOCKING THE USER INTERFACE )TS PURPOSE IS SPECIFIC TO THIS ROLE AND IT SHOULD ONLY BE USED IN A 70& OR 7INDOWS &ORMS APPLICATION 4HE CONCEPT OF THE 1PRZVa^d]SF^aZTa COMPONENT IS PRETTY STRAIGHTFORWARD IT EXPOSES EVENTS AND METHODS YOU CAN USE TO RUN A SPECIFIC SET OF COMMANDS ASYNCHRONOUSLY AND GET UPDATES AND FEEDBACK FROM THEM WITHOUT LOCKING THE 5) )N ORDER TO ADD A NEW 1PRZVa^d]SF^aZTa COMPONENT TO YOUR 70& WINDOW YOU HAVE TO CREATE ONE IN THE CODE BEHIND FILE $ESIGN SUPPORT FOR THIS COMPONENT IN 6ISUAL 3TUDIO IS ENABLED ONLY WITH THE 7INDOWS &ORMS TECHNOLOGY ,ISTING SHOWS HOW TO DECLARE A NEW 1PRZVa^d]SF^aZTa COMPONENT IN A 70& WINDOW ,ISTING #REATING A 1PRZVa^d]SF^aZTa #OMPONENT dbX]V dbX]V dbX]V dbX]V
BhbcT\* BhbcT\2^\_^]T]c<^ST[* BhbcT\CWaTPSX]V* BhbcT\FX]S^fb*
]P\Tb_PRT CWaTPS2[Pbb j _dQ[XR _PacXP[ R[Pbb
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
_aXePcT 1PRZVa^d]SF^aZTa f^aZTa* _dQ[XR
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
4HE EVENT HANDLER FOR THE 3^F^aZ EVENT CARRIES WITH IT A A^dcTS4eT]c0aVb ARGUMENT TYPE THAT CAN BE USED TO CARRY PARAMETERS IN THE METHOD &OR EXAMPLE YOU CAN CALL Ad]F^aZTa0bh]R BY PASSING IN A CUSTOM STRING AND READ THE STRING IN THE 3^F^aZ EVENT HANDLER AS THE 0aVd\T]c PROPERTY OF 3^F^aZ4eT]c0aVb AS IN ,ISTING ,ISTING 0ASSING 0ARAMETERS IN THE "ACKGROUND7ORKER aTPSX]V cWT _PaP\TcTa X] cWT 3^F^aZ TeT]c WP]S[Ta \TcW^S cgc2daaT]cEP[dT3Xb_PcRWTa8]e^ZT ]Tf 0RcX^] ,j cgc2daaT]cEP[dTCTgc , T0aVd\T]cC^BcaX]V* l* _PbbX]V cWT _PaP\TcTa eP[dT Ua^\ cWT Ad]F^aZTa0bh]R \TcW^S f^aZTaAd]F^aZTa0bh]R7T[[^ F^a[S* &INALLY THE 1PRZVa^d]SF^aZTa ALSO ALLOWS YOU TO INTERACT WITH IT &OR EXAMPLE YOU MAY NEED TO SEND NOTIFICATIONS BACK TO THE 5) WHILE YOU ARE EXECUTING THE LONG RUNNING TASK )N THIS CASE THE 1PRZVa^d]SF^aZTa EXPOSES SOME EVENTS YOU CAN USE TO TRACK THE EXECUTION PROCESS OF THE WORKER v
Ad]F^aZTa2^\_[TcTS IS RAISED WHEN ALL THE CODE IN THE 3^F^aZ EVENT HANDLER IS SUCCESSFULLY EXECUTED
v
Ad]F^aZTa2^\_[TcTS4eT]c0aVb PROVIDES DATA FOR THE
v
?a^VaTbb2WP]VTS IS RAISED EVERY TIME THE AT_^ac?a^VaTbb METHOD IS CALLED IN THE WORKER THIS EVENT CARRIES WITH IT THE PROGRESS hAMOUNTv AS AN X]c VALUE THAT IS PASSED USING THE ?a^VaTbb2WP]VTS4eT]c0aVb ARGUMENT
)F YOU NEED TO CANCEL A PROCESS THAT IS RUNNING ASYNCHRONOUSLY USING THIS COMPONENT YOU HAVE TO CALL THE 2P]RT[0bh]R METHOD EXPOSED BY THE 1PRZVa^d]SF^aZTa INSTANCE
Summary 4HIS CHAPTER PROVIDES A BRIEF OVERVIEW OF THE DIFFERENT TECHNIQUES THAT CAN BE USED TO RUN ASYNCHRONOUS CODE IN A 70& APPLICATION USING # 4HE .%4 &RAMEWORK SUPPLIES A COMPLETE SET OF COMPONENTS AND FACILITIES AVAILABLE IN THE BhbcT\CWaTPSX]V NAMESPACE THAT CAN BE USED TO CREATE PARALLEL CODE AND ASYNCHRONOUS OPERATIONS AND TO MONITOR THEM 4HE FIRST COMPONENT YOU SAW WAS THE CWaTPS OBJECT A POWERFUL OBJECT USED TO CREATE NEW THREADS THAT WILL RUN AS ISOLATED PARALLEL PROCESSES "EFORE USING THIS OBJECT TO RUN PARALLEL CODE YOULL NEED TO CONSIDER SOME MAJOR ARCHITECTURAL ISSUES LIKE THREAD SYNCHRONIZATION AND THREAD RESOURCES SHARING !NOTHER WAY OF RUNNING CODE THAT DOESNT BLOCK THE MAIN PROCESS OF THE APPLICATION IS TO USE THE ASYNCHRONOUS PATTERN OFFERED BY THE .%4 &RAMEWORK DELEGATE OBJECT !NY DELEGATE IN # CAN BE CALLED ASYNCHRONOUSLY USING THE 1TVX]8]e^ZT AND 4]S8]e^ZT METHODS EXPOSED BY THE DELEGATE OBJECT 7ITH THIS APPROACH ANY CODE WILL RUN ASYNCHRONOUSLY IN A SEPARATE THREAD WITHOUT BLOCKING THE CURRENT 5) AND WILL CALL A CALLBACK METHOD WHEN THE ASYNCHRONOUS EXECUTION IS COMPLETED !N EASIER WAY TO EXECUTE ASYNC CODE IS TO USE THE CWaTPS?^^[ OBJECT A COMPONENT EXPOSED IN THE BhbcT\CWaTPSX]V NAMESPACE THAT CAN BE USED TO hCALL AND FORGETv PARALLEL CODE 5NFORTUNATELY THIS COMPONENT IS EASIER TO USE BUT PROVIDES LESS CONTROL SO IT SHOULDNT BE USED FOR COMPLEX SOLUTIONS
#(!04%2 ■ 70& !.$ -5,4)4(2%!$).'
9OU ALSO HAD A LOOK AT THE NEW CPbZ THAT IS PART OF THE 0ARALLEL ,IBRARY SECTION OF THE BhbcT\CWaTPSX]VCPbZb NAMESPACE INTRODUCED IN .%4 AND YOU SAW A NEW THREAD OBJECT HANDLER CALLED CPbZ THAT ALLOWS YOU TO WRITE MULTITHREADED CODE WITH THE SAME CONTROL AS THE CWaTPS OBJECT BUT WITH A MORE UNDERSTANDABLE SYNTAX AND EASIER MANAGEMENT PROVIDED BY THE CWaTPS?^^[ COMPONENT &INALLY YOU SAW THAT THE 1PRZVa^d]SF^aZTa COMPONENT CAN BE USED IN 70& AND IT IS THE EASIEST WAY TO RUN ASYNCHRONOUS CODE IN A 70& WINDOW WITHOUT THE NEED TO USE ANY OTHER MORE COMPLICATED MULTITHREADING APPROACH
CHAPTER ■■■
Interacting with WCF $URING THE DEVELOPMENT OF THE 4IME4RACKER APPLICATION YOU HAVE SEEN HOW TO CREATE A LAYERED 70& APPLICATION AND HOW TO SEPARATE THE CODE INTO DIFFERENT COMPONENTS AND LAYERS +EEP IN MIND THOUGH THAT YOU STILL HAVE TO v
EXPOSE ALL THE COMPONENTS TO THE CLIENT COMPUTER
v
REDISTRIBUTE THE COMPONENTS EVERY TIME YOU RELEASE A NEW VERSION
)N #HAPTER YOU SAW HOW EASY THE PROCESS OF REDISTRIBUTING UPDATED COMPONENTS CAN BE BUT IN THE END YOU STILL HAVE TO ENSURE THAT EVERY CLIENT IS USING THE LATEST VERSION AND YOU ARE STILL hEXPOSINGv THE COMPONENTS TO THE CLIENT SIDE 9OU ALSO NEED TO CONSIDER THE DISTRIBUTION OF THE DOMAIN LAYER OR ANY OTHER OF 4IME4RACKERS LAYERS IN A DIFFERENT APPLICATION ,ETS SAY THAT THE ABSTRACT DATA LAYER YOU BUILT FOR 4IME4RACKER IS TO BE REUSED BY A NEW 70& APPLICATION NOW YOU HAVE TWO DIFFERENT APPLICATIONS THAT NEED TO BE REDISTRIBUTED EVERY TIME YOU MODIFY THE $!, ASSEMBLY BECAUSE IT IS SHARED BETWEEN THEM !N ALTERNATIVE WAY OF REDISTRIBUTING COMPONENTS IS TO USE 3ERVICE /RIENTED !RCHITECTURE 3/! )N THE 3/! WORLD COMMUNICATION BETWEEN SERVICES IS ACCOMPLISHED USING 8-, MESSAGES THAT ARE SENT AND RECEIVED BY THE CLIENT AND THE SERVER 5SING 8-, MESSAGES ENCODED WITH A STANDARD COMMUNICATION PROTOCOL SUCH AS 3IMPLE /BJECT !CCESS 0ROTOCOL 3/!0 YOU CAN REDISTRIBUTE YOUR COMPONENTS NOT ONLY TO VARIOUS 70& APPLICATIONS BUT ALSO TO OTHER TECHNOLOGY PLATFORMS LIKE *AVA 4HE BASIC PLAYERS IN 3/! ARE v
!N ENDPOINT THE ENTRY POINT TO ACCESS THE ARCHITECTURE
v
/NE OR MORE SERVICES THAT EXPOSE METHODS FOR THE CLIENT TO USE
v
-ESSAGES THAT ARE EXCHANGED BETWEEN THE CLIENT AND THE SERVER SOMETIMES ALSO KNOWN AS HOST AND CONSUMER
)N EARLY -ICROSOFT ALONG WITH )"- AND SOME OTHER COMPANIES INTRODUCED A WEB TECHNOLOGY KNOWN AS WEB SERVICES THAT HAS BEEN ADOPTED IN THE WEB COMMUNITY 4HIS TECHNOLOGY STILL ALIVE IS USED TO DISTRIBUTE MESSAGES THROUGHOUT THE 7EB USING THE 3/!0 COMMUNICATION PROTOCOL 4HE BIG DIFFERENCE WITH THIS APPROACH IS THAT WEB SERVICES INTERACT WITH THE 7EB PROGRAMMATICALLY IN CONTRAST TO THE OLD APPROACH THAT TYPICALLY INVOLVED A USER AT A TERMINAL )N WITH VERSION OF THE .%4 &RAMEWORK -ICROSOFT INTRODUCED 7INDOWS #OMMUNICATION &OUNDATION 7#& A NEW WEB TECHNOLOGY MORE POWERFUL THAN THE STANDARD WEB SERVICES 4HIS TECHNOLOGY IS A FRAMEWORK FOR BUILDING SERVICE ORIENTED APPLICATIONS AND IT IS AN ENTIRE SET OF SERVICES FOR CREATING 3/! APPLICATIONS
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
)N THIS CHAPTER WE ARE GOING TO EXPLORE THIS TECHNOLOGY FIRST WITH AN OVERVIEW OF HOW IT WORKS AND THEN WELL SEE HOW EASY IT CAN BE USED TO TRANSFORM THE 4IME4RACKER 70& APPLICATION INTO A DISTRIBUTED SERVICE APPLICATION
Introduction to WCF 7INDOWS #OMMUNICATION &OUNDATION 7#& IS HOSTED ON )NTERNET )NFORMATION 3ERVICES ))3 AND IT USES THE 3/!0 PROTOCOL AMONG OTHER PROTOCOLS THAT WE WONT LOOK AT IN THIS CHAPTER TO DISTRIBUTE MESSAGES BETWEEN THE ENDPOINTS 7#& CAN BE USED FOR DIFFERENT PURPOSES )T CAN BE USED TO SEND AND RECEIVE SIMPLE DATA OR A COMPLEX GRAPH REPRESENTED BY AN 8-, STRUCTURE IT CAN BE USED TO ENABLE LIVE COMMUNICATIONS AMONG VARIOUS CLIENTS OR IT CAN BE USED SIMPLY TO SHARE A BUSINESS LAYER WITHOUT THE TYPICAL DISTRIBUTION PROBLEMS BECAUSE IT DOESNT REQUIRE A REDISTRIBUTION PROCESS LIKE THE TYPICAL .%4 ASSEMBLY DLL REQUIRES 7#& OUT OF THE BOX OFFERS THE FOLLOWING FEATURES v
3ERVICE ORIENTATION 7#& LETS YOU CREATE SERVICE ORIENTED APPLICATIONS THAT OFFER AN EASY WAY TO IMPLEMENT LOOSELY COUPLED SERVICES THAT CAN BE USED BY DIFFERENT TECHNOLOGIES AND PLATFORMS
v
-ULTIPLE MESSAGES PATTERNS 7ITH 7#& YOU CAN SEND AND RECEIVE MESSAGES IN DIFFERENT WAYS &OR EXAMPLE YOU MAY HAVE A SERVICE THAT SENDS AND RECEIVES MESSAGES OR A SERVICE THAT JUST RECEIVES MESSAGES AND PROCESSES THEM
v
3TANDARD METADATA 7#& IS DESIGNED TO USE COMMON COMMUNICATION METADATA STANDARDS BEYOND THOSE USED BY THE .%4 &RAMEWORK 7#& SUPPORTS 73$, 8-, AND 73 POLICY WHICH CAN ALSO BE USED BY NON .%4 TECHNOLOGIES
v
$ATA CONTRACTS 7#& PROVIDES A METHOD OF ENCAPSULATING INTO SERIALIZABLE CONTRACT OBJECTS THE INFORMATION EXCHANGED BETWEEN THE MESSAGES AND THE SERVICES
v
2%34 SUPPORT 7ITH 7#& YOU CAN CREATE PLAIN 8-, MESSAGES THAT ARENT WRAPPED IN A 3/!0 ENVELOPE THAT CAN BE ACCESSED FROM ANY TYPE OF DEVICE
7#& IS NOT ONLY A TECHNOLOGY FOR IMPLEMENTING 3/!0 OR PLAIN 8-, MESSAGES )N .%4 THERE ARE OTHER TECHNOLOGIES LIKE 7INDOWS 7ORKFLOW &OUNDATION 77& OR 7& OR "IZ4ALK OR 7INDOWS !ZURE !PP&ABRIC THAT RELY ON THIS POWERFUL 3/! TOOL )N THIS CHAPTER YOULL SEE HOW TO INTEGRATE %NTITY &RAMEWORK AND 70& WITH 7#& BUT YOU MAY ALSO WANT TO LOOK AT HOW 7#& WORKS IN CONJUNCTION WITH v
7INDOWS 7ORKFLOW &OUNDATION \bS]\XRa^b^UcR^\[XQaPahQQ!%%&(Pb_g
v
"IZ4ALK \bS]\XRa^b^UcR^\[XQaPahQQ(&"! $Pb_g
v
7INDOWS !ZURE !PP&ABRIC fff\XRa^b^UcR^\fX]S^fbPidaT0__5PQaXR>eTaeXTfSTUPd[cPb_g
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
!N ENDPOINT CONSISTS OF THREE COMPONENTS v
4HE ADDRESS OF AN ENDPOINT IS USUALLY A PHYSICAL NETWORK ADDRESS OR A 52) OBJECT )N COMPLEX SCENARIOS THE ADDRESS CARRIES ADDITIONAL INFORMATION STORED IN THE HEADERS AND IN THE IDENTITY
v
4HE BINDING IDENTIFIES THE SERVICE METADATA TYPE )N THE BINDING ELEMENT YOU SPECIFY HOW THE DATA WILL BE TRANSPORTED TO THE EXTERNAL WORLD 4#0 (440 ETC AND HOW THE COMMUNICATION FOR THAT SPECIFIC BINDING WILL BE ORCHESTRATED
v
4HE DESCRIPTION IS HANDLED BY THE 2^]caPRc3TbRaX_cX^] CLASS IN WHICH YOU DESCRIBE THE SERVICE OPERATIONS AND THE SERVICE MESSAGES 7ITH THE BEHAVIORS COLLECTION YOU CAN EXTEND THE SERVICE STRUCTURE AND BEHAVIOR BY OVERRIDING THE DEFAULT SETTINGS
The Service and the Client 4HE SERVICE IN ORDER TO BE VISIBLE HAS TO BE EXPOSED BY AN ENDPOINT WHICH DESCRIBES THE WAY THE MESSAGES WILL BE SENT AND THE WAY THE SERVICE WILL BEHAVE /F COURSE AN ENDPOINT NEEDS A HOST SOMETHING THAT CAN MAKE THE ENDPOINT VISIBLE AND ACCESSIBLE TO THE EXTERNAL WORLD )N THE BTaeXRT3TbRaX_cX^] CLASS YOU CAN DESCRIBE THE ENDPOINTS THAT WILL BE USED BY THE SERVICE AND THE SERVICE BEHAVIORS 4HIS INFORMATION CAN BE MANUALLY ADDED TO A SERVICE USING THE beRdcX[TgT .%4 UTILITY OR BY CHANGING THE STRUCTURE OF THE CONFIGURATION FILE OF THE APPLICATION THAT IS HOSTING THE 7#& SERVICE )F YOU DONT SUPPLY THIS INFORMATION THE 7#& RUNTIME WILL DO IT WITH SOME DEFAULT VALUES )N THE SAME WAY THE 2WP]]T[BTaeXRT CLASS IS USED TO DESCRIBE ENDPOINTS BEHAVIORS AND TYPES USED AND HOSTED BY THE CLIENT 7#& COMPONENT 4HE MAIN DIFFERENCE IS THAT A BTaeXRT CAN HOST MORE THAN ONE ENDPOINT CHANNEL WHILE A 2WP]]T[BTaeXRT IS ABLE TO EXPOSE ONLY ONE ENDPOINT THAT WILL BE USED FOR THE COMMUNICATION 9OULL FIND MORE INFORMATION ABOUT SERVICE AND CHANNELS AT \bS]\XRa^b^UcR^\[XQaPah \b&!('#Pb_g
A Sample WCF Project .OW LETS CREATE A SIMPLE 7#& APPLICATION IN ORDER TO BETTER UNDERSTAND HOW TO WORK WITH THIS TECHNOLOGY /PEN 6ISUAL 3TUDIO AND SELECT .EW 0ROJECT ➤ 6ISUAL # ➤ 7#& 9OULL SEE A DIALOG LIKE THE ONE IN &IGURE
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
&IGURE 7#& PROJECT TYPES IN 6ISUAL 3TUDIO 9OU CAN CHOOSE FROM FOUR DIFFERENT PROJECT TYPES v
7#& 3ERVICE ,IBRARY 7ITH THIS PROJECT TYPE YOU CAN CREATE AN ISOLATED ASSEMBLY DLL THAT CONTAINS A SET OF 7#& SERVICES THAT CAN BE HOSTED IN A 7#& WEB APPLICATION OR IN A 7INDOWS 3ERVICE
v
7#& 3ERVICE !PPLICATION 4HIS TEMPLATE CREATES A NEW !30.%4 WEB APPLICATION THAT ALLOWS YOU TO HOST AND DISTRIBUTE 7#& SERVICES WITHIN THE PROJECT OR IN AN EXTERNAL 7#& SERVICE LIBRARY PROJECT
v
7#& 7ORKFLOW 3ERVICE !PPLICATION 7ITH THIS PROJECT TYPE YOU CAN CREATE NEW 7#& SERVICES THAT CAN INTERACT WITH 7INDOW 7ORKFLOW &OUNDATION 7&
v
3YNDICATION 3ERVICE ,IBRARY 4HIS TEMPLATE CREATES SERVICES TO HOST SYNDICATION FEEDS IN STANDARD FORMATS LIKE 233 !4/- AND SO ON
)N THE DIALOG BOX SELECT THE SECOND OPTION 7#& 3ERVICE !PPLICATION .AME THE PROJECT 3AMPLE!PPLICATION AND PRESS /+ 6ISUAL 3TUDIO WILL CREATE A NEW !30.%4 WEB SITE TO HOST BY DEFAULT 7#& SERVICES 4AKE A LOOK AT THE PROJECT STRUCTURE THAT THE 7#& APPLICATION TEMPLATE CREATES YOULL SEE THREE FILES IN THE ROOT FOLDER v
8BTaeXRT Rb REPRESENTS THE BTaeXRT2^]caPRc INFORMATION USED BY 7#& TO HOST THE SERVICE
v
BTaeXRT beR IMPLEMENTS THE 8BTaeXRT INTERFACE AND CONTAINS THE CONCRETE IMPLEMENTATION OF THE SERVICE CONTRACT INTERFACE
v
FTQR^]UXV DESCRIBES THE ENDPOINT AND THE BEHAVIORS AVAILABLE IN THIS 7#& WEB APPLICATION
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
4HE 7#& 4EST #LIENT 5TILITY .OW IF YOU PRESS & 6ISUAL 3TUDIO WILL BUILD THE PROJECT AND OPEN THE BROWSER POINTING TO THE PROJECTS DEFAULT SERVICE ADDRESS USING A WEB ADDRESS LIKE Wcc_)[^RP[W^bc) %&BTaeXRT beR WHICH WILL SHOW AN INFORMATION PAGE USED BY 7#& TO DESCRIBE THE DEFAULT BEHAVIOR OF A 7#& SERVICE )F YOUR BROWSER IS NOT SHOWING THIS PAGE YOU CAN CHANGE THE BEHAVIOR OF THE WEB APPLICATION BY RIGHT CLICKING THE 7#& PROJECT AND SELECTING ?a^_TacXTb THEN CHANGING THE SETTINGS AS SHOWN IN &IGURE
&IGURE 7EB SETTINGS FOR A 7#& PROJECT )F YOU PRESS & AGAIN YOU SHOULD SEE A NEW APPLICATION CALLED F25CTbc2[XT]c WHICH IS ALSO AVAILABLE FROM THE .%4 BY TYPING F25CTbc2[XT]cTgT IN THE COMMAND PROMPT WINDOW 7ITHIN THIS 7#& CLIENT APPLICATION YOU CAN RUN AND TEST YOUR SERVICES WITHOUT HAVING TO CREATE A CUSTOM CLIENT APPLICATION &IGURE SHOWS YOU HOW THE F25CTbc2[XT]c 5) SHOULD APPEAR AFTER YOU RUN THE BP\_[T0__[XRPcX^] PROJECT FOR THE FIRST TIME
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
&IGURE 4HE F25CTbc2[XT]cTgT APPLICATION RUNNING IN A 7#& PROJECT )F YOU DOUBLE CLICK ONE OF THE METHODS EXPOSED BY THE DEFAULT ENDPOINT AND AVAILABLE IN THE LEFT SIDE TREEVIEW THE TEST CLIENT APPLICATION WILL CREATE A NEW TAB ON THE RIGHT THAT ALLOWS YOU TO INVOKE THE METHOD AND SEE THE RESULTS &OR EXAMPLE YOU CAN ENTER A NUMBER LIKE AND THE SERVICE WILL RETURN THE MESSAGE h9OU ENTERED v 9OU CAN USE THIS 5) TO TEST THE MAJOR PARTS OF YOUR SERVICES /F COURSE YOU CANT USE THIS TO TEST COMPLEX 5) SERVICES WHERE ONE METHOD REQUIRES A COMPLEX 3PcP2^]caPRc AS AN INPUT PARAMETER FOR EXAMPLE
#USTOMIZE THE 3AMPLE 3ERVICE .OW YOU HAVE A SERVICE THAT RUNS AND THAT EXPOSES TWO DEFAULT METHODS SO ITS TIME TO CREATE YOUR OWN SERVICE AND TRY TO EXPOSE SOMETHING FOR EXAMPLE A SMALL OBJECT THAT INCLUDES SOME DATA 4O START DELETE THE EXISTING 8BTaeXRT Rb AND BTaeXRT beR FILES FROM THE CURRENT PROJECT THEN SELECT !DD .EW )TEM FROM THE PROJECT CONTEXT MENU AND CHOOSE TO CREATE A NEW 7#& 3ERVICE FROM THE 7EB SECTION .AME THE SERVICE ?Tab^]BTaeXRTbeR AND PRESS /+ 4HE CONFIGURATION FILE HAS NOT BEEN TOUCHED YET BECAUSE WITH 7#& YOU CAN USE THE DEFAULT CONFIGURATION THAT 6ISUAL 3TUDIO PROVIDES WHICH ALLOWS US TO RUN THE SERVICE 9OU SHOULD NOW HAVE TWO NEW FILES IN THE ROOT FOLDER OF YOUR 7#& APPLICATION ONE NAMED 8?Tab^]BTaeXRTRb AND ONE NAMED ?Tab^]BTaeXRTbeR 4HE FIRST WILL CONTAIN THE CONTRACTS METHODS YOULL EXPOSE WITH 7#& AND THE SECOND WILL CONTAIN THE CONCRETE IMPLEMENTATION OF THESE CONTRACTS "EFORE EVEN STARTING TO WRITE AND IMPLEMENT THE SERVICES METHODS YOU NEED TO CREATE A NEW OBJECT THAT WILL BE SERIALIZED TRANSLATED INTO AN UNDERSTANDABLE 8-, MESSAGE BY THIS SERVICE )N THE PROJECT ROOT CREATE A NEW FOLDER AND CALL IT 3ERVICES THEN CREATE ANOTHER ROOT FOLDER AND CALL IT $ATA#ONTRACTS .OW MOVE THE 8?Tab^]BTaeXRTRb AND ?Tab^]BTaeXRTbeR FILES INTO THE 3ERVICES FOLDER AND ADD A NEW CLASS CALLED ?Tab^]2^]caPRc TO THE $ATA#ONTRACTS FOLDER 4HE SOLUTION SHOULD LOOK LIKE THE ONE IN &IGURE
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
&IGURE 4HE BP\_[T0__[XRPcX^] PROJECT STRUCTURE
Working with DataContracts )F YOU PLAN TO SHARE AN OBJECT BETWEEN THE SERVICE AND THE CLIENT LIKE THE ?Tab^]2^]caPRc CLASS YOU NEED TO EXPLAIN TO 7#& HOW THIS OBJECT SHOULD BE TRANSLATED INTO AN 8-, MESSAGE AND MORE PRECISELY HOW IT SHOULD BE SERIALIZED &OR EXAMPLE THE ?Tab^]2^]caPRc CLASS MAY EXPOSE SOME PROPERTIES AND BEHAVIORS THAT CANT BE SERIALIZED WHILE IT MAY HAVE SOME PROPERTIES THAT MUST BE SHARED BETWEEN SERVICE AND CLIENT ,ISTING SHOWS THE FINAL ?Tab^]2^]caPRc FILE ,ISTING 4HE ?Tab^]2^]caPRc #LASS dbX]V BhbcT\Ad]cX\TBTaXP[XiPcX^]* ]P\Tb_PRT BP\_[T0__[XRPcX^]3PcP2^]caPRcb j J3PcP2^]caPRcL _dQ[XR R[Pbb ?Tab^]2^]caPRc j _dQ[XR X]c 83 j VTc* bTc* l J3PcP
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
,ISTING HAS A CLASS CALLED ?Tab^]2^]caPRc THATS DECORATED WITH A NEW ATTRIBUTE CALLED J3PcP2^]caPRcL YOU USE THIS ATTRIBUTE TO INFORM 7#& THAT THE CLASS CAN BE SHARED BETWEEN THE SERVICE AND THE CLIENT AND THAT IT CAN BE SERIALIZED IN THE DEFAULT WAY )F YOU PLAN TO ADD ADDITIONAL INFORMATION TO THE SERIALIZATION PROCESS YOU CAN USE ONE OF THE PROPERTIES EXPOSED BY THE DATA CONTRACT CLASS AS IN THE CODE IN ,ISTING )N THIS CASE THE CODE TELLS 7#& THAT WHEN THE ?Tab^]2^]caPRc CLASS IS SERIALIZED IN THE CLIENT THE NEW CLASS NAME SHOULD BE ?Tab^] NOT ?Tab^]2^]caPRc AND THAT THE NEW NAMESPACE WILL BE BP\_[T=P\Tb_PRT ,ISTING %XTENDED ?Tab^]2^]caPRc #LASS J3PcP2^]caPRc8bATUTaT]RT , UP[bT =P\T , ?Tab^] =P\Tb_PRT , BP\_[T=P\Tb_PRTL _dQ[XR R[Pbb ?Tab^]2^]caPRc j J3PcP
Add Methods to the Service .OW THAT YOU HAVE AN OBJECT YOU CAN SHARE YOU NEED TO CREATE A NEW METHOD IN THE SERVICE THAT WILL BE USED TO SHARE THIS INFORMATION FROM THE SERVICE TO THE CLIENT /PEN THE 8?Tab^]BTaeXRTRb INTERFACE FILE AND CHANGE IT AS SHOWN IN ,ISTING ,ISTING ?Tab^]BTaeXRT -ODIFICATION dbX]V BhbcT\BTaeXRT<^ST[* dbX]V BP\_[T0__[XRPcX^]3PcP2^]caPRcb* ]P\Tb_PRT BP\_[T0__[XRPcX^]BTaeXRTb j JBTaeXRT2^]caPRcL _dQ[XR X]cTaUPRT 8?Tab^]BTaeXRT j
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
J>_TaPcX^]2^]caPRcL e^XS 0SS?Tab^]?Tab^]2^]caPRc _Tab^]2^]caPRc* J>_TaPcX^]2^]caPRcL Q^^[ 7Pb?Tab^]?Tab^]2^]caPRc _Tab^]2^]caPRc* J>_TaPcX^]2^]caPRcL ?Tab^]2^]caPRc 6Tc?Tab^]X]c XS* l l 5SING THE JBTaeXRT2^]caPRcL ATTRIBUTE THE CODE DEFINES A NEW SERVICE THAT WILL BE EXPOSED BY 7#& 4HE ATTRIBUTE IS USED BY 7#& TO UNDERSTAND AND DESCRIBE THE SERVICE IN FACT THIS ATTRIBUTE ALSO EXPOSES ADDITIONAL PROPERTIES THAT CAN BE USED TO CUSTOMIZE THE WAY THE SERVICE SHOULD LOOK 4HE J>_TaPcX^]2^]caPRcL ATTRIBUTE IS USED TO DESCRIBE AN OPERATION THATS AVAILABLE IN THE SERVICE AND CAN BE CALLED BY THE CLIENT IF THE OPERATION DESCRIBED NEEDS A COMPLEX OBJECT LIKE THE ?Tab^]2^]caPRc AS AN INPUT PARAMETER OR RETURNS ONE AS A METHOD RESULT THIS OBJECT HAS TO BE DECORATED WITH THE 3PcP2^]caPRc ATTRIBUTE IN ORDER TO BE USED BY THE 7#& RUNTIME 3IMPLE TYPES LIKE X]c 6dXS bcaX]V ETC ARE ALL KNOWN ENTITIES IN THE VARIOUS SPECIFICATIONS SO THEY DONT NEED TO BE MARKED AS A 3PcP2^]caPRc IN ORDER TO BE SERIALIZED WITH 7#& .OW LETS OPEN THE ?Tab^]2^]caPRcbeR FILE 9OU NEED TO IMPLEMENT THE PREVIOUS METHODS AS WELL AS TO INSTANTIATE A PRIVATE COLLECTION OF ?Tab^]2^]caPRc OBJECTS THAT WILL ACT AS A REPOSITORY FOR THE SAMPLE APPLICATION 3EE ,ISTING ,ISTING 4HE ?Tab^]BTaeXRTbeR #USTOMIZATION dbX]V dbX]V dbX]V dbX]V dbX]V
BhbcT\* BhbcT\2^[[TRcX^]b6T]TaXR* BhbcT\;X]`* BhbcT\BTaeXRT<^ST[* BP\_[T0__[XRPcX^]3PcP2^]caPRcb*
]P\Tb_PRT BP\_[T0__[XRPcX^]BTaeXRTb j JBTaeXRT1TWPeX^a8]bcP]RT2^]cTgc<^ST , 8]bcP]RT2^]cTgc<^STBX]V[TL _dQ[XR R[Pbb ?Tab^]BTaeXRT ) 8?Tab^]BTaeXRT j _aXePcT aTPS^][h ;Xbc+?Tab^]2^]caPRc- _Tab^]b , ]Tf ;Xbc+?Tab^]2^]caPRc-* aTVX^] 8\_[T\T]cPcX^] ^U 8?Tab^]BTaeXRT _dQ[XR e^XS 0SS?Tab^]?Tab^]2^]caPRc _Tab^]2^]caPRc j _Tab^]b0SS_Tab^]2^]caPRc* l _dQ[XR Q^^[ 7Pb?Tab^]?Tab^]2^]caPRc _Tab^]2^]caPRc j aTcda] _Tab^]b2^]cPX]b_Tab^]2^]caPRc* l
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
_dQ[XR ?Tab^]2^]caPRc 6Tc?Tab^]X]c XS j aTcda] _Tab^]bFWTaT_Tab^] ,- _Tab^]83 ,, XS5Xabc>a3TUPd[c* l T]SaTVX^] l l 4HE ONLY THING IN THIS CODE THAT YOU HAVENT SEEN BEFORE IS THE BEHAVIOR ATTRIBUTE USED IN THE CLASS DECLARATION TO DESCRIBE THE BEHAVIOR OF THIS SERVICES IMPLEMENTATION )N THIS CASE YOU WANT THE FIRST TIME THE SERVICE IS CALLED THE 7#& SERVICE RUNTIME TO HANDLE ONE AND ONLY ONE INSTANCE OF THIS CLASS FOR ALL THE SESSIONS THAT CALL IT )N THIS WAY THE COLLECTION OF PERSONS WILL BE ABLE TO HANDLE ALL PERSONS IN A SINGLE CONTAINER
■ .OTE /F COURSE THE CODE IS TRIVIAL 4HE IDEA IS JUST TO SHOW YOU HOW TO HANDLE A SINGLE COLLECTION OF PERSONS IN ONE PLACE )N A PRODUCTION ENVIRONMENT YOULL NEVER HAVE A SINGLETON 7#& SERVICE THAT WILL NEVER DISPOSE OF ITS RESOURCES )N THE 3INGLETON PATTERN THE SERVICE INSTANCE IS ALWAYS ALIVE AND THE RESOURCES USED BY THE INSTANCE ARE NEVER DISPOSED )N TERMS OF PERFORMANCE MEMORY AND RESOURCES THIS CAN BE A VERY BAD APPROACH THAT ) DO NOT RECOMMEND
.OW BEFORE YOU CAN RUN THIS SERVICE YOU NEED TO CHANGE THE WEB PROJECT PROPERTIES YOU MODIFIED IN THE INITIAL PROJECT SETUP BY SPECIFYING THE NEW PATH FOR THE ?Tab^]BTeXRTbeR SERVICE CLASS !FTER THAT YOU CAN PRESS & AND YOU SHOULD BE ABLE TO RUN AND TEST THESE THREE NEW METHODS IN THE F25CTbc2[XT]c UTILITY 9OU CAN RUN THE FRUCTbc2[XT]cTgT UTILITY AND EDIT THE COMPLEX TYPE FROM THERE
Customize the Way the Service Will Behave )F YOU HAVE EVER WORKED WITH A .%4 WEB APPLICATION LIKE !30.%4 OR !30 7EB 3ERVICES YOU MAY HAVE ALREADY NOTICED THAT THE 7#& APPLICATION STRUCTURE IS VERY MUCH LIKE A NORMAL !30.%4 WEB APPLICATION /F COURSE THE DIFFERENCES ARE BIG IN TERMS OF TECHNOLOGY BUT THERES STILL A CONFIGURATION FILE CALLED FTQR^]UXV THAT YOU CAN USE TO CUSTOMIZE THE BEHAVIOR AND STRUCTURE OF THE SERVICE "UT IF YOU DONT ALREADY KNOW THE CORRECT 8-, STRUCTURE OF AN ENDPOINT A SERVICE CONTRACT AND A BEHAVIOR THIS STEP CAN BE VERY PAINFUL .O WORRIES )F YOU PLAN TO MODIFY SOMETHING IN A 7#& APPLICATION YOU CAN SIMPLY RIGHT CLICK ON THE PROJECT FILE IN THE 3OLUTION %XPLORER PANEL AND SELECT 4SXc F25 2^]UXVdaPcX^] /R YOU CAN OPEN THE .%4 COMMAND PROMPT AND START beRR^]UXVTSXc^aTgT OR CALL IT FROM THE 6ISUAL 3TUDIO 4OOLS MENU BY SELECTING THE 7#& 3ERVICE #ONFIGURATION %DITOR OPTION /PEN THE APPROPRIATE FTQR^]UXV FILE IN THIS UTILITY AND YOU SHOULD BE ABLE TO ACCESS ALL THE FEATURES OF 7#& THAT CAN BE CONFIGURED &IGURE SHOWS THE MAIN WINDOWS OF THE UTILITY USED TO CUSTOMIZE THE SERVICE BY CREATING NEW ENDPOINTS USING DIFFERENT CHANNELS 4#0 73 ETC OR BY ADDING NEW BEHAVIORS
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
&IGURE #USTOMIZING A 7#& SERVICE !S SOON AS YOU ARE DONE WITH THE CHANGES JUST SELECT THE 3AVE COMMAND AND CLOSE THE FILE AND YOU WILL SEE RIGHT AWAY THE CHANGES IN THE FTQR^]UXV FILE 4HIS METHOD CAN BE ALSO A USEFUL WAY FOR YOU TO LEARN HOW THE 7#& BTaeXRT2^]caPRc ATTRIBUTES ARE PERSISTED IN THE FTQR^]UXV FILE
4EST THE 3AMPLE 3ERVICE 9OU CREATED THE SERVICE AND EXECUTED IT BY PRESSING & 4HE F25CTbc2[XT]c APPLICATION STARTS UP AND YOU CAN CALL THE SAMPLE METHODS EXPOSED BY THE SERVICE BUT THIS DOESNT MEAN YOUVE REALLY TESTED THE SERVICE 4O BE SURE THAT THE 7#& APPLICATION IS WORKING AS EXPECTED CREATE A NEW PROJECT OF TYPE CTbc IN THE SAME SOLUTION AND CALL IT BP\_[T0__[XRPcX^]5XgcdaTb )N THIS UNIT TEST PROJECT YOU CAN VERIFY THAT THE PROJECT CAN BE HOSTED AND CALLED AS EXPECTED $ELETE THE EXISTING D]XcCTbc Rb FILE ADD A NEW BASIC UNIT TEST OBJECT AND CALL IT ?Tab^]BTaeXRT5XgcdaT 9OULL USE THIS UNIT TEST TO VERIFY THAT THE ENDPOINT WORKS CORRECTLY AND THAT THE 7#& SERVICE IS HOSTED PROPERLY ON ))3 &IRST YOU NEED TO DEPLOY THE SERVICE TO AN ))3 LOCATION AS YOU DID FOR THE REPORTING SERVICE APPLICATION IN #HAPTER AND FOR THE #LICK/NCE DEPLOYMENT IN #HAPTER 9OU CAN DO THIS BY CLICKING ON THE ?a^_TacXTb CONTEXT MENU OF THE 7#& SAMPLE APPLICATION PROJECT FILE THEN CLICKING ON THE 7EB TAB AND CHOOSING TO RUN THE PROJECT ON A SPECIFIC ))3 SERVER 9OU CAN ALSO JUST DEPLOY THE PROJECT USING THE DEPLOY CONTEXT MENU EVERY TIME THE PROJECT IS READY TO BE DEPLOYED )N THIS CASE LETS KEEP THE PROJECT ON THE LOCAL ))3 AND USE THE 7EB TAB OF THE 6ISUAL 3TUDIO PROPERTIES PAGE 2EMEMBER THAT IF YOU ARE RUNNING 7INDOWS OR 7INDOWS 6ISTA ON YOUR DEVELOPMENT MACHINE YOU MUST ALSO INSTALL THE ))3 COMPATIBILITY COMPONENT AND THE ))3 MANAGEMENT CONSOLE USING THE 7INDOWS #ONTROL 0ANEL .OTE THAT IF YOURE RUNNING 7INDOWS YOU HAVE TO RUN 6ISUAL 3TUDIO WITH ADMINISTRATOR PRIVILEGES IN ORDER TO BE ABLE TO
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
DEPLOY A 7#& APPLICATION ON THE LOCAL ))3 9OU CAN DO THAT BY RIGHT CLICKING THE 63 ICON AND SELECTING 2UN AS !DMINISTRATOR 9OU CAN TEST THAT EVERYTHING HAS BEEN SUCCESSFULLY INSTALLED BY SELECTING THE #REATE 6IRTUAL $IRECTORY COMMAND ON YOUR 6ISUAL 3TUDIO WEB PANEL PAGE AND BE SURE THAT THE BP\_[T0__[XRPcX^] IS RUNNING ON THE LOCAL ))3 BY PRESSING & SEE &IGURE 9OU SHOULDNT SEE THE PORT NUMBER IN THE 52, ANYMORE
&IGURE BP\_[T0__[XRPcX^] RUNNING UNDER LOCAL ))3 &IGURE SHOWS THAT THE BP\_[T0__[XRPcX^] IS NOW RUNNING ON THE LOCAL ))3 AS THE PORT NUMBER HAS BEEN REMOVED FROM THE [^RP[W^bc PATH
Add Service Reference 9OU TELL THE 4EST PROJECT THAT IT WILL WORK WITH THE SAMPLE 7#& APPLICATION BY ADDING A SERVICE REFERENCE TO IT 4HIS STEP CAN BE ACCOMPLISHED BY RIGHT CLICKING THE 4EST PROJECT AND CHOOSING !DD 3ERVICE 2EFERENCE 4HIS BRINGS UP A DIALOG LIKE THE ONE SHOWN IN &IGURE WHERE YOU CAN SEARCH FOR AND IDENTIFY 7#& SERVICES
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
&IGURE 4HE !DD 3ERVICE 2EFERENCE WINDOW 4HIS DIALOG LETS YOU ADD OR DISCOVER 7#& SERVICES AVAILABLE ON YOUR CURRENT NETWORK )F YOU DONT FIND THE SERVICE YOU NEED YOU CAN MANUALLY ADD A 52, IN THE !DDRESS CTgc1^g AND SPECIFY THE beR SERVICE PATH 9OU ALSO HAVE TO PROVIDE THE NAMESPACE THAT WILL BE USED IN THE CLIENT PROJECT AND THEN PRESS /+ 7HEN YOU DO 6ISUAL 3TUDIO WILL CREATE ALL THE NECESSARY CODE FOR INSTANTIATING THE PROXIES 7#& NEEDS TO RUN THE SERVICE LOCALLY 4HIS INFORMATION WILL BE RETRIEVED FROM THE beR SERVICE OBJECT BY READING THE ATTRIBUTES 3PcP2^]caPRc AND BTaeXRT2^]caPRc USED PREVIOUSLY TO CREATE THE SAMPLE SERVICE
Test the Endpoint ,ETS WRITE A SAMPLE TEST METHOD THAT WILL TRY TO OPEN THE CURRENTLY CONFIGURED ENDPOINT AND VERIFY THAT EVERYTHING IS ACTIVE AND WORKING ,ISTING SHOWS THE TEST METHOD USED TO VERIFY THAT THE 7#& SERVICE IS WORKING PROPERLY ,ISTING 4EST THE 7#& 3ERVICE #ONNECTION dbX]V BhbcT\BTaeXRT<^ST[* dbX]V <XRa^b^UcEXbdP[BcdSX^CTbcC^^[bD]XcCTbcX]V* dbX]V BP\_[T0__[XRPcX^]5XgcdaTb?Tab^]BTaeXRTATUTaT]RT*
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
]P\Tb_PRT BP\_[T0__[XRPcX^]5XgcdaTb j JCTbc2[PbbL _dQ[XR R[Pbb ?Tab^]BTaeXRT5XgcdaT j JCTbc
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
#ONCLUSION AND #ONSIDERATIONS )N THIS FIRST PART OF THIS CHAPTER YOU SAW HOW 7#& WORKS HOW IT CAN BE CONFIGURED AND HOW IT CAN BE TESTED AND USED IN A CLIENT APPLICATION 4HE TOPIC IS COMPLEX AND IT REALLY DESERVES AN ENTIRE BOOK TO ITSELF 3O IF YOUR PLAN IS TO MOVE YOUR 70& APPLICATION INTO THE 3/! WORLD ) RECOMMEND YOU READ ONE OR BOTH OF THE BOOKS MENTIONED IN THE FOOTNOTE EARLIER IN THIS CHAPTER !N ADDITIONAL CONSIDERATION IS ABOUT THE DOMAIN YOULL HOST USING 7#& )F YOU PLAN TO WORK WITH 7#& YOU HAVE TO MAKE YOUR DOMAIN hSERIALIZABLEv 4HIS PROCESS IS PRETTY STRAIGHTFORWARD IF YOU ARE WORKING WITH %NTITY &RAMEWORK AS IT MAKES THE ENTITY SERIALIZABLE "UT YOU MAY STILL ENCOUNTER SOME ARCHITECTURAL ISSUES IN SERIALIZING YOUR DOMAIN MODEL FOR EXAMPLE SOME OF THE BUSINESS LOGIC CODE MAY NOT BE EASY TO TRANSLATE INTO 8-, 4HE DEBATE ABOUT SERIALIZING OR NOT SERIALIZING THE DOMAIN MODEL HAS BEEN EXERCISING THE 3/! COMMUNITY FOR A WHILE !CCORDING TO THE PURIST APPROACH YOU SHOULD HAVE TWO DIFFERENT DOMAINS ONE EXPOSED IN THE CLIENT AND ONE IN THE SERVER 4HIS APPROACH SHOULD BE USED BECAUSE THE DOMAIN SHOULD CONTAIN THE APPLICATION BUSINESS LOGIC AND AS ) ALREADY SAID IN .%4 YOU CANT SERIALIZE A METHOD SO YOU CANT SERIALIZE THE BUSINESS LOGIC
Integrate WCF into the TimeTracker Application .OW ITS TIME TO OPEN THE 4IME4RACKER 6ISUAL 3TUDIO SOLUTION AND INTEGRATE THE 7#& TECHNOLOGY INTO IT 4HE MAIN ROLE OF THIS TECHNOLOGY IN 4IME4RACKER WILL BE TO ADD FURTHER AVAILABILITY TO THE 4IME4RACKER DOMAIN 9OU WILL DISTRIBUTE THE DOMAIN OVER THE NETWORK USING 7#& AND $ATA 3ERVICES A NEW COMPONENT IN .%4 AND 6ISUAL 3TUDIO THAT WORKS WITH 7#& TECHNOLOGY /PEN THE 4IME4RACKER APPLICATION AND ADD TO THE SOLUTION A NEW 7#& WEB APPLICATION PROJECT WITH THE NAME 0?A4BBCX\TCaPRZTaBTaeXRT;PhTa THEN CONFIGURE THE SERVICE TO RUN ON YOUR LOCAL ))3 USING THE SAME PROCESS AS IN THE FIRST PART OF THIS CHAPTER #ALL THE APPLICATION FOLDER IN ))3 CX\TCaPRZTaBTaeXRT .OW DELETE THE DEFAULT 8BTaeXRT Rb AND BTaeXRTbeR FILES AND ADD A NEW 7#& $ATA 3ERVICE FILE NAME IT CX\TCaPRZTa3PcPBTaeXRTbeR AND PRESS /+ 4HE NEXT STEP IS TO ADD A REFERENCE TO THE %NTITY &RAMEWORK PROJECT IN THE 7#& WEB APPLICATION 9OU CAN DO THAT BY RIGHT CLICKING THE 7#& WEB APPLICATION AND SELECTING THE !DD 3ERVICE 2EFERENCE MENU ITEM THEN CHOOSING THE "USINESS ,AYER PROJECT FROM THE 0ROJECTS TAB
7#& $ATA 3ERVICE 4HE 7#& $ATA 3ERVICE IS AN EXTENSION OF 7#& THAT ALLOWS YOU TO EXPOSE YOUR DATA USING THE O$ATA PROTOCOL A PROTOCOL USED TO TRANSFORM OPERATIONS EXPOSED BY 7#& INTO A REPRESENTATIONAL STATE TRANSFER 2%34 9OU CAN THINK OF A 7#& DATA SERVICE AS AN ALTERNATIVE TO THE CLASSIC AT_^bXc^ah OBJECT EXPOSED BY A 3/!0 SERVICE 4HE ADVANTAGE OF USING THE 7#& DATA SERVICE IS THAT THE SAME OPERATION FOR EXAMPLE A 6Tc0__^X]c\T]cb METHOD CAN BE USED BY BOTH A 70& APPLICATION AND A SIMPLE WEB BROWSER !N ADDITIONAL ADVANTAGE IS THE ABILITY TO SHARE THE hCODEv OF THE DATA LAYER THE DOMAIN MODEL AND THE BUSINESS LOGIC IN ONE SINGLE BIG REPOSITORY /F COURSE A TECHNOLOGY LIKE THE 7#& DATA SERVICE COMES WITH A SIGNIFICANT RISK OF EXPOSING CRITICAL INFORMATION OVER THE 7EB AND FOR THIS REASON THE 7#& DATA SERVICE ALLOWS YOU TO FULLY CONTROL ACCESS TO ONLY SOME SPECIFIC ITEMS OF THE DOMAIN MODEL 4O CREATE A NEW 7#& DATA SERVICE YOU NEED TO MODIFY THE beR SERVICE YOU CREATED USING THE 7#& DATA SERVICE TEMPLATE AS IN ,ISTING
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
,ISTING 5SING THE 7#& $ATA 3ERVICE TO %XPOSE !PPOINTMENTS dbX]V BhbcT\3PcPBTaeXRTb* dbX]V BhbcT\3PcPBTaeXRTb2^\\^]* dbX]V 0?A4BBCX\TCaPRZX]V1dbX]Tbb;PhTa* ]P\Tb_PRT 0?A4BBCX\TCaPRZTaBTaeXRT;PhTa j _dQ[XR R[Pbb CX\TCaPRZTa3PcPBTaeXRT ) 3PcPBTaeXRT+CX\TCaPRZTa<^ST[2^]cPX]Taj _dQ[XR bcPcXR e^XS 8]XcXP[XiTBTaeXRT3PcPBTaeXRT2^]UXVdaPcX^] R^]UXV j R^]UXVDbTETaQ^bT4aa^ab , cadT* R^]UXV3PcPBTaeXRT1TWPeX^a
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
+bhbcT\fTQ+R^\_X[PcX^] STQdV,cadT cPaVTc5aP\Tf^aZ,# +bhbcT\fTQ+bhbcT\bTaeXRT<^ST[+QTWPeX^ab+bTaeXRT1TWPeX^ab+QTWPeX^a+bTaeXRT
.AVIGATE THE $ATA 5SING O$ATA 1UERIES 4HE DATA HOSTED BY AN O$ATA SERVICE CAN BE NAVIGATED USING THE O$ATA SYNTAX &OR EXAMPLE IF YOU WANT TO NAVIGATE THE LIST OF CURRENT EMPLOYEES YOU CAN TYPE THIS ADDRESS IN YOUR WEB BROWSER Wcc_)[^RP[W^bcCX\TCaPRZTaBTaeXRTcX\TcaPRZTaSPcPbTaeXRTbeR4\_[^hTTb WHILE IF YOU PLAN TO RETRIEVE THE INFORMATION FOR THE EMPLOYEE WITH AN )$ EQUAL TO YOU CAN TYPE THIS ADDRESS Wcc_)[^RP[W^bcCX\TCaPRZTaBTaeXRTcX\TcaPRZTaSPcPbTaeXRTbeR4\_[^hTTb' )F IN THE METHOD EXPOSED IN THE ,ISTING YOU ADD ADDITIONAL ENTITIES LIKE A^[T 0SSaTbb 0__^X]c\T]c AND SO ON YOU CAN FILTER THE INFORMATION USING O$ATA SO THAT WITH THE FOLLOWING CODE YOU CAN FOR EXAMPLE RETRIEVE ALL THE ROLES ASSOCIATED WITH A SPECIFIC EMPLOYEE Wcc_)[^RP[W^bcCX\TCaPRZTaBTaeXRTcX\TcaPRZTaSPcPbTaeXRTbeR4\_[^hTTb'A^[Tb AND SO ON 7ITH O$ATA YOU CAN ALSO QUERY THE DATA AS IF YOU WERE USING ,).1 ,ETS SAY FOR EXAMPLE THAT YOU WANT TO RETRIEVE ALL THE A^[TS WITH THE 83 GREATER THAN YOU CAN WRITE THIS ^3PcP 52, Wcc_)[^RP[W^bcCX\TCaPRZTaBTaeXRTcX\TcaPRZTaSPcPbTaeXRTbeRA^[Tb.UX[cTa,8S Vc !
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
4HE SYNTAX FOR USING THE O$ATA QUERY ENGINE IS TO ADD THE .UX[cTa KEYWORD AFTER THE 52, )F YOU WANT A BETTER LOOK AT HOW THE O$ATA SYNTAX WORKS YOULL FIND A USEFUL PAGE AT \bS]\XRa^b^UcR^\[XQaPahSS&!'!'"Pb_g 4HE PAGE EXPLAINS HOW TO CREATE SIMPLE QUERIES USING CLASSIC KEYWORDS THAT YOU CAN ALSO USE WITH ,).1 OR 4 31, TO QUERY THE DATA DIRECTLY FROM THE ENTITY FRAMEWORK CONTAINER
)NTEGRATE THE 7#& $ATA 3ERVICE IN 70& .OW ) WANT TO SHOW YOU HOW YOU CAN INTEGRATE THE 7#& $ATA 3ERVICE IN AN EXISTING APPLICATION LIKE 4IME4RACKER SO YOU CAN REMOVE ALL THE DEPENDENCIES FROM THE DATA LAYER AND BUSINESS LAYER COMPONENTS AND MAKE YOUR APPLICATION MORE LOOSELY COUPLED FROM THE BUSINESS LOGIC 4HE FIRST STEP IS TO ADD A REFERENCE IN THE 70& PROJECT TO THE 7#& DATA SERVICE JUST CREATED 4O DO SO YOU NEED TO ADD A NEW SERVICE REFERENCE TO THE 4IME4RACKER APPLICATION AND POINT TO THE SPECIFIC beR SERVICE AS &IGURE SHOWS
&IGURE 3ERVICE REFERENCE DIALOG FOR THE 7#& DATA SERVICE .OW THAT YOUVE ADDED THE REFERENCE TO THE 7#& DATA SERVICE YOU CAN HAVE FULL ACCESS DEPENDING ON THE READ AND WRITE SECURITY PERMISSIONS YOU PREVIOUSLY SET UP FOR THE ENTITIES TO THE DOMAIN MODEL FROM THE 7#& SERVICE 4HAT SAID YOU MAY WONDER NOW HOW YOU CAN ACCESS THE ENTITIES FROM 7#& INSTEAD OF ACCESSING THEM FROM THE BUSINESS LAYER 4HE SYNTAX IS VERY CLOSE TO WHAT YOU JUST SAW BECAUSE THE 7#& DATA SERVICE ALSO LETS YOU USE ,).1 SYNTAX TO QUERY THE ENTITIES "UT IN THE 6IEW-ODEL INSTEAD OF USING THE REPOSITORY OBJECT YOULL USE THE 7#& SERVICE
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
/PEN THE 4\_[^hTT;XbcEXTf<^ST[ AND CHANGE THE METHOD USED TO SEARCH THE EMPLOYEES AS SHOWN IN ,ISTING ,ISTING 3EARCH THE %MPLOYEES 5SING 7#& ^3PcP 3ERVICE _aXePcT e^XS BTPaRW4\_[^hTTb j DaX beRDaX , ]Tf DaXWcc_)[^RP[W^bcCX\TCaPRZTaBTaeXRTcX\TcaPRZTaSPcPbTaeXRTbeR* 2^]cTgc R^]cTgc , ]Tf 2^]cTgcbeRDaX* ePa aTbd[c , Ua^\ T X] R^]cTgc4\_[^hTTb fWTaT T5d[[=P\T5Xabc=P\T2^]cPX]bBTPaRW2aXcTaXP kk T5d[[=P\T;Pbc=P\T2^]cPX]bBTPaRW2aXcTaXP bT[TRc TC^;Xbc* ;Xbc+4\_[^hTT- aTbd[c , aT_^bXc^ahBTPaRW4\_[^hTTbBTPaRW2aXcTaXP* XU aTbd[c ,, ]d[[ kk aTbd[c2^d]c ,, j SXP[^VBTaeXRTBW^f0[Tac=^ 4\_[^hTTb U^d]S CWTaT PaT ]^ 4\_[^hTTb dbX]V cWT« U^[[^fX]V RaXcTaXP* aTcda]* l 2^[[TRcX^]2[TPa* U^aTPRW 4\_[^hTT T\_[^hTT X] aTbd[c j 2^[[TRcX^]0SS]Tf 4\_[^hTT3TcPX[bEXTf<^ST[T\_[^hTT R^]cPX]Ta* l l 7ITH THIS APPROACH YOU FIRST CREATE A NEW 52) OBJECT THAT CONTAINS THE FULL PATH TO THE beR DATA SERVICE FILE THEN YOU CAN CREATE A NEW INSTANCE OF THE DATA SERVICE CONTEXT OBJECT AND USE IT IN THE SAME WAY YOUD USE A CLASSIC %NTITY &RAMEWORK DATA CONTEXT OBJECT .OTE THAT THE POINT OF THIS CODE IS SIMPLY TO SHOW YOU HOW THE >QYTRc2^]cTgc CAN BE USED )N A REAL APPLICATION HOWEVER YOUD HIDE THE SERVICE PATH IN A CONFIGURATION VARIABLE TO MAKE THE APPLICATION MORE hCONFIGURABLEv 4HIS EXAMPLE ASSOCIATES THE CURRENT DATA SERVICE CONTEXT OBJECT WITH THE ALIAS 2^]cTgc THIS STEP CAN BE ACCOMPLISHED BY USING THE FOLLOWING STATEMENT AT THE BEGINNING OF THE VIEW MODEL CLASS FILE dbX]V 2^]cTgc , 0?A4BBCX\TCaPRZTaCX\TCaPRZTa3PcPBTaeXRTCX\TCaPRZTa<^ST[2^]cPX]Ta* )N THIS WAY WITH FEW LINES OF CODE YOU SWITCHED THE APPLICATION FROM USING THE DATA LAYER CREATED PREVIOUSLY TO USING A GLOBAL 7#& SERVICE LAYER
WCF Security )N THIS CHAPTER YOU SAW HOW YOU CAN DISTRIBUTE INFORMATION OVER THE NETWORK USING 7#& AND YOU SAW HOW POWERFUL BUT AT THE SAME TIME DANGEROUS IT CAN BE IF YOU EXPOSE THE WRONG COMPONENT OVER THE NETWORK USING 3/! )F YOU EXPOSE THE WRONG COMPONENT UNWANTED ACCESS TO YOUR 3/! SERVICE MAY CAUSE DAMAGE TO YOUR INTERNAL INFRASTRUCTURE &OR EXAMPLE AN INTRUDER ACCESSING YOUR 4IME4RACKER 7#& $ATA 3ERVICE COULD CAUSE A LOSS OF THE DATABASE DATA )N THIS CHAPTER AS ALREADY POINTED OUT WE CANT ANALYZE 7#& IN DEPTH "UT IT IS ESSENTIAL TO SAY SOMETHING ABOUT SECURITY AND HOW YOU CAN CREATE A SECURE SERVICE STRUCTURE USING 7#& 0LEASE USE THIS SECTION AS AN OVERVIEW OF HOW THE 7#& SECURITY MECHANISM WORKS BUT REMEMBER THAT IF YOU PLAN TO
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
WORK WITH 7#& YOULL HAVE TO EXPLORE THIS POWERFUL TECHNOLOGY IN A LOT MORE DEPTH 9OU CAN FIND ADDITIONAL RESOURCES RELATED TO THE IMPLEMENTATION OF SECURITY IN 7#& AT \bS]\XRa^b^UcR^\ [XQaPah\b&"#&"%Pb_g )N 7#& YOU CAN HANDLE SECURITY IN DIFFERENT WAYS &OR EXAMPLE YOU CAN v
3ECURE THE ENDPOINT THAT IS EXPOSING THE SERVICE METHODS BY INTRODUCING AN AUTHENTICATION PROCESS IN ORDER TO SHARE THE DATA ONLY WITH AUTHENTICATED USERS
v
6ERIFY THE INTEGRITY OF THE MESSAGE IN ORDER TO BE SURE THAT THE MESSAGE HAS NOT BEEN hHACKEDv DURING ITS TRANSMISSION
v
%NCRYPT THE MESSAGES OR TRANSMIT THEM THROUGH A SECURE CHANNEL (4403 USING A CERTIFICATE SO THAT THE SENDER AND THE RECEIVER WILL BE THE ONLY ONES ABLE TO TRANSLATE AND EXCHANGE THE MESSAGE
v
0ROVIDE AUTHORIZATION MECHANISMS FOR THE CODE SO THAT IT CANT BE USED FOR ATTACKS BY ARTFUL hBLACK HATSv
3ECURITY 2EQUIREMENTS "EFORE EVEN STARTING TO TACKLE THE SECURITY ASPECTS OF A 7#& SERVICE YOU NEED TO UNDERSTAND WHAT THE REQUIREMENTS OF YOUR APPLICATION ARE IN TERMS OF SECURITY AND HOW YOU WANT TO HANDLE THEM &OR EXAMPLE AN INTRANET 7#& SERVICE THAT WILL NEVER BE EXPOSED OUTSIDE THE COMPANY NETWORK CAN GENERALLY BE DEPLOYED WITH A LESS COMPLEX SECURITY IMPLEMENTATION THAN A SERVICE EXPOSED OVER THE 7ORLD 7IDE 7EB !FTER ALL IF THE SERVICE EXISTS ONLY hINSIDEv THE COMPANY NETWORK IT WILL BE MORE DIFFICULT FOR SOMEONE OUTSIDE THE NETWORK TO ACCESS THE SERVICE AND USE IT IN A HARMFUL WAY 7HEN YOU TACKLE SECURITY IN 7#& YOU SHOULD DIVIDE YOUR APPLICATION REQUIREMENTS INTO THREE DIFFERENT ASPECTS v
!UTHENTICATION 9OU NEED TO ANALYZE HOW YOU WANT TO AUTHENTICATE THE USERS AND THE EXTERNAL COMPONENTS THAT WILL ACCESS YOUR SERVICE &OR EXAMPLE YOU MAY CONSIDER AUTHENTICATING THE SENDER AND RECEIVER SO THAT NOBODY hIN THE MIDDLEv OF THE TRANSMISSION PATH WILL BE ABLE TO SEE AND CHANGE MESSAGES
v
!UTHORIZATION 9OU CAN SPECIFY WHAT CAN BE EXECUTED IN THE SERVICE AND BY THE SERVICE AND WHAT CANT BE DONE 4HIS ADDS AN ADDITIONAL LAYER OF SECURITY IN CASE THE SERVICE IS HACKED
v
)NTEGRITY YOU CAN USE CERTIFICATES TO ENCRYPT THE CONTENT OF YOUR MESSAGES CREATING A MORE SECURE STRUCTURE
"INDINGS 4HE SECOND STEP IN DETERMINING YOUR SECURITY REQUIREMENTS IS TO UNDERSTAND WHERE THE MESSAGES WILL BE TRANSMITTED IN ORDER TO DECIDE WHAT TYPE OF ENDPOINT YOU WILL USE &OR THE 7EB 3/!0 IS PREFERRED OVER (440 DUE TO ITS STANDARDIZED STRUCTURE &OR AN INTRANET APPLICATION ) PREFER TO USE 4#0 WITH BINARY ENDPOINTS AS )VE FOUND THIS TO BE FASTER IN TERMS OF PERFORMANCE
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
%ACH BINDING IN 7#& CAN BE CONFIGURED IN ONE OF THE FOLLOWING WAYS v
.ONE 4URNS SECURITY OFF
v
4RANSPORT 5SES TRANSPORT SECURITY FOR MUTUAL AUTHENTICATION AND MESSAGE PROTECTION
v
-ESSAGE 5SES MESSAGE SECURITY FOR MUTUAL AUTHENTICATION AND MESSAGE PROTECTION
v
"OTH !LLOWS YOU TO SUPPLY SETTINGS FOR TRANSPORT AND MESSAGE LEVEL SECURITY ONLY -3-1 SUPPORTS THIS
v
4RANSPORT7ITH-ESSAGE#REDENTIAL #REDENTIALS ARE PASSED WITH THE MESSAGE AND MESSAGE PROTECTION AND SERVER AUTHENTICATION ARE PROVIDED BY THE TRANSPORT LAYER
v
4RANSPORT#REDENTIAL/NLY #LIENT CREDENTIALS ARE PASSED WITH THE TRANSPORT LAYER AND NO MESSAGE PROTECTION IS APPLIED
%ACH BINDING TECHNIQUE HAS ITS OWN MECHANISM FOR IMPLEMENTING THE SECURITY OPTION YOUVE CHOSEN AND IT IS PART OF 7#& TO IMPLEMENT SECURITY IN ANY OF THE POSSIBLE BINDING COMBINATION
0ROVIDE THE #REDENTIALS )TS ALSO IMPORTANT TO CONSIDER HOW YOU PLAN TO TRANSPORT THE SECURITY CREDENTIALS FROM THE CLIENT TO THE SERVER $EPENDING ON THE TYPE OF SECURITY IMPLEMENTATION YOUVE DECIDED ON AND DEPENDING ON THE ENDPOINT YOU WANT TO USE TO EXPOSE THE SERVICE YOU MAY BE ABLE TO USE ONE OF THE FOLLOWING CREDENTIALS TYPES v
.ONE
v
7INDOWS
v
5SER.AME
v
#ERTIFICATE
v
)SSUED4OKEN
!UTHENTICATE AND !UTHORIZE THE #ODE 4HE FINAL STEP IS TO AUTHENTICATE THE USER THAT IS CALLING THE SERVICE AND BE SURE IT IS AUTHORIZED TO EXECUTE A SPECIFIC STEP &OR EXAMPLE SUPPOSE YOU HAVE A SERVICE THAT EXPOSES A SET OF METHODS ONE OF WHICH IS USED TO DELETE DATA FROM A DATABASE 9OU WANT TO BE SURE THAT THE DELETE METHOD CAN BE EXECUTED ONLY BY AN AUTHENTICATED 7INDOWS USER WHOS RECOGNIZED BY THE REMOTE 31, 3ERVER DATABASE AS A USER WHO CAN DELETE DATA
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
!UTHENTICATION IN 7#& CAN BE OBTAINED IN THREE DIFFERENT WAYS DEPENDING ON HOW IT HAS BEEN CONFIGURED v
0ROCESS )DENTITY 3ERVICE OPERATIONS ARE EXECUTED UNDER THE PROCESS IDENTITY OF THE SERVICE HOST &OR !30.%4 HOSTS THIS IS USUALLY THE !30.%4 ACCOUNT &OR SELF HOSTING IT MAY BE A DIFFERENT SERVICE ACCOUNT 4HE PROCESS IDENTITY IS THE 7INDOWS ACCOUNT THAT GOVERNS WHAT THE SERVICE CODE CAN DO AT RUNTIME WHEN ATTEMPTING TO ACCESS PROTECTED RESOURCES SUCH AS THE DATABASE REGISTRY OR FILE SYSTEM
v
4OKEN !UTHENTICATION 2UNTIME IDENTITIES SECURITY PRINCIPALS AND AUTHORIZATION POLICIES ALSO PLAY AN IMPORTANT ROLE IN THE 7#& SECURITY STORY
v
3ECURITY 0RINCIPAL )F YOU ARE FAMILIAR WITH TRADITIONAL .%4 ROLE BASED SECURITY YOU KNOW THAT THERE IS A SECURITY PRINCIPAL ATTACHED TO EACH EXECUTING THREAD 4HAT SECURITY PRINCIPAL HOLDS THE CALLERgS IDENTITY WHICH MAY OR MAY NOT BE TIED TO A 7INDOWS ACCOUNT AND ITS ROLES 2OLES GOVERN WHICH OPERATIONS CAN BE EXECUTED BY THE AUTHENTICATED USER WHEN TRADITIONAL .%4 ROLE BASED SECURITY IS APPLIED
v
3ERVICE3ECURITY#ONTEXT 4HIS PROVIDES RUNTIME ACCESS TO OTHER RELEVANT INFORMATION ABOUT THE SECURITY CONTEXT FOR A SERVICE OPERATION 4HE BTaeXRTBTRdaXch2^]cTgc IS A RUNTIME TYPE THAT INCLUDES IDENTITIES A SET OF CLAIMS AND AUTHORIZATION POLICIES 4HIS INFORMATION CAN BE USED TO APPLY A MORE FINE GRAINED SECURITY STRATEGY SPECIFICALLY FOR SERVICES
&IGURE ILLUSTRATES HOW 7#& PROCESSES A SECURE REQUEST BY APPLYING AUTHENTICATION AND AUTHORIZATION ON A SPECIFIC IDENTITY
&IGURE !UTHENTICATION AND AUTHORIZATION IN 7#&
Summary )N THIS FINAL CHAPTER YOU SAW HOW TO MAKE THE 70& 4IME4RACKER APPLICATION MORE LOOSELY COUPLED BY IMPLEMENTING A 3/! APPROACH WHICH WAS DONE IN OUR CASE USING 7INDOWS #OMMUNICATION &OUNDATION
#(!04%2 ■ ).4%2!#4).' 7)4( 7#&
7#& IS A FLEXIBLE WEB TECHNOLOGY USED TO DESIGN DISTRIBUTED ENTERPRISE APPLICATIONS WHERE THE BUSINESS LOGIC AND MORE GENERALLY A SPECIFIC COMPONENT OR SET OF COMPONENTS NEED TO BE SHARED OVER THE NETWORK TO MANY CLIENTS AND DIFFERENT APPLICATIONS 4HE MECHANISM OF 7#& IS COMPLEX AND REQUIRES DEDICATED STUDY BUT THE FOLLOWING STATEMENT SUMMARIZES THE BASICS )N 7#& THERE ARE TWO MAJOR ACTORS /NE IS A HOST THAT IS IN CHARGE OF EXPOSING ONE OR MORE SERVICES USING ONE OR MORE ENDPOINTS THE ENDPOINT SPECIFIES HOW THE DATA SHOULD BE EXPOSED BY THE SERVICE 4HE SECOND ACTOR IS THE CLIENT OR RECEIVER A COMPONENT OR OTHER PIECE OF SOFTWARE ABLE TO OPEN A DIALOG WITH THE (OST AND CALL THE METHODS EXPOSED BY THE SERVICE )N CONCLUSION YOU CAN USE 7#& INSTEAD OF DISTRIBUTING THE DATA LAYER AND THE BUSINESS LAYER TO THE CLIENT BUT IF YOU PLAN TO DO THAT
v
9OU SHOULD ALWAYS CALL THE SERVICE USING A MULTITHREADING APPROACH AS YOU SAW IN #HAPTER BECAUSE THERE MAY BE A SUBSTANTIAL LAG BETWEEN THE SERVICE CALL AND THE SERVICE ANSWER
v
)F YOU PLAN TO HOST YOUR DATA LAYER USING A 7#& SERVICE CONSIDER INCLUDING SOME SECURITY MECHANISM IN ORDER TO PREVENT UNAUTHORIZED ACCESS TO AND POSSIBLY MODIFICATION OF YOUR DATA
v
"EAR IN MIND THAT THE BUSINESS LOGIC HOSTED BY THE DOMAIN MODEL WONT BE AVAILABLE ANY MORE IN THE PRESENTATION LAYER BECAUSE IT IS NOW HIDDEN BY THE 7#& DATA LAYER
Index ■
!PPLICATION &ILES BUTTON
SYNTAX
APPLICATION LAYER
$ CUBE CREATION PROCESS
APPLICATION MANIFEST FILES !PPLICATION 5PDATES DIALOG
■A
!PPLICATION!CTIVATED METHOD
!BORT METHOD n
!PPLICATION$EACTIVATED METHOD
ACTION BEHAVIORS
!PPLICATION2UN METHOD
!CTIVE 2ECORD !2 PATTERN n
!PPLICATION3HUT$OWN METHOD
!DD .EW 4EST DIALOG
!PPLICATION#OMMANDS CLASS n
!DD 3ERVICE 2EFERENCE WINDOW
APPLICATIONS
!DD.EW 4EST WIZARD
CONFIGURATION n
!DD/BJECT METHOD
DEPLOYMENT n
!DDRESS ENTITY n
PUBLISHING OPTIONS n
ADDRESS OF ENDPOINT
REQUIREMENTS FOR n
!GGREGATE FUNCTION
SECURITY n
!GILE DEVELOPMENT APPROACH
4IME4RACKER APPLICATION n
ANIMATIONS n
DEVELOPING
!PP CLASS
DATABASE SCHEMA
!PPXAML CLASS
REQUIREMENTS n
!PPXAML FILE
USING THIRD PARTY RESOURCES n
!PPXAML 2ESOURCE ELEMENT
LAYERED n n
!PP?3TART METHOD
LOOSLEY COUPLED
■ ).$%8
N TIER n
ASYNCHRONOUS PATTERN n
UPDATES n
ATTACHED PROPERTIES
APPOINTMENT CALENDAR CONTROL n
ATTRIBUTE SYNTAX
!PPOINTMENT ENTITY n
AUTHENTICATION n
!PRESS 4IME4RACKER 3EE 4IME4RACKER APPLICATION
!UTHENTICODE
!2 OBJECTS n ARCHITECTURE 7#& n
AUTHORIZATION n !UTO'ENERATE#OLUMNS PROPERTY AUTOGENERATED CODE n !UTO-APPER
70& n ASSEMBLIES n TECHNOLOGIES
■ BACKGROUND PROCESSES
DATA TEMPLATES n
"ACKGROUND PROPERTY
$ATA"INDING
"ACKGROUND7ORKER COMPONENT n
STYLES n !SSEMBLY PROPERTIES WINDOW
"ASE$ETAILS6IEW-ODEL CLASS n
!SSERT CLASS
"ASE,IST6IEW-ODEL CLASS n
!SSERT)NCONCLUSIVE METHOD
"ASE6IEW-ODEL CLASS n
!SSET PANEL
"EGIN)NVOKE METHOD
ASSOCIATION OBJECT
BEHAVIORS n
ASSOCIATIONS BETWEEN ENTITIES n
BIDIRECTIONAL DATA BINDING
ASYNCHRONOUS CODE
BINDING 3EE DATA BINDING
"ACKGROUND7ORKER COMPONENT n
"INDING CLASS
4HREAD CLASS n
BINDING TARGET n
4HREAD OBJECT n
BINDINGS
4HREAD0OOL COMPONENT
BITMAPS
ASYNCHRONOUS OPERATIONS n
"IZ4ALK
BINDING SOURCE n
■ ).$%8
BLENDABILITY FEATURE
SPLITTING DEFINITION OF
"ORDER 8!-, ELEMENT
#LEAN5P METHOD
BOX MODEL TECHNIQUE
#LICK EVENT
"RUSH#ONVERTER
#LICK/NCE DEPLOYMENT n
BUSINESS LAYER
4IME4RACKER APPLICATION n
BUSINESS LOGIC n
PUBLISHING OPTIONS n
"UTTON CONTROL
FILE DEPENDENCIES n
"UTTON#LICK EVENT
UPDATES n SECURITY n
■C
CERTIFICATES n
# CODE
#ODE !CCESS 3ECURITY n
#ANCEL!SYNC METHOD
CLIENT TIER
#AN%XECUTE EVENT
CLOSING BRACKET
#AN%XECUTE METHOD n
#,2 n n
#AN%XECUTE#HANGED EVENT
CODE
#AN%XECUTE2OUTED%VENT!RGS
#
#ANVAS
ASYNCHRONOUS n
#ASCADING 3TYLE 3HEETS #33
COLORING
#ERTIFICATE !UTHORITY SERVER
LOOSLEY COUPLED
CERTIFICATES n
SEQUENTIAL EXECUTION OF
#HANNEL3ERVICE CLASS
#ODE !CCESS 3ECURITY n
CHARTS n
#ODE #OVERAGE TOOL n n
#HECK"OX#OLUMN
#ODE &IRST DEVELOPMENT
CHILD CONTROLS
CODE BEHIND FILE n
CHILD 6IEW-ODELS n
CODED 5) TEST
CLASS LIBRARY
#ODE0LEX
CLASSES
CODE SIGNING CERTIFICATES
BINDING TO DATA TEMPLATE n
COLLECTION PROPERTY
■ ).$%8
#OLUMN$EFINITION PROPERTY
#ONTENT PROPERTY n
#OMBO"OX
#ONTENT0RESENTER 70& CONTROL
#OMBO"OX#OLUMN
#ONTINUE7ITH METHOD
#OMMAND PATTERN n
#ONTRACT$ESCRIPTION CLASS
#OMMAND PROPERTY
CONTROLS
#OMMAND"INDING
ADDING TO VIEWS n
#OMMAND-ANAGER n
CREATING STYLES FOR n
#OMMAND-ANAGER)NVALIDATE2EQUERY3UG GESTED METHOD
CUSTOMIZING n n
COMMANDS #25$
DATA BINDING TO n DEFAULT
DYNAMICALLY RE EVALUATING EXECUTION OF n
DEFINING DATA TEMPLATE FOR n
)#OMMAND INTERFACE n
DOCUMENT
INITIALIZING
STYLES n
INTERACTIVE n
#ONTROLS FOLDER
INTRODUCTION TO n
#ONTROL4EMPLATE
LISTENING FOR CHANGES
#ONVERT METHOD n
2OUTED#OMMAND CLASS n
#ONVERT"ACK METHOD n
4IME4RACKER n
CORE CODE
COMPLEX TYPES
#/5.4 FUNCTION
#OMPLEX0ROPERTY PROPERTY
#REATE $ATASET DIALOG
COMPONENTS REDISTRIBUTING
#REATE 2EAD 5PDATE AND $ELETE #25$ COMMANDS
COMPOSITE KEY CONCURRENCY #ONFIGURE#ONTAINER METHOD #ONTACT ENTITY #ONTAINER PROPERTY
CUSTOMIZING LAYOUT OF n
#REATE%NTITY+EY METHOD #REATE/BJECT3ET4 METHOD n CREDENTIALS #RYSTAL 2EPORTS #2
■ ).$%8
CUSTOM CONTROLS CREATING n n CUSTOM DATA SOURCE CREATING n CUSTOM EFFECTS n CUSTOM FONTS CUSTOM REFERENCES #USTOMER ENTITY n
TARGET n 5) VALIDATION n UNIDIRECTIONAL 5PDATE3OURCE4RIGGER PROPERTY 6IEW-ODELS n IN 70& n ;$ATA#ONTRACT= ATTRIBUTE DATA CONTRACTS
■ DASHBOARD
$ATA $EFINITION ,ANGUAGE $$, SCRIPTS
LAYOUT n
DATA INTEGRITY
USER CONTROLS FOR n
DATA LAYER
$ATA !CCESS ,AYER $!, DATA BINDING n BIDIRECTIONAL
CREATING n 5NIT OF 7ORK IMPLEMENTATION n
BINDING EXPRESSIONS n
REPOSITORY PATTERN n
$ATA4EMPLATE n
TESTING n
DIRECTION
TRANSACTION MANAGEMENT n
ENDPOINTS
$ATA -ANIPULATION ,ANGUAGE $-,
).OTIFY0ROPERTY#HANGED INTERFACE n
;$ATA-EMBER= ATTRIBUTE
INTRODUCTION TO
DATA TEMPLATES n
)6ALUE#ONVERTER INTERFACE n
DATA VALIDATION n n
OF -ODEL TO 6IEW-ODEL
$ATA!NNOTATIONS
/NE4IME
DATABASE
DATA SERVICE OBJECT
/NE7AY
CREATING NEW
/NE7AY4O3OURCE
INDEPENDENCE
SOURCE n
LOGIC
SYNTAX n
■ ).$%8
OBJECTS
$EPENDENCY )NJECTION PATTERN
UNIT TEST
DEPENDENCY PROPERTIES
$ATABASE &IRST DEVELOPMENT DATABASE SCHEMA
PUBLISHING OPTIONS n
GENERATING
REQUIREMENTS FOR n
/2- AND
SECURITY n
DATABASE TIER
4IME4RACKER APPLICATION n
$ATA"INDING
DESCRIPTION OF ENDPOINT
$ATA#ONTEXT
DESIGN PATTERNS n
$ATA#ONTRACTS n
!CTIVE 2ECORD !2 n
$ATA%RROR6ALIDATION2ULE COMPONENT
ADVANTAGES OF USING
$ATA'RID CONTROL n
APPLYING TO GRAPHICAL LAYER
DATASET CREATING FOR REPORTS n
ASYNCHRONOUS n
DATASOURCE
#OMMAND n
CREATING FOR REPORTS n
$EPENDENCY )NJECTION
CUSTOM
-ODEL 6IEW #ONTROLLER -6#
$ATA4EMPLATES n $ATE#ONV
-ODEL 6IEW 0RESENTER -60 n
DEBUGGING
-ODEL 6IEW 6IEW-ODEL -66- n
DEFAULT CONTROLS
0ASSIVE 6IEW -60 n
DEFAULT PROPERTIES
0RESENTATION -ODEL 0-
DELEGATE METHODS
2EPOSITORY n n
$ELETE#OMMAND
3UPERVISING #ONTROLLER n
DELETING
FOR 5) n
OBJECTS n ROLES n DEPENDENCY INJECTION $) n 3EE ALSO INVERSION OF CONTROL
DEPLOYMENT n
5NIT OF 7ORK 5O7 n DESIGN TIME DATA n DETACHED ENTITY UPDATING n
■ ).$%8
$ETAILS VIEW n n n
RELATIONSHIPS BETWEEN n
$IALOG3ERVICE
UPDATING DETACHED n
DICTIONARIES ACCESSING
DOMAIN LAYER DOMAIN MODEL n
CREATING
BEHAVIORS AND RELATIONSHIPS IN n
EMBEDDING IMAGES IN n
CREATING NEW ENTITIES IN n
$ISPATCHER CLASS $ISPATCHER OBJECT $ISPLAY-EMBER0ATH $ISPOSE METHOD DOCKABLE PANELS $OCK0ANEL CONTROL DOCUMENT CONTROLS $OCUMENT0AGE6IEW DOMAIN $OMAIN $RIVEN $ESIGN $$$ #ODE &IRST DEVELOPMENT AND 2EPOSITORY PATTERN n 5NIT OF 7ORK 5O7 PATTERN n DOMAIN ENTITIES !DDRESS ENTITY n !PPOINTMENT ENTITY n #ONTACT ENTITY CREATING NEW n #USTOMER ENTITY n CUSTOMIZING %MPLOYEE DOMAIN ENTITY n
CREATING WITH %NTITY &RAMEWORK n MAPPING SERIALIZING DOMAIN FIRST DEVELOPMENT DOMAIN SPECIFIC LANGUAGE $3, DOT SYNTAX $OUBLE!NIMATION ELEMENT $O7ORK EVENT n DRAWING IN %XPRESSION "LEND n $ROP3HADOW EFFECT DUMMY DATA n DYNAMIC RESOURCES
■E %DIT#OMMAND %& $ESIGNER TOOL %& &RAMEWORK %& PROXY CLASSES EMBEDDED STYLES %MPLOYEE DOMAIN ENTITY n %MPLOYEE$ETAILS2EPORT n
MARKING STATUS OF
■ ).$%8
%MPLOYEE$ETAILS6IEW n %MPLOYEE$ETAILS6IEW-ODEL n
%NTITY&RAMEWORK%XTENSIONS CLASS
%MPLOYEE,IST2EPORT n
%NTITY+EY CLASS
%MPLOYEE,IST6IEW n n
%NTITY/BJECT CLASS
%MPLOYEE,IST6IEW-ODEL n
%NTITY3ET PROPERTY
%MPLOYEE2EPOSITORY CLASS %ND)NVOKE METHOD n ENDPOINTS n ADDRESS BINDING DESCRIPTION TESTING n %NTERPRISE ,IBRARY ENTITIES 3EE DOMAIN ENTITIES %NTITY &RAMEWORK n !CTIVE 2ECORD PATTERN n ADDING OBJECTS n #ODE &IRST DEVELOPMENT CREATING DOMAIN MODEL WITH n DATA LAYER n DELETING OBJECTS n $ESIGNER FILE EXTENSION METHODS n MODIFYING OBJECTS n
%NTITY3TATE PROPERTY ERROR TEMPLATES CUSTOM EVENT HANDLERS n EVENT LISTENERS ATTACHING TO )#OMMAND n CONTROLLING EVENT MODEL EVENT TRIGGERS n EVENTS IN 8!-, PREDEFINED ROUTING n TRIGGERING BEHAVIORS WITH %VENTS TAB 0ROPERTIES PANEL n EXE FILE %XECUTE METHOD n %XECUTE#OMMAND METHOD %XECUTED EVENT %XECUTED2OUTED%VENT!RGS
OBJECT RELATIONAL MAPPERS n
%XPRESSION "LEND 3EE -ICROSOFT %XPRESSION "LEND
2EPOSITORY PATTERN n n
%XPRESSION 'ALLERY WEB SITE
4IME4RACKER DOMAIN n
UNIT OF WORK n n
■ ).$%8
%XTENSIBLE !PPLICATION -ARKUP ,ANGUAGE 3EE 8!-, EXTENSION METHODS n %XTREME 0ROGRAMMING 80
■H (EADER PROPERTY (ELP TAB %XPRESSION "LEND (ORIZONTAL!LIGNMENT PROPERTY
■ FILE DEPENDENCIES n FILES IMPORTING &IXTURES
(YPERLINK#OLUMN
■ )!SYNC2ESULT INTERFACE
FONTS CUSTOM
)#OMMAND INTERFACE n n
FOREIGN KEYS
ATTACHING EVENT LISTENERS n
FORMAT TEXT
INTERACTION WITH ).OTIFY0ROPERTY#HANGED INTERFACE
&ORMAT#OMMAND PROPERTY FRAMEWORK ELEMENT FULL BEHAVIORS
■ GENERIC TEST 'ENERIC7EAK%VENT-ANAGER 'ET!LL METHOD n 'ET"Y)D METHOD n GRAPHICAL LAYER APPLYING DESIGN PATTERN TO
)#OMMAND OBJECTS n ICONS )D PROPERTY )$ATA%RROR)NFO INTERFACE n )$IALOG3ERVICE INTERFACE n )$ISPOSABLE INTERFACE n IMAGE BUTTON n )MAGE"UTTON CONTROLS IMAGES EMBEDDING IMAGES IN n
'RID CONTAINER
).AVIGATION3ERVICE INTERFACE
'RID CONTROL
IN BROWSER APPLICATIONS
'RID6IEW
)NCONCLUSIVE METHOD
'ROUP "Y FUNCTION n
)NITIALIZE METHOD )NITIALIZE#OMMANDS METHOD )NITIALIZE3ERVICE METHOD
■ ).$%8
)NLINES PROPERTY
■
).OTIFY0ROPERTY#HANGED INTERFACE n n
+!8!-,
INPUT CONTROLS )NSERT4 METHOD )NT PROPERTY )NTELLI3ENSE FEATURE INTERACTIVE COMMANDS n INTERNAL SCHEDULER )NTERNET )NFORMATION 3ERVICES ))3 n
KEY KEYFRAMES
■ ,ABEL CONTROL LAMBDA EXPRESSION ,ANGUAGE )NTEGRATED 1UERY ,).1 LAYERED APPLICATIONS n n
INVERSION OF CONTROL )O# n
LAYERS n
)NVOKE METHOD
LAYOUT CONTAINERS
)NVOKER
LAYOUT CONTROLS
)0ERSON3ERVICECS INTERFACE FILE
LIBRARIES THIRD PARTY
)2EPOSITORY INTERFACE n
LINE OF BUSINESS ,/" APPLICATIONS
)S!TTACHED4O METHOD
LIST CONTROLS
)S%NABLED PROPERTY
,IST VIEW MODEL n
)S)N4RANSACTION PROPERTY
LIST VIEWS n n
)S2EFERENCE ATTRIBUTE
,ISTBOX CONTROL n
)TEMS#ONTROL
CUSTOMIZING LAYOUT OF
)TEM4EMPLATE
DEFINING DATA TEMPLATE FOR n
)5NIT/F7ORK INTERFACE n
,IST6IEW-ODEL n
)5NITY#ONTAINER INTERFACE
LOAD TEST
)6ALUE#ONVERTER INTERFACE n
LOGICAL CODE SEPARATION OF IN TIERS
)7EAK%VENT,ISTENER INTERFACE
,OGIN VIEW n LOOSELY COUPLED APPLICATIONS
■
LOOSELY COUPLED CODE
*AVA &ORMS
,OST&OCUS MECHANISM
■ ).$%8
■
HARDWARE REQUIREMENTS n
-AGE5)EXE UTILITY
MOCKUPS n
MAIN SCREEN PROTOTYPING n
NEW FEATURES
-AIN6IEW n
.EW 0ROJECT WINDOW
-AKECERTEXE UTILITY
OVERVIEW n
-ANAGED %XTENSIBILITY &RAMEWORK -%&
3KETCH&LOW n
MANIFEST FILES
STYLING AND TEMPLATE CONTROLS n
MANY TO MANY RELATIONSHIPS n
WORKSPACE n
STARTUP PAGE
-ARGIN PROPERTY
PANELS n
-ARK$ELETED METHOD n
RESIZEABLE REGIONS
-ARK$IRTY METHOD n -ARK.EW METHOD n -AX(EIGHT MEDIA INTEGRATION LAYER MILCORE MESSAGES n METDATA -ETHOD.AME#OMPLETED EVENT -ICROSOFT %XPRESSION "LEND n n
-ICROSOFT %XPRESSION 3TUDIO 5LTIMATE VERSION -ICROSOFT 2EPORTING 3ERVICES 3EE 31, 3ERVER 2EPORTING 3ERVICES REPORTS -ICROSOFT 5NITY MIDDLE TIER MILCORE MOCKUPS n n -ODEL
ANIMATIONS n
BINDING TO 6IEW-ODEL
BEHAVIORS n
IN -66- PATTERN n
CREATING MOCK UP AND PROTOTYPING n
-ODEL "ROWSER PANEL
DESIGN TIME DATA n
-ODEL 6IEW 0RESENTER -60 PATTERN
DRAWING n
-ODEL 6IEW #ONTROLLER -6# PATTERN
SHAPES AND OBJECTS n
0ASSIVE 6IEW n
TEXT AND TEXT EFFECTS n
3UPERVISING #ONTROLLER n
DUMMY DATA IN
WHEN TO USE
■ ).$%8
-ODEL 6IEW 6IEW-ODEL -66- PATTERN n n
■ .AME PROPERTY
EXPOSING THE COMMANDS n
NAMESPACES n
).OTIFY0ROPERTY#HANGED INTERFACE n
NAMING CONVENTIONS
MODIFYING OBJECTS n -OUSE$RAG%LEMENT"EHAVIOR
NAVIGATION BETWEEN VIEWS n .AVIGATION3ERVICE CLASS .%4 &RAMEWORK
-3$. 5LTIMATE SUBSCRIPTION
INSTALLATION AND DISTRIBUTION
-34EST n
MULTITHREADING CODE IN n
MULTI CORE PROCESSORS #05S MULTITHREADING n ASYNCHRONOUS OPERATIONS n CODE IN .%4 FRAMEWORK n "ACKGROUND7ORKER COMPONENT n 4ASK OBJECT n 4HREAD CLASS n 4HREAD OBJECT n 4HREAD0OOL OBJECT n
PARALLEL PROCESSES IN .(IBERNATE N TIER ARCHITECTURE n .ULL2EFERENCE%XCEPTION ERROR
■ OBJECT DATA SOURCES OBJECT PERSISTENCE n OBJECT RELATIONAL MAPPERS /2- n
DEFINITION OF
/BJECT#ONTEXT CLASS n
WHEN TO USE
OBJECTS
-66- TOOLKITS
ADDING NEW n
-VVM#OMMAND n
!2 n
ENABLING LISTENING
CREATING DATA TEMPLATE FOR
IMPLEMENTAION WITH
DELETING n
REFACTORED
DRAWING n IN 8!-, MODIFYING n n
■ ).$%8
SERIALIZATION OF VALIDATING n /BJECT3ET4 METHOD /BSERVABLE#OLLECTION4 METHOD /BSERVABLE/BJECT CLASS /BSERVABLE/BJECT4 METHOD O$ATA PROTOCOL n O$ATA QUERIES n O$ATA SERVICE O$ATA SYNTAX n /N#AN%XECUTE#HANGED METHOD /NE4IME DATA BINDING ONE TO MANY RELATIONSHIPS ONE TO ONE RELATIONSHIPS /NE7AY DATA BINDING /NE7AY4O3OURCE DATA BINDING /N,OAD EVENT /N0ROPERTY#HANGED EVENT //0 PRINCIPLES OPENING BRACKET ;/PERATION#ONTRACT= ATTRIBUTE ORDERED TEST /RIGINAL4EXT PROPERTY
PARALLEL PROCESSES n 3EE ALSO MULTITHREADING PARAMETERS ARRAY OF CREATING REPORTS WITH n PARTIAL KEYWORD 0ASSIVE 6IEW -60 PATTERN n 0ASSIVE 6IEW PATTERN PERSISTENCE LAYER PREDEFINED EVENTS PRESENTATION LAYER PRESENTATION LOGIC 0RESENTATION -ODEL 0- PATTERN PRESENTATION PATTERNS PRESENTATION TIER 0RESENTATION#ORE 0RESENTATION&RAMEWORK 0RESENTATION-ODEL 0RESENTER n 0REVIEW#AN%XECUTE EVENT 0REVIEW%XECUTED EVENT PRIMARY KEYS PRINCIPLE OF LAST KNOWLEDGE 0RINT#OMMAND
■P
PROCESS IDENTITY
0ADDING PROPERTY
PRODUCTION PROJECTS
0AGE
0ROGRESS#HANGED EVENT 0ROGRESS#HANGED%VENT!RGS ARGUMENT
■ ).$%8
0ROJECT TAB %XPRESSION "LEND PROPERTIES DEPENDENCIES OF MODIFYING n 0ROPERTIES PANEL %XPRESSION "LEND n PROPERTY ELEMENT SYNTAX n PROPERTY T RIGGERS 0ROPERTY#HANGED EVENT n n
RED GREEN REFACTOR 3EE ALSO 4EST $RIVEN $EVELOPMENT REFERENCES n n REGION GROUPS RELATIONSHIPS BETWEEN ENTITIES n 2ELAY#OMMAND n 2EPORT0ARAMETER ARRAY 2EPORT0ROGRESS METHOD REPORTS n
0ROPERTY#HANGED%VENT!RGS ARGUMENT
CREATING DATASET n
0ROPERTY#HANGED%VENT(ANDLER CLASS
CREATING DATASOURCE n
0ROPERTY#HANGED%VENT-ANAGER CLASS
DESIGNING n
PROTOTYPING n
DETAILED VIEW n
PUBLIC ATTRIBUTES
DISPLAYING IN 70& VIEW n
0UBLISH COMMAND
FOOTER EDITING
PUBLISHHTM PAGE
HEADER EDITING INTRODUCTION TO
■
DETAILED VIEW n
1UERY $ESIGNER WINDOW n
LIST VIEW n
■R RASTER IMAGES
LOADING
QUERIES n
WITH PARAMETERS n 31, 3ERVER 2EPORTING 3ERVICES 3323 n
RDLC FILE EXTENSION
CONFIGURING n
RDLC REPORTS
CREATING DATASET n
2ECEIVE7EAK%VENT METHOD
CREATING DATASOURCE n
2ECTANGLE CONTROL
CREATING REPORTS n
2ECTANGLE 3KETCH CONTROL
DESIGNING REPORT n
■ ).$%8
DETAILED VIEW n
2OLE ENTITY n
HOSTING IN A 70& CONTROL n
2OLE2EPOSITORY CLASS
4IME4RACKER n
2OLE2EPOSITORY&IXTURE TEST CLASS
2EPORT6IEWER CONTROL n n
2OLE2ESPOSITORY DELETE METHOD
2EPORT6IEW-ODEL
ROLES
2EPOSITORY CLASS
ADDING n
2EPOSITORY OBJECT
DELETING n
2EPOSITORY PATTERN n n
'ET!LL METHOD n
ADDING ROLE n
'ET"Y)D METHOD n
ADDITIONAL METHODS n
UPDATING n
DELETING ROLE n
ROOT ELEMENTS n
'ET!LL ROLES n
2OUTED#OMMAND CLASS n
'ET"Y)D METHOD n
2OUTED#OMMAND OBJECT
)2EPOSITORY INTERFACE n
2OW$EFINITION PROPERTY
UPDATING ROLE n
2UN7ORKER!SYNC METHOD
2EPOSITORY"ASE CLASS
2UN7ORKER#OMPLETED EVENT
2EPOSITORY"ASE OBJECT
2UN7ORKER#OMPLETED%VENT!RGS EVENT
REPRESENTATIONAL STATE TRANSFER 2%34 2EQUERY3UGGESTED EVENT RESOURCE DICTIONARY n n 2ESOURCE PROPERTY 2ESOURCE$ICTIONARYXAML FILE RESOURCES n 2%34 SUPPORT IN 7#& 2ESUME METHOD 2ICH4EXT"OX
■ SAMPLE DATA 3AMPLES TAB %XPRESSION "LEND 3!0 #RYSTAL 2EPORTS 3AVE#HANGES METHOD SCHEDULE CONTROLS n 3EARCH#OMMAND n 3EARCH-ETHOD
2OLE DOMAIN ENTITY n
■ ).$%8
SECURITY BINDINGS
SETTER PROPERTY
CERTIFICATES n
SHAPES DRAWING n
#LICK/NCE n
3HARED $ATASET 0ROPERTIES DIALOG
#ODE !CCESS 3ECURITY n
3HOW2EPORT$IALOG METHOD
CREDENTIALS
3HUTDOWN-ODE PROPERTY
4IME4RACKER APPLICATION
3ILVERLIGHT
7#& n
3IMPLE /BJECT !CCESS 0ROTOCOL 3/!0 n
SECURITY PRINCIPAL 3%,%#4 STATEMENT SELF CLOSED ELEMENTS SELF REFERENCE RELATIONSHIPS SELF SIGNED CERTIFICATES SEPARATION OF CONCERNS CONCEPT SEQUENTIAL EXECUTION OF CODE ;3ERVICE#ONTRACT= ATTRIBUTE
SINGLE RESPONSIBILITY CONCEPT SINGLE THREAD DESIGN 3INGLETON PATTERN 3KETCHABLE CONTROLS TOOLKIT 3KETCH&LOW CONTROLS n 3KETCH&LOW -AP PANEL n 3KETCH&LOW PLUG IN n n n
3ERVICE$ESCRIPTION CLASS
BEHAVIORS n
SERVICE ORIENTED APPLICATIONS 7#& AND
CREATING MOCK UP AND PROTOTYPING WITH n
3ERVICE /RIENTED !RCHITECTURE 3/! SERVICES ADDING METHODS TO n ADDING SERVICE REFERENCES n CREATING IN 7#& CUSTOMIZING BEHAVIOR OF n IN 7#& n TESTING n
3ERVICE3ECURITY#ONTEXT
3KETCH2ECTANGLE5# 3LEEP METHOD 3/! APPLICATIONS 7#& AND 3/!0 COMMUNICATION PROTOCOL n SOLUTION FILE SLN SOLUTION FOLDERS 3OLUTION )TEMS FOLDER 31, 3ERVER 2 %XPRESS %DITION
■ ).$%8
31, 3ERVER -ANAGEMENT 3TUDIO 33-3 31, 3ERVER 2EPORTING 3ERVICES 3323 n
DEFINING IN %XPRESSION "LEND FOR IMAGE BUTTON n
CONFIGURING n
THEMES
CREATING REPORTS n
TRIGGERS n
CREATING DATASET n
3TYLE4EMPLATE
CREATING DATASOURCE n
STYLING CONTROLS n
DESIGNING REPORT n
3UPERVISING #ONTROLLER PATTERN n
DETAILED VIEW n
3USPEND METHOD
DEPLOYMENT OF 63 SOLUTION IN
SVCUTILEXE
HOSTING REPORTS n
3YNDICATION 3ERVICE ,IBRARY
PORTAL PAGE
SYNTAX
3TACK0ANEL #OMMAND"INDING COLLECTION
BINDING n 8!-, n
3TACK0ANEL CONTROL n
3YSTEM#OMPONENT-ODEL NAMESPACE
STAND ALONE APPLICATIONS 3TART METHOD
3YSTEM#OMPONENT-ODEL$ATA!NNOTATIO NS ASSEMBLY
3TARTUP METHOD
3YSTEM%VENT!RGS CLASS
STARTUP PROCESS CUSTOM n
3YSTEM4HREADING NAMESPACE
3TARTUP5RI ATTRIBUTE
3YSTEM4HREADING$ISPATCHER/BJECT CLASS
STATIC RESOURCES STORED PROCEDURES STORYBOARDS n STYLES n n
3YSTEM4HREADING4ASKS NAMESPACE 3YSTEM7INDOWS#ONTROLS CLASS 3YSTEM7INDOWS$EPENDENCY/BJECT CLASS
BASIC CONCEPTS n
3YSTEM7INDOWS&ORMS NAMESPACE
CONTROL n
3YSTEM7INDOWS&RAMEWORK%LEMENT CLASS
CREATING n CUSTOM n
3YSTEM7INDOWS-EDIA6ISUAL CLASS
■ ).$%8
3YSTEM7INDOWS3ETTER"ASE CLASS
TEST FIRST PRINCIPLE
3YSTEM7INDOWS5)%LEMENT CLASS
TESTSETTINGS FILE TEST WINDOWS
■T TABBED PANELS
CALCULATOR EXAMPLE n
4AB#ONTROL n
#LEAN5P METHOD
TABLE OBJECT
FOR UPDATED OBJECTS n
TABLES GROUPING
)NITIALIZE METHOD
4ARGET4YPE ATTRIBUTE
2OLE2EPOSITORY CLASS
4ASK OBJECT n
TRANSACTION TESTING n
4ASK 0ARALLEL ,IBRARY
UNIT n
TECHNOLOGIES
TEXT
DATA TEMPLATES n
FORMATTING
$ATA"INDING
IN %XPRESSION "LEND n
STYLES n
TEXT CONTROLS
TEMPLATE CONTROLS n
4EXT"LOCK
4EMPLATE#OLUMN
4EXT"OX
TEMPLATES
4EXT"OX#OLUMN
DATA n n
THEMES
IN %XPRESSION "LEND
THIRD PARTY $,,S n
TEMPORARY CERTIFICATES
THIRD PARTY RESOURCES n
TEST CLIENT UTILITY 7#& n
THIRD PARTY TOOLS
4EST $RIVEN $EVELOPMENT 4$$ n
THREAD AFFINITY
ADVANTAGES OF
4HREAD CLASS n
APPROACH n
4HREAD OBJECT n
CODE COVERAGE n n
4HREAD0OOL OBJECT n
4IME4RACKER APPLICATION AND n
THREADS
WITH -34EST n
TESTING n n
IN BACKGROUND
■ ).$%8
PAUSING
APPOINTMENT ENTITY
RESUMING
!PPOINTMENT ENTITY n
STARTING
#ONTACT ENTITY
STOPPING n
CUSTOMER ENTITY
SYNCHRONIZING
#USTOMER ENTITY n
THREE TIER APPLICATION ARCHITECTURE n
%MPLOYEE DOMAIN ENTITY n
4IME4RACKER APPLICATION EXAMPLE
EMPLOYEE ENTITY
ADDING FEATURES TO n
RELATIONSHIPS BETWEEN n
ARCHITECTURE n
2OLE DOMAIN ENTITY n
COMMANDS n
ROLE ENTITY
CONFIGURATION n
DOMAIN MODEL n
CREATING PROTOTYPE n
%NTITY &RAMEWORK
CUSTOM STARTUP PROCESS n
EXTERNAL RESOURCES IN n
CUSTOMIZING 5) n
INTEGRATING 7#& INTO n
DATA LAYER n
LAYER DIAGRAM
)5NIT/F7ORK INTERFACE n
LAYERS n
REPOSITORY PATTERN n
LIST VIEWS n
TESTING CODE n
/BJECT#ONTEXT n
TRANSACTION MANAGEMENT n
OVERVIEW
5NIT OF 7ORK IMPLEMENTATION n
PUBLISHING n
DATABASE SCHEMA DEPLOYMENT n $ETAILS VIEW n DICTIONARIES DOMAIN ENTITIES IN ADDRESS ENTITY !DDRESS ENTITY n
REPORTS CREATING n REQUIREMENTS n SECURITY SETTINGS 4EST $RIVEN $EVELOPMENT AND n USER CONTROLS FOR DASHBOARD n USER INTERFACE USER STORIES n USER TASKS
■ ).$%8
VIEW MODELS n
)$ISPOSABLE INTERFACE
VIEWS ADDING n
)5NIT/F7ORK INTERFACE n
4IME4RACKER COMMANDS n
OBJECTS
4IME4RACKER DOMAIN n
ADDING NEW n
4IME4RACKER-ODEL$ESIGNERCS FILE
DELETING n
4IME4RACKER-ODEL#ONTAINER CLASS
MODIFYING n
4IME4RACKER-ODEL#ONTAINER OBJECT
TRANSACTION MANAGEMENT n
TOKEN AUTHENTICATION
UNIT TESTING n
TOOLS 70& n
5NIT OF 7ORK 5O7 PATTERN n
ADDITIONAL n
5NIT 4EST WIZARD
EXPRESS EDITIONS
UNIT TESTING n
%XPRESSION "LEND n
5NIT/F7ORK CLASS n
6ISUAL 3TUDIO n
5NITY n
4O3TRING METHOD
5PDATE METHOD n
TRANSACTION MANAGEMENT n
UPDATES n n
TRANSITIONS
5PDATE3OURCE4RIGGER PROPERTY
4RANSPARENT3TOP PROPERTY
USE CASE DIAGRAMS
TRIGGERS n n
USER CASES
4RIGGERS PROPERTY ELEMENT
USER CONTROLS FOR DASHBOARD n
4 31, CODE
USER INPUT VALIDATION OF n
TWO WAY BINDING
USER INTERFACE 5)
4YPE#ONVERTER!TTRIBUTE n
CONTROLS CREATING
■
CUSTOMIZING n
5) 3EE USER INTERFACE 5)
ADDING FEATUES n
5)3PYEXE
APPLYING STYLES n
UNIDIRECTIONAL DATA BINDING
DATA BINDING AND
5NIT OF 7ORK n
DESIGN PATTERNS FOR n
■ ).$%8
LAYERS
6ALUE#ONVERSION ATTRIBUTE
MOCKUPS OF n
VECTOR GRAPHIC IMAGES
TESTING n
6ERI3IGN
USER INTERFACE 5) CONTROLS
6ERTICAL!LIGNMENT PROPERTY
COMMON CONTROLS n
6IEW-ODEL CLASS n
CONTROLS CONTAINERS n
VIEW MODELS n
CREATING WITH 8!-, OR #
ADDITIONAL n
POSITIONING n
BASE IMPLEMENTATION
USER INTERFACE 5) DESIGN PROCESS CREATING MOCK UP AND PROTOTYPING n OVERVIEW n USER REQUESTS REACTING TO USER STORIES n 5SER#ONTROL CONTROL n 54&
"ASE$ETAILS6IEW-ODEL "ASE6IEW-ODEL n BINDING MODEL TO CHILD n DATA BINDING n $ETAILS n %MPLOYEE n %MPLOYEE$ETAILS6IEW-ODEL n %MPLOYEE,IST6IEW-ODEL n
■ 6ALIDATES/N$ATA%RROR VALIDATION
EXPOSING )#OMMAND IN ).OTIFY0ROPERTY#HANGED INTERFACE n
ADDING TO 6IEW-ODELS n
,IST n
OF BOUND DATA n
IN -66- PATTERN n
DATA n
TESTING
VALIDATION ERRORS VALIDATION RULES n
VALIDATORS n VIEWS
6ALIDATION(AS%RROR PROPERTY
ADDING CONTROLS TO n
6ALIDATION2ULE CLASS
BOUND TO 6IEW-ODELS
VALUE OBJECTS
CREATING n n
■ ).$%8
$ETAILS VIEW n
7#& 3ERVICE ,IBRARY
,IST VIEW n
7#& 7ORKFLOW 3ERVICE !PPLICATION
,OGIN VIEW n
WEAK EVENT PATTERN
MOCK UP AND PROTOTYPING n
7EAK%VENT
OVERVIEW n
7EAK%VENT,ISTENER n
$ETAIL VIEW n n n
7EAK%VENT-ANAGER OBJECT n
LIST n n
7EB PERFORMANCES TEST
IN -66- PATTERN
WEB SERVICES
NAVIGATION BETWEEN n
7(%2% 31, STATEMENT
OPENING
7INDOW OBJECT
IN 0RESENTATION -ODEL PATTERN
7INDOW ROOT ELEMENT
FOR REPORT VIEWER
7INDOWS !ZURE !PP&ABRIC
IN 3UPERVISOR #ONTROLLER PATTERN n
7INDOWS #OMMUNICATION &OUNDATION 7#& n
TESTING
ADDING METHODS TO SERVICES n
6ISUAL 3TUDIO
ADDING SERVICE REFERENCES
6ISUAL 3TUDIO n
ARCHITECTURE n
CODE COVERAGE WITH n
CREATING SERVICE
CREATING APPLICATION IN
CUSTOMIZING SERVICES n
REPORTING IN n
$ATA#ONTRACTS n
SOLUTION FOLDERS
$ATA 3ERVICE n
TEST SETTINGS
ENDPOINTS n
TESTING IN
FEATURES HOSTING DOMAIN FOR
■ 7#& 3EE 7INDOWS #OMMUNICATION &OUNDATION 7#& 7#& $ATA 3ERVICE n 7#& 3ERVICE !PPLICATION
INTEGRATING INTO 4IME4RACKER APPLICATION n INTERACTING WITH n INTRODUCTION TO n
■ ).$%8
PROJECT TYPES
%XPRESSION "LEND n
SAMPLE PROJECT n
6ISUAL 3TUDIO n
SECURITY n
5) CONTROLS n
SERVICE AND CLIENTS
USE OF EXTERNAL RESOURCES BY n
TEST CLIENT UTILITY n TESTING ENDPOINTS n TESTING SERVICES n
7INDOWS 7ORKFLOW &OUNDATION 7& 7INDOWS&ORMS(OST CONTROL n
USES OF
7INDOWS&ORMS)NTEGRATIONDLL ASSEMBLY
WEB SETTINGS FOR
7ORKER$O7ORK METHOD
7INDOWS &ORMS CONTROL n 7INDOWS &ORMS 2EPORT6IEWER CONTROL 7INDOWS 0RESENTATION &OUNDATION 70& ARCHITECTURE n
70& 3EE 7INDOWS 0RESENTATION &OUNDATION 70& 70& !PPLICATION 3TRUCTURE 70& APPLICATIONS IN 6ISUAL 3TUDIO
ASSEMBLIES AND #,2 n
IN BROWSER
MILCORE
MULTITHREADING AND n
0RESENTATION#ORE
STAND ALONE
0RESENTATION&RAMEWORK
70& $ATABOUND !PPLICATION
TECHNOLOGIES n INTEGRATING 7#& $ATA 3ERVICE IN n INTRODUCTION TO n THEMES TOOLS n ADDITIONAL n EXPRESS EDITIONS
70& "INDING CLASS 70& $ATABOUND !PPLICATION 70& DISPATCHER 70& 3CHEDULE #ONTROL 70& 3KETCH&LOW !PPLICATION 70& 4OOLKIT n 7RAP0ANEL
■ ).$%8
■
ROOT ELEMENTS n
8!-, %XTENSIBLE !PPLICATION -ARKUP ,ANGUAGE
4YPE#ONVERTER!TTRIBUTE n
CODE
8!-, FILES PARTS OF
EVENTS IN
8!-, MARKUP
SYNTAX n
8!-, 0OWER 4OYS
ATTACHED PROPERTIES
8!-,0!$
ATTRIBUTES
XBAP EXTENSION
CODE BEHIND n
X+EY ATTRIBUTE n
CONTENT PROPERTY
8-, DATA SOURCES
NAMESPACES n
XMLNS ATTRIBUTE
OBJECTS AND COMMON ELEMENTS n
XMLNSX ATTRIBUTE
PROPERTY ELEMENTS n
XAML FILE EXTENSION