7 October 2023

Factory Pattern in C#

 The Factory Pattern is a creational design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created. Here's an example of the Factory Pattern in C#:

Let's say we have an interface called `IVehicle`:

```csharp

public interface IVehicle

{

    void Drive();

}

We have two classes implementing this interface: `Car` and `Motorcycle`:

```csharp

public class Car : IVehicle

{

    public void Drive()

    {

        Console.WriteLine("Driving the car.");

    }

}


public class Motorcycle : IVehicle

{

    public void Drive()

    {

        Console.WriteLine("Riding the motorcycle.");

    }

}

Now, let's create a `VehicleFactory` class that acts as the factory for creating objects of `IVehicle`:

```csharp

public class VehicleFactory

{

    public IVehicle CreateVehicle(string vehicleType)

    {

        if (vehicleType.Equals("Car", StringComparison.OrdinalIgnoreCase))

        {

            return new Car();

        }

        else if (vehicleType.Equals("Motorcycle", StringComparison.OrdinalIgnoreCase))

        {

            return new Motorcycle();

        }

        else

        {

            throw new ArgumentException("Invalid vehicle type");

        }

    }

}

```

In this example, `VehicleFactory` is the factory class responsible for creating objects of `IVehicle`. It has a method `CreateVehicle` that takes a string representing the type of vehicle and returns an object of `IVehicle`.

Here's how you can use the Factory Pattern:

csharp

class Program

{

    static void Main(string[] args)

    {

        VehicleFactory vehicleFactory = new VehicleFactory();


        Console.WriteLine("Enter vehicle type (Car/Motorcycle): ");

        string vehicleType = Console.ReadLine();

        IVehicle vehicle = vehicleFactory.CreateVehicle(vehicleType);

        vehicle.Drive();

        Console.ReadLine();

    }

}

In this example, the user is prompted to enter a vehicle type (either "Car" or "Motorcycle"). The `VehicleFactory` creates an object of the corresponding type, and the `Drive` method of the created object is called without needing to know the specific class of the object. This demonstrates the use of the Factory Pattern to create objects based on user input.

21 May 2022

Azure Service Fabric

 

·    Azure Service Fabric is Microsoft’s Platform-as-a-Service (PaaS) and is used to build and deploy microservices-based cloud applications.

·       Support Stateless and Stateful service

·       development through deployment, daily monitoring, management, and maintenance, to eventual decommissioning.

·       Support in both Windows and Linux.

31 December 2021

Azure Service Bus

·       Reliable cloud messaging as a service (MaaS) and simple hybrid integration

·       Distribute messages to multiple independent back-end systems

·       Brokering messaging between client and server with asynchronous operations along with structured first-in, first-out (FIFO) messaging and publish/subscribe capabilities.

·       Safely routing and transferring data and control across service and application boundaries

·       While a queue is often used for point-to-point communication, topics are useful in publish/subscribe scenarios.

·      Dead-lettering - Service Bus supports a dead-letter queue (DLQ) to hold messages that cannot be delivered to any receiver, or messages that cannot be processed. You can then remove messages from the DLQ and inspect them.

21 April 2019

Step by step for deploying .Net application in Azure

Step by step for deploying .Net application in Azure

Group INSERT, UPDATE, and DELETE operations from a DataSet or DataTable

Group INSERT, UPDATE, and DELETE operations from a DataSet or DataTable

Batch support in ADO.NET allows a DataAdapter to group INSERT, UPDATE, and DELETE operations from a DataSet or DataTable to the server, instead of sending one operation at a time. The reduction in the number of round trips to the server typically results in significant performance gains. Batch updates are supported for the .NET data providers for SQL Server (System.Data.SqlClient) and Oracle (System.Data.OracleClient).

public static void BatchUpdate(DataTable dataTable,Int32 batchSize) 

    // Assumes GetConnectionString() returns a valid connection string. 
    string connectionString = GetConnectionString(); 
 
    // Connect to the AdventureWorks database. 
    using (SqlConnection connection = new 
      SqlConnection(connectionString)) 
    { 
 
        // Create a SqlDataAdapter. 
        SqlDataAdapter adapter = new SqlDataAdapter(); 
 
        // Set the UPDATE command and parameters. 
        adapter.UpdateCommand = new SqlCommand( 
            "UPDATE Production.ProductCategory SET " 
            + "Name=@Name WHERE ProductCategoryID=@ProdCatID;", 
            connection); 
        adapter.UpdateCommand.Parameters.Add("@Name", 
           SqlDbType.NVarChar, 50, "Name"); 
        adapter.UpdateCommand.Parameters.Add("@ProdCatID", 
           SqlDbType.Int, 4, "ProductCategoryID"); 
         adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.None; 
 
        // Set the INSERT command and parameter. 
        adapter.InsertCommand = new SqlCommand( 
            "INSERT INTO Production.ProductCategory (Name) VALUES (@Name);", 
            connection); 
        adapter.InsertCommand.Parameters.Add("@Name", 
          SqlDbType.NVarChar, 50, "Name"); 
        adapter.InsertCommand.UpdatedRowSource = UpdateRowSource.None; 
 
        // Set the DELETE command and parameter. 
        adapter.DeleteCommand = new SqlCommand( 
            "DELETE FROM Production.ProductCategory " 
            + "WHERE ProductCategoryID=@ProdCatID;", connection); 
        adapter.DeleteCommand.Parameters.Add("@ProdCatID", 
          SqlDbType.Int, 4, "ProductCategoryID"); 
        adapter.DeleteCommand.UpdatedRowSource = UpdateRowSource.None; 
 
        // Set the batch size. 
        adapter.UpdateBatchSize = batchSize; 
 
        // Execute the update. 
        adapter.Update(dataTable); 
    } 
}

Referred from following link
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/performing-batch-operations-using-dataadapters

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...