Onion model in asp.net core
QA_DataAccess[Class Library Folder]
Package
- "Microsoft.EntityFrameworkCore.Design" Version="5.0.0"
BaseEntity.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace QA_DataAccess{public class BaseEntity{public int Id { get; set; }}}Product.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace QA_DataAccess{public class Product : BaseEntity{public string Name { get; set; }public string Details { get; set; }public int StockAvailable { get; set; }}}
QA_Repository[Class Library Folder]
Package
"Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0"
ApplicationDbContext.cs
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using QA_DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QA.Repository
{
public class ApplicationDbContext : IdentityDbContext
{
//Ctor 2Tab to Generate Constructor
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
}
}
IRepository.cs
using QA_DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QA.Repository
{
//IRepository is Inherited with T Class
public interface IRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
T GetById(int Id);
}
}
Repository.cs
using Microsoft.EntityFrameworkCore;
using QA_DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QA.Repository
{
//Repository is Inherited with the IRepository of T
public class Repository<T> : IRepository<T> where T : BaseEntity
{
//Generate constructor of ApplicationDbContext
private readonly ApplicationDbContext _context;
private readonly DbSet<T> entity;
public Repository(ApplicationDbContext context)
{
_context = context;
entity = _context.Set<T>();
}
//Generate Implemented Interface of IRepository
public IEnumerable<T> GetAll()
{
return entity.AsEnumerable();
}
public T GetById(int Id)
{
return entity.FirstOrDefault(x => x.Id == Id);
}
}
}
QA_Service[Class Library Folder]
IProductRepo.cs
using QA_DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QA.Service
{
public interface IProductRepo
{
IEnumerable<Product> GetAllProduct();
Product GetProductById(int Id);
}
}
ProductRepo.cs
using QA.Repository;
using QA_DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QA.Service
{
//ProductRepo is Inherited with IProductRepo
public class ProductRepo : IProductRepo
{
private IRepository<Product> _repository;
//Generate implement Interface
public ProductRepo(IRepository<Product> repository)
{
_repository = repository;
}
public IEnumerable<Product> GetAllProduct()
{
return _repository.GetAll();
}
public Product GetProductById(int Id)
{
return _repository.GetById(Id);
}
}
}
QA_WebAPI[Class Library Folder]
Package
- "Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.0"
- "Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0"
- "Microsoft.EntityFrameworkCore.Tools" Version="5.0.0"
Controllers[Folder]
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using QA.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace QA.WebAPI.Controllers
{
public class HomeController : Controller
{
//IProductRepo come from startup.cs file
//Generate Constructor of IProductRepo
private readonly IProductRepo _repo;
public HomeController(IProductRepo repo)
{
_repo = repo;
}
[HttpGet]
public IActionResult GetProducts()
{
var products = _repo.GetAllProduct();
return View(products);
}
}
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=CHETUIWK1689\\MSSQL2019;Database=OADatabase;Trusted_Connection=True;MultipleActiveResultSets=True"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using QA.Repository;
using QA.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace QA.WebAPI
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext<ApplicationDbContext>
(Options => Options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"),
b => b.MigrationsAssembly("QA_Repository")
));
//Add some Dependency Injection
services.AddTransient(typeof(IRepository<>), typeof(Repository<>));
services.AddTransient<IProductRepo, ProductRepo>();
//services.AddSwaggerGen(c =>
//{
// c.SwaggerDoc("v1", new OpenApiInfo { Title = "QA_WebAPI", Version = "v1" });
//});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseSwagger();
//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QA_WebAPI v1"));
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
0 Comments