- using Static.
- Auto property initializer.
- Dictionary Initializer.
- nameof Expression.
- New way for Exception filters.
- await in catch and finally block.
- Null – Conditional Operator.
- Expression – Bodied Methods
- Easily format strings – String interpolation
Tuesday, 13 October 2020
Local Storage vs Session Storage vs Cookie
LocalStorage
localStorage is a way to store data on the client’s computer. It allows the saving of key/value pairs in a web browser and it stores data with no expiration date. localStorage can only be accessed via JavaScript, and HTML5. However, the user has the ability to clear the browser data/cache to erase all localStorage data.
Pros:
- stores data with no expiration date
- storage limit is about 5MB
- data is never transferred to the server
- plaintext, hence not secure by design
- limited to string data, hence need to be serialized
- can only be read on client-side
- Stores data only for a session, meaning that the data is stored until the browser (or tab) is closed
- data is never transferred to the server
- can only be read on client-side
- storage limit is about 5-10MB
- opening multiple tabs/windows with the same URL creates sessionStorage for each tab/window
- Stores data that has to be sent back to the server with subsequent XHR requests. Its expiration varies based on the type and the expiration duration can be set from either server-side or client-side .
- Cookies are primarily for server-side reading (can also be read on client-side), localStorage and sessionStorage can only be read on client-side.
- Size must be less than 4KB.
- Cookies can be made secure by setting the httpOnly flag as true for that cookie. This prevents client-side access to that cookie.
call() , apply() and bind() in JavaScript
Bind( )
The bind method creates a new function and sets the this keyword to the specified object.
For example: Let’s suppose we have two person objects.
Let’s add a greeting function:
We can use the bind method on the greeting function to bind the this keyword to john and jane objects.
For example:
Here greeting.bind(john) creates a new function with this set to john object, which we then assign to greetingJohn variable. Similarly for greetingJane.
Bind() can also accept arguments: We can also pass extra arguments to the bind method.
Call ( )
The call method sets the this inside the function and immediately executes that function.
The difference between call() and bind() is that the call() sets the this keyword and executes the function immediately and it does not create a new copy of the function, while the bind() creates a copy of that function and sets the this keyword.
Call () can also accept arguments: Call() also accepts a comma-separated list of arguments.
The apply() method is similar to call(). The difference is that the apply() method accepts an array of arguments instead of comma separated values.
- bind: It binds the function with provided value and context but it does not executes the function. To execute function you need to call the function.
- call: It executes the function with provided context and parameter.
- apply: It executes the function with provided context and parameter as array.
We have learned that how this keyword behaves differently in JavaScript than in other object-oriented languages. The call, bind and apply methods can be used to set the this keyword independent of how a function is called.
The bind method creates a copy of the function and sets the this keyword, while the call and apply methods sets the this keyword and calls the function immediately.
Reference: https://blog.bitsrc.io/, https://www.codementor.io
Monday, 12 October 2020
HTTP Methods
REST APIs enable you to develop any kind of web application having all possible CRUD (create, retrieve, update, delete) operations. REST guidelines suggest using a specific HTTP method on a particular type of call made to the server (though technically it is possible to violate this guideline, yet it is highly discouraged).
Use below-given information to find a suitable HTTP method for the action performed by API.
- HTTP GET
- HTTP POST
- HTTP PUT
- HTTP DELETE
- HTTP PATCH
GET: GET method is used to retrieve data from a server at the specified resource. For example, say you have an API with a /users endpoint. Making a GET request to that endpoint should return a list of all available users.
For any given HTTP GET API, if the resource is found on the server, then it must return HTTP response code 200 (OK) – along with the response body, which is usually either XML or JSON content (due to their platform-independent nature).
Since a GET request is only requesting data and not modifying any resources, it's considered a safe and idempotent method.
Idempotence means that applying an operation once or applying it multiple times has the same effect. Examples: Multiplication by zero. No matter how many times you do it, the result is still zero.
POST: In web api, POST requests are used to send data to the API server to create or update a resource. The data sent to the server is stored in the request body of the HTTP request.
The simplest example is a contact form on a website. When you fill out the inputs in a form and hit Send, that data is put in the response body of the request and sent to the server. This may be JSON, XML, or query parameters.
Ideally, if a resource has been created on the origin server, the response SHOULD be HTTP response code 201 (Created) and contain an entity which describes the status of the request and refers to the new resource.
It's worth noting that a POST request is non-idempotent. It mutates data on the backend server (by creating or updating a resource), as opposed to a GET request which does not change any data.
PUT: Similar to POST, PUT requests are used to send data to the API to update or create a resource. The difference is that PUT requests are idempotent. So if you send a request multiple times, that should be equivalent to single request modification. In contrast, calling a POST request repeatedly make have side effects of creating the same resource multiple times.
Generally, when a PUT request creates a resource the server will respond with a 201 (Created), and if the request modifies existing resource the server will return a 200 (OK) or 204 (No Content).
The difference between the POST and PUT APIs can be observed in request URIs. POST requests are made on resource collections, whereas PUT requests are made on a single resource.
PATCH: HTTP PATCH requests are to make partial update on a resource. If you see PUT requests also modify a resource entity, so to make more clear – PATCH method is the correct choice for partially updating an existing resource, and PUT should only be used if you’re replacing a resource in its entirety.
DELETE: As the name applies, DELETE APIs are used to delete resources.
A successful response of DELETE requests should be HTTP response code 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has been queued, or 204 (No Content) if the action has been performed but the response does not include an entity.
DELETE operations are idempotent. If you DELETE a resource, it’s removed from the collection of resources. Repeatedly calling DELETE API on that resource will not change the outcome – however, calling DELETE on a resource a second time will return a 404 (NOT FOUND) since it was already removed. Some may argue that it makes the DELETE method non-idempotent. It’s a matter of discussion and personal opinion.
PUT VS PATCH:
When a client needs to replace an existing Resource entirely, they can use PUT. When they’re doing a partial update, they can use HTTP PATCH.
For instance, when updating a single field of the Resource, sending the complete Resource representation can be cumbersome and uses a lot of unnecessary bandwidth. In such cases, the semantics of PATCH make a lot more sense.
Glossary:-
Safe Methods
As per HTTP specification, the GET and HEAD methods should be used only for retrieval of resource representations – and they do not update/delete the resource on the server. Both methods are said to be considered “safe“.
This allows user agents to represent other methods, such as POST, PUT and DELETE, in a unique way so that the user is made aware of the fact that a possibly unsafe action is being requested – and they can update/delete the resource on server and so should be used carefully.
Idempotent Methods
The term idempotent is used more comprehensively to describe an operation that will produce the same results if executed once or multiple times. Idempotence is a handy property in many situations, as it means that an operation can be repeated or retried as often as necessary without causing unintended effects. With non-idempotent operations, the algorithm may have to keep track of whether the operation was already performed or not.
In HTTP specification, The methods GET, HEAD, PUT and DELETE are declared idempotent methods.
https://restfulapi.net/idempotent-rest-apis/
Sunday, 11 October 2020
Angular: ViewChild and ContentChild
Essentially ViewChild and ContentChild are used for component communication in Angular. Therefore, if a parent component wants access of child component then it uses ViewChild or ContentChild.
Any component, directive, or element which is part of a template is ViewChild and any component or element which is projected in the template is ContentChild.
- Child Component
- Directive
- DOM Element
- Import ViewChild and AfterViewInit from @angular/core
- Implement AfterViewInit life cycle hook to component class
- Create a variable with decorator @ViewChild
- Access that inside ngAfterViewInit life cycle hook
- Import ContnetChild and AfterContnetInit from @angular/core
- Implement AfterContnetInit life cycle hook to component class
- Create a variable with decorator @ContnetChild
- Access that inside ngAfterContnetInit life cycle hook
- ViewChildren don’t include elements that exist within the ng-content tag.
- ContentChildren includes only elements that exists within the ng-content tag.
Typescript: Iterators (for..in and for..of)
for..of statements
for..of loops over an iterable object. Here is a simple for..of loop on an array:
for..of vs. for..in statements
Both for..of and for..in statements iterate over lists; the values iterated on are different though, for..in returns a list of keys on the object being iterated, whereas for..of returns a list of values of the numeric properties of the object being iterated.
Here is an example that demonstrates this distinction:
Another distinction is that for..in operates on any object; it serves as a way to inspect properties on this object. for..of on the other hand, is mainly interested in values of iterable objects. Built-in objects like Map and Set implement Symbol.iterator property allowing access to stored values.
Saturday, 10 October 2020
Javascript: Map and Set
Now we’ve learned about the following complex data structures:
- Objects for storing keyed collections.
- Arrays for storing ordered collections.
But that’s not enough for real life. That’s why Map and Set also exist.
Map
Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows keys of any type.
Methods and properties are:
- new Map() – creates the map.
- map.set(key, value) – stores the value by the key.
- map.get(key) – returns the value by the key, undefined if key doesn’t exist in map.
- map.has(key) – returns true if the key exists, false otherwise.
- map.delete(key) – removes the value by the key.
- map.clear() – removes everything from the map.
- map.size – returns the current element count.
For instance:
Map can also use objects as keys.
Every map.set call returns the map itself, so we can “chain” the calls:
Iteration over Map
For looping over a map, there are 3 methods:
- map.keys() – returns an iterable for keys,
- map.values() – returns an iterable for values,
- map.entries() – returns an iterable for entries [key, value],
Set
A Set is a special type collection – “set of values” (without keys), where each value may occur only once.
Its main methods are:
- new Set(iterable) – creates the set, and if an iterable object is provided (usually an array), copies values from it into the set.
- set.add(value) – adds a value, returns the set itself.
- set.delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
- set.has(value) – returns true if the value exists in the set, otherwise false.
- set.clear() – removes everything from the set.
- set.size – is the elements count.
The main feature is that repeated calls of set.add(value) with the same value don’t do anything. That’s the reason each value appears in a Set only once.
For example, we have visitors coming, and we’d like to remember everyone. But repeated visits should not lead to duplicates. A visitor must be “counted” only once.
Set is just the right thing for that:
Monday, 5 October 2020
ASP.NET Core Dependency Injection
How can we inject the service dependency into the controller?
There are three easy steps to add custom service as a dependency on the controller.
Step 1: Create the service
Step 2: Add this service to Service container (service can either added by singleton, transient or scoped)
Step 3: Use this service as a dependency in the controller
Service lifetimes
Services can be registered with one of the following lifetimes:
- Transient
- Scoped
- Singleton
Singleton
ASP.NET Core will create and share a single instance of the service through the application life. The service can be added as a singleton using AddSingleton method of IServiceCollection. ASP.NET Core creates service instance at the time of registration and subsequence request use this service instance. Here, we do not require to implement Singleton design pattern and single instance maintained by the ASP.NET Core itself.
Transient
Transient lifetime services are created each time they're requested from the service container. This lifetime works best for lightweight, stateless services. Register transient services with AddTransient.
In apps that process requests, transient services are disposed at the end of the request.
Scoped
Scoped lifetime services are created once per client request (connection). Register scoped services with AddScoped.
In apps that process requests, scoped services are disposed at the end of the request.
When using Entity Framework Core, the AddDbContext extension method registers DbContext types with a scoped lifetime by default.
Reference: https://docs.microsoft.com
Topics
Pages
- Dotnet Core Interview Q/A
- React JS Interview Questions and Answers
- Angular Interview Q/A
- Node JS Interview Q/A
- Azure Interview Q/A
- .Net Interview Q/A
- WEB API Interview Q/A
- Javascript Interview Q/A (Part- 1)
- Javascript Interview Q/A (Part- 2)
- SQLServer Interview Questions
- SQL - Query Related Interview Q/A
- ASP .Net MVC Interview Q/A
- Angular Testing Interview Q/A
- Lead level Interview Q/A
- WCF Interview Q/A
- ExpressJS Interview Q/A
- MongoDB Interview Q/A
- C# Programs Output Q/A
- InterView Programming
- ASP .Net Interview Q/A
- Angularjs Interview Q/A
- SQL Server 2005/2008 Functions
- Entity Framework Interview Q/A
- JQuery Selectors / Methods
- Ado .Net InterView Q/A
- LINQ Operators and Lambda Expression
Dotnet Guru Archives
-
▼
2020
(31)
-
▼
Oct 2020
(10)
- New Features in C# 6.0
- Local Storage vs Session Storage vs Cookie
- call() , apply() and bind() in JavaScript
- HTTP Methods
- Angular: ViewChild and ContentChild
- Typescript: Iterators (for..in and for..of)
- Javascript: Map and Set
- ASP.NET Core Dependency Injection
- ASP.NET Core Custom Middleware
- map(), filter(), and reduce() in JavaScript
-
▼
Oct 2020
(10)