Apply Inversion of Control to your AspNetCoreKata.
Note: as you complete steps, you should make commits for the changes made, unless it doesn't make sense to do so. For example, DO NOT commit your connection string to Git.
We don't want to store the secure connection string in Git, since this is considered an insecure, bad practice. Move your connection string to your appsettings.json
file:
Move your appsettings.Development.json
file out of your working directory (perhaps move it to your Desktop temporarily)
Commit the "deletion" of that file from Git.
Open your .gitignore
file and add this somewhere in that file:
# ignore appsettings
**/appsettings.development.json
**/appsettings.staging.json
**/appsettings.production.json
Now, a file matching this description will be ignored by Git.
Move your appsettings.Development.json
file back into your working directory
If you run git status
, you'll see this file doesn't show up as "added" or "modified". Because it's ignored, you don't have to worry about committing sensitive information.
Add your connection string to your appsettings.Development.json
file and a corresponding "Bread Crumb" value within your appsettings.json
file.
Add a class library for your Product Repository so that you can move the Model out of your Controller.
- Create a new .NET Core class library called
AspNetCoreKata.ProductRepo
- Copy the
IProductRepository
,ProductRepository
, andProduct
files from your Inversion of Control project into theAspNetCoreKata.ProductRepo
folder, and remove them from yourAspNetCoreKata
project - Add these existing files to your class library
- Navigate to the list of references in your
AspNetCoreKata
project and add a reference to your class library
Remove the Product.cs
model from your main project
- There should be no reference to your
Product.cs
anywhere in your Models folder or in theAspNetCoreKata.Models
namespace. You'll be referencing the Product class from your Class Library
Add inversion of control via. the ConfigureServices
method in your Startup.cs
file:
- Downgrade
Unity.Mvc
to the most advanced3
version. Something like this:3.x.x
- Register your
IProductRepository
interface with yourProductRepository
class - Register the
IDbConnection
interface with theMySQLConnection
class
Add a constructor for your ProductController
and a private readonly
property to store your IProductRepository
. Should look something like this:
private readonly IProductRepository repo;
public ProductController(IProductRepository repo)
{
this.repo = repo;
}
This allows your IoC container to set the repo
to an instance of your ProductRepository
Confirm that everything is done. By the end of the above tasks, your project should:
- Have no reference to
using Dapper
within your main project - Have no connection strings within your source code; it should be located in your
appsettings.json
file - Have no Product class within your
Models
folder in your main project