Monday, 25 November 2024

Difference Between View and Temp Table

 A view exists only for a single query. Each time you use the name of a view, its table is recreated from existing data. A temporary table exists for the entire database session in which it was created. A view is automatically populated with the data retrieved by the query that defines it.

A view contains no data of its own but it is like a ‘window’ through which data from tables can be viewed or changed.  A view is a query of one or more tables that provides another way of presenting the information. In layman’s terms, a view is a “stored query “.  It is also called a derived table because it is derived from another table. Views do not store data rather they derive their data from tables on which they are based, referred to as the base table of the view. The view is stored as a SELECT statement in the data dictionary. Creating a view fulfills the requirement without storing a separate copy of the data because a view does not store any data of its own and always takes the data from a base table. as the data is taken from the base table, accurate and up-to-date information is required.

Advantages of views

Security

Each user can be given permission to access the database only through a small set of views that contain the specific data the user is authorized to see, thus restricting the user's access to stored data

Query Simplicity

A view can draw data from several different tables and present it as a single table, turning multi-table queries into single-table queries against the view.

Structural simplicity

Views can give a user a "personalized" view of the database structure, presenting the database as a set of virtual tables that make sense for that user.

Consistency

A view can present a consistent, unchanged image of the structure of the database, even if the underlying source tables are split, restructured, or renamed.

Data Integrity

If data is accessed and entered through a view, the DBMS can automatically check the data to ensure that it meets the specified integrity constraints.

Logical data independence.

View can make the application and database tables to a certain extent independent. If there is no view, the application must be based on a table. With the view, the program can be established in view of above, to view the program with a database table to be separated.

Disadvantages of views

Performance

Views create the appearance of a table, but the DBMS must still translate queries against the view into queries against the underlying source tables. If the view is defined by a complex, multi-table query then simple queries on the views may take considerable time.

Update restrictions

When a user tries to update rows of a view, the DBMS must translate the request into an update on rows of the underlying source tables. This is possible for simple views, but more complex views are often restricted to read-only.

When a table is dropped, associated view become irrelevant.

Since view are created when a query requesting data from view is triggered, its bit slow

 When views are created for large tables, it occupy more memory .

Webhook in ASP.NET

 A webhook is a mechanism that allows one application to send real-time data or notifications to another application when a specific event occurs. Instead of constantly polling for updates, the recipient application registers a webhook URL with the sender application, which then sends a POST request to that URL whenever the event takes place.

Webhooks are commonly used for various purposes, including:

  1. Real-time notifications: Instantly inform users of events, such as new messages or updates.
  2. Data synchronization: Keep data between systems in sync without the need for manual intervention.
  3. Automation: Trigger actions or workflows based on external events.

Implementing Webhooks in .NET

To implement webhooks in a .NET application, you'll need to create a web API endpoint to receive incoming webhook requests and process the data. Let's walk through the steps with a practical example using ASP.NET Core.

Step 1. Create a .NET Web API Project

Start by creating a new ASP.NET Core Web API project in Visual Studio or your preferred development environment.

Step 2. Define a Webhook Model

Create a model class to represent the data you expect to receive from the webhook, for example:

public class WebhookData
{
    public string EventName { get; set; }
    public string Payload { get; set; }
}
C#

Step 3. Create a Webhook Controller

Generate a controller to handle incoming webhook requests. Here's a basic example:

[ApiController]
[Route("api/webhooks")]
public class WebhooksController : ControllerBase
{
    [HttpPost]
    public IActionResult ReceiveWebhook(WebhookData webhookData)
    {
        // Handle the incoming webhook data here
        // You can perform actions based on the event and payload

        // For demonstration purposes, we'll just return a success response
        return Ok("Webhook received successfully");
    }
}
C#

Step 4. Configure Route and Validate Requests

In a production environment, you should validate incoming webhook requests to ensure they are from a trusted source. You can use authentication tokens or other security measures to validate requests.

Step 5. Register Your Webhook URL

Once you've deployed your webhook API, you'll need to register the webhook URL with the service or application that will send webhook events. This is typically done through the service's webhook settings, where you specify the URL and the events you want to subscribe to.

Step 6. Handle Webhook Events

Inside the ReceiveWebhook action in your controller, you can implement logic to handle specific webhook events. Depending on your use case, this could involve updating the database, sending notifications, or triggering other actions.

Testing Webhooks

To test your webhook implementation, you can use tools like ngrok to expose your local development environment to the internet, allowing external services to send test webhook requests to your API.

Monday, 18 November 2024

What are the Advantages and Disadvantages of Microservices Architecture?

 What are Microservices?

Microservices are a software architectural style that structures an application as a collection of loosely coupled, independently deployable services. Each microservice is designed to perform a specific business function and can be developed, deployed, and scaled independently. This approach contrasts with monolithic architecture, where an application is built as a single, tightly integrated unit.


Advantages of Microservices Architecture

Microservices architecture is a design approach where a large application is divided into smaller, loosely coupled services. Each service is responsible for a specific business capability and communicates with other services via APIs. Here’s an in-depth look at microservices architecture with examples for each point:

Advantages of Microservices Architecture

1. Modularity and Decoupling

In a microservices architecture, each service is designed to handle a specific business function. This modularity helps in isolating services, making them easier to develop, test, and deploy independently.


Example:


Consider an e-commerce application. Instead of a monolithic application handling everything from user authentication to order processing, you have separate microservices:


User Service: Manages user accounts and authentication.

Product Service: Manages product listings and details.

Order Service: Handles order processing and payment.

2. Scalability

Microservices can be scaled independently based on their individual demands. This allows you to allocate resources more efficiently and handle varying loads for different services.


Example:


In an online retail application, the Product Service might need to handle high traffic during sales events, while the User Service might not experience the same level of load. You can scale the Product Service separately to handle increased traffic without affecting the User Service.


3. Fault Isolation

Since services are independent, a failure in one service doesn’t necessarily impact others. This isolation helps maintain overall system stability and reliability.


Example:


If the Order Service encounters a problem, it won't affect the Product Service or the User Service. Customers can still browse products and manage their accounts while the order processing issue is addressed.


4. Technology Diversity

Different services can be developed using different technologies and programming languages that best fit their specific requirements.


Example:


The User Service could be developed using Java for its strong support for security features, while the Product Service might use Node.js for its fast, non-blocking I/O, and the Order Service could use Python for its ease of writing and maintaining complex business logic.


5. Independent Deployment

Each microservice can be deployed independently, allowing for more frequent updates and quicker iteration without affecting other services.


Example:


If you need to update the Product Service to add new filtering options, you can deploy this update without touching the User Service or the Order Service. This reduces deployment risk and accelerates release cycles.


6. Improved Developer Productivity

Teams can work on different services simultaneously without waiting for others to complete their work. This parallel development speeds up overall progress and fosters innovation.


Example:


A development team focused on the Order Service can add new payment methods while another team enhances the User Service with additional authentication features, allowing both services to evolve independently.


Disadvantages of Microservices Architecture

Microservices architecture, while offering many benefits, also comes with its own set of challenges and disadvantages. Here’s an in-depth look at the potential drawbacks of microservices architecture, illustrated with examples:


Disadvantages

Disadvantages of Microservices Architecture

1. Increased Complexity

Managing multiple microservices increases overall system complexity, as each service must be developed, deployed, and maintained independently. Coordinating communication and data consistency across services can be challenging.


Example:


In an e-commerce platform with separate User, Product, and Order Services, developers must handle the interactions between these services, ensuring data integrity and consistency, which can become complex and error-prone.


2. Distributed System Overheads

Microservices introduce network communication between services, which adds overhead compared to in-process communication in monolithic systems. This can lead to latency and performance issues.


Example:


If the Order Service needs to fetch product details from the Product Service and user information from the User Service, the delays and network latencies between these calls can impact overall response times.


3. Data Management Challenges

Each microservice typically manages its own data, which can lead to difficulties in maintaining data consistency and implementing distributed transactions.


Example:


In an application where the Order Service and Inventory Service need to ensure that inventory levels are updated when an order is placed, managing consistent data updates across services without a centralized database can be complex.


4. Increased Deployment and Operational Overhead

Deploying and managing numerous services requires robust infrastructure and tooling. Continuous integration and deployment processes become more complicated, requiring sophisticated monitoring and management systems.


Example:


Each microservice in the e-commerce platform must be deployed, monitored, and logged separately, requiring a comprehensive system for managing service deployments, logging, and health checks.


5. Inter-Service Communication Issues

Microservices communicate over a network, which introduces potential issues such as network failures, latency, and the need for proper API versioning and management.


Example:


If the Product Service's API changes, all dependent services like the Order Service must be updated accordingly. Network issues or API changes can disrupt the communication between services.


6. Testing Complexity

Testing microservices can be more complicated than testing a monolithic application, as it involves ensuring that each service functions correctly both in isolation and in interaction with other services.

Conclusion

Microservices architecture offers significant benefits, including improved scalability, fault isolation, and technology diversity. By breaking applications into small, independently deployable services, it enables more efficient resource utilization, enhances developer productivity, and facilitates faster updates. These advantages make microservices an attractive choice for modern, dynamic applications that demand flexibility and resilience.

Wednesday, 13 November 2024

Azure Table Storage Using Azure Storage Explorer

 Microsoft provides Azure Storage Explorer and Azure Storage Emulator tools for developers who want to test and use storage accounts and services without getting a paid Azure subscription. In this article, I will discuss how to use Azure Table Storage using Development Storage Account, which is free.

Installation Process

Download Microsoft Azure Storage Explorer from the below URL

https://azure.microsoft.com/en-in/features/storage-explorer/

Download Microsoft Azure Storage Emulator - v5.8 (please download v5.8 version only)

https://azure.microsoft.com/en-in/downloads/

Key Point - Microsoft Azure Storage Explorer and Microsoft Azure Storage Emulator both should be active or running.

Start Explorer and Emulator

First, run your Explorer and Emulator. Please see the below snapshot; this will be your explorer.

What is Azure Table Storage?

Azure Table storage stores large amounts of structured data. The service is a NoSQL datastore which accepts authenticated calls from inside and outside the Azure cloud. Azure tables are ideal for storing structured, non-relational data.

How To Create Azure Table storage?

Two Ways:

  1. In Azure Storage Explorer, Right-click on Table and select create table option, in text box enter your table name. Please refer to the below figure.

    Key Note:- table name should be in lowercase.
     
  2. We can create through program instruction also.

For Programing, What Things Are Required? 

The below namespace is required so please run the below NuGet command in your solution.

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
C#

Click On Tools => Hover On => Nuget Package Manager and select package manager console

Execute the below command.

Install-Package WindowsAzure.Storage -Version 9.3.3
HTTP

This will be the default credentials and we are storing at the web config file.

DefaultEndpointsProtocol=http;
AccountName=devstoreaccount1;
AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
HTTP

Please add the below key in webconfig, connection string section.

<connectionStrings>
   <add name="azurestorageconfigurationkey" connectionString="DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" providerName="System.Data.SqlClient"/>
</connectionStrings>
Markup

Add the necessary using statements,

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
C#

Create Table

Get a CloudStorageAccount object that represents your storage account information.

Use the following code, using the name of your storage account and the account key, which you can find in the storage connection string in the webconfig file. 

CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();
C#

Get a CloudTableClient object to reference the table objects in your storage account.

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable studentTable = tableClient.GetTableReference("studenttable");
C#

To create the Azure table, create an async method and within it, call CreateIfNotExistsAsync(),

await studentTable.CreateIfNotExistsAsync();
C#

Please find the bellow figure for code implimentation.

 

Add An Entity(data) To A Table

To add an entity to a table, you create a class that defines the properties of your entity. The following code defines an entity class called StudentEntity that uses the student's "registration number" as the row key and colleage_code_as_partitionkeyvalue as the partition key.

Code Snippet For StudentEntity  Class 

Public class StudentEntity: TableEntity {
    public StudentEntity(string RegistrationNumber, string colleage_code_as_partitionkeyvalue) {
        this.partitionkey = colleage_code_as_partitionkeyvalue);
    this.Rowkey = RegistrationNumber;
}
public StudentEntity() {}
public string StudentName {
    get;
    set;
}
public string Colleage {
    get;
    set;
}
public string Branch {
    get;
    set;
}
C#

Before adding an entity to the table let's discuss two important topics. 

PartitionKey

In Table Storage, you have to decide on the PartitionKey yourself.

In essence, you are responsible for the throughput you’ll get on your system.

If you put every entity in the same partition (by using the same partition key), you’ll be limited to the size of the storage machines for the amount of storage you can use. Plus, you’ll be constraining the maximal throughput as there’s lots of entities in the same partition.

Should you set the PartitionKey to the same value for every entity stored? No. You’ll end up with scaling issues at some point.

RowKey

The RowKey property stores string values that uniquely identify entities within each partition. The PartitionKey and the RowKey together form the primary key for the entity.

We can insert entities two ways,

  1. single entity insert
  2. batch of entity insert

Single Entity Insert

TableOperation - It is class and it Represents a single table operation.

Insert - Creates a new table operation that inserts the given entity into a table.

ExecuteAsync - Initiates an asynchronous operation that executes an asynchronous table operation.

Please refer to the below figure, whatever we inserted into entity is stored under the student table.

Batch Of Entity Insert 

 

TableBatchOperation

Represents a batch operation on a table. Batch operation is a collection of table operations that are executed by the Storage Service REST API as a single atomic operation, by invoking an Entity Group Transaction.

A batch operation may contain up to 100 individual table operations, with the requirement that each operation entity must have the same partition key. A batch with a retrieve operation cannot contain any other operations. Note that the total payload of a batch operation is limited to 4MB.

Insert the specified entity into a table.

Please refer to the below figure for output. In the below figure observe that entities are stored properly.

Get All Of The Entities Using Partition Key

To query a table for all of the entities in a partition, use a TableQuery object. The following code example specifies a filter for entities where '4UB' is the partition key. This example prints the fields of each entity in the query results to the console.

Get A Single Entity

You can write a query to get a single, specific entity. The following code uses a TableOperation object to specify a student registration number '4ub12cs505'. The method returns just one entity, rather than a collection, and the returned value in TableResult. A result is a StudentEntity object.

Specifying both partition and row keys in a query is the fastest way to retrieve a single entity from the Table service.

Delete An Entity

You can delete an entity after you find it. The following code looks for and deletes a student entity register number "4ub12cs502".

Summary

In this blog, we have discussed the below things,

  • Installation of Azure storage explorer and Azure storage emulator.
  • Create an Azure storage table.
  • Add an entity(data) to a table
  • Get a single entity
  • Get all of the entities using partition key
  • Delete an entity

Code Snippet

//Create Storage Account:-
CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
string connectionString = ConfigurationManager.ConnectionStrings["azurestorageconfigurationkey"].ToString();
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable studentTable = tableClient.GetTableReference("studenttable");
await studentTable.CreateIfNotExistsAsync();
//Create a new student entity, single object or row insertion to the table.
StudentEntity studentobject = new StudentEntity("4ub12cs505", "4UB");
studentobject.Branch = "Computer Science";
studentobject.Colleage = "University BDT College";
studentobject.StudentName = "Kiran";
TableOperation insertOperation = TableOperation.Insert(studentobject);
await studentTable.ExecuteAsync(insertOperation);
//Create the batch operation
TableBatchOperation batchOperation = new TableBatchOperation();
StudentEntity stdentdata1 = new StudentEntity("4ub12cs502", "4UB");
stdentdata1.Branch = "Computer Science";
stdentdata1.Colleage = "University BDT College";
stdentdata1.StudentName = "Madan Sorab";
StudentEntity stdentdata2 = new StudentEntity("4ub12cv402", "4UB");
stdentdata2.Branch = "Civil";
stdentdata2.Colleage = "University BDT College";
stdentdata2.StudentName = "Mahesh";
batchOperation.Insert(stdentdata1);
batchOperation.Insert(stdentdata2);
await studentTable.ExecuteBatchAsync(batchOperation);
//Construct the query operation for all student entities where //PartitionKey="4UB". :-
TableQuery < StudentEntity > query = new TableQuery < StudentEntity > ().
Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "4UB"));
TableContinuationToken token = null;
do {
    TableQuerySegment < StudentEntity > resultSegment = await studentTable.ExecuteQuerySegmentedAsync(query, token);
    token = resultSegment.ContinuationToken;
    foreach(StudentEntity entity in resultSegment.Results) {
        Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey, entity.StudentName, entity.Branch);
    }
}
while (token != null);
//Create a retrieve operation that takes a student entity.
TableOperation retrieveOperation = TableOperation.Retrieve < StudentEntity > ("4UB", "4ub12cs505");
TableResult retrievedResult = await studentTable.ExecuteAsync(retrieveOperation);
StudentEntity dataresult = new StudentEntity();
if (retrievedResult.Result != null) {
    dataresult = (StudentEntity) retrievedResult.Result;
} else {
    dataresult = null;
}
//Delete Entity:
TableOperation retrieveOperation1 = TableOperation.Retrieve < StudentEntity > ("4UB", "4ub12cs502");
TableResult retrievedResult1 = await studentTable.ExecuteAsync(retrieveOperation1);
StudentEntity deleteEntity = (StudentEntity) retrievedResult1.Result;
if (deleteEntity != null) {
    TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
    await studentTable.ExecuteAsync(deleteOperation);
}