MVC 4 Project Using Repository Pattern (1)

(based on the CodeCamper project – available at the following address https://github.com/NicholasMurray/CodeCamper)
Create a new MVC 4 Internet application project, and add these other projects:

The first 3 are C# Class Library, and the last one is a MVC 4 web site. image
Adding IRepository.cs
This is the interface file for data repository. You can add more methods as long as they’re shared by all entities. (such as SearchByXXXX)
image

So in the Contracts project, we have IRepository.cs. Build the DST.Data.Contracts project now.
image

Now in the DSP.Data project, you need to have a class that implements the IRepository.cs interface. It’s EFRepository.cs (it’s an Entity Framework implementation, so named) Make sure in DSP.Data you include the following three references:
(1) Entity Framework 5.0
(2) System.Data.Entity
(3) The DSP.Data.Contracts project

So you have:
image

EFRepository.cs inherits IRepository.cs and therefore implements all methods defined in the contract. Any entity (T) can use this EFRepository to perform data access. Build the DSP.Data project and it should build successfully.
image

Useful Tips: Manage references in difference projects: Go to Nuget Package manager, select “Installed Packages”, locate EF 5.0, and clock [Manage] button. Check the project that you would like to have EF 5.0 installed/uninstalled.
image 00:15am, 01/19/2013

Copy the CodeCamper model files to directly under your DSP.Model project. Update the namespace from CodeCamper… to DSP…. and then make sure you can compile the project.
image
Copy the repositories from CodeCamper to DSP.Data and DSP.Data.Contracts. These three models has different method (or method signatures) than the others so need to create the to allow custom DB access for these three models. Compile and make sure all references are there.
image 
Unit of Work Pattern (UOW)
1. It’s a layer between the controller and the data repository. 
2. Series of IRepository<T>
3. Commit (call commit on the UOW to commit changes to database)

Copy the Unit of Work interface file to DSP.Data.Contracts project and update as needed.
image

Copy /SampleData folder under the CodeCamper.Data project to under DSP.Data project> These are the sample data that will be used as seed data.
image
CodeCamper uses a random text generator with “LoremIpsum” generator, so create a new folder (lib) under DSP.Data, and copy the DLL to it. Make a reference to this DLL from inside your DSP.Data project.
image
You also need to copy the /configuration folder – this is similar to the “Mapping” files generated by the EF power Tool. It defines the relationship between models if any. Compile and make sure there is no error.
image

Copy to DSP.Web/Controllers folder the ApiControllerBase.cs. ApiController all inherits from ApiController class. But we need to add UoW to all controllers, so it makes send to create a ApiControllerBase class that inherits from ApiController, and then all your application controllers can inherit from ApiControllerBase.
old:
Your controller <—ApiController
now:
Your controller <—ApiControllerBase (with Uow) <—ApiController
This new ApiControllerBase class only has one line: (UOW)
image
Copy CodeCamperUOW.cs to DSP.Data and update filename or namespace as needed. Also, copy the /Helper folder to DSP.Data project. These are classes that are needed or data repository.
image
Now let’s do Ninject.
(1) Use NuGet to install Ninject to your DSP.Web project.
(2) Copy IocConfig.cs to your DSP.Web/App_Start folder, and update namespace.
image
In global.asax, add this line. Compile and fix issues.
image
Copy these two files to DST.Web/App_Start. needed for Ninject.
image
09:41pm, 01/19/2013, Video 4-5
Move the seeding method from Context class to Global.asax of the DSP.Web project.
image
Then do these in the console to create and seed the new DB:
(1) enable-migrations
(2) add-migration (name: {random})
(3) update-database
Then you will have all tables seeded:
image
Create a new SpeakersController (API Controller)
image
When you query all speakers, you get:
image
10:09pm, 01/19/2013

Now whenever you add a new controller, just make sure you do the following:
(1) Include the DSP.Data.Contracts namespace (because you need to reference the IDSPUow interface)
(2) Inherit from ApiControllerBase
(3) Add the constructor that accepts UOW as a parameter.
image
Create a new Lookups Controller that returns Rooms/Tracks/TimeSlots, or all of them in one Lookups instance.
image
Update the WebApiConfig so it will work for all possible routes
image
Get all Rooms/TimeSlots/Tracks
image
Get Only Rooms
image
Create PersonsController
image

In your /App_Start/WebApiConfig, add three static variables that represent each of the three available API routes.
image
So if you need to dynamically use the route name, you can do this:
image

POST: Insert (returns HttpStatusCode.Created)
PUT: Update (returns HttpStatusCode.NoContent)
A typical insert/update/delete scenario. Powerful!!!!!
image
If you want WebAPI to always return JSON (default is XML), create a GlobalConfig.cs under /App_Start. In it you remove the XML formatter. image

Then you call this static method from your Application_Start method from Global.asax:
image
Now you will get JSON by default!!!
image

Global ModelState Checking (global filter). Instead of doing ModelState checking in every action code, you place it in a custom filter. Under /App_Start, create ValidationActionFilter.cs.
image
Then add this Action Filter.
image
03:07pm, 01/20/2013

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s