Search This Blog

Wednesday, June 16, 2021

Q31-Q35

Q31. Difference between Middleware and filters in .net core mvc?
Q32. Life cycle of dotnet 8 MVC application?
Q33. When to use middleware and when to use filters in .net core?
Q34: What is CLEAN or Onion architecture design pattern?

=======================================================================Q31. Difference between Middleware and filters in .net core mvc?

Answer:
Middlewares
1) They are part of .net core and have access to httpcontext objects. they can be present even is project is not in mvc, api structure. 
2) Good in case you want to do changtes at httpcontet objects like authentication etc. 

Filters
1) They are present only in case of MVC and api. 
2) Good in case you want tocheck something around MVC contexts objects like post action filter

=======================================================================Q32. Life cycle of .net core MVC application?

Answer:
Life cycle of .net core application consists of two parts = 1st life cycle part specific to .net core + 2nd General life cycle part of MVC. 
  1. program.cs
  2. Authentication, Authorization etc middlewares
  3. Routing middleware will create routing map on hosted application server
  4. Controller 
  5. Action
  6. View
=======================================================================Q33. When to use middleware and when to use filters in .net core?

Answer:
Filters are something specific to MVC patterns that can be applied on top of our controllers. 
Filters and Middleware does the same functionality, but main difference lies in their scope. 

Middleware applies globally to all requests. eg : logging request and response, Handling exceptions globally, Managing CORS, Authentication/ Authorization. 

Filters applies specifically to a controller to an action of a controller. its mainly used to do validation of the model. 

=======================================================================






=======================================================================
Q34: What is CLEAN or Onion architecture design pattern?

Answer:
The Clean Architecture, often also referred to as Onion Architecture, is a software design pattern that aims to separate concerns and enforce dependency inversion, making systems more maintainable, testable, and adaptable to changes.
     
Key Concepts:
Clean Architecture is composed of layers arranged like an onion, where:
Core logic is at the center and Outer layers depend on inner layers, but not the other way around.



=======================================================================

Monday, June 14, 2021

Q26-Q30

Q26. What is the use of appsettings.json in .net core?
Q27. What are user secrets in .net core?
Q28. What is reverse proxy server .net core?
Q29. What is the use of launchSettings.json file in .net core?
Q30. What is the difference between  MapWhen and Map in dotnet 8?

==========================================================================================
Q26. What is the use of appsettings.json in .net core?

Answer:
1) Used to store configuration settings such as connection string, application level global variable, logging level configurations etc
2) We can store JWT key in appsettings.json file but it is NOT advisable. it can be saved here for temporary development phase. 
3) appSettings.json file could be environment specific as well. appsettings.<environment>.json

==========================================================================================
Q27. What are user secrets in .net core?

Answer:
If we do not want to share the secrets like API keys, connection string, connection information to cloud service (azure, aws) etc to anyone who can clone your project. 

For this .net core has given an option of user secret file which is outside the project tree structure. you can manage this file by right click the project and choose manage secrets option. 

Secret.json is the file which will be created to store secrets. 




==========================================================================================
Q28. What is reverse proxy server .net core?

Answer:
1) reverse proxy came into picture when we are using kestral server in .net core. 
2) Kestrel can be used by itself or with a reverse proxy server, such as Internet Information Services (IIS), Nginx, or Apache. A reverse proxy server receives HTTP requests from the network and forwards them to Kestrel.



==========================================================================================
Q29. What are the use of launchSettings.json file in .net core?

Answer:
launchSetting.json under property folder is mainly for local development. 
Hosted application url in local, Environment variables are part of launchSettings.json file. 


==========================================================================================
Q30. What is the difference between Map and MapWhen in dotnet 8?

Answer:
Both Map and MapWhen are used to branch middleware pipeline. 

Map: 
This method is used to branch the pipeline based on specific requested path. 

app.Map("/maptest", HandleMapTest);

MapWhen:
Enables branching pipeline. Runs specified middleware if condition is met.


    app.MapWhen(context => {
        return context.Request.Query.ContainsKey("somekey");
    }, HandleBranch);
==========================================================================================

Thursday, April 22, 2021

Q21-Q25

Q21. What is the difference between Include and Join in entity framework?
Q22. What is the strategy to migrate from .net framework to .net core?
Q23. What are the 3 parts of JWT?
Q24. How JWT works? How we get values of JWT token?
Q25. What will be your approach in case you want to call a manual stored procedure from your dot net code with Entity Framework?
------------------------------------------------------------------------------------------------------------------------------------------
Q21. What is the difference between Include and Join in entity framework?

Answer:
At times we get same results on using Include or Join but below are the differences. 

Include
1. It does the eager loading of underlying property for you. 
2. No filter on columns.
3. Include can only be used with actual inter related tables. They must have primary-foreign key relation between them. eg Product and Product details table. 
4. In example below it generates the left join between table product and productdetails behind the scenes. 

var products = context.Products
    .Include(p => p.ProductDetails)
    .ToList();

------------
Join
1. It does not do the eager loading. 
2. We can put filter on number of columns and columns we want. The advantage of Join is, if you dont need additional data you wont get it. 
3. Joins are flexible enough to query around tables which are not directly related like student and Collage over city to check all collages present in student city. 
4. For above product and productDetails below will be the query. Join gives more flexibility but bigger code. 

var productsWithDetails = from product in context.Products
                          join details in context.ProductDetails
                          on product.Id equals details.ProductId
                          select new
                          {
                              Product = product,
                              ProductDetails = details
                          };

var result = productsWithDetails.ToList();

------------------------------------------------------------------------------------------------------------------------------------------
Q22. What is the strategy to migrate from .net framework to .net core?

Answer:
At times we have to migrate our old application from .net framework to .net core for containerization, plateform independent etc. 
Below are the points we have to take care while migrating. 

So migration will happen in 3 phases. 
1. Data/Model layer: Migrate the Entity framework to EF core. One way is to upgrade the asp.net EF to EF 6 which is much similar to EF core. 
2. Business/ Controller layer: .net classes to .net core classes. 
3. UI layer: Asp.net page to Asp.net core pages. 


------------------------------------------------------------------------------------------------------------------------------------------
Q23. What are the 3 parts of JWT?

Answer:
JWT - Json Web Token.
JWT token consists of 3 parts separated by . dots
1. Header
2. Payload. 
3. Signature

Header - contains two part type and algo. 
{
  "alg": "HS256",
  "typ": "JWT"
}

Payload: Usually contains the information about the user like role, name, status etc. These are called claims ie in payload we have claims details like role, name, status etc
{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

Signature: To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)


------------------------------------------------------------------------------------------------------------------------------------------
Q24. How JWT works? How we get values of JWT token?

Answer:
In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned.
Whenever the user wants to access a protected route, it should send the JWT, typically in the Authorization header using the Bearer schema. Therefore the content of the header should look like the following.

Authorization: Bearer <token>




------------------------------------------------------------------------------------------------------------------------------------------
Q25. What will be your approach in case you want to call a manual stored procedure from your dot net code with Entity Framework?

Answer:

1) If the stored procedure is doing an update/insert/delete (not returning entities), I’d use Database.ExecuteSqlCommand in EF6 or Database.ExecuteSqlRaw in EF Core.

2) If the stored procedure returns rows that map to an entity, I’d use SqlQuery in EF6 or FromSqlRaw in EF Core. 

Thursday, February 18, 2021

EF core with .net core 3.1

Steps to use EF Core in project.  (easy)

>> Go to Nuget packages and install "Microsoft.EntityFramework.SqlServer". This will install few more dependent packages with it. 
>> Right click and create a folder named "Database"
>> Add a class and named it AppDbContext.cs (it could be any name)
>> Inherit the above class with "DbContext" class. "DbContext" class is available  when we use 'using Microsoft.EntityFrameworkCore;' on top of a class file. 
>> Create entities (tables) classes in model folder (or in same folder)
>> Create DB set property for above model class in AppDbContext class.

    public class AppDBContext: DbContext
    {
        public AppDBContext(DbContextOptions<AppDBContext> options) : base(options)
        {
        }

        public DbSet<Student> Students { get; set; }
    }

>> Now register the DBContext class in startup.ConfigureService method.

  public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppDBContext>(options =>
               options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        }

Q36-Q40(EF)

Q36.  What is the difference between Include and Join in entity framework? Q37. What will be your approach in case you want to call a manual...