Exercise 1: Injecting a ControllerIn this exercise, you will learn how to use Dependency Injection in MVC Controllers, by integrating Unity Application Block. For that reason you will include services into your MVC Music Store controllers to separate the logic from the data access. The service will create a new dependence into the controller constructor that will be resolved using Dependency Injection with the help of Unity application block. With this approach you will learn how to generate less coupled applications, which are more flexible and easier to maintain and test. Additionally, you will also learn how to integrate MVC with Unity. About StoreManager ServiceThe MVC Music Store provided in the begin solution now includes a service that manages the Store Controller data, StoreService. Below you will find the Store Service implementation. Note that all the methods return Model entities. C# - StoreService.cs using System;FakePre-482e07a31d834d1690233e79a087a610-b1f78c48118341aa9c67edc549e61d3bFakePre-48a3ba0cfb4b49f397b80b37980a5f2a-8909501d52314068966d6d367ec29053FakePre-9ffdf06e7dc24c968b0979e7386900a0-3d0f35cb69924a6480ea2046b2f5bcfbFakePre-6a7bdb7c787946ee866bd27a477bc152-45790d300fca4204af172e65579c1448FakePre-0dc21fce8c7a4a2a91c6a9067f664048-4f423e68d6b64215a16ae4dc9ff795e8FakePre-cb393aea07374e5c9b949e1e70a6e358-6b210c8c82f44cb098548986d10e8615FakePre-d768b3e067ae4345a53f609521865d59-3bebf80d1f8944e7a203e2cff611d5c3FakePre-04d39acc4608436695ce8452c939e79d-2eb603d7f69f48a4a8b35d476bd01962FakePre-ad2783f023104a228c106f7a272c92a1-26d0540d30bc437293b5cd149013ddb3 MusicStoreEntities storeDB = new MusicStoreEntities();FakePre-4f509c5c61ee4c01ab1ec5744d3ec7ac-1de144c4606d4c7a89a24c11011670c3FakePre-49dc126ff67142a2a974ac68ca13cc0c-5a8d87e8e9334b25b84c124fdcd49207FakePre-e174ab886a8d4036a73bf01293917201-bfacc9e6a6ab475591d76c6b2ba7bd13FakePre-47518c4a408949b19bb0e0702cb05368-54ca8cb86b544209b2b433d8141c3c57FakePre-e14ce58a421a426297ae591454122aa4-4856ddd33b98479590053f03ebd16dbfFakePre-fc2731917da349e69683b91e05800435-6653de7e22fb4b26923e683a35dbf6edFakePre-3674ff15930444d0933d628892cf3819-9d83ecfc22004e8c822162f8f9b808f3FakePre-a4a35630b57341d58f90c44813821646-e506443320774e898d1d6625f45f7670FakePre-f5197dbedb474f6aba2668a0c2ddef3b-049b92823e604439945fe807750a7d0fFakePre-bf31d585eb5443c99644d9706e36aea7-2db77e5c585d4ab4a0a489c4098cf815FakePre-9b236dafcc3b4683b914143609dd0216-eec1c84a208e4807b3f3126d11871a3cFakePre-eb14ae792ae94651ad8e84e613e4896c-59a95bc29d22478ead54c0d78534a45cFakePre-6b01f38fb61948428438fa441977a518-bc2b7f0ed01c435f8348d3d0c107f504FakePre-133275ad7639400a9cda0cb840ecdabd-80c0c6fd2a034cb49fd13935f07d3844FakePre-e6096d064eba4aa995e0657d89d999ab-0a30c2e32b3c4824a0c1c2a30f5583d9FakePre-dc70cfeb555242749bd346dd353fa62d-6934286b95044b0c8884466bc4d73553FakePre-8d88253769d1408294924ccf8db9abab-0e94daae551a4127b0b63d2b90f5ca19FakePre-9c2792f74774424aaa7a9b425cdfb715-e598a36ef0f24f998e33ac55ad5d26deFakePre-0a0bcba1805a4f1496627392bcb75827-691331fb7a514c139227cc7694fc9636FakePre-5ee3feefefce44e89932a811e3e29439-5a22072783764216a4d14036345f3e35FakePre-d7f06c9126314ccaaebdd2fa8716819e-4618fd14bcce4b9f9df51f93e801054fFakePre-ab4ce5573abb419ca7f7c44a5ac73984-38686ce561374f789bc389225c76f5a9FakePre-d8e7e1078aa14db481509d00be3afe6f-b5745408757a4081b94a785ab7caa37bFakePre-eb3ba52ae4f54b5bbe042234d82cf3bd-b53e83275d9c40f9b5809ace6cb7f9b0 Visual Basic - StoreService.vb Public Class StoreServiceFakePre-df6f710366b14c4e97831e8b99487610-8925f4c0b2174e70bb04723fffbd3ccbPrivate storeDB As New MusicStoreEntitiesFakePre-136b2046ce02427aae08c1baba59f38d-75707b5af71345a1adb64abbdff68d7bFakePre-7f6ee301a4774c99b5ddb51ef32c9df3-f3ff11fa08e5440eacb44b33735eef54FakePre-8818839829a34ae69f286826b10f69da-f70520862a034e068047891945f4a729FakePre-fb538079e0bf4ac9a892daeb07b2f64f-4fb7e7de08094d92a095a420732265b0FakePre-b980ea76f0584451b179195518d49719-35298f80c50c4dfa83b0d09179053670FakePre-0b51c9963c8e49b7b2ce7481942ffe41-6db8af86276f41539727ffedb5043f7bFakePre-6e16e6046bda49f4aafa84e738315edd-a11f2307a3324a268872760d3192d90fFakePre-812d3115188a42a98cf70daad185e9c3-506898beec8247ebb1a8887811f9c49aFakePre-932bc14cd3a349299a8cd7ac6be60835-9fcad557839b4d62ac10f101e2e7a35aFakePre-a85415b767cb4da0a96cfeb9354aa722-bcec7c25c42d479ab2b8710d74031e40FakePre-3af090a11b114c099335f35fd13c4592-31713ce5d0bd4f2ca072efe78b56b3eeFakePre-81c7267a21d845c8be4e9e7f12cde55f-61bd9895aaa6412792cf1d492f11c8e8FakePre-d093cd4598bf45f299b7ad43ff109c1f-788b15b48e234980bb014d04387730cdFakePre-24774ee33c2f4c4e9e3a14e0e1b48f69-7789eb2d503f413792f3498971e94be4FakePre-30b4d884171941659f44dbb4e7a9dbd5-7e74763275a3431ca3c27e55ba4ce111FakePre-aebae52f8a6b42879a7b7a952957c58a-a310db3c6f2747ca88eaca35e0c83ef5FakePre-606706dc074043c6b9eaef05bf75aed6-f1cb5bad422347e881cf479d752b09e4FakePre-a85792e6455b4fd09b1142b742ae7c2a-0b50fb37932a43a9addc9ab59ce2c969FakePre-d08017fdc0a7478199f1a50b2a7d6a6d-49aca1816118429cbc544c159951882d Additionally, in the StoreController you will find in the begin solution now uses StoreService. All data references were removed from Store Controller, and therefore it is possible to modify the current data access provider without making changes at any method that consumes the Store Service. You will find below that the Store Controller implementation has a dependency with the Store Service inside the class constructor.
Note:
The dependency introduced in this exercise is related to MVC Inversion of Control (IoC).
The StoreController class constructor receives an IStoreService parameter, which is essential to perform service calls inside the class. However, StoreController does not implement the default constructor (with no parameters) that any controller must have to work with IoC.
To resolve the dependency, the controller should be created by an abstract factory (a class that returns any object of the specified type).
C# - StoreController.cs using System;FakePre-d727cf3f5d6a48deb9883d8c01b9ea0a-93df8369d8974086ac02b1abc68c170bFakePre-08ae2f0fc2bd4c4095494ffc144ad57c-249c27fc2b0a403a8ec8ae96e99b209bFakePre-83f56c46815a4faaaf0a49947b444431-41d2aec0596c4d2287f207ee5b9f1c4dFakePre-992e42c01d574a77ad55855fc247ea08-142e7286df1b46908928dba64b3922d9using MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-8e141cde29304a7cabb7e2cbe17bcfa1-7d8ed64a7a9c4c728f00cad7da604ba6FakePre-ebdbb4ce2c994dac8f3cc1ea2a860e1e-c25bb487509244bc9ad5aae105f7c839FakePre-d8452d44a6164ea286c5fe3421b18d13-df1dfa144af44e16b419d399f42dbc35FakePre-6133ec98526842f1a08c55d073629fe1-813912e4585f44ea897f137e39c30f2aFakePre-4f4f2e44827d432c8be531db51f0f257-7cfedd081a9a4b308f205cc433f6d4e9using MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-c92c0171e61a45d3bc5103ea1144a0ee-c160b9b162d94c45b09e8df8ca5a1e18FakePre-4f09ed820f1e4f1eb6393688224ca873-acd0a386837a4fce9601e7fa5e666c59FakePre-fe26b6bcee3f452ab63d817bde762225-a97a9a6520f04a15949a308de495761fusing MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-ec469ea7fb114f1288aa5815ed918c66-fab0d452d4794321a3ddb835c1d79be1FakePre-7db6ecd0d38b418b993e1b6c6b63d748-9a5dbec48c8d47d0b0069258b1607015FakePre-e161f2fadc10452cabe28419ea85dfc9-8dae5a0736c249bfb6c0c7c4e29591faFakePre-29891c8feafc4368bc2fafe0698b03d5-bfa9c54fc4144f3bb84a5fbdc92191a9FakePre-2647580b4ab24f4fb4c1e1c9992f1b72-0601e3b3a9124d7f81232a55e76b28f1FakePre-e9ef932ce738498e84f5f3d7559f02b4-709006004ab84a06aef289b59e0ea330FakePre-5f403e54bda548819dff9642da16b07e-a0c34490954b4b64b188a060d445c46ausing MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-9bb80aaa9d564d8888f17c75dba515e5-9ee7d3dba15e47f5882e164c07f7ce21FakePre-fde9366349314ba1950a2031025c72d8-f010eaf8fc444263aa3b9c2c6927985cFakePre-1e8f69eeb07b402bac3e6087be0db5e0-a9e36562f022420aa1e265e3430b093eFakePre-6d4664da56724fe59c8f9cdbce0dfe01-1c1a39325c5b454c9496e6c8bd9e4133FakePre-044e75211235487bb45d961723bdeefb-fbe11c9da17641809af05039b2f22273FakePre-6e39bee6f7924e0d81f1a9e6180763e6-222c6622456040c38b1acd831e1c1fa4FakePre-1a56908dcf304192823902c3f4d32472-13b6a98177b2448195808f19f0604191FakePre-04429ae1266a48bc80c057e11d36eee4-2b29a67963404222b440646f0c56933bFakePre-592d3c2b0cec47cead828b82b117def9-59bba53d040e4ae3be622dfb9b1adb71FakePre-52011231c44f4457ba573236714f6515-23c44e20c4ca4facbf265962f2cf3f67FakePre-62be0601e435471caf016d369ddaeed5-991c01173ae14720a07e7957ba5413ffFakePre-db6a9c0e0bac4b3c84b8dcb5dcd09abf-b44466e174e94e96986194ae33f62ffdFakePre-82a35a72cd5d43029a0e1949b21c6e01-eabbc0e6fd794334a0ae09b644a011aeFakePre-decbbd8c88594bd386b5751ca3e973cb-a450d391826541e6af07cbcd4b4269cdFakePre-9851e2a5419444c3a5b5d2996b2effac-b96dbeaee81543f4a9abd2e76c74800busing MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-d7cfd8bf8bec4155a8600756099fac45-4119c0d120b842abb7e7b4d49d9a6318FakePre-1641d944a8414f54ac965498e8afec2c-c940a319d1ec44df964a3d2839c46c6bFakePre-c00173dbfcb94bfe89c652cd2fecc703-fb305f9c14244cd297074495b7b84eb7FakePre-e47277251e6a4686a94d3d85f8a7e481-7f4327bbd8a242b181337373082e9da9FakePre-d951127b8e4648a7a651b716e386a46b-f011f08df1894af5b638eff57ea540b8FakePre-773280b0c9ab49c98fe2662b2b33a1bc-573f65760677488b85da4f5ef6f9857eFakePre-a6c711de51714a7dbf6ec719b878c241-9059cfb27cb84352b074d3bd06b95cb0FakePre-4253e51f32a1489ba9c7871c5c20d0fa-6fc121ddde2e4f27884a35ab6eeb572eFakePre-7a24d379cc454e349d578d06c15100cf-a022922ef17a46d782c8dc19158c074dFakePre-26278f6cb7b24a828c21ac607b9d1801-bb5c74db5eb846fcbd423e34f215f749FakePre-808dc90dcb544515a5fc980874b8f2d2-087fcdfd2b694e74bc9bcc887ec0ca68FakePre-0e82a08c139747c088b67a3ad9bf8d01-2132a680499c4b70bedc829e0a131e90FakePre-b9ccd829e8494246a0dc8d0333d14a97-0628441970ed4d39af18fd91c19342a2FakePre-12e7261524c047afbfcf819d1f90654f-6599e6a9ba6c4aa0a8dc0d8148af289cusing MvcMusicStore.ViewModels; using MvcMusicStore.Models; using MvcMusicStore.Services; private IStoreService service; this.service = service; var genres = this.service.GetGenreNames(); var genreModel = this.service.GetGenreByName(genre); var album = this.service.GetAlbum(id);FakePre-e8cb4cc9827c406ca8071fe198677b3a-f202075846c243da9300ff6ad7e80b56FakePre-961e0d4f65224f5e9c0cf7ecdb33e24e-3a7822bd771946fdb0079feca2cb638fFakePre-7eee251315e3486f867da6200b2dcdd4-ffdb9cbf8bad4e4fbbc21225b7e5c7f6FakePre-3d444d86531e448bb940c36f48b282ac-80036fc2d96a4211842b85c6ec36d6a8FakePre-ad0d86511d9249ac8d9c9aee0bd75aa4-35422d6baccc4a9d92ba7f0fd48ac2a4 Visual Basic - StoreController.vb Public Class StoreControllerFakePre-6f985d75b9184ebcb4533b8149ae94d4-ba02c99e4f9d4f1f870f687a2d913bdc Private service As IStoreService Me.service = service Dim genres = Me.service.GetGenreNames() Dim genreModel = Me.service.GetGenreByName(genre) Dim album = Me.service.GetAlbum(id)FakePre-b98f4ad46e9e45ddbd4fbae2059b9ab2-88035b87736045f0973fc33a5b3ba3c4FakePre-07eb03b14b53443d8051e51f64228bcd-3ef15f9e76b24da1ba53bc98bfb2e982 Private service As IStoreService Me.service = service Dim genres = Me.service.GetGenreNames() Dim genreModel = Me.service.GetGenreByName(genre) Dim album = Me.service.GetAlbum(id)FakePre-64a1b8bba75d4824b1f3a6db196e9bdd-da6e55010ffa4f968ba2acc880170e84FakePre-5d1a623f73f84e0b9ec8f8200b14f182-e8852e1da0fa4903a105326fe7f10b26FakePre-ac27b08dfa2246d4a29aa8cd26b035e1-8cdf2b8920aa48c593902cafb14f0f95FakePre-ed31374f323444e690d6501c132b5bfa-97da1e266857403c87c63f35ed4e4edaFakePre-82b1dfa603254eabab5616d6b188888b-aa7f39a94cb349cc99e8933a24de2cdcFakePre-48ec7d18bbd546d48c32ebb888e2e271-b38bae2f4b9d442fb55c0e3c4725f06eFakePre-042be5f9c9f44aa389e59853acdcde49-e8de8ee695b045b19438575699bcacec Private service As IStoreService Me.service = service Dim genres = Me.service.GetGenreNames() Dim genreModel = Me.service.GetGenreByName(genre) Dim album = Me.service.GetAlbum(id)FakePre-dce66b16616f42599b21e5e8b1e9e9ba-1b8e486990af458fb145eb152d9a0438FakePre-56111bf1b29d4479947364f46c8ede05-97466c65388f411d8fcda9d6ebef30f4FakePre-dadd5fc92ef04384999703d77a29469d-cefd0b226662491981b5edc8d08e94b1FakePre-775dbaebcc144373b336796e09373864-d899d152bb324514842e8eb4929ef0cbFakePre-d8cf848f995f4d38909e18372688cc96-73beaa6692304f239fecff6887c66021FakePre-09e834cc233f4755a70ee80f2628cd69-2026c2da55834461bbeec4dbda32974aFakePre-da027be6679345488100f517447df946-c8c887bad9e0415e95987ec26031189aFakePre-59c6128cb70a4e3fa9c3d5cb5819be3a-17d94c936ec4479b82fbe329157b49dbFakePre-6ca4510dbf484aabb45167aa9fd60197-716be7744b9b41d7b1bf8a2737b98688FakePre-7018176cd6214b26b5097a88941916ca-0e77eaca1ef54ae5872793468caf7db7FakePre-13b70d9521fe4a488993eb151dc1a3fe-d7008ef6b5ec45ec8358e3c2537cb614FakePre-014af8573c2b497fa08a4bf3aa0e23cc-78317241599c4bdebac01b59da75c376FakePre-bd3278870bc8479d8bff0987b8970ea8-f5aa573ad96745959f0b0aa69f501143 Private service As IStoreService Me.service = service Dim genres = Me.service.GetGenreNames() Dim genreModel = Me.service.GetGenreByName(genre) Dim album = Me.service.GetAlbum(id)FakePre-2566e8fcc8a54a1ca3bc768e34349874-2f6140ab39774bbf90b7f332c9af5808FakePre-2320951254294bb78a151658403291d3-e1eed7d8f930401eb6c1fafdc0ecc054FakePre-7a4f76071c604e6e8ae64e1a93e1db7f-7e5101d6adad43f3aae3bfd7727ae0e4FakePre-aed201c2241743bfac7a78ae98370ba9-b5f8031b543e4fa185b4dbf2a9afb0beFakePre-6d9d035dd01641f78891c63a28d1376f-d5b7bccb7fa449dba9a4b5ab3b97ad7dFakePre-3b11f3fb6088497b9491d69668bc43f0-e0035dc0ab774838a6e8cf87f72659f4FakePre-354c8caf1d914cbd95078a2e9e615328-f9788ef2581a473e90d2a3dd7a1d629bFakePre-a559646419d24007bd18dd27409af0ff-b5dce581f3e84f25b8b1cc7c84a8cd73FakePre-f5e6a8ca77424e72924c1026f1a01d57-c30dbeb386404702ad8ef95bed91be36FakePre-224ecb7358854636ad660ddd46ddc924-036700ead0b346cbb6b2bcfd78b2bd96FakePre-1e73f1c9999c427c9203bcf374f50e05-ca6cbb41151f4140b10488fc4029507fFakePre-f39deac3e8d8464d95986da83a8a5860-ee9638a65c1c43af80cafe7edf1910d4 Private service As IStoreService Me.service = service Dim genres = Me.service.GetGenreNames() Dim genreModel = Me.service.GetGenreByName(genre) Dim album = Me.service.GetAlbum(id)FakePre-ea02f029ad264412a03573ca8d43191e-b872c3a54eb34744b44623825f1b46a4FakePre-4c081ada0af747e6816589670ae22f55-860c27ab7d174492b691cacb35a66ee7FakePre-9f50eb10fd5e4f22a087edefe6904d9f-212f0f778db048d4ad135b3f9a3dc338FakePre-806dbdb31be84a028314a2e369d14788-8a87c182f9eb457caf4df4daa8442544
Note:
You will get an error when a class tries to create this Store Controller without sending the service interface, because there is not a parameterless constructor declared.Through this lab you will learn how to deal with this problem using Dependency Injection with Unity.
Task 1 – Running the ApplicationIn this task, you will run the Begin application, which is now including the service into the Store Controller that separates the data access from the application logic. After browsing to the store you will receive an exception since the controller service is not passed as a parameter by default:
In the following steps you will work on the Music Store Solution to inject the dependency this controller needs. Task 2 – Including Unity into MvcMusicStore SolutionIn this task, you will include Unity Application Block 2.0 into your solution.
Note:
The Unity Application Block (Unity) is a lightweight, extensible dependency injection container with optional support for instance and type interception. It’s a general-purpose container for use in any type of .NET application. It provides all the common features found in dependency injection mechanisms including: object creation, abstraction of requirements by specifying dependencies at runtime and flexibility, be deferring the component configuration to the container.
You could read more about Unity 2.0 at msdn.
Task 3 – Adding a Unity Controller FactoryIn this task, you will add to the solution a custom controller factory for Unity. This class implements IControllerFactory interface, extending CreateController and ReleaseController methods to work with Unity. This factory will create the instances of the controllers that work with Dependency Injection.
Note:
A controller factory is an implementation of the IControllerFactory interface, which is responsible both for locating a controller type and for instantiating an instance of that controller type.
The following implementation of CreateController finds the controller by name inside the Unity container and returns an instance if it was found. Otherwise, it delegates the creation of the controller to an inner factory. One of the advantages of this logic is that controllers can be registered by name.
You can find IControllerFactory interface reference at msdn.
Task 4 – Registering Unity in Global.asax.[cs|vb] Application_StartIn this task, you will register Unity library into Global.asax.[cs|vb] Application Start.
Task 5 – Running the ApplicationIn this task, you will run the application to verify that the Store can now be loaded after including Unity.
In the following exercises you will learn how to extend the Dependency Injection scope, and use it inside MVC Views and Action Filters. Next Step |