First the difference between List<T>, IEnumerable<T> and IQueryable<T>.
The content of List<T> will always be loaded into memory, because it uses an array in the background.
This blog contains Azure, .Net, SQL & Full stack development concepts and code fixes.
First the difference between List<T>, IEnumerable<T> and IQueryable<T>.
The content of List<T> will always be loaded into memory, because it uses an array in the background.
The content of IEnumerable<T> will only load the requested item into the memory. Compiler sets up a state machine during compiling a method with IEnumerable<T> that has a yield return T. So only one item at a time will be loaded into memory in this case.
IQueryable<T> is "special" version of IEnumerable<T>. Specifically designed to query data sources, and implemented by data providers. Like EF, NHibernate. So when you tell EF to fetch the data with .ToListAsync<T>() EF will only then go the database and execute the generated query. See the docs for more info about IQueryable<T>.
1.Add Mapster packages from NuGet
2.Create Source class
3.Create Destination class
4.Create Service or repository class where mapping required to convert Dto to Enity object.
Simple Save and Retrieve example below for using mapster and entity framework.
Why Mapster over Auto mapper
| Method | Mean | StdDev | Error | Gen 0 | Gen 1 | Gen 2 | Allocated |
|---|---|---|---|---|---|---|---|
| 'Mapster 6.0.0' | 108.59 ms | 1.198 ms | 1.811 ms | 31000.0000 | - | - | 124.36 MB |
| 'Mapster 6.0.0 (Roslyn)' | 38.45 ms | 0.494 ms | 0.830 ms | 31142.8571 | - | - | 124.36 MB |
| 'Mapster 6.0.0 (FEC)' | 37.03 ms | 0.281 ms | 0.472 ms | 29642.8571 | - | - | 118.26 MB |
| 'Mapster 6.0.0 (Codegen)' | 34.16 ms | 0.209 ms | 0.316 ms | 31133.3333 | - | - | 124.36 MB |
| 'ExpressMapper 1.9.1' | 205.78 ms | 5.357 ms | 8.098 ms | 59000.0000 | - | - | 236.51 MB |
| 'AutoMapper 10.0.0' | 420.97 ms | 23.266 ms | 35.174 ms | 87000.0000 | - | - | 350.95 MB |
Azure Cosmo DB used to store non Sql data that is collection of objects.
Important key stack properties are id and partition key.
Partition Key is kind of grouping / or like disk drive C, D drive in our PC,Lap.
Make sure partition key as perfect column Ex: need to retrieve data frequently day wise means then i will make CreatedDate as partition key (CreatedDate= DateTime.UtcNow.Date)
id: its mandatory property & non-nullable
Unique/Primary key: will be combination of Partition Key + id
we can insert same id again but it's partition should be different. also, we can store different type of object but that object should contain same id and partition key.
Hierarchy: Example based OrderGroups is container
-->Data Base
|__ Container: kind of table or entity name
|__ Items: Kind of Table Entity values
|__Stored Procedure
|__User Defined Functions & Stored Procedure
|__ Triggers
1. Constraints
2. Data Types
3.Diff between where and having clause
| 1. | WHERE Clause is used to filter the records from the table based on the specified condition. | HAVING Clause is used to filter record from the groups based on the specified condition. |
| 2. | WHERE Clause can be used without GROUP BY Clause | HAVING Clause cannot be used without GROUP BY Clause |
| 3. | WHERE Clause implements in row operations | HAVING Clause implements in column operation |
| 4. | WHERE Clause cannot contain aggregate function | HAVING Clause can contain aggregate function |
| 5. | WHERE Clause can be used with SELECT, UPDATE, DELETE statement. | HAVING Clause can only be used with SELECT statement. |
| 6. | WHERE Clause is used before GROUP BY Clause | HAVING Clause is used after GROUP BY Clause |
| 7. | WHERE Clause is used with single row function like UPPER, LOWER etc. SELECT COUNT(CustomerID), Country FROM Customers where Country='Brazil' or Country!='France' GROUP BY Country HAVING COUNT(CustomerID) > 5; | HAVING Clause is used with multiple row function like SUM, COUNT etc. Below result first group by Country then filter from group data. SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5; |
Temp tables: are created in tempdb, it contains two types #local & ##Global
These tables act as the normal table and also can have constraints, an index like normal tables.
#local temp tables available only for within session alone.
CREATE TABLE #LocalTemp
(
UserID int,
Name varchar(50),
Address varchar(150)
)
GO
insert into #LocalTemp values ( 1, 'Shailendra','Noida');##Global temp tables can access to all sessions.
CREATE TABLE ##GlobalTemp
(
UserID int,
Name varchar(50),
Address varchar(150)
)
GO
insert into ##GlobalTemp values ( 1, 'Shailendra','Noida');
GO
Select * from ##GlobalTempCTE: CTE is a named temporary result set which is used to manipulate the complex sub-queries data. This exists for the scope of a statement. This is created in memory rather than the Tempdb database. we cannot create an index on CTE
CTE improves readability and ease in maintenance of complex queries and sub-queries. Always begin CTE with a semicolon.
;With CTE1(Address, Name, Age)--Column names for CTE, which are optional
AS
(
SELECT Addr.Address, Emp.Name, Emp.Age from Address Addr
INNER JOIN EMP Emp ON Emp.EID = Addr.EID
)
SELECT * FROM CTE1 --Using CTE
WHERE CTE1.Age > 50
ORDER BY CTE1.NAMETable Variable: This acts like a variable and exists for a particular batch of query execution. It gets dropped once it comes out of the batch. This is also created in the tempdb database but not the memory. This also allows you to create a primary key, identity at the time of Table variable declaration but not non-clustered index.
GO DECLARE @TProduct TABLE ( SNo INT IDENTITY(1,1), ProductID INT, Qty INT ) --Insert data to Table variable @Product INSERT INTO @TProduct(ProductID,Qty) SELECT DISTINCT ProductID, Qty FROM ProductsSales ORDER BY ProductID ASC --Select data Select * from @TProduct --Next batch GO Select * from @TProduct --gives error in next batch
5.Indexes
Indexes are special lookup tables that the database search engine can use to speed up data retrieval. Simply put, an index is a pointer to data in a table. An index in a database is very similar to an index in the back of a book.
6.ACID: properties
A- Atomicity : fail or success it should apply for all transaction not partial.
7. Joins i. Inner Join : retrieve only matched rows between tables
ii. Outer Join : retrieve only Not matched rows between tables
iii. Left Join : Table A Left join Table B => Retrieve all from Table A and Only Matched rows From Table B.
iv. Right Join: Vice versa of Left join
8. Union(without duplicate rows) Vs Union All (include duplicate rows)
Default exception will handled by ExceptionFilter, incase we need to override this then we can derived from IExceptionFilter and we can customize it.
Predefined interface for Exception filter is below
Let us create a class and inherit from IExceptionFilter file name like GlobalExceptionFilter.cs
Above interface contains OnException method so when we derived from tat interface we need to provide definition.
Now we need create Startup.cs class and map in program.cs file like below.
we mentioned above like all configuration based on Startup class.
Now startup class we need add like below.
so in startup class below highlighted code is called exception filter config.