ASP.NETCore interview questions and answers-part2

31)What is Dependency Injection, What is the use of it? 

-Dependency Injection is design pattern help us to develop loosely coupled code.
-Loosely coupled components need not know much details about the components to use it.

32)What is dependency graph ?

-Dependency Graph is the collection of objects that must be created in ordered to request root object.

33) What is the DI Container (or ) IOC container in ASP.NET Core?

-DI container responsible creation of an instance of service.
-This container knows how to create an instance including all its dependencies and passing these to constructor.

34) How many ways inject the dependency?

1)Using constructor 
2)Using Property 

Using constructor is re commanded  , ASP.NETCore by default supporting constrctor level

35) Do we have  default constructor for controller class in MVC?

-Yes ,by default it is not showing but if you want can place it. 
-It is automatically called by runtime when it is executing an action method.

36) How do we inject service into an action method in ASP.NETCore?

-By using [FromServices] attribute inject the service in action method.
-During the model binding mvc middleware can resolve the parameter from the DI container.

public IActionResult SignInStudent([FromServices]ISignServ signser,string username,string pwd)
{
signser.CheckUser(username,pwd);
return view()
}

37) How do we inject service in view with ASP.NETCore?

-By using @inject directive

@inject UIGenerator UG

UIGenerator - service name
UG - Alias name of the service

<h1> UG.Title</h1>

38)How many ways set the lifetime of instance in ASP.NETCore?

- Using 3 ways
1)Transient
2)Scoped
3)Singleton

Transient: For every service request new instance created.

services.AddTransient<DataContext>( );

Scoped: Depends on scope , For every service request new instance created. For different scopes different instances created.
 
services.AddScoped<DataContext>( );

Singleton: For all requests one instance created and used.

services.AddSingleton<DataContext>( );

39)What are the things needs to be take care while working with singleton in ASP.NETCore?

-First thing is to service instance is thread safe because it is sharing between multiple threads.
-When object creation is expensive and object not holding any states.

40)What are the things needs to be take care while configuring  lifetime of a service  in ASP.NETCore?

-A service always use dependent services which are having the lifetime longer or equal to the service 

For Example:

-If service registered as a singleton service dependent service should be only singleton 
-If service registered as a scoped service dependent service should be scoped or singleton 
-If service registered as a transient service dependent service should be anything.



 41) What is the meaning of configuration?


Configuration is a collection of external parameters that controls the application functionality in someway.

42)What is the method using for configuring ASP.NETCore application?

-using CreateDefaultBuilder( args)

43)What is the main components in ASP.NETCore configuration?

-Two main components 
1) ConfigurationBuilder 
2) IConfigurationRoot  

ConfigurationBuilder : Describe the components final configuration of application
IConfigurationRoot : Contain configuration values.

Steps of configuration :

Step 1: Create an instance for ConfigurationBuilder 
Step 2: Add providers to builder so that  provider loads the data from different data sources.
Step 3: Call Build() , Configuration manager loads values from provider stores it IConfigurationRoot , Which represent the configuration object so that can use this in Configure( ) or ConfigurService( ) methods.

44)What are the different configurations providers available in ASP.NETCore?

  • appsettings.json
  • EnvironmentVariables 
  • AzureKeyVault 
  • AzureAppConfiguration
  • CommandLineArguements 
  • DirectoryFiles 
  • In-Memory .NET objects
  • CustomProviders 
-In case of having multiple provides IConfigurationRoot includes values from all providers 
-While configuration providers order is very important , If we have duplicate keys in providers latest one override earlier one.

45)What type of classes are eligible for binding in ASP.NETCore?

-Abstract classes not eligible 
-Class should have public default constructor 
 

46)What is the use of options pattern in ASP.NETCore?

-Using this pattern bind the collection of configuration values to strongly typed object. 
-For this binding process ASP.NETCore uses the IOptions<T> interaface 

Public class StudentController : Controller
{
public StudentController(IOptions<StudentDetails> options)
{
// From the options will get class information 
}
}

47) What is the IOptions<T> in ASP.NETCore?

-IOptions<T> create an instance when it is first needed , reuse the same configuration 

48) What is the IOptionsSnapshot<T> in ASP.NETCore?

-IOptionsSnapshot<T> create an instance when it is needed , It always contains the configuration when it was last created.

49)What is migrations in ASP.NET Core?

-Migrations is mechanism provided by ASP.NETCore.
-Its used for database schema management based on data model changes
-It is provide a record over DB schema changes 
-By applying migrations create a database in case not exist, if exist update the DB with EFCore data model.

50)How many ways will do migrations in ASP.NET?

-By using .Net CLI
-By using vspowershell cmdlets 
-By calling Migrate() method in code on context class.
context.Database.Migrate( )

51) What is filters in ASP.NETCore MVC?
  
-Filter is a piece of code, used for filtering the requests and responses. 
-Filters will execute before request processed or response transferred.
-If something goes wrong  filters stops application execution sending response to the users.
-Some of the filters executing twice  first time for request  and second time for response 

52) How many types of filters available in ASP.NETCore MVC?

in ASP.NETCore 5 types of filters available 

1)Authorization Filters 
2)Resource Filters 
3)Action Filters 
4)Exception Filters 
5)Result Filters 

Resource , Action and Result filters executed twice depends on usage 

53) How can we implement filters in ASP.NETCore MVC?

-Using classes for implementing the filters in MVC
-Always use this filters as C# attributes 
-Needs to decorate this attributes to controllers and action methods

54)How many ways to applying filters in ASP.NETCore MVC?

Applying filters in three levels 
1)Action method level
2)Controller Level
3)Globally at application level

55)Use of applying filters at action method level with an example in ASP.NET Core MVC?

user able to access the application , if tried to access the protected action method then it will ask for login

public class StudentController : Controller
{
[LogDetailsFilter]
public IActionResult Index()
{
return view( );
}
}

56)Use of applying filters at controller level with an example in ASP.NET Core MVC?

user able to access the application , if tried to access the protected controller action method then it will ask for login.
-user able to access the action methods from other controllers. 

[LogDetailsFilter]
public class StudentController : Controller
{
public IActionResult Index()
{
return view( );
}
}

57)Use of applying filters at globally at application level with an example in ASP.NET Core MVC ?

-User needs to login  first then only able to access controller action methods 
-For this , filters directly added to MVC services

public class Startup
{
Public void ConfigureServices(IServiceCollection service)
{
services.AddMvc(options => options.Filters.Add(new LogDetailsFilter()));
}
}

58)What is the filters execution order in ASP.NETCore MVC?

-Global fitters first priority , Controller scoped filters run second priority and action method level filters third priority. 
-If filters applied to both base class and derived class, derived class filters execution first later base class filters.
-Resource , Action and Result filters have both execution and executed methods, Executing method will evaluates before action method execution , Executed method evaluate after action method execution. This methods execution depends on action methods execution i.e either GET or POST etc.
-For all requests execution order is Global->Controller -> Action method level , For response it is vice-versa.

59)Can we change filters execution order in ASP.NETCore MVC?

Yes we can change using Order property of IOrderedFilter 

[FilerName(order=1)]

60)What is the use of ResourceFilters in ASP.NETCore MVC?

-If you want to check (or) perform an action before modal binding you can use ResourceFilters.

For Part-1   click here
For Part-3  click here

Thanks for visiting this blog. How is the content?. Your comment is great gift to my work. Cheers.

No comments:

Post a Comment