Home
%3CLINGO-SUB%20id%3D%22lingo-sub-662345%22%20slang%3D%22ja-JP%22%3EDe%3A%20Code%202019%20Xamarin.Forms%20Application%20Design%20Pattern%20description%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-662345%22%20slang%3D%22ja-JP%22%3E%3CP%3EDe%3A%20I%20have%20been%20on%20the%20podium%20in%20code%202019.%3C%2FP%3E%0A%3CP%3EHere%2C%20I%20would%20like%20to%20summarize%20the%20content%20of%20the%20design%20phase%20of%20the%20content%20of%20the%20speaker.The%20presentation%20material%20is%20up%20to%20the%20SlideShare.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.slideshare.net%2Fokazuki0130%2Fxamarinforms-147881590%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EHttps%3A%2F%2Fwww.slideshare.net%2Fokazuki0130%2Fxamarinforms-147881590%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3EWhat%20to%20think%20in%20the%20design%20phase%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EFirst%2C%20this%20session%20is%20for%20the%20track%20owner%20Mr.%20Inoue%20Akira%20(%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fchack411%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40chack441%3C%2FA%3E)%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fja-jp%2Fxamarin%2Fxamarin-forms%2Fenterprise-application-patterns%2F%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3EEnterprise%20Application%20Patterns%20using%20xamarin.forms%20eBook%20%3C%2FA%3EIt%20was%20the%20beginning%20that%20I%20was%20told%20to%20talk%20about%20the%20content%20like.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20I%20read%20this%2C%20and%20read%20the%20sample%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fdotnet-architecture%2FeShopOnContainers%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Eeshopcontainers%20samples%3C%2FA%3E%20that%20have%20been%20touched%20in%20this%2C%20I%20decided%20to%20tell%20you%20that%20I%20was%20in%20the%20session%20content.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20why%20bother%20to%20look%20at%20such%20a%20simple%20class%20division%2C%20programming%20for%20the%20interface%2C%20and%20why%20are%20the%20classic%20patterns%20such%20as%20DI%20are%20used%3FI%20talked%20with%20you%20in%20mind%20that%20it%20is%20easy%20to%20understand.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20we%20share%20the%20common%20perception%20that%20creating%20an%20application%20is%20a%20struggle%20against%20change.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116814iEC9CD99D782AD584%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20101356.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20101356.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAs%20I%20was%20writing%20this%20slide%2C%20I%20remembered%20the%20%3CA%20href%3D%22http%3A%2F%2Feiki.hatenablog.jp%2Fentry%2Fmeteo_fall%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emeteor-fall%20development%3C%2FA%3E%20in%20the%20%22Voice%20of%20Heaven%22%20part.If%20you%20go%20up%20to%20the%20phase%20of%20Ragnarok%2C%20even%20if%20you%20do%20everything%20you're%20talking%20about%20here%2C%20you'll%20feel%20like%20a%20bucket%20of%20water%2C%20but%20you're%20going%20to%20go%20ahead%20with%20the%20project%20while%20praying%20that%20Ragnarok%20won't%20happen%2C%20or%20you%20can%20agree%20with%20the%20stakeholders%20as%20much%20as%20possible.%20We%20have%20to%20reduce%20the%20risk%2C%20but%20there%20is%20another%20story.(God%20is%20a%20god%20because%20he%20comes%20suddenly%2C%20and%20may%20not%20be%20allowed%20to%20root%3F)%EF%BC%89%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20even%20if%20you%20put%20the%20voice%20of%20heaven%2C%20the%20specifications%20of%20the%20application%20will%20change.%3C%2FP%3E%0A%3CP%3EThat%20function%20is%20unnecessary.I%20want%20you%20to%20change%20that%20function%20a%20little...%20And%20so%20on.So%20it's%20better%20to%20make%20the%20application%20stronger%20than%20making%20it%20weaker%3F.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116815iDDAC2774E7C49F99%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20101755.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20101755.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20what%20does%20it%20mean%20to%20be%20strong%20in%20change%3FThere%20are%20many%20things%20to%20consider%20when%20thinking%20about%20it%2C%20but%20I%20gave%20two%20topics%20that%20I%20think%20are%20the%20most%20important%20for%20the%20time%20being%20personally.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESeparation%20of%20appearance%20and%20logic%3C%2FLI%3E%0A%3CLI%3ELoosely%20coupled%20components%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116816iBB330241EFD9EBB8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20101941.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20101941.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt's%20got%20a%20mix%20of%20looks%20and%20logic.%20A%20few%20thousand%20lines%20of%20code%20are%20written%20in%20the%20Click%20event%2C%20so%20I%20think%20the%20code%20is%20going%20to%20be%20in%20a%20state%20where%20I%20don't%20know%20what%20is%20a%20strong%20element%20to%20change.%20And%20I%20think%20it's%20a%20common%20recognition%20somehow%2C%20from%20the%20word%20of%20tightly%20coupled%20components%2C%20so%20I%20feel%20like%20a%20loosely%20coupled%20component%20is%2C%20I%20personally%20like%20to%20create%20a%20strong%20feeling%20to%20change%20the%20atmosphere.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20we%20will%20continue%20to%20talk%20about%20these%20two%20axes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ESeparation%20of%20appearance%20and%20logic%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3EWell%2C%20that's%20the%20separation%20of%20the%20look%20and%20logic%2C%20but%20what%20does%20it%20look%20like%20in%20other%20XAML-based%20technologies%20such%20as%20WPF%20and%20UWP%2C%20in%20addition%20to%20the%20Xamarin.Forms%20of%20the%20subject%3FThat%20would%20mean%20a%20XAML%20file%20and%20a%20code-behind%20class%20for%20it.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116817i8A0D036431D0A630%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20102330.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20102330.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20other%20words%2C%20if%20you%20are%20honest%20about%20the%20separation%20of%20appearance%20and%20logic%2C%20you%20will%20write%20logic%20in%20a%20different%20place%20than%20the%20XAML%20%2B%20code-behind.As%20follows.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116818i8CF965C4079EF7E0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20102440.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20102440.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EHowever%2C%20there%20is%20a%20gap%20in%20the%20contents%20of%20the%20logic%20close%20to%20reflect%20the%20appearance%20as%20it%20is%2C%20if%20you%20ride%20the%20data%20binding%20mechanism%20of%20the%20XAML%20system%20platform%2C%20the%20source%20of%20data%20binding%20(Xamarin.Forms%20BindingContext%2C%20WPF%20or%20UWP%2C%20DataContext)%20must%20contain%20some%20class.Is%20this%20a%20logic%3F%3FBecause%20it%20is%20different%2C%20it%20enters%20another%20class%20roughly%20between.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116819i3051155FA42BD878%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20102711.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20102711.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20other%20words%2C%20this%20form%20is%20the%20MVVM%20pattern%20that%20was%20born%20in%20the%20XAML-based%20neighborhood%20and%20now%20listens%20to%20mobile%20app%20development%20and%20Web%20app%20development%20well.It%20is%20an%20image%20like%20the%20following.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116820i033F794B003CC585%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20102912.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20102912.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESo%2C%20when%20you're%20trying%20to%20create%20a%20change-resistant%20application%20on%20a%20XAML-based%20platform%20and%20you%20want%20to%20isolate%20the%20look%20and%20logic%2C%20you'll%20get%20to%20the%20MVVM%20pattern%20with%20the%20flow%20described.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20if%20you%20want%20to%20isolate%20the%20appearance%20and%20logic%20of%20MVVM%20in%20the%20XAML-based%20platform%2C%20I%20feel%20good%20and%20hotspots.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3ELoosely%20coupled%20components%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIt%20is%20a%20strong%20application%20to%20change%20because%20I%20was%20able%20to%20separate%20the%20appearance%20and%20logic!!You%20did%20it!%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116821i7702FA4517EF73A3%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103247.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103247.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EBecause%20the%20separation%20of%20the%20appearance%20and%20logic%20was%20made%20to%20replace%20only%20the%20appearance%20as%20follows%2C%20the%20logic%20is%20very%20likely%20to%20be%20able%20to%20cope%20with%20no%20impact%20even%20if%20the%20change%20enters%20the%20appearance.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116822iEC6DCD085065A316%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103424.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103424.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIt's%20hard%20to%20imagine%20only%20the%20design%20story%2C%20so%20let's%20take%20a%20look%20at%20the%20code.I'm%20using%20the%20Model%20class%20in%20a%20ViewModel%20code.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116823i9EFE850E1C5A430B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103602.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103602.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EBecause%20I%20use%20Model%20from%20ViewModel%2C%20I%20use%20the%20class%20of%20Model%20in%20ViewModel%20class%20new.It's%20normal.Even%20if%20you%20split%20it%2C%20it's%20still%20a%20single%20application.Eventually%2C%20it%20will%20stick%20together.That's%20what%20this%20is%20about.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116824i78086424B4F8ECF6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103743.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103743.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWell%2C%20yes.Well%2C%20let's%20see%20what%20happens%20in%20this%20state.For%20example%2C%20consider%20a%20dependency%20between%20classes%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116825i8E32D7E9D7C8F333%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103904.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103904.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAnd%2C%20I%20want%20to%20test%20it%20because%20a%20class%20of%20a%20certain%20ViewModel%20was%20able%20to%20be%20done.That's%20why%20we%20have%20a%20solid%20implementation%20of%20the%20model%2C%20so%20we%20need%20to%20implement%20the%20model%20class%20to%20test%20it.Perhaps%20a%20common%20program%20is%20going%20to%20depend%20on%20a%20different%20class%20in%20the%20model%2C%20and%20generally%2C%20when%20you%20follow%20the%20dependency%2C%20you%20eventually%20call%20the%20Web%20API%20or%20access%20the%20database.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116826i644EA18BEB834F15%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20103957.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20103957.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESadly%2C%20if%20you%20try%20to%20test%20the%20ViewModel%20class%2C%20you%20won't%20be%20able%20to%20test%20it%20unless%20you've%20completed%20the%20Web%20API%20before.Even%20if%20you're%20making%20the%20same%20project%2C%20how%20do%20you%20progress%20to%20the%20server-side%20person%3FWhat%20do%20you%20do%20when%20you%20have%20to%20adjust%20to%20listen%20to%20various%20things%2C%20or%20even%20if%20it%20was%20something%20outside%20that%20you%20can%20not%20do%20in%20the%20worst%20of%20our%20own%3F.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOf%20course%2C%20the%20same%20is%20the%20same%20for%20another%20class%20that%20relies%20on%20classes%20that%20call%20the%20same%20Web%20API.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116828iB09591332452CD78%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20104341.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20104341.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%2C%20in%20the%20end%20it%20will%20be%20a%20feeling%20that%20the%20screen%20is%20finished%20properly%20and%20the%20test%20is%20made%20all%20through.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116829iDDD76D08E878C6B5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20104424.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20104424.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20this%20is%20probably%20something%20that%20many%20people%20are%20experiencing.I%20aimed%20at%20the%20design%20of%20the%20ikeike%2C%20but%20it%20becomes%20the%20state%20that%20the%20disadvantage%20due%20to%20the%20layer%20that%20it%20is%20difficult%20to%20understand%20and%20distributed%20that%20the%20management%20is%20difficult%20to%20divide%20somehow%20has%20prevailed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWell%2C%20what%20kind%20of%20solution%20is%20there%20for%20this%3FSo%2C%20%22programming%20the%20Interface%22%20and%20%22basically%20do%20not%20do%20another%20class%20in%20the%20class.%22%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116830iC776E05212B8EE67%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20104713.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20104713.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20means%20that%20you%20have%20dependencies%20on%20the%20interface%20only%20without%20having%20dependencies%20on%20the%20implementation%20of%20the%20class%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116831iF6CE1E3CE8DA9BBC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20104826.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20104826.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EHow%20do%20you%20write%20code%20to%20achieve%20this%3FThis%20means%20that%20the%20interface%20will%20be%20received%20from%20the%20outside%20and%20the%20class%20will%20be%20processed%20using%20the%20interface%20it%20receives.To%20feel%20like%20the%20following.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116832i86AF0F86DF86A4CC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20105005.%20jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20105005.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EThis%20is%20called%20a%20dependency%20injection%20(Dependency%20Injection)%20or%20inversion%20of%20control%20(Inversion%20of%20control).%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116833i1654BA54882156B8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20105319.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20105319.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20look%20at%20the%20flow%20so%20far%2C%20you%20have%20to%20put%20the%20dependency%20from%20the%20outside%20Ah!Or%2C%20I%20think%20that%20the%20image%20of%20whether%20the%20reversal%20of%20control%20by%20the%20control%20from%20the%20outside%20rather%20than%20the%20control%20by%20yourself%20oh.Just%20look%20at%20the%20word%20dependency%20injection%2C%20Aha!This%20is%20it!I%20did%20not.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20is%20the%20difference%20between%20before%20and%20after%20this%20dependency%20injection%3FWhat%20good%20is%20there%3FIn%20terms%20of%20that%2C%20this%20is%20another%20simple%20class%20that%20can%20be%20tested!It%20is%20exhausted.Because%20it%20depends%20only%20on%20the%20interface%2C%20you%20can%20test%20it%20by%20moving%20it%20if%20you%20pass%20the%20appropriate%20test%20interface.Nice.This%20way.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116834iB3A9AD1CEEF1BB42%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20105527.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20105527.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20separation%20of%20the%20appearance%20and%20the%20logic%20can%20be%20done%20by%20this%2C%20and%20the%20test%20in%20the%20part%20unit%20which%20separates%20it%2C%20and%20the%20test%20that%20concentrated%20on%20the%20logic%20of%20the%20part%20can%20be%20done%2C%20and%20it%20is%20possible%20to%20test%20with%20high%20density%20than%20the%20test%20of%20hitting%20it%20all%20because%20it%20is%20possible%20to%20do.Now%20you%20win...%20I%20feel%20that%20this%20is%20still%20troublesome%20in%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20if%20it%20is%20in%20a%20loosely%20coupled%20state%20in%20parts%2C%20it%20will%20eventually%20become%20a%20single%20application%2C%20so%20you%20have%20to%20make%20a%20part%20of%20it%20somewhere%20apart.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116835iCB46B3E630C9074D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20105723.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20105723.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIt's%20better%20to%20isolate%20it.%20Because%20dependencies%20are%20made%20to%20receive%20interfaces%20in%20constructors%20and%20so%20on%2C%20in%20fact%2C%20it%20is%20not%20a%20simple%20form%20like%20the%20one%20above%2C%20and%20you%20have%20to%20build%20up%20a%20tree%20of%20dependencies%20for%20several%20classes%20as%20follows%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116836i923421655C0F7B18%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20105929.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20105929.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20the%20above%20example%20I%20have%20all%20new%2C%20for%20example%2C%20when%20I%20think%20of%20a%20class%20such%20as%20on-memory%20cache%20in%20the%20app%20every%20time%20it%20is%20a%20cache%20I'll%20feel%20like%20goodbye.It's%20OK%20to%20leave%20the%20cache%20in%20a%20static%20area%2C%20but%20it%20seems%20to%20seemed%20wearisome%20that%20you%20want%20to%20manage%20multiple%20caches.That's%20why%20I%20want%20to%20manage%20the%20cache%20class%20in%20a%20singleton%20anyway!When%20you%20start%20thinking%20about%20things%2C%20Mendokusa%20will%20double.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYes%2C%20if%20you%20don't%20assemble%20and%20use%20the%20parts%20you've%20created%20in%20isolation%2C%20it%20will%20become%20a%20hotbed%20for%20bugs%2C%20and%20it's%20a%20pain%20in%20the%20neck.So%2C%20there%20are%20libraries%20that%20specialize%20in%20assembling%20objects%20in%20the%20world.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20called%20a%20DI%20container.%3C%2FP%3E%0A%3CP%3EIf%20this%20interface%20is%20requested%20in%20the%20DI%20container%2C%20pass%20an%20instance%20of%20this%20implementation%20class!!The%20life%20cycle%20of%20an%20instance%20of%20this%20class%20uses%20a%20new%20one%20every%20time!Or%20manage%20it%20with%20a%20singleton!If%20you%20define%20a%20rule%2C%20you%20will%20be%20able%20to%20follow%20the%20rules%20to%20create%20an%20instance%20of%20the%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20a%20library%20of%20various%20DI%20containers%2C%20but%20roughly%20the%20following%20is%20a%20feeling%20of%20use.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116837i690E42226F88EEB8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20110417.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20110417.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ERegistration%20of%20the%20assembly%20rules%20so%20you%20just%20have%20to%20do%20it%20once%20somewhere%2C%20everyone%20is%20better%20without%20knowing%20the%20details%20of%20the%20rules%2C%20it%20is%20very%20convenient.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20using%20the%20DI%20container%20in%20the%20MVVM%20pattern%2C%20what%20instance%20do%20you%20want%20to%20manage%20in%20the%20DI%20container%3FI%20have%20to%20think%20about%20that.Only%20Model%3FOr%20ViewModel%20and%20Model%3FOr%20View%20and%20ViewModel%20and%20Model%20all%3FAnd%20such%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELooking%20at%20the%20general%20public%2C%20most%20samples%20and%20libraries%20have%20been%20instance-managed%20in%20the%20DI%20container%20until%20ViewModel%2C%20and%20the%20instance%20to%20be%20set%20to%20the%20BindingContext%20and%20DataContext%20of%20the%20View%20DI%20container%20It%20is%20made%20in%20the%20form%20of%20acquiring%20from.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20the%20eShopContainers%20sample%20also%20registers%20an%20instance%20of%20ViewModel%20and%20Model%20in%20the%20DI%20container%20in%20the%20class%20ViewModelLocator%2C%20and%20it%20is%20automatically%20ViewModel%20from%20the%20class%20name%20of%20the%20View%20Class%2C%20and%20then%20take%20it%20out%20of%20the%20DI%20container%20and%20set%20it%20to%20View.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EEShopOnContainers%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fdotnet-architecture%2FeShopOnContainers%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2Fdotnet-architecture%2FeShopOnContainers%3C%2FA%3E%3CBR%20%2F%3Esrc%2FMobile%2F%20EShopOnContainers%2FeShopContainers.Core%2FeShopOnContainers.Core%2FViewModels%2FBase%2FViewModelLocator.cs%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20addition%20to%20the%20Prism%20for%20Xamarin.Forms%20Library%2C%20the%20MVVM%20Light%20Toolkit%20also%20takes%20an%20approach%20such%20as%20retrieving%20ViewModel%20instances%20from%20the%20DI%20container.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EPrism%20for%20xamarin.forms%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fprismlibrary.github.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fprismlibrary.github.io%2F%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3EMVVM%20Light%20Toolkit%3CBR%20%2F%3E%3CA%20href%3D%22http%3A%2F%2Fwww.mvvmlight.net%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.mvvmlight.net%2F%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EDoing%20so%20far%2C%20and%20finally%20applying%20the%20MVVM%20pattern%2C%20while%20separating%20the%20look%20and%20logic%2C%20in%20a%20realistic%20Mendokusa%2C%20it%20is%20possible%20to%20test%20the%20individual%20parts%20unit.In%20addition%2C%20it%20came%20to%20be%20able%20to%20replace%20it%20to%20another%20part%20if%20it%20kept%20even%20the%20interface.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%20is%20convenient%20to%20make%20it%20like%20this.For%20example%2C%20when%20I%20was%20making%20a%20demo%20app%20in%20WPF%20in%20the%20old%20days%2C%20I%20made%20it%20like%20this%2C%20and%20at%20the%20end%2C%20%22after%20all%2C%20this%20function%20is%20unstable%2C%20so%20I%20do%20not%20use%20it%20in%20the%20demo%2C%22%20when%20it%20was%20said%2C%20the%20corresponding%20processing%20to%20the%20DI%20container%201%20There%20was%20a%20correspondence%20completion%20by%20the%20line%20comment%20out.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWell%2C%20that%20can%20be%20solved%20neatly%20and%20end%20up%20with%20bad%20luck%2C%20but%20it%20is%20not%20quite%2C%20but%20it%20is%20not%20a%20loss%20to%20do%20because%20it%20is%20possible%20to%20do%20it%20with%20a%20realistic%20effort%20to%20raise%20the%20possibility%20that%20it%20is.It%20should%20be%20easier%20than%20a%20tightly%20coupled%20thing%20even%20if%20the%20change%20cannot%20be%20applied%20rapidly...!%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%20was%20the%20story%20of%20a%20basic%20MVVM%20application.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3EI%20want%20a%20function%20like%20this.%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EI%20really%20want%20to%20make%20an%20application%20like%20this...%20You%20may%20think.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EScreen%20transitions%3C%2FLI%3E%0A%3CLI%3EHow%20to%20transition%20from%20ViewModel%20to%20screen%3C%2FLI%3E%0A%3CLI%3EShow%20dialog%3C%2FLI%3E%0A%3CLI%3EDialog%20display%20function%20from%20ViewModel%3C%2FLI%3E%0A%3CLI%3EValidating%20input%20values%3C%2FLI%3E%0A%3CLI%3EGeneral%20implementation%20by%20DataAnnotations%2C%20etc.%3C%2FLI%3E%0A%3CLI%3ECertification%20and%20authorization%3C%2FLI%3E%0A%3CLI%3EAre%20there%20a%20lot%20of%20OAuth%20correspondence%20recently%3F%3CBR%20%2F%3EAzure%20AD%20%2B%20ADAL%20for%20.NET%20or%20MSAL.NET%2C%20features%20such%20as%20MBaaS%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThe%20person%20who%20takes%20care%20of%20the%20whole%20can%20be%20happy%20when%20the%20mechanism%20of%20this%20neighborhood%20is%20prepared%20by%20an%20additional.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%2C%20I%20know%20MVVM%2C%20but%20what%20happens%20in%20Model%3FI%20often%20hear%20that.Honestly%2C%20there%20is%20no%20silver%20bullet%20like%20this%2C%20but%20personally%20I%20try%20to%20read%20the%20famous%20things%20such%20as%20Clean%20Architecture%20and%20try%20to%20pick%20up%20the%20parts%20that%20are%20necessary%20for%20ourselves%20and%20experiment%20a%20small%20phase%2C%20I%20think%20it's%20a%20good%20thing%20to%20add%20what%20you%20think%20is%20not%20enough.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116838iD473AB6AD57E7F4F%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20111459.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20111459.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20think%20about%20the%20shape%20of%20the%20following%20roughly%20as%20a%20starting%20point%2C%20and%20I%20think%20that%20there%20is%20a%20layer%20like%20this%2C%20and%2C%20if%20it%20is%20this%20scale%2C%20it%20will%20be%20OK%20even%20if%20the%20hand%20is%20pulled%20out.(You%20may%20regret%20it%20later%20if%20you%20pull%20out%20your%20hand...)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116839i31525540F8128D0D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20111703.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20111703.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20personally%20recommend%20that%20you%20consider%20using%20a%20library%20to%20assist%20MVVM%20in%20making%20the%20actual%20creation.%3C%2FP%3E%0A%3CP%3EIn%20the%20case%20of%20Xamarin.Forms%2C%20Prism%20for%20Xamarin.Forms%20is%20recommended%20for%20a%20lot%20of%20functions.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116840i1BACDD85DA8938BF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20111838.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20111838.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EEven%20if%20you%20don't%20actually%20use%20it%2C%20making%20choices%20that%20you%20don't%20use%20means%20that%20you%20make%20the%20functions%20that%20the%20library%20provides.At%20that%20time%2C%20what%20kind%20of%20function%20should%20I%20have%3FHow%20is%20it%20useful%20to%20be%20able%20to%20use%20it%3FIt%20becomes%20very%20helpful.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20example%2C%20if%20you%20want%20to%20do%20things%20like%20activating%20a%20command%20in%20an%20MVVM%20pattern%2C%20and%20then%20monitoring%20and%20modifying%20a%20specific%20property%20change%2C%20you%20might%20want%20to%20write%20a%20change%20notification%20for%20a%20command%20in%20a%20property%20setter.%3C%2FP%3E%0A%3CPRE%3Eprivate%20DelegateCommand%20_fooCommand%3B%0Apublic%20DelegateCommand%20FooCommand%0A%7B%0A%20%20%20%20get%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(_fooCommand%20!%3D%20null)%20%7B%20return%20_fooCommand%3B%20%7D%0A%20%20%20%20%20%20%20%20_fooCommand%20%3D%20new%20DelegateCommand(....)%3B%0A%20%20%20%20%7D%0A%7D%0A%0Aprivate%20string%20_someProperty%3B%0Apublic%20string%20SomeProperty%0A%7B%0A%20%20%20%20get%20%3D%26gt%3B%20_someProperty%3B%0A%20%20%20%20set%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20SetProperty(ref%20_someProperty%2C%20value)%3B%20%2F%2F%20%E5%80%A4%E3%81%AE%E6%9B%B4%E6%96%B0%E3%81%A8PropertyChanged%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%AF%E7%99%BA%E8%A1%8C%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8C%E3%82%8B%E3%82%88%E3%81%86%E3%81%AA%E4%BE%BF%E5%88%A9%E3%81%AA%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%0A%20%20%20%20%20%20%20%20FooCommand.RaiseCanExecuteChanged()%3B%20%2F%2F%20%E3%81%93%E3%81%93%E3%81%A7%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%81%AE%E6%B4%BB%E6%80%A7%E5%8C%96%E3%83%BB%E9%9D%9E%E6%B4%BB%E6%80%A7%E5%8C%96%E3%81%AE%E5%86%8D%E8%A9%95%E4%BE%A1%E3%82%92%E4%BE%9D%E9%A0%BC%E3%81%99%E3%82%8B%0A%20%20%20%20%7D%0A%7D%3C%2FPRE%3E%0A%3CP%3EIn%20other%20words%2C%20just%20looking%20at%20a%20command%20makes%20it%20difficult%20to%20see%20how%20this%20command%20depends%20on%20what%20properties%20it%20changes.If%20there%20is%20one%20property%20that%20depends%2C%20it%20is%20still%20good%2C%20but%20it%20is%20troublesome%20in%20various%20ways%20when%20becoming%20plural.%3C%2FP%3E%0A%3CP%3EIn%20the%20case%20of%20Prism%2C%20there%20is%20a%20method%20called%20ObservesProperty%20in%20the%20command%20class%2C%20which%20allows%20the%20command%20itself%20to%20monitor%20the%20properties%20and%20to%20activate%20and%20deactivate%20them.Very%20convenient.%3C%2FP%3E%0A%3CPRE%3Eprivate%20DelegateCommand%20_fooCommand%3B%0Apublic%20DelegateCommand%20FooCommand%0A%7B%0A%20%20%20%20get%20%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20if%20(_fooCommand%20!%3D%20null)%20%7B%20return%20_fooCommand%3B%20%7D%0A%20%20%20%20%20%20%20%20_fooCommand%20%3D%20new%20DelegateCommand(....)%0A%20%20%20%20%20%20%20%20%20%20%20%20.ObservesProperty(()%20%3D%26gt%3B%20SomeProperty)%3B%20%2F%2F%20%E6%B4%BB%E6%80%A7%E5%8C%96%E3%83%BB%E9%9D%9E%E6%B4%BB%E6%80%A7%E5%8C%96%E3%81%AE%E7%8A%B6%E6%85%8B%E5%A4%89%E6%9B%B4%E3%81%AF%E3%80%81%E3%81%93%E3%81%AE%E3%83%97%E3%83%AD%E3%83%91%E3%83%86%E3%82%A3%E3%81%AE%E5%80%A4%E3%81%AE%E5%A4%89%E6%9B%B4%E3%82%92%E7%9B%A3%E8%A6%96%E3%81%97%E3%81%A6%E8%87%AA%E5%8B%95%E3%81%A7%E8%A1%8C%E3%81%86%0A%20%20%20%20%7D%0A%7D%0A%0Aprivate%20string%20_someProperty%3B%0Apublic%20string%20SomeProperty%0A%7B%0A%20%20%20%20get%20%3D%26gt%3B%20_someProperty%3B%0A%20%20%20%20set%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20SetProperty(ref%20_someProperty%2C%20value)%3B%20%2F%2F%20%E5%80%A4%E3%81%AE%E6%9B%B4%E6%96%B0%E3%81%A8PropertyChanged%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E3%81%AF%E7%99%BA%E8%A1%8C%E3%81%97%E3%81%A6%E3%81%8F%E3%82%8C%E3%82%8B%E3%82%88%E3%81%86%E3%81%AA%E4%BE%BF%E5%88%A9%E3%81%AA%E3%83%A1%E3%82%BD%E3%83%83%E3%83%89%0A%20%20%20%20%7D%0A%7D%3C%2FPRE%3E%0A%3CP%3EAs%20such%2C%20because%20there%20is%20also%20a%20function%20to%20take%20care%20of%20such%20parts%20and%20troublesome%20to%20do%20in%20various%20normal%2C%20even%20when%20you%20do%20it%20yourself%2C%20or%20try%20to%20use%20the%20existing%20library%20in%20order%20to%20know%20in%20advance%20that%20there%20is%20such%20a%20function%20and%20useful%2C%20It%20is%20recommended%20that%20you%20investigate.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20size%3D%225%22%3E%3CSTRONG%3ESummary%3C%2FSTRONG%3E%3C%2FFONT%3E%3C%2FP%3E%0A%3CP%3EIt%20is%20summarized%20in%20that.Let's%20make%20it%20easy%20for%20unit%20testing%20to%20be%20aware%20of%20the%20separation%20of%20appearance%20and%20logic%20and%20loosely%20coupled%20components.And%2C%20please%20check%20it%20by%20all%20means%20because%20the%20library%20is%20full%20of%20the%20wisdom%20of%20the%20ancestors.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116844i6099E1B21B263167%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20113008.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20113008.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20measures%20we%20are%20doing%20here%20are%20intended%20to%20reduce%20the%20hotness.%3C%2FP%3E%0A%3CP%3EIf%20it's%20hard%20to%20do%20something%20wrong%2C%20or%20you%20might%20be%20missing%20a%20way%20to%20make%20it%20easier%20if%20you're%20not%20happy%2C%20how%20are%20you%20doing%20in%20the%20world%3FI%20think%20that%20it%20is%20good%20to%20ask%20the%20person%20who%20seems%20to%20be%20strong.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20if%20it%20is%20not%20applicable%20in%20the%20current%20project%2C%20the%20next%2C%20and%20also%20to%20make%20it%20easy%20next.So%2C%20please%20develop%20a%20fun%20app%20by%20all%20means.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116845i054AB6C3AB624CC2%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20113200.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20113200.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-662345%22%20slang%3D%22ja-JP%22%3E%3CP%3EMake%20it%20easy%20to%20MVVM%20(Nordhaven)%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F116846i52D036CFB3A0BED7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Comments%202019-06-01%20102912.jpg%22%20title%3D%22%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%202019-06-01%20102912.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Microsoft

de:code 2019 で登壇してきました。

ここでは、そこでの登壇内容のうち設計フェーズの内容についてまとめて書きたいと思います。登壇資料は SlideShare に上がっています。

 

https://www.slideshare.net/okazuki0130/xamarinforms-147881590

 

設計フェーズで考えること

まず、このセッションはトラックオーナーのちゃっくさんこと井上 章さん(@chack441)に Enterprise Application Patterns using Xamarin.Forms eBook のような内容を話してくれと言われたのが始まりでした。

 

なので、これを読んで、この中で触れられているサンプルの eShopContainers のサンプルを読んで自分ならこうかなぁというのを伝えようと考えてセッション内容をくみたてました。

 

そこで何故わざわざこんな単純に見るとイチイチめんどくさいと思えるようなクラスの分割、インターフェースに対するプログラミング、DIなどの定番パターンが何故使われているのだろう?ということをわかりやすく伝えるということを念頭にお話しをしました。

 

まず、アプリケーションを作るということは変化との闘いであるという共通認識を全員に対して共有します。

コメント 2019-06-01 101356.jpg

このスライドを書いていて "天の声" の部分でメテオフォール型開発のことを思い出しました。ラグナロクのフェーズまで進むと、ここで話してることをすべてしてても焼け石に水な気もしますがラグナロクが起きないことを祈りながらプロジェクトを進めていく、もしくは利害関係者には可能な限り合意をとっておくということで、リスクを減らすことをしていかないといけないですが、そこはまた別のお話し。(神は突然やってくるから神なので、根回ししてもダメなのかも?)

 

ということで、天の声を置いておいてもアプリケーションの仕様は変わっていきます。

あの機能いらない。あの機能ちょっと変えてほしいんだけど…などなど。ということで、アプリケーションは変化に弱く作るよりは強く作った方がいいよね?ということになります。

コメント 2019-06-01 101755.jpg

 

では、変化に強いということはどういうことなのか?というのを考えたときに色々なことが考えられますが、とりあえず個人的に一番重要だと思う 2 つのトピックをあげました。

  • 見た目とロジックの分離
  • 疎結合なコンポーネント

コメント 2019-06-01 101941.jpg

 

見た目とロジックが混ざってるクリックイベントに数千行のコードが書かれてるコードは、もう何が変化に強い要素なのかわからない状態になると思うので見た目とロジックは少なくとも分離したほうがよいというのは、なんとなく共通認識なのかなと思うのと、密結合なコンポーネントという単語からは、そこはかとなくきな臭い感じがするので疎結合なコンポーネントが、雰囲気変化に強そうな感じを醸し出してて個人的に好きです。

 

ということで、この 2 つを軸に話を進めていきます。

 

見た目とロジックの分離

さて、ということで見た目とロジックの分離ですが今回主題の Xamarin.Forms のほかにも WPF や UWP などの XAML 系テクノロジーにおいて見た目とはなんだろう?と考えると XAML ファイルと、それのコードビハインドクラスということになります。

コメント 2019-06-01 102330.jpg

つまり見た目とロジックの分離ということを素直にすると以下のように XAML + コードビハインドとは別の場所にロジックを書くということになります。以下のように。

 コメント 2019-06-01 102440.jpg

ただ、ロジック寄りの中身を、そのまま見た目に反映させるとギャップがあったりするのと、XAML 系プラットフォームのデータバインディングの仕組みに乗る場合には、データバインドのソース(Xamarin.Forms では BindingContext, WPF や UWP では DataContext) に何らかのクラスを入れる必要があります。これはロジックか??というと違うので、大体間にもう 1 クラス入ります。

コメント 2019-06-01 102711.jpg

つまり、この形が XAML 系界隈でうまれて今ではモバイルアプリ開発や Web アプリ開発でもよく聞くようになった MVVM パターンということになります。下のようなイメージですね。

コメント 2019-06-01 102912.jpg

ということで、XAML 系プラットフォームで変化に強いアプリケーションを作ろうとして見た目とロジックの分離を進めていくと、ここまで説明したような流れで MVVM パターンに行きつきます。

 

なので見た目とロジックの分離がしたい場合には MVVM をすると XAML 系プラットフォームでは、割とすぽっといい感じにはまります。

 

疎結合なコンポーネント

見た目とロジックの分離が出来たので変化に強いアプリケーションだ!!やったね!!

コメント 2019-06-01 103247.jpg

ということで見た目とロジックの分離がされたので以下のように見た目だけ差し替えるということや、見た目に対して変更が入ってもロジックには無影響で対応できる可能性が非常に高くなりました。

コメント 2019-06-01 103424.jpg

設計の話だけだとイメージがつきにくいので、少しコードを見てみましょう。とある ViewModel のコードで、Model のクラスを使っている様子です。

コメント 2019-06-01 103602.jpg

ViewModel から Model を使うので ViewModel のクラスで Model のクラスを new して使ってます。普通ですね。分割したとしても、しょせんは 1 つのアプリケーションです。最終的には、くっついて動きます。つまりこういうことです。

コメント 2019-06-01 103743.jpg

まぁ、そうですね。じゃぁこの状態で何が起きるか見てみましょう。例えば以下のようにクラス間で依存関係があるようなもので考えてみます。

コメント 2019-06-01 103904.jpg

そして、ある ViewModel のクラスが出来たのでテストしてみたいと思います。そうすると、モデルの実装をがっちりつかんでるので当然テストするにはモデルのクラスの実装が必要になります。おそらく一般的なプログラムだとモデルの中で別のクラスに依存しているということが起きていて、一般的に依存をたどっていくと最終的には Web API を呼んでいたりデータベースにアクセスしていたりといったことが起きます。

コメント 2019-06-01 103957.jpg

悲しいことに ViewModel のクラスをテストしようとすると Web API の先まで完成していないとテストが出来ません。同じプロジェクトで作ってるにしても、サーバー側担当の人に進捗どうですか?とか色々聞いて調整しないといけなかったり、さらに最悪なことに自分たちではどうしようも出来ない外部のものだったりしたときには、どうすればいいんだろう?ということになります。

 

もちろん同じ Web API を呼ぶクラスに依存してる別のクラスでも同じことがおきますね。

コメント 2019-06-01 104341.jpg

 

なので、結局は最終的に画面がちゃんと出来上がって全部作って通しでテストしてみるという感じになります。

コメント 2019-06-01 104424.jpg

 

これは恐らく多くの人が体験していることではないかと思います。イケイケの設計を目指したけど、なんか分割したぶん管理が大変だとか分散してわかりにくいというレイヤーわけしてしまったことによるデメリットが勝っているような状態になります。

 

じゃぁ、これに対してどういう解決策があるの?ということで「インターフェースに対してプログラミングをする」と「基本的にクラス内で別のクラスを new しない」というのがあります。

コメント 2019-06-01 104713.jpg

 

つまり、以下のようにクラスの実装に対して依存関係を持たせずにインターフェースのみに依存関係を持たせるということです。

コメント 2019-06-01 104826.jpg

これを実現するには、どういう風にコードを書けばいいのか?ということになりますが、これはインターフェースを外部から受け取るようにして、クラス内では受け取ったインターフェースを使って処理をするということになります。以下のような感じに。

コメント 2019-06-01 105005.jpg

こういうのが依存性注入(Dependency Injection)や制御の反転(Inversion of Control)と言ったりします。

コメント 2019-06-01 105319.jpg

 

ここまでの流れを見ると、あぁ外から依存関係入れてもらうのね!とか、あぁ自分で制御するんじゃなくて外から制御してもらうってことで制御の反転なのかなぁっていうイメージがわくと思います。依存性注入という単語だけ見て、なるほど!こういうことか!って私はなりませんでした。

 

この依存性注入をする前と、した後でどういう違いがあるのか?どんないいことがあるのか?という点についてですが、これは、もうシンプルにクラス単体でテストが出来るようになる!ということに尽きます。インターフェースにしか依存してないので、適当なテスト用のインターフェースを渡してやれば動かしてテスト出来ます。素敵。こんな感じで。

コメント 2019-06-01 105527.jpg

 

これで見た目とロジックの分離が出来て、なおかつ分離した部品単位でのテストによって、その部分のロジックに集中したテストが出来るので全部繋いでやる打検テストよりも、密度の高いテストが出来るようになりました!!これで勝つる…という感じですけど、これはこれでまだめんどくさいことが残ってます。

 

部品単位でバラバラな疎結合な状態にしても、最終的には 1 つのアプリケーションになるので、どこかでバラバラにした部品をくみたてないといけません。

コメント 2019-06-01 105723.jpg

分離したほうがいい依存関係はインターフェースをコンストラクターなどで受け取るように作っているので実際には上記のような単純な形ではなく、以下のように複数のクラスの依存関係のツリーをくみたてないといけません。

コメント 2019-06-01 105929.jpg

上記の例では全部 new してますが、例えばアプリでオンメモリキャッシュしてるようなクラスを考えると毎回 new してるとキャッシュよさようならみたいな感じになりますよね。static な領域にキャッシュ残しててもいいけど、それはそれでキャッシュを複数管理したいとか色々考えだすとめんどくさそう。ということでキャッシュクラスはシングルトンでとりあえず管理したいんだ!ってなったり色々考えだすと、さらにめんどくささが倍増していきます。

 

そう、分離して作った部品を皆が正しく組み立てて使ってくれないと、それはそれでバグの温床になったりしますし、何よりもめんどくさいです。なので、世の中にはオブジェクトの組み立てを専門にやってくれるライブラリーがあったりします。

 

そういうのを DI コンテナーと呼んでたりします。

DI コンテナーに、このインターフェースが要求されたら、この実装クラスのインスタンスを渡して!!このクラスのインスタンスのライフサイクルは毎回新しいのを使って!とかシングルトンで管理して!といったように、ルールを定義しておくと、そのルールに従ってよしなにクラスのインスタンスをくみたててくれます。

 

色々な DI コンテナーのライブラリーがありますが大体以下のような使用感です。

コメント 2019-06-01 110417.jpg

組み立てルールの登録は一度どこかでやっておけばいいので、全員がルールを細部まで把握しなくてよくなり、とても便利です。

 

MVVM パターンで DI コンテナーを使う時に、何処のインスタンスまで DI コンテナーで管理するのか?といったことを考えないといけません。Model だけ?とか ViewModel と Model ?とか、View と ViewModel と Model 全部?とか。

 

世間一般を見渡してみると、大体のサンプルやライブラリーが ViewModel まで DI コンテナーでインスタンス管理をしていて、View の BindingContext や DataContext に設定するインスタンスを DI コンテナーから取得するといった形で作られています。

 

例えば eShopContainers のサンプルも ViewModelLocator というクラス内で ViewModel と Model のインスタンスを DI コンテナーに登録していて、View のクラス名から自動的に ViewModel のクラスを予測して、DI コンテナーから取り出して View に設定するといったことをやっています。

 

その他にも Prism for Xamarin.Forms というライブラリーでも、MVVM Light Toolkit でも同じように ViewModel のインスタンスを DI コンテナーから取得するといったアプローチがとられています。

ここまでやって、やっと MVVM パターンを適用して見た目とロジックを分離しつつ、現実的なめんどくささで、個別の部品単位のテストが出来る。さらにはインターフェースさえ守ってれば別の部品にさくっと差し替えるといったことが出来るようになりました。

 

こんな感じで作っておくと便利です。例えば私は、昔にデモ用アプリを WPF で作ってるときに、こんな感じで作っていて、終盤になって「やっぱり、この機能不安定だからデモでは使わないから消しといて」と言われた時に、DI コンテナーへの該当処理を 1 行コメントアウトすることで対応完了ということがありました。

 

まぁ、運が悪いとそれだけで終わるといった綺麗に解決できることは、なかなか無いのですが、そうなる可能性を上げておくというのを現実的な労力で行えるのでやっておいて損はないです。綺麗にすぱっと変更が適用できなくても密結合なものよりは楽なはず…!

 

ということで基本的な MVVM を適用した場合のお話しでした。

 

こんな機能も欲しい

実際にアプリケーションを作ってると、こんな機能欲しいな…と思うことがあります。

  • 画面遷移
  • ViewModel からの画面遷移方法
  • ダイアログの表示
  • ViewModel からのダイアログ表示機能
  • 入力値の検証
  • DataAnnotations などによる汎用的な実装
  • 認証・認可
  • OAuth 対応が最近は多い?
    Azure AD + ADAL for .NET or MSAL.NET、MBaaS の機能など

全体の面倒を見る人は、ここらへんくらいの仕組みは追加で用意してあげると、幸せになれます。

 

さて、MVVM はわかったけど Model の中ってどうなるの?といったこともよく聞きます。正直これといった銀の弾丸はないのですが、個人的には Clean Architecture などの有名どころを読んでみて自分たちに必要な部分をピックアップして小さく試してみるというフェーズをやってみて、足りないと思ったものを足すという事をするのがいいと思ってます。

コメント 2019-06-01 111459.jpg

 

私は大体以下のような形をスタート地点として考えて、こういうレイヤーもあったほうがいいなとか、今回の規模ならここは手を抜いても大丈夫だろうとか考えたりします。(手を抜くと後で後悔することもありますが…)

コメント 2019-06-01 111703.jpg

 

そして、実際に作るにあたって MVVM を支援するライブラリーの使用を検討してみることを個人的にはお勧めします。

Xamarin.Forms の場合は機能が多いということで Prism for Xamarin.Forms をお勧めします。

コメント 2019-06-01 111838.jpg

実際に使わないにしても、使わない選択をするということは、ライブラリーが提供しているような機能を自分たちで作るという事です。その時に、どんな機能があればいいのか?どういう風に使えるようにすると便利なのか?といった参考にとてもなります。

 

例えば MVVM パターンでコマンドの活性化、非活性化を特定のプロパティの変更を監視して変更するといったような処理をする場合ですが、プロパティのセッターでコマンドに対して変更通知を行うような処理を書いたりします。

private DelegateCommand _fooCommand;
public DelegateCommand FooCommand
{
    get 
    {
        if (_fooCommand != null) { return _fooCommand; }
        _fooCommand = new DelegateCommand(....);
    }
}

private string _someProperty;
public string SomeProperty
{
    get => _someProperty;
    set
    {
        SetProperty(ref _someProperty, value); // 値の更新とPropertyChangedイベントは発行してくれるような便利なメソッド
        FooCommand.RaiseCanExecuteChanged(); // ここでコマンドの活性化・非活性化の再評価を依頼する
    }
}

つまり、コマンドを見ただけでは、このコマンドが何のプロパティに依存して変化するのかということがわかりにくくなります。依存してるプロパティが 1 つならまだいいですが複数になってきたりすると色々めんどくさいです。

Prism の場合はコマンドのクラスに ObservesProperty というメソッドがあって、プロパティを監視して活性化・非活性化をコマンド自身が行うようにする仕組みがあります。とても便利。

private DelegateCommand _fooCommand;
public DelegateCommand FooCommand
{
    get 
    {
        if (_fooCommand != null) { return _fooCommand; }
        _fooCommand = new DelegateCommand(....)
            .ObservesProperty(() => SomeProperty); // 活性化・非活性化の状態変更は、このプロパティの値の変更を監視して自動で行う
    }
}

private string _someProperty;
public string SomeProperty
{
    get => _someProperty;
    set
    {
        SetProperty(ref _someProperty, value); // 値の更新とPropertyChangedイベントは発行してくれるような便利なメソッド
    }
}

といったように、色々普通にやっているとめんどくさい部分などを面倒見てくれる機能もあったりするので、自分でやるときも、こういう機能があると便利なんだなというのを事前に把握しておくためにも既存のライブラリーを使ってみたり、調査してみることはお勧めです。

 

まとめ

ということでまとめです。見た目とロジックの分離、疎結合なコンポーネントを意識して単体テストしやすいように作りましょう。そしてライブラリーは先人の知恵が詰まってるので是非チェックしてみてください。

コメント 2019-06-01 113008.jpg

 

ここでやってる施策などは、辛さを減らすためのものです。

逆につらくなるなら何か間違ってたり、楽する方法を見逃してる可能性があるので楽しくない辛い場合には、世間ではどうやってるんだろう?とか強そうな人に聞いてみるのがいいと思います。

 

今のプロジェクトでは適用できなくても、次や、また次で楽するためにも。ということで、是非楽しいアプリ開発をしてください。

コメント 2019-06-01 113200.jpg