Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Wednesday, August 5, 2020

Dependency Injection in C#

Dependency Injection (DI) is a software design pattern. It allows us to develop a loosely-coupled code. The intent of Dependency Injection is to make code maintainable. Dependency Injection helps to reduce the tight coupling among software components. Dependency Injection reduces the hard-coded dependencies among your classes by injecting those dependencies at run time instead of design time technically. This article explains how to implement Dependency Injection in C# and .NET code.

We have the following ways to implement Dependency Injection.

1) Constructor Injection
2) Property Injection
3) Method Injection


Sunday, August 2, 2020

Recent questions asking in interview - C# , asp.net , asp.net mvc , asp.net core , sql , entity framework

 Q. Sql Injection 
 Q. Sql cache 
 Q. Exception Filter purpose
Q. MVC Validations - Jquery ka use yaha par 
Q. Await Async




1. What is the difference between SingleOrDefault and FirstOrDefault?

LINQ provides element operators that return a single element or a specific element from a collection. The elements operators are Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault.

Single

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.
SingleOrDefault

It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.

First

It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.

FirstOrDefault

It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.

When to use Single, SingleOrDefault, First and FirstOrDefault

You should take care of following points while choosing Single, SingleOrDefault, First and 
FirstOrDefault:
When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
When you want a default value is returned if the result set contains no record, use SingleOrDefault.
When you always want one record no matter what the result set contains, use First or FirstOrDefault.
When you want a default value if the result set contains no record, use FirstOrDefault.

Perfomance of SingleOrDefault and FirstOrDefault

FirstOrDefault usually performs faster as compared to SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterates the whole collection to find one single match.

Q.2 AllowAnonymous Attribut and Authorize Attribute.?

One of the new features in ASP.NET MVC 4 is the AllowAnonymous Attribute that helps you secure an entire ASP.NET MVC 4 Website or Controller while providing a convenient means of allowing anonymous users access to certain controller actions, like the login and register Actions.


AllowAnonymous Attribute in ASP.NET MVC 4

As mentioned, if you create a new ASP.NET MVC 4 Internet Project in Visual Studio 2010 or Visual Studio 11 and view the AccountController you will notice the generous use of the AllowAnonymous Attribute on various login and register controller actions. Here are a few of those controller actions.

[Authorize] 
public class AccountController : Controller 
{ 
 [AllowAnonymous] 
 public ActionResult Login() 
{ 
 // ... 
 } 
 [AllowAnonymous]
 [HttpPost] 
 public JsonResult JsonLogin(LoginModel model, string returnUrl) 
{ 
 // ...
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Login(LoginModel model, string returnUrl) 
{ 
 // ... 
 }
 [AllowAnonymous] public ActionResult Register() 
{ 
 // ... 
 } 
 [AllowAnonymous] 
 [HttpPost] 
 public ActionResult JsonRegister(RegisterModel model)
 {
 // ... 
 }
 [AllowAnonymous]
 [HttpPost] 
 public ActionResult Register(RegisterModel model) 
{ 
 // ... 
 } 
}

The idea is pretty simple. The Authorize Attribute on the AccountController in this ASP.NET MVC 4 Application denies anonymous access to every controller action. However, we need to allow anonymous access to the login and register controller actions so we decorate them with the AllowAnonymous Attribute which negates the Authorize Attribute and allows anonymous access.

Q.3 What is mvc Filters and custom filters . ?


ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.

Q.4 What is Dependency injection with examples . ?

In software engineering, dependency injection is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical "using" relationship the receiving object is called a client and the passed object is called a service.


Wednesday, July 29, 2020

Microsoft SEAL is an easy-to-use and powerful open source homomorphic encryption library,



Microsoft SEAL is an easy-to-use and powerful open-source homomorphic encryption library, developed by researchers in the Cryptography and Privacy Research Group at Microsoft Research. Microsoft SEAL is licensed under the MIT license.
How to use this Microsoft SEAL open source  in .net

Examples : -

Rewrite whole Web Site System and Sub System with data migration to new system.

Can you please share me your skype id or whats up number for better communications  my skype id is - jitendra.tech  whats up - +919617741414...