Tuesday, 19 April 2022

Exception Handling Techniques & Tricks C#

 Exception handling could turn to costly operation in your code if you don’t pay much attention to it.

  1. Use Catch only if needed as it is costly operation, if possible use if instead.
    i.e. Divide by zero exception. why not do a check for zero before divide operation if possible.
try
{
var result = myVal/someParam;
}
catch(DivideByZeroException ex)
{
//do what you want
}
above code can be refactored like below to avoid the costly catch operation.if(someParam != 0)
{
var result = myVal/someParam;
}
else
{
//do what you wanted to do in the catch block
}
try
{
//code
}
catch(Exception)
{
}
return something;
try
{
//code
}
catch(Exception ex)
{
throw ex;
}
return something;
ORtry
{
//code
}
catch(Exception)
{
throw;
}
return something;
//Bad Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw ex;
}
return something;
//better Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw;
}
return something;
//Bad code
try
{
//some code
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
throw ex;
}
try //inner try catch 2
{
//some code
}
catch(Exception ex)
{
throw;
}
}
catch(Exception ex)
{
//code to handle the exception
}
{
method1();
method2();
}
private void method1()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
try
{
method1();
method2();
}
catch(NullException ex)
{
//Handle exception. If needed catch can be classified well based on the exception type behavior of method1 and method 2.
}
catch(Exception ex)
{
//Handle exception
}
private void method1()
{
//code
}
private void method2()
{
//code
}
{
var result = method1();
method2();
}
private int method1()
{
int result;
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
result = 0;
}
return result;
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
//clean your mess-ups, like closing the open connection, fallback mechanism etc.
}
}
//Bad Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
throw;
}
}
//Good Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
return new CustomException("An internal error occured");
}
}
public class CustomException : Exception
{
}

Wednesday, 6 April 2022

Anti-corruption Layer pattern a life saver

 Anti-corruption layer pattern allows communication between one system or subsystem to another system or sub system without corrupting each other system domain or model or the objectives.

When to use this pattern

Exception Handling Techniques & Tricks C#

 Exception handling could turn to costly operation in your code if you don’t pay much attention to it.

try
{
var result = myVal/someParam;
}
catch(DivideByZeroException ex)
{
//do what you want
}
above code can be refactored like below to avoid the costly catch operation.if(someParam != 0)
{
var result = myVal/someParam;
}
else
{
//do what you wanted to do in the catch block
}
try
{
//code
}
catch(Exception)
{
}
return something;
try
{
//code
}
catch(Exception ex)
{
throw ex;
}
return something;
ORtry
{
//code
}
catch(Exception)
{
throw;
}
return something;
//Bad Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw ex;
}
return something;
//better Code
try
{
//code
}
catch(Exception ex)
{
//do something
throw;
}
return something;
//Bad code
try
{
//some code
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
throw ex;
}
try //inner try catch 2
{
//some code
}
catch(Exception ex)
{
throw;
}
}
catch(Exception ex)
{
//code to handle the exception
}
{
method1();
method2();
}
private void method1()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
}
try
{
method1();
method2();
}
catch(NullException ex)
{
//Handle exception. If needed catch can be classified well based on the exception type behavior of method1 and method 2.
}
catch(Exception ex)
{
//Handle exception
}
private void method1()
{
//code
}
private void method2()
{
//code
}
{
var result = method1();
method2();
}
private int method1()
{
int result;
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
result = 0;
}
return result;
}
private void method2()
{
try //inner try catch 1
{
//some code
}
catch(Exception ex)
{
//Handle exception and do not throw back to caller.
}
finally
{
//clean your mess-ups, like closing the open connection, fallback mechanism etc.
}
}
//Bad Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
throw;
}
}
//Good Practice
public Result PerformAction()
{
try
{
}
catch(Exception ex)
{
return new CustomException("An internal error occured");
}
}
public class CustomException : Exception
{
}

Wednesday, 15 December 2021

Tune up your Docker file — Best Practices

 Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

1. Use the appropriate specific version image as base image instead of using generalized base image and start installing required packages.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
From node:17.2.0

2. Always try to use the minimum light weight image as suits your requirements.

docker image inspect mcr.microsoft.com/dotnet/aspnet:5.0

3. Optimize caching image layer

FROM node:17.2.0-alpineWORKDIR /appCOPY package.json package-lock.json .RUN npm install --productionCOPY myapp /appCMD ["node", "src/index.js"] 

4. Avoid files/folder to copy to image not required

**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

5. Use the Multi-stage builds concepts.

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["CoreWebAPIDemo.csproj", "."]
RUN dotnet restore "./CoreWebAPIDemo.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "CoreWebAPIDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CoreWebAPIDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

ENTRYPOINT ["dotnet", "CoreWebAPIDemo.dll"]

6. Use the least privileged user to start the application

USER ContainerUserENTRYPOINT ["dotnet", "CoreWebAPIDemo.dll"]

7. Perform Vulnerability scanning for Docker image