Lab 5Creating a Foundational Module.docx
《Lab 5Creating a Foundational Module.docx》由会员分享,可在线阅读,更多相关《Lab 5Creating a Foundational Module.docx(12页珍藏版)》请在冰豆网上搜索。
![Lab 5Creating a Foundational Module.docx](https://file1.bdocx.com/fileroot1/2023-2/2/0db011e3-6e00-479c-963e-5a20e59527f5/0db011e3-6e00-479c-963e-5a20e59527f51.gif)
Lab5CreatingaFoundationalModule
Lab5:
CreatingaFoundationalModule
Purpose
Estimatedtimetocompletethislab:
25minutes.
Inthislab,youwillusetheSmartClientDevelopmentguidancepackagetoaddafoundationalmoduletoyousmartclientapplicationsolution.Youwillalsocreateanewglobalservicethatwillsimulatearealprintingservice.
Aftercompletingthislab,youwillbeableto:
Understandthecomponentsofafoundationalmodule
Addanewfoundationalmoduletoasmartclientapplicationusingtheguidancepackage.
Createaglobalservice
Preparation
Beforeproceedingwiththislab,youmustinstallandconfiguretheprerequisitesoftware.Formoreinformation,seethetopicStartHere.
Openthesolutionforthepreviouslab(eithertheonethatyoucreatedortheendsolutionforthatlab.)
Background:
FoundationalModules
Afoundationalmoduleisamodulethateitherprovidesservicestotheshellandothermodules,providesalayout,orboth.ItdoesnotimplementausecaseorcontainaWorkItem.Anexampleofafoundationalmoduleisamodulethatcontainsinfrastructureservices,likecachingorloggingservices.SinceafoundationalmoduledoesnotcontainaWorkItem,alltheservicescreatedwithinafoundationalmoduleareregisteredasglobalserviceswiththerootWorkItem.
Exercise1:
CreatingaNewFoundationalModule
Inthisexerciseyoucreateafoundationalmodulewhichwillprovideaprintingserviceforallthemodulesintheapplication.TheShippingModulemodulewillconsumeittoprintpackingslipsandshippinglabelsforsalesorders.
Task1.UsetheGuidancePackagetoaddanewFoundationalModule
Inthistaskyouwilladdafoundationmoduletoyourapplication.TheguidancepackageincludestheVisualStudiotemplatenamedAddFoundationalModule(C#).Thistemplateunfoldsanewclasslibraryprojectforthemodule.
1.InSolutionExplorer,right-clicktheSourcesolutionfolder,pointtoSmartClientSoftwareFactory,andthenclickAddFoundationalModule(C#).TheAddNewProjectdialogboxappearswiththeAddFoundationalModule(C#)templateselected.
2.EnterPrintingModuleastheNameandsettheLocationtotheSourcefolderofthesolution.
ClickOK.TheguidancepackagedisplaystheAddFoundationalModulewizard.
Figure1
AddFoundationalModuleWizard
3.DeselecttheoptionCreateaninterfacelibraryforthismodule.Ifyouselectthisoption,therecipewillcreateanadditionalprojecttocontaintheelementsthatprovidethepublicinterfacetotheassembly.Formoreinformationaboutseparatingtheinterfacefromtheimplementationofmodules,seetheModuleInterfaceSeparationpatternintheSmartClientSoftwareFactorydocumentation.
4.DeselecttheoptionCreateaunittestprojectforthismodule.Ifyouselectthisoption,therecipewillcreateatestprojectforthemodulewithtestclassesforyourmodulecomponents.Inthislabyouwillnotwriteunittestsforthemodule.
5.Optionally,selecttheoptionShowdocumentationafterrecipecompletesifyouwanttoseeasummaryoftherecipeactionsandsuggestednextstepsaftertherecipecompletes.
6.ClickFinish.TheguidancepackagewillgenerateanewclasslibraryprojectnamedPrintingModule.
Figure2
PrintingModulemoduleinSolutionExplorer
Therootfolderoftheprojectcontainsaclass,Module.TheModuleclassderivesfromtheCompositeUIApplicationBlockclassModuleInit.TheCompositeUIApplicationBlockcallstheLoadmethodofthisclassonstartup.
Theprojectalsocontainsthefollowingfolders:
Constants.ThisfoldercontainsfourclassesnamedCommandNames,EventTopicNames,UIExtensionSiteNames,andWorkspaceNames.Youcanmodifytheseclassestodefinemodule-specificidentifiersforyourcommands,eventtopics,UIExtensionSites,andWorkSpaces.
Services.Youusethisfoldertostoretheimplementationofglobalservices.
TherecipealsoaddsthefollowingXMLentryforthemoduletotheprofilecatalogoftheapplication(definedinthefileShell\ProfileCatalog.xml).ThismeansthattheCompositeUIApplicationBlockwillloadthemoduleatapplicationinitializationtime.
XML
TheCompositeUIApplicationBlockloadsmodulesintheordertheyarespecifiedintheprofilecatalogfile,unlessyouexplicitlydefineinter-moduledependencies.Thus,theShippingModulemodulewillbeloadedbeforethePrintingModule.ThismightleadtoproblemsiftheShippingModuleinstantiatesaserviceduringmoduleinitializationthathasadependencyinacomponentthatbelongstothePrintingServicemodule.Toavoidthisproblem,youcanspecifymoduledependenciesbyarrangingmodulesintosectionsandestablishingdependenciesbetweenthesections.Whenyoucreateasmartclientsolution,twosectionsaredefinedbydefault:
Services.Includeinthissectionmodulesthatexposeglobalservicestoothermodules.Typicallyyouwillincludefoundationalmodulesinthissection.
Apps.Includeinthissectionmodulesthatmighthavedependenciesonservicespublishedbyothermodules.Typicallyyouwillincludebusinessmodulesinthissection.
InthenextstepyouwillmovethePrintingModulemoduletotheServicessectionsothattheCompositeUIApplicationBlockloadsitbeforetheShippingModulemodule.
7.MovetheelementforthePrintingModulemoduleintotheServicessection,asshowninthefollowingcode.
XML
Note:
Youcanalsodefinedependenciesforaparticularmoduleincodeusingthe[ModuleDependency]attribute.Formoreinformation,seeCreatingaModuleintheSmartClientSoftwareFactoryhelp.
Task2.MoveBusinessEntitiestoInfrastructure.Interfaceproject
Inthenexttask(Task3)youwilladdaprintingservicetothePrintingModulemodule.Thisservicewillsimulatetheprintingofasalesorder,thusitsmethodswillreceiveanOrderentityasaparameter.SincetheOrderclassisdefinedintheShippingModuleproject,youwouldneedtoaddareferencetoitinthePrintingModuleproject.ToavoidhavingareferencetotheShippingModuleprojectinthePrintingModuleproject,inthistaskyouwillmovetheOrderandOrderLineItementitiestotheInfrastructure.Interfaceproject.
8.InSolutionExplorer,selecttheOrder.csandOrderLineItem.csfilesintheBusinessEntitiesfolderoftheShippingModuleproject,cutthem,andpastethemintheBusinessEntitiesfolderintheInfrastructure.Interfaceproject.
EventhoughyoumovedthesourcecodefilestotheInfrastructure.Interfaceproject,theclasses´namespaceisstillAdventureWorks.ShippingModule.BusinessEntities.InthefollowingstepsyouwillrenamethenamespacetoAdventureWorks.Infrastructure.Interface.BusinessEntities.
9.GototheEditmenu,pointtoFindandReplaceandthenselectQuickReplace.
10.SetAdventureWorks.ShippingModule.BusinessEntitiesintheFindwhatfield.
11.SetAdventureWorks.Infrastructure.Interface.BusinessEntitiesintheReplacewithfield.
12.IntheLookindropdownlist,setEntireSolution.
13.ClickReplaceAll.13occurrencesshouldbereplaced.
Task3.Implementtheprintingservice
InthistaskyouwillcreateasimpleprintingservicethatwillbeconsumedbytheShipNewOrderViewview.
14.InSolutionExplorer,right-clicktheServicesfolderinthePrintingModuleproject,pointtoAddandthenclickNewItem.
15.FromtheTemplateswindow,selectInterfaceandsetthenametoIPrintingService.
16.ClickAdd.
17.Addthefollowingusingstatementtothefile.YouwilluseittorefertotheOrderandOrderLineItemclasses.
C#
usingAdventureWorks.Infrastructure.Interface.BusinessEntities;
18.Changetheinterfacesignaturetomakeitpublic:
C#
publicinterfaceIPrintingService
19.AddamethodnamedPrintPackingSliptotheinterfacebody.Thismethodwillbeusedtoprintthepackingslipsforanorder.
C#
voidPrintPackingSlip(Orderorder);
20.AddamethodnamedPrintShippingLabelstotheinterfacebody.Thismethodwillbeusedtoprinttheshippinglabesforanorder.
C#
voidPrintShippingLabels(Orderorder);
21.InSolutionExplorer,right-clicktheServicesfolderinthePrintingModuleproject,pointtoAddandthenclickClass.
22.SettheclassnametoLocalPrintingServiceandthenclickAdd.
23.Addthefollowingusingstatementsatthetopofthefile:
C#
usingSystem.Windows.Forms;
usingAdventureWorks.Infrastructure.Interface.BusinessEntities;
24.ChangetheclasssignaturetomakeitpublicandtoimplementtheIPrintingServiceinterface:
C#
publicclassLocalPrintingService:
IPrintingService
25.ImplementthePrintPackingSlipmethod.Inthismethod,displayamessageboxasdemonstratedinthefollowingcode.
C#
publicvoidPrintPackingSlip(Orderorder)
{
MessageBox.Show(String.Format("PackingslipforOrder#{0}senttoprinter...",order.OrderId.ToString()));
}
26.ImplementthePrintShippingLabelsmethod.Inthismethod,displayamessageboxasshowninthefollowingcode.
C#
publicvoidPrintShippingLabels(Orderorder)
{
MessageBox.Show(String.Format("ShippinglabelsforOrder#{0}senttoprinter...",order.OrderId.ToString()));
}
Task4.RegisterthePrintingServiceasaGlobalService
Inthistask,youwillregistertheprintingserv