Search This Blog

Monday, June 14, 2021

Q41-Q45

Q41. What are partial View in MVC?
Q42. How to do MVC model binding?
Q43. What is remote validation in MVC?
Q44. What is CSRF or XSRF. Cross site scripting attack? how can we avoid in mvc?
Q45. What is repository pattern in .net?

====================================================================================
Q41. What are partial View in MVC?

Answer:
Partial view is something which renders itself inside another view. 
Partial views can be put in View folder also but it is recommended to place them in "shared" folder. 
We usually have _ in the beginning of name of partial view. 

Advantages:
1. Partial view is a reusable portion of a view which can be used in multiple places across the other views. 
2. It reduces code duplication. 
3. You can break your large markup files into small partial views for better maintenance. 
====================================================================================
Q42. How to do MVC model binding?

Answer:
Model binding is a process where we bind model to controller and view. 
Steps:
1. Create a model class eg StudentModel
2. Create a Controller class and give path till Model classes folder in using reference. 
3. Create a view. We can use scaff folding way to bind the view to a model. 
Check at top of view below model linkage
@model MvcApplicationDemo.Models.Student  


====================================================================================
Q43. What is remote validation in MVC?

Answer:
 Remote validation is the process where we validate specific data posting data to a server without posting the entire form data to the server. Let's see an actual scenario, in one of my projects I had a requirement to validate an email address, whether it already exists in the database. Remote validation was useful for that; without posting all the data we can validate only the email address supplied by the user.

  1. public class UserModel  
  2. {
  3.     [Required]
  4.     public string UserName { getset; }
  5.     [Remote("CheckExistingEmail","Home",ErrorMessage = "Email already exists!")]
  6.     public string UserEmailAddress { getset; }
  7. }

Here UserEmailAddress required remote validation on mvc.

  1. public ActionResult CheckExistingEmail(string UserEmailAddress)
  2. {
  3.     bool ifEmailExist = false;
  4.     try
  5.     {
  6.         ifEmailExist = UserEmailAddress.Equals("mukeshknayak@gmail.com") ? true : false;
  7.         return Json(!ifEmailExist, JsonRequestBehavior.AllowGet);
  8.     }
  9.     catch (Exception ex)
  10.     {
  11.         return Json(false, JsonRequestBehavior.AllowGet);
  12.     }
  13. }


====================================================================================
Q44. What is CSRF or XSRF. Cross site scripting attack? how can we avoid in mvc?

Answer:
We can avoid CSRF by having token based authentication and authorization with every call. 


====================================================================================
Q45. What is repository pattern in .net?

Answer:
Repository pattern: It is a design pattern used in ENTITY FRAMEWORK for database connection. We have two things repository classes and IRepository interface.  like EmployeeRepository.cs and IEmployeeRepository 

Layer1 (MVC contoller) --> 
Layer2 (Repository classes, Domain classes and Database contexts) --> 
Layer3 SQL server database

Two approaches. 
1) Create one repository per model. 
2) Create a Generic repository for all. 

In Repository class we write all CRUD functions with help of dbcontext object. 



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

Q36-Q40(Kafka)

Q36. How to create Topics in Kafka?
Q37. How to enforce schema validation in kafka?

==============================================================================
Q36. How to create Topics in Kafka?

Answer:
follow below steps
1. check if java is installed or not? if not install java
2. install kafka is not already installed. 
3. install zookeeper is not already installed. zoo keeper helps to manage brokers. 
4. Go to zookeeper folder and launch zoopkeeper and new terminal window will be alunched. 
5. Go to kafa folder and launch kafka. again a new terminal window will be launched. 
6.  Open another terminal session and run:
$ bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

Step 4:
# Start the ZooKeeper service
# Note: Soon, ZooKeeper will no longer be required by Apache Kafka.
$ bin/zookeeper-server-start.sh config/zookeeper.properties

Step 5:
# Start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties

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




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





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





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

Q46-Q50

Q46.  If we have 3 types of constructor in code static, default, parametrized. In which order they will be called.  Q47. What is GAC? Q48.  ...