Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9444223

Browse files
Dependency Injection in asp.net core
1 parent f0a40ee commit 9444223

File tree

7 files changed

+66
-40
lines changed

7 files changed

+66
-40
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Components/TopBooksViewComponent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace Webgentle.BookStore.Components
99
{
1010
public class TopBooksViewComponent : ViewComponent
1111
{
12-
private readonly BookRepository _bookRepository;
12+
private readonly IBookRepository _bookRepository;
1313

14-
public TopBooksViewComponent(BookRepository bookRepository)
14+
public TopBooksViewComponent(IBookRepository bookRepository)
1515
{
1616
_bookRepository = bookRepository;
1717
}

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/BookController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ namespace Webgentle.BookStore.Controllers
1414
{
1515
public class BookController : Controller
1616
{
17-
private readonly BookRepository _bookRepository = null;
18-
private readonly LanguageRepository _languageRepository = null;
17+
private readonly IBookRepository _bookRepository = null;
18+
private readonly ILanguageRepository _languageRepository = null;
1919
private readonly IWebHostEnvironment _webHostEnvironment;
2020

21-
public BookController(BookRepository bookRepository,
22-
LanguageRepository languageRepository,
21+
public BookController(IBookRepository bookRepository,
22+
ILanguageRepository languageRepository,
2323
IWebHostEnvironment webHostEnvironment)
2424
{
2525
_bookRepository = bookRepository;

‎Webgentle.BookStore/Webgentle.BookStore/Repository/BookRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Webgentle.BookStore.Repository
1010
{
11-
public class BookRepository
11+
public class BookRepository:IBookRepository
1212
{
1313
private readonly BookStoreContext _context = null;
1414

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Webgentle.BookStore.Models;
4+
5+
namespace Webgentle.BookStore.Repository
6+
{
7+
public interface IBookRepository
8+
{
9+
Task<int> AddNewBook(BookModel model);
10+
Task<List<BookModel>> GetAllBooks();
11+
Task<BookModel> GetBookById(int id);
12+
Task<List<BookModel>> GetTopBooksAsync(int count);
13+
List<BookModel> SearchBook(string title, string authorName);
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Webgentle.BookStore.Models;
4+
5+
namespace Webgentle.BookStore.Repository
6+
{
7+
public interface ILanguageRepository
8+
{
9+
Task<List<LanguageModel>> GetLanguages();
10+
}
11+
}
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Threading.Tasks;
6-
using Webgentle.BookStore.Data;
7-
using Webgentle.BookStore.Models;
8-
9-
namespace Webgentle.BookStore.Repository
10-
{
11-
public class LanguageRepository
12-
{
13-
private readonly BookStoreContext _context = null;
14-
15-
public LanguageRepository(BookStoreContext context)
16-
{
17-
_context = context;
18-
}
19-
20-
public async Task<List<LanguageModel>> GetLanguages()
21-
{
22-
return await _context.Language.Select(x => new LanguageModel()
23-
{
24-
Id = x.Id,
25-
Description = x.Description,
26-
Name = x.Name
27-
}).ToListAsync();
28-
}
29-
30-
}
31-
}
1+
using Microsoft.EntityFrameworkCore;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Webgentle.BookStore.Data;
7+
using Webgentle.BookStore.Models;
8+
9+
namespace Webgentle.BookStore.Repository
10+
{
11+
public class LanguageRepository:ILanguageRepository
12+
{
13+
private readonly BookStoreContext _context = null;
14+
15+
public LanguageRepository(BookStoreContext context)
16+
{
17+
_context = context;
18+
}
19+
20+
public async Task<List<LanguageModel>> GetLanguages()
21+
{
22+
return await _context.Language.Select(x => new LanguageModel()
23+
{
24+
Id = x.Id,
25+
Description = x.Description,
26+
Name = x.Name
27+
}).ToListAsync();
28+
}
29+
30+
}
31+
}

‎Webgentle.BookStore/Webgentle.BookStore/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public void ConfigureServices(IServiceCollection services)
3434
// option.HtmlHelperOptions.ClientValidationEnabled = false;
3535
//});
3636
#endif
37-
services.AddScoped<BookRepository, BookRepository>();
38-
services.AddScoped<LanguageRepository, LanguageRepository>();
37+
services.AddScoped<IBookRepository, BookRepository>();
38+
services.AddScoped<ILanguageRepository, LanguageRepository>();
3939
}
4040

4141
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /