Monday, August 26, 2019
Now that we have our data in the database it is time for us to show the data to our users and we going to use the repository pattern to show our data. A repository pattern is basically a conduit between the database and our business objects that has built in CRUD operations. It is ideal for LOB applications. There are many websites and books that explains the repository pattern better than I do. Feel free to explore them.
Here are the steps to create a product repository for the products in the database:
1. Create a folder in the NorthwindCafe.Web solution call "Data"
2. Create an interface call IProductRepository in the "Data" folder, this will be the interface that we implement our repository from.
The code of the IProductRepository should look like this
As you can currently there's only one method and that's the GetProducts method that returns an enumerable of Products. Let's keep it simple for now.
3. Now we want to implement the interface with the ProductRepository class. Here is how the ProductRepository class looks like
4. Now we are at the final step which is where the magic happens, we are going to the map the interface to the implementation of the ProductRepository in the Startup.cs file. So open up the Startup.cs file and type in the following code in the ConfigureServices method
services.AddScoped<IProductRepository, ProductRepository>();
AddScoped adds the mapping between IProductRepository to the ProductRepository. If you have a mockup of the repository this is where you would make the switch. AddScoped adds the object that's the same within the same request, which is what we want.
Your ConfigureServices method should look like the following up to this point
5. Now that we've set up all the plumbing it's time to use the repository in our controller. Open the HomeController in the "Controllers" folder. 6. Create a readonly private variable call _repository and then create a HomeController constructor with the NorthwindContext injected into it, the code should look like the following.
github: https://github.com/techjunkiejh/NorthwindCafe/tree/Hour15
Previous: Hour 14 ASP.NET Core: Seeding The NorwindCafe Database
Next: Hour 16: ASP.NET Core Display Products
Here are the steps to create a product repository for the products in the database:
1. Create a folder in the NorthwindCafe.Web solution call "Data"
2. Create an interface call IProductRepository in the "Data" folder, this will be the interface that we implement our repository from.
The code of the IProductRepository should look like this
using NorthwindCafe.Web.Models; using System.Collections.Generic; namespace NorthwindCafe.Web.Data { public interface IProductRepository { IEnumerable<Product> GetProducts(); } }
As you can currently there's only one method and that's the GetProducts method that returns an enumerable of Products. Let's keep it simple for now.
3. Now we want to implement the interface with the ProductRepository class. Here is how the ProductRepository class looks like
using NorthwindCafe.Web.Models; using System.Collections.Generic; using System.Linq; namespace NorthwindCafe.Web.Data { public class ProductRepository : IProductRepository { private readonly NorthwindContext _context; public ProductRepository(NorthwindContext context) { _context = context; } public IEnumerable<Product> GetProducts() { return _context.Products.OrderBy(p => p.Name).ToList(); } } }What code above does is basically injecting the NorthwindContext object into the constructor of the ProductRepository so that we can use throughout the class and use to get the Products from the database using Entity Framework in the GetProducts method.
4. Now we are at the final step which is where the magic happens, we are going to the map the interface to the implementation of the ProductRepository in the Startup.cs file. So open up the Startup.cs file and type in the following code in the ConfigureServices method
services.AddScoped<IProductRepository, ProductRepository>();
AddScoped adds the mapping between IProductRepository to the ProductRepository. If you have a mockup of the repository this is where you would make the switch. AddScoped adds the object that's the same within the same request, which is what we want.
Your ConfigureServices method should look like the following up to this point
public void ConfigureServices(IServiceCollection services) { services.AddMvc(); var connectionString = Startup.Configuration["Data:NorthwindContextConnection"]; services.AddDbContext<NorthwindContext>(options => options.UseSqlServer(connectionString)); services.AddScoped<IProductRepository, ProductRepository>(); }
5. Now that we've set up all the plumbing it's time to use the repository in our controller. Open the HomeController in the "Controllers" folder. 6. Create a readonly private variable call _repository and then create a HomeController constructor with the NorthwindContext injected into it, the code should look like the following.
private readonly IProductRepository _repository; public HomeController(IProductRepository repository) { _repository = repository; }7. Now we are going to call the GetProducts method in the repository and return it in the Index view
public IActionResult Index() { var products = _repository.GetProducts(); return View(products); }8. Build the solution and run it in debug mode, you should see that data is coming through from the database.
github: https://github.com/techjunkiejh/NorthwindCafe/tree/Hour15
Previous: Hour 14 ASP.NET Core: Seeding The NorwindCafe Database
Next: Hour 16: ASP.NET Core Display Products
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
This comment has been removed by the author.
ReplyDeleteThank you for sharing the informative post.
ReplyDeletehttps://www.bahrainhelplinegroup.com/australia-apostille
Australia Apostille
Aivivu chuyên vé máy bay, tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
chuyến bay từ mỹ về việt nam hôm nay
vé máy bay từ canada về việt nam
giá vé nhật việt
bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về Việt Nam
vé máy bay chuyên gia nước ngoài