Friday, March 17, 2023

methods available in Dapper

 Dapper is a micro ORM library for .NET and .NET Core applications that allows you to execute SQL queries and map the results to objects. Dapper provides various methods that you can use to perform different operations on data sources. Some of the most common methods are:

  • Execute: This method executes a command one or multiple times and returns the number of affected rows in the database tables. It can be used for INSERT, UPDATE, DELETE, or other statements that do not return any result set.
  • Query: This method executes a query and returns an IEnumerable<T> of mapped objects. It can be used for SELECT statements that return one or more rows of data.
  • QueryFirst: This method executes a query and returns the first result as an object. It can be used for SELECT statements that return a single row of data or when you only need the first row of a result set.
  • QueryFirstOrDefault: This method executes a query and returns the first result as an object or a default value if no result is found. It can be used for SELECT statements that may return zero or one row of data.
  • QuerySingle: This method executes a query and returns the single result as an object. It throws an exception if no result or more than one result is found. It can be used for SELECT statements that are expected to return exactly one row of data.
  • QuerySingleOrDefault: This method executes a query and returns the single result as an object or a default value if no result is found. It throws an exception if more than one result is found. It can be used for SELECT statements that may return zero or one row of data.
  • QueryMultiple: This method executes a query and returns a GridReader object that can access multiple result sets by using methods such as Read<T> and ReadAsync<T>. It can be used for stored procedures or queries that return more than one result set.

Saturday, January 14, 2023

code for backend pagination

code for backend pagination Pagination is a technique for breaking up large sets of data into smaller, more manageable chunks. Here is an example of how pagination could be implemented on the backend using Node.js and MongoDB: // Import the required modules const express = require('express'); const mongoose = require('mongoose'); // Connect to the MongoDB database mongoose.connect('mongodb://localhost/mydb'); // Create the express app const app = express(); // Define the model for the data const Data = mongoose.model('Data', { name: String }); // Define the route for pagination app.get('/data', (req, res) => { // Get the page number and page size from the query parameters const pageNumber = req.query.page || 1; const pageSize = req.query.size || 10; // Calculate the skip and limit for the query const skip = (pageNumber - 1) * pageSize; const limit = pageSize; // Find the data in the database, skipping and limiting as necessary Data.find({}).skip(skip).limit(limit).exec((err, data) => { if (err) { res.status(500).send(err); } else { res.json(data); } }); }); // Start the server app.listen(3000, () => { console.log('Server started on port 3000'); }); This is a very basic example that demonstrates the basic concepts of pagination. In a real-world application, you would likely want to add additional error handling, validation, and security. You can also use other libraries like mongoose-pagination, mongoose-paginate-v2, mongoose-paginator, etc that can make the pagination process more easy and efficient.

Saturday, November 19, 2022

asp.net middleware extension methods

  •  UseDevelopeerExceptionPage
    • Captures synchronous and asynchronous System.Exception instances from the pipeline and generates HTML error responses.
  • UseHsts
    • Adds middleware for using HSTS, which adds the Strict-Transport-Security header.
  • UseRouting
    • Adds middleware that defines a point in the pipeline where routing decisions are made and must be combined with a call to UseEndpoints where the processing is then executed. This means that for our code, any URL paths that match / or /index or /page will be mapped to Razor Pages and match on /hello will be mapped to the anonymous delegate. Any other URL paths will be passed on to the next delegate for matching, for example, static files. This is why, although it looks like the mapping for Razor Pages and /hello happen after static files in the pipeline, they actually take priority because the call to UseRouting happens before UseStaticFiles.
  • UseHttpsRedirection
    • Adds middleware for redirecting HTTPS requests to HTTPS, so in our code request for htttp://lcocalhost:5000 would be modified to https://localhost:5001
  • UseDefaultFiles
    • Adds middleware that enables default file mapping on the current path, so in our code it would identify files such as index.html
  • UseStaticFiles
    • Adds middleware that looks in wwwroot for static files to return in the HTTP response.
  • UseEndpoints
    • Adds middleware to execute to generate response from decision made earlier in the pipeline. Two endpoints are added, as shown in the following sub-list.
      • MapRazorPages
        • Adds middleware that will map URL paths such as /suppliers to Razor Page file in the /Pages folder named suppliers.cshtml and return the result as the HTTP response.
      • MapGet
        • Adds middleware that will map URL paths such as /hello to an inline delegate writes plain test directly to the HTTP response.

run, map and use methods in asp.net

 Run

Adds a middleware delegate that determinates the pipeline by immediately returning a response instead of calling the next middleware delegate.

Map

Adds a middleware delegate that creates a branch in the pipeline when there is a matching request usually based on a URL path like /hello


Use

Adds a middleware delegate that forms part of the pipeline so it can decide if it wants to pass the request to the next delegate in the pipeline and it can modify the request and response before and after the next delegate.


Registering services in the ConfigureServices method

AddMvcCore
minimum set of services to route and invoke controller. most websites will need more configuration than this.

AddAuthorization
authentication and authorization services.

AddDataAnnotations
mvc data annotations service.

AddCacheTagHelper
mvc cache tag helper service

AddRazorPages
razor pages service includes the razor view engine. commonly used in simple website projects. it calls the following additional methods.
  • AddMvcCore
  • AddAuthorization
  • AddDataAnnotations
  • AddCacheTagHelper
  • AddApiExplorer
  • AddCors
  • AddFormaterMappings
AddViews
support for .cshtml views including default conventions.

AddRazorViewEngine
support for razor view engine including proessing the @symbol.

AddControllerWithViews
controller, views, and pages services. commonly used in ASP.NET Core MVC projects. it calls the following additional methods.
  • AddMvcCore
  • AddAuthorization
  • AddDataAnnotations
  • AddCacheTagHelper
  • AddApiExplorer
  • AddCors
  • AddFormattermappings
  • AddViews
  • AddRazorViewEngine
AddMvc
similar to AddControllerWithViews, but you should only use it for backward compatibility.

AddContext<T>
your DbContext type and its optional DbContextOptions<TContext>

AddCustomContext
a custom extension method we created to make it easier to register the NorthwindContext class for either SQLite or SQL Server based on the project referenced.





Friday, November 18, 2022

The Object Oriented Thought Process 5th Edition

Object Wrappers
object wrappers are object-oriented code that includes other code inside for example. you can take structured code (such as loops and conditions) and wrap it inside an object to make it look like an object. You can also use object wrappers to wrap functionality such as security features, nonportable hardware features, and so on.


sound class design guidelines
keep in mind that it is possible to create poorly designed oo classes that do not restrict access to class attributes. The bottom line is that you can design bad code just as efficiently with oo design as with any other programming methodology.

in general, objects should not manipulate the internal data of other objects.


Wednesday, November 9, 2022

How to pass data to parent in vue.js

 
This is how we can pass data from child to parent component in vue js.

  1. child component should emit a custom event which pass data.
  2. parent component should listen for it via v-on

methods available in Dapper

  Dapper is a micro ORM library for .NET and .NET Core applications that allows you to execute SQL queries and map the results to objects. D...