using micro orm "dapper" in .netcore5 webapi - sample project -

 


//*** startup.cs
 
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebApiCoreWithDapper.Data;

namespace WebApiCoreWithDapper
{
    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<dbContextNW>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DBConnection")));
            //*****************************************************************************

        }

        // 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.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

///************ productscontroller
 using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using WebApiCoreWithDapper.Data;

namespace WebApiCoreWithDapper.Controllers
{
    [Route("[controller]")]
    [ApiController
    
    public class ProductsController : ControllerBase
    {

        private readonly dbContextNW _context;
        private readonly SqlConnection connection;
        public ProductsController(dbContextNW context)
        {
            _context = context;

            connection = new SqlConnection(_context.Database.GetConnectionString());
        }


        //************************************* 
        [HttpGet]
        [Route("getAll")]
        public async Task<IEnumerable<Products>> GetAllProductsAsync()
        {
            string sql = "SELECT * FROM dbo.PRODUCTS";
            return await connection.QueryAsync<Products>(sql); 
            //return connection.Query<Products>(sql).ToList(); 
        }

        //*************************************
        [HttpGet]
      
        public async Task<ActionResult<IEnumerable<Products>>> GetProducts()
        {
            return await _context.Products.ToListAsync();
        }

         
        [HttpGet("{id}")]
        public async Task<ActionResult<Products>> GetProduct(int id)
        {
            var products = await _context.Products.FindAsync(id);

            if (products == null)
            {
                return NotFound();
            }

            return products;
        }






    }
}

**** GitHub public :
https://github.com/doktoralban/WebApiCoreWithDapper








Yorumlar