Exercise 1: Injecting a Controller

In 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 Service

The 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 StoreService
FakePre-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 StoreController
FakePre-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 Application

In 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:

  1. Open the begin solution MvcMusicStore.sln at Source\Ex01-Injecting Controller\Begin.
  2. Press F5 to run the application.
  3. Browse to /Store to load Store Controller. You will get the error message “No parameterless constructor defined for this object”:

    Figure 1

    Error while running MVC Begin Application

  4. Close the browser.

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 Solution

In 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.

  1. Open the begin solution MvcMusicStore.sln at Source\Ex01-Injecting Controller\Begin.
  2. In the MvcMusicStore project, add a reference to Microsoft.Practices.Unity.dll, which is included in Source\Assets\Unity 2.0\ folder of this lab.

Task 3 – Adding a Unity Controller Factory

In 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.

  1. In the MvcMusicStore project, create a new folder named Factories, and add the UnityControllerFactory class, which is included in the Source\Assets[C#|VB] folder of this lab.

    C# - UnityControllerFactory.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Microsoft.Practices.Unity; using System.Web.Routing; namespace MvcMusicStore.Factories { public class UnityControllerFactory : IControllerFactory { private IUnityContainer _container; private IControllerFactory _innerFactory; public UnityControllerFactory(IUnityContainer container) : this(container, new DefaultControllerFactory()) { } protected UnityControllerFactory(IUnityContainer container, IControllerFactory innerFactory) { _container = container; _innerFactory = innerFactory; } public IController CreateController(RequestContext requestContext, string controllerName) { try { return _container.Resolve<IController>(controllerName); } catch (Exception) { return _innerFactory.CreateController(requestContext, controllerName); } } public void ReleaseController(IController controller) { _container.Teardown(controller); } public System.Web.SessionState.SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName) { return System.Web.SessionState.SessionStateBehavior.Default; } } }

    Visual Basic - UnityControllerFactory.vb

    Imports Microsoft.Practices.Unity Public Class UnityControllerFactory Implements IControllerFactory Private _container As IUnityContainer Private _innerFactory As IControllerFactory Public Sub New(ByVal container As IUnityContainer) Me.New(container, New DefaultControllerFactory) End Sub Protected Sub New(ByVal container As IUnityContainer, ByVal innerFactory As IControllerFactory) _container = container _innerFactory = innerFactory End Sub Public Function CreateController(ByVal requestContext As RequestContext, ByVal controllerName As String ) As IController Implements IControllerFactory.CreateController Try Return _container.Resolve(Of IController)(controllerName) Catch e1 As Exception Return _innerFactory.CreateController(requestContext, controllerName) End Try End Function Public Sub ReleaseController(ByVal controller As IController) Implements IControllerFactory.ReleaseController _container.Teardown(controller) End Sub Public Function GetControllerSessionBehavior(ByVal requestContext As RequestContext, ByVal controllerName As String) As System.Web.SessionState.SessionStateBehavior Implements IControllerFactory.GetControllerSessionBehavior Return System.Web.SessionState.SessionStateBehavior.Default End Function End Class

    Note:
    This factory class can be reused in any project that uses Dependency Injection for Controllers.

Task 4 – Registering Unity in Global.asax.[cs|vb] Application_Start

In this task, you will register Unity library into Global.asax.[cs|vb] Application Start.

  1. Open Global.asax.[cs|vb] file.
  2. Include Microsoft.Practices.Unity Application Block, and references to the namespaces Services, Factories and Controllers:

    (Code Snippet – ASP.NET MVC Dependency Injection – Ex1 Injecting Controllers Global using– Csharp)

    C#

    using System;
    FakePre-1b199afeecc8440fa69bac8f8ea7ff96-0aa15b9ad396474c885a68ea2bd62cd4FakePre-6dd6408ae55a4365a4bc8ec80276d092-0c6d1211f4b74b2d8ed8ddf7e03688bcFakePre-9fbaa96c9c884d9e89a74ddbb7424d13-bcd4754c31a04cf9ad37387ed551551cFakePre-304e613b1ae4427c9f514cbc15fc11a8-903162169cb141a2ab598dfb53c393dbFakePre-3a96cd8488b64b65980a3a65e8160636-7bac1ae6cb074493a12ccdfc95cec6d5using Microsoft.Practices.Unity; using MvcMusicStore.Services; using MvcMusicStore.Factories; using MvcMusicStore.Controllers;

    (Code Snippet – ASP.NET MVC Dependency Injection – Ex1 Injecting Controllers Global using– VB)

    Visual Basic

    Imports System
    FakePre-6155d6e2406c458da5013b4bde53fafd-1805da7bb0ef4b488f891de8dba12d00FakePre-063732557ef541caaf5afe33a725b698-4657b44a615243b190d6f4d592247945FakePre-666b713f8531432484dd02360b2b1f74-97d6e287feee44fd8b86613012092aefFakePre-c6f058f574004a7590c7cf30b98ce5a5-601ecb7874d3420ca1c090381d6f7d03FakePre-22d368eaed5a4aff8acf6d889c1c56a7-145635bdb494462e92d0a82d0d1942f7Imports Microsoft.Practices.Unity

  3. Create a new Unity Container in Global.asax.[cs|vb]Application_Start and register the Store Service and the Store Controller.

    (Code Snippet – ASP.NET MVC Dependency Injection –Ex1 Injecting Controllers Unity Container – Csharp)

    C#

    FakePre-8a9d2dd1e6c041b888a2c0cfc2e47eb9-05cf2823de2e4fb9947fdaee22279139FakePre-29a784f0d4b94cf2aebb36eed1294443-b10caeab799c40e0b08c68fe6a1d0ca1FakePre-cf60014b19b6486d89c2588917374706-70521f11bb3746b3a75c20ef05d07454FakePre-0a707361642448d1833f75bed9edc2a2-7b937d193f164127a726384f22e3b249FakePre-6e1349352baa401fadecf777af763c01-38f7267a292449f39d65d655b3df0bcbFakePre-3ac18f87d1d84bfea04cadd04b214a7b-e0c0b0e67c50486082cc620dbf350ffcFakePre-f79b2372d7cb41068d8bffcb90a33609-acd224bb410b44a6bf8d1d1e809a6154 var container = new UnityContainer(); container.RegisterType<IStoreService, StoreService>(); container.RegisterType<IController, StoreController>("Store");FakePre-7d33fd1708dc40599edc03f60cf36873-9921b86667e749cba2dc3c28622dfd0aFakePre-dcb322bad05a45ceb2d6daa902b62da0-7c6d0b2ad2a04e1bb79c6dff3a612037FakePre-df5e01b31a0947589d1d63097b035269-d1365d72bef14b0e926ddbc39e79c002

    (Code Snippet – ASP.NET MVC Dependency Injection –Ex1 Injecting Controllers Unity Container – VB)

    Visual Basic

    FakePre-6cd2835f54e04b05828ea7fee91b1743-b2bf94d33818465da95be7f7143e4ea9FakePre-6661aa3eed6045dd81d3a1dea975e141-116398cd79414c40940d559bc1638adaFakePre-882784ea2b5642db999bbc72debb869e-1648ff70722d45e2bdd0dd4fae1f0738FakePre-8446a632926844909b4362fde2e192e8-eeed26369e88412181b657220a4043b5FakePre-7e4f9bf0fee847b0bfe79b0186772e10-65fa07cc11c04a1dabf0cdab0522753aFakePre-72798d5ee11b4059af3e4f241214eaba-35fd8efd8655413393ca33e74553dbd6 Dim container = New UnityContainer container.RegisterType(Of IStoreService, StoreService)() container.RegisterType(Of IController, StoreController)("Store")FakePre-eb7fa8b04c12431bb196c5d868789b42-009d80fce5f8431eb43e4b2af5d498a7FakePre-c35315f0b503478f924946be1fe17f02-3b62b69750d9482ab5d892476befd170FakePre-e4f35554e5f34e6e8d60d8c73bc6fbdd-694c8cfe9280420897e3c2fbca1d4524

  4. Register a UnityControllerFactory of the previous container inside MVC ControllerBuilder as the current factory for the controllers:

    (Code Snippet – ASP.NET MVC Dependency Injection – Ex1 Injecting Controllers Global application start – Csharp)

    C#

    FakePre-9cf3fa06026d4a36a8251fd6bc1d51ec-ba890856a86c464bb9c1d63468d36d51FakePre-3fa6edb9c35b4493b1c54a2904f621fe-fc9d2b30b0064ab4a255bfae00a89856FakePre-20dc54ff8e72442080b8d03f76393447-cc08fc3e9a664ef8aa045f55f73167a5FakePre-9d20801d84c14870ad8d2ad7a9f0750d-ede3cc921648463a8e02fd6bc8e22bcfFakePre-95380028a6ca4fd6b5c5c46b851a167d-3834d12ee3fb48c8ba46e79687ce47ebFakePre-9f1d2cc0521d4fa8ae29e301a13f8f2e-754ac430588b4118bbc436b738b8d6e7FakePre-ba3efefacef84866a022686228a0a593-47cf178c87d7411d90dca38124c6e675FakePre-58043559925e4fd88a93251fbd31e986-d0f2ecd031944f51807259c9493ff3ffFakePre-352a6b3f249c4c52a2b3071bbfea4fb6-9bba737f2211418bb9de6bf93743543eFakePre-6ab6b4863f3f434080ac23954bc35928-2a26373459b14aa48ea92ee884ba393fFakePre-a5fd9088a16a49019d686a91b3f37be6-74cc78ed8e9a4b44a4732c051b5649b0FakePre-b1f13a9ad27344d593f1b29a2378564e-133936a2ccf1431d910ca1497664f866 var factory = new UnityControllerFactory(container); ControllerBuilder.Current.SetControllerFactory(factory);FakePre-1aa4befb64d948ed92d21db244761e0e-c985545d58cc4f5e87fc7996c0aaa733FakePre-fbf1a5b1b0864449ab35f616ae5f53d8-30498595421147218464dceff2d168d2

    (Code Snippet – ASP.NET MVC Dependency Injection – Ex1 Injecting Controllers Global application start – VB)

    Visual Basic

    FakePre-f0995c91053047058cc1539ff060d9a7-76502587c9d74180af3fd8fb08972189FakePre-7c69aea0b0fc4633916ab16cf0e61144-e77757eae91a4ee39ee9021637dbdae9FakePre-13555fd52bb74176ae39daee71ab5afb-ae76220e0c36461d8e3020546cb1b62bFakePre-db157658bce24733ba3fc2b701301ac7-1214086f8d5046dbb87a683c15ecd608FakePre-32767d2d0ce54295bf2bd1fe379c98fd-5de2719a279d4ebb87667b883e9788bcFakePre-8227dec6d7054ac4b26122e901b48962-0dcc4fb73d5843eeb7cc3b723f9c03adFakePre-cca179a330304625baecc52b6e0ee2a7-0a49b07de54543f7af89d79395a561cbFakePre-2ee5c6f97a9c4e9ab9de1dcf19fe1150-ea40ef60da6a468aa004441b5ba1b5cfFakePre-8ab95e423f4948589734399828c2d9e6-d68a76470d034300b804d068b5f8742eFakePre-f523089044fe4716bc656ede77f192e9-64f647ce6bff415c854e3b615de85a2d Dim factory = New UnityControllerFactory(container) ControllerBuilder.Current.SetControllerFactory(factory) FakePre-5352c94570e34f4cb30c3481751d06c6-fc28f9349abf4834b5b239e3f5b2aa52FakePre-3944dd10c53a446e9df37ab747fc61b8-af650fd18183423ca190b8784b870086FakePre-2a4009d9f2d64ea69e8b917dad7a0597-104e989940804c518287fc8ba0ac4e07

    Note:
    ControllerBuilder is an MVC class responsible for dynamically building a controller.

    You can read more about ControllerBuilder at msdn.

Task 5 – Running the Application

In this task, you will run the application to verify that the Store can now be loaded after including Unity.

  1. Press F5 to run the application.
  2. Browse to /Store. This will invoke StoreController, which is now created by using UnityControllerFactory.

    Figure 2

    MVC Music Store

  3. Close the browser.

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

Exercise 2: Injecting a View