C# Flashcards

1
Q

Inversion of Control (IoC) -Principle

A

Inversion of Control (IoC) is a design principle (although, some people refer to it as a pattern). As the name suggests, it is used to invert different kinds of controls in object-oriented design to achieve loose coupling. Here, controls refer to any additional responsibilities a class has, other than its main responsibility. This include control over the flow of an application, and control over the flow of an object creation or dependent object creation and binding.

IoC is all about inverting the control. To explain this in layman’s terms, suppose you drive a car to your work place. This means you control the car. The IoC principle suggests to invert the control, meaning that instead of driving the car yourself, you hire a cab, where another person will drive the car. Thus, this is called inversion of the control - from you to the cab driver. You don’t have to drive a car yourself and you can let the driver do the driving so that you can focus on your main work.

The IoC principle helps in designing loosely coupled classes which make them testable, maintainable and extensible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Dependency Inversion Principle -DIP

A

DIP is one of the SOLID object-oriented principle invented by Robert Martin (a.k.a. Uncle Bob)

1: High-level modules should not depend on low-level modules. Both should depend on the abstraction.
2: Abstractions should not depend on details. Details should depend on abstractions.

A high-level module is a module which depends on other modules. In our example, CustomerBusinessLogic depends on the DataAccess class, so CustomerBusinessLogic is a high-level module and DataAccess is a low-level module. So, as per the first rule of DIP, CustomerBusinessLogic should not depend on the concrete DataAccess class, instead both classes should depend on abstraction.

In English, abstraction means something which is non-concrete. In programming terms, the above CustomerBusinessLogic and DataAccess are concrete classes, meaning we can create objects of them. So, abstraction in programming means to create an interface or an abstract class which is non-concrete. This means we cannot create an object of an interface or an abstract class. As per DIP, CustomerBusinessLogic (high-level module) should not depend on the concrete DataAccess class (low-level module). Both classes should depend on abstractions, meaning both classes should depend on an interface or an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Dependency Injection (DI)

A

Dependency Injection (DI) is a design pattern which implements the IoC principle to invert the creation of dependent objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Inversion of Control (IoC) -Principle-Control Over the Flow of a Program

A

In the console app, the Main() function of the program class controls the flow of a program. It takes the user’s input for the first name and last name. It saves the data, and continues or exits the console, depending upon the user’s input. So here, the flow is controlled through the Main() function.

IoC can be applied to the above program by creating a GUI-based application such as the following windows-based application, wherein the framework will handle the flow of a program by using events.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Inversion of Control (IoC) -Principle-Control Over the Dependent Object Creation

A
UI-->Service Layer-->Business Logic -->DataAccess
In the typical n-tier architecture, the User Interface (UI) uses Service layer to retrieve or save data. The Service layer uses the BusinessLogic class to apply business rules on the data. The BusinessLogic class depends on the DataAccess class which retrieves or saves the data to the underlying database. This is simple n-tier architecture design. Let's focus on the BusinessLogic and DataAccess classes to understand IoC.

The following is an example of BusinessLogic and DataAccess classes for a customer.

public class CustomerBusinessLogic
{
    DataAccess _dataAccess;
    public CustomerBusinessLogic()
    {
        _dataAccess = new DataAccess();
    }
public string GetCustomerName(int id)
{
    return _dataAccess.GetCustomerName(id);
} }
public class DataAccess
{
    public DataAccess()
    {
    }
public string GetCustomerName(int id) {
    return "Dummy Customer Name"; // get it from DB in real app
} }

As you can see in the above example, the CustomerBusinessLogic class depends on the DataAccess class. It creates an object of the DataAccess class to get the customer data.

Now, let’s understand what’s wrong with the above classes.

In the above example, CustomerBusinessLogic and DataAccess are tightly coupled classes because the CustomerBusinessLogic class includes the reference of the concrete DataAccess class. It also creates an object of DataAccess class and manages the lifetime of the object.

Problems in the above example classes:

1: CustomerBusinessLogic and DataAccess classes are tightly coupled classes. So, changes in the DataAccess class will lead to changes in the CustomerBusinessLogic class. For example, if we add, remove or rename any method in the DataAccess class then we need to change the CustomerBusinessLogic class accordingly.
2: Suppose the customer data comes from different databases or web services and, in the future, we may need to create different classes, so this will lead to changes in the CustomerBusinessLogic class.
3: The CustomerBusinessLogic class creates an object of the DataAccess class using the new keyword. There may be multiple classes which use the DataAccess class and create its objects. So, if you change the name of the class, then you need to find all the places in your source code where you created objects of DataAccess and make the changes throughout the code. This is repetitive code for creating objects of the same class and maintaining their dependencies.
4: Because the CustomerBusinessLogic class creates an object of the concrete DataAccess class, it cannot be tested independently (TDD). The DataAccess class cannot be replaced with a mock class.

To solve all of the above problems and get a loosely coupled design, we can use the IoC and DIP principles together. Remember, IoC is a principle, not a pattern. It just gives high-level design guidelines but does not give implementation details. You are free to implement the IoC principle the way you want.

The following pattern (but not limited) implements the IoC principle.
IoC can be done with Service Locator, Factory,Template Method,Dependency injection,Abstract Factory,
Pattern for IOC
Let’s use the Factory pattern to implement IoC in the above example, as the first step towards attaining loosely coupled classes.

First, create a simple Factory class which returns an object of the DataAccess class as shown below.

Example: DataAccess Factory
public class DataAccessFactory
{
    public static DataAccess GetDataAccessObj() 
    {
        return new DataAccess();
    }
}
Now, use this DataAccessFactory class in the CustomerBusinessLogic class to get an object of DataAccess class.
Example: Use Factory Class to Retrieve Object
public class CustomerBusinessLogic
{
    public CustomerBusinessLogic()
    {
    }
public string GetCustomerName(int id)
{
    DataAccess _dataAccess =  DataAccessFactory.GetDataAccessObj();
        return _dataAccess.GetCustomerName(id);
    }
}
As you can see, the CustomerBusinessLogic class uses the DataAccessFactory.GetCustomerDataAccessObj() method to get an object of the DataAccess class instead of creating it using the new keyword. Thus, we have inverted the control of creating an object of a dependent class from the CustomerBusinessLogic class to the DataAccessFactory class.

This is a simple implementation of IoC and the first step towards achieving fully loose coupled design. As mentioned in the previous chapter, we will not achieve complete loosely coupled classes by only using IoC. Along with IoC, we also need to use DIP, Strategy pattern, and DI (Dependency Injection).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

loosely coupled classes

A

In an object-oriented design, classes should be designed in a loosely coupled way. Loosely coupled means changes in one class should not force other classes to change, so the whole application can become maintainable and extensible.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Static Methods

A
the static methods in C# do not need an object to call them
we could call them in another class by ClassName.StaticMethodName();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Abstraction

A

In English, abstraction means something which is non-concrete. In programming terms, the above CustomerBusinessLogic and DataAccess are concrete classes, meaning we can create objects of them. So, abstraction in programming means to create an interface or an abstract class which is non-concrete. This means we cannot create an object of an interface or an abstract class. As per DIP, CustomerBusinessLogic (high-level module) should not depend on the concrete DataAccess class (low-level module). Both classes should depend on abstractions, meaning both classes should depend on an interface or an abstract class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dependency Inversion Principle -DIP - Example

A
public interface ICustomerDataAccess
{
    string GetCustomerName(int id);
}
public class CustomerDataAccess: ICustomerDataAccess
{
    public CustomerDataAccess() {
    }
public string GetCustomerName(int id) {
    return "Dummy Customer Name";        
} }
public class DataAccessFactory
{
    public static ICustomerDataAccess GetCustomerDataAccessObj() 
    {
        return new CustomerDataAccess();
    }
}
public class CustomerBusinessLogic
{
    ICustomerDataAccess _custDataAccess;
    public CustomerBusinessLogic()
    {
        _custDataAccess = DataAccessFactory.GetCustomerDataAccessObj();
    }
public string GetCustomerName(int id)
{
    return _custDataAccess.GetCustomerName(id);
} }

The advantages of implementing DIP in the above example is that the CustomerBusinessLogic and CustomerDataAccess classes are loosely coupled classes because CustomerBusinessLogic does not depend on the concrete DataAccess class, instead it includes a reference of the ICustomerDataAccess interface. So now, we can easily use another class which implements ICustomerDataAccess with a different implementation.

Still, we have not achieved fully loosely coupled classes because the CustomerBusinessLogic class includes a factory class to get the reference of ICustomerDataAccess. This is where the Dependency Injection pattern helps us.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Dependency Injection

A

Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

The Dependency Injection pattern involves 3 types of classes.

Client Class: The client class (dependent class) is a class which depends on the service class
Service Class: The service class (dependency) is a class that provides service to the client class.
Injector Class: The injector class injects the service class object into the client class.

As you can see, the injector class creates an object of the service class, and injects that object to a client object. In this way, the DI pattern separates the responsibility of creating an object of the service class out of the client class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Constructor Dependency Injection example

A
Example: Constructor Injection
public class CustomerBusinessLogic
{
    ICustomerDataAccess _dataAccess;
    public CustomerBusinessLogic(ICustomerDataAccess custDataAccess)
    {
        _dataAccess = custDataAccess;
    }
    public CustomerBusinessLogic()
    {
        _dataAccess = new CustomerDataAccess();
    }
public string ProcessCustomerData(int id)
{
    return _dataAccess.GetCustomerName(id);
} }
public interface ICustomerDataAccess
{
    string GetCustomerData(int id);
}
public class CustomerDataAccess: ICustomerDataAccess
{
    public CustomerDataAccess()
    {
    }
    public string GetCustomerName(int id) 
    {
        //get the customer name from the db in real application        
        return "Dummy Customer Name"; 
    }
}
In the above example, CustomerBusinessLogic includes the constructor with one parameter of type ICustomerDataAccess. Now, the calling class must inject an object of ICustomerDataAccess.

Example: Inject Dependency
public class CustomerService
{
CustomerBusinessLogic _customerBL;

    public CustomerService()
    {
        _customerBL = new CustomerBusinessLogic(new CustomerDataAccess());
    }
    public string GetCustomerName(int id) {
        return _customerBL.GetCustomerName(id);
    }
}
As you can see in the above example, the CustomerService class creates and injects the CustomerDataAccess object into the CustomerBusinessLogic class. Thus, the CustomerBusinessLogic class doesn't need to create an object of CustomerDataAccess using the new keyword or using factory class. The calling class (CustomerService) creates and sets the appropriate DataAccess class to the CustomerBusinessLogic class. In this way, the CustomerBusinessLogic and CustomerDataAccess classes become "more" loosely coupled classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Defining the services for Dependency injection

A

The Startup.ConfigureServices method is responsible for defining the services that the app uses, including platform features, such as Entity Framework Core and ASP.NET Core MVC. Initially, the IServiceCollection provided to ConfigureServices has services defined by the framework depending on how the host was configured. It’s not uncommon for an app based on an ASP.NET Core template to have hundreds of services registered by the framework. A small sample of framework-registered services is listed in the following table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Extension methods

A

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Constructor Dependency injection into controllers in ASP.NET Core

A

Constructor Injection
Services are added as a constructor parameter, and the runtime resolves the service from the service container. Services are typically defined using interfaces. For example, consider an app that requires the current time. The following interface exposes the IDateTime service:

C#

Copy
public interface IDateTime
{
    DateTime Now { get; }
}
The following code implements the IDateTime interface:

C#

Copy
public class SystemDateTime : IDateTime
{
    public DateTime Now
    {
        get { return DateTime.Now; }
    }
}
Add the service to the service container:

C#

Copy
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton();

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); } For more information on AddSingleton, see DI service lifetimes.

The following code displays a greeting to the user based on the time of day:

C#

Copy
public class HomeController : Controller
{
    private readonly IDateTime _dateTime;
    public HomeController(IDateTime dateTime)
    {
        _dateTime = dateTime;
    }
    public IActionResult Index()
    {
        var serverTime = _dateTime.Now;
        if (serverTime.Hour < 12)
        {
            ViewData["Message"] = "It's morning here - Good Morning!";
        }
        else if (serverTime.Hour < 17)
        {
            ViewData["Message"] = "It's afternoon here - Good Afternoon!";
        }
        else
        {
            ViewData["Message"] = "It's evening here - Good Evening!";
        }
        return View();
    }
Run the app and a message is displayed based on the time.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Action injection with FromServices with out using Constructor Dependency Injection

A

Action injection with FromServices
The FromServicesAttribute enables injecting a service directly into an action method without using constructor injection:

C#

Copy
public IActionResult About([FromServices] IDateTime dateTime)
{
ViewData[“Message”] = $”Current server time: {dateTime.Now}”;

    return View();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Multiple Startup classes for different environments

A

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/startup?view=aspnetcore-3.1

When the app defines separate Startup classes for different environments (for example, StartupDevelopment), the appropriate Startup class is selected at runtime. The class whose name suffix matches the current environment is prioritized. If the app is run in the Development environment and includes both a Startup class and a StartupDevelopment class, the StartupDevelopment class is used. For more information, see Use multiple environments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

App startup in ASP.NET Core

A

The Startup class configures services and the app’s request pipeline.

The Startup class
ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class:

Optionally includes a ConfigureServices method to configure the app’s services. A service is a reusable component that provides app functionality. Services are registered in ConfigureServices and consumed across the app via dependency injection (DI) or ApplicationServices.
Includes a Configure method to create the app’s request processing pipeline.
ConfigureServices and Configure are called by the ASP.NET Core runtime when the app starts:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

IHostBuilder in program.cs

A

The Startup class is specified when the app’s host is built. The Startup class is typically specified by calling the WebHostBuilderExtensions.UseStartup method on the host builder:

C#

Copy
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup();
        }); } The host provides services that are available to the Startup class constructor. The app adds additional services via ConfigureServices. Both the host and app services are available in Configure and throughout the app.

Only the following service types can be injected into the Startup constructor when using the Generic Host (IHostBuilder):

IWebHostEnvironment
IHostEnvironment
IConfiguration
C#

Copy
public class Startup
{
    private readonly IWebHostEnvironment _env;
    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        Configuration = configuration;
        _env = env;
    }
public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        if (_env.IsDevelopment())
        {
        }
        else
        {
        }
    }
}
Most services are not available until the Configure method is called.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

ConfigureServices method in startup

A

The ConfigureServices method is:

Optional.
Called by the host before the Configure method to configure the app’s services.
Where configuration options are set by convention.
The host may configure some services before Startup methods are called. For more information, see The host.

For features that require substantial setup, there are Add{Service} extension methods on IServiceCollection. For example, AddDbContext, AddDefaultIdentity, AddEntityFrameworkStores, and AddRazorPages:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/?view=aspnetcore-3.1&tabs=windows#host

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{

    services.AddDbContext(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity(
        options => options.SignIn.RequireConfirmedAccount = true)
        .AddEntityFrameworkStores();

    services.AddRazorPages();
} Adding services to the service container makes them available within the app and in the Configure method. The services are resolved via dependency injection or from ApplicationServices.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

ASP.NET Core Middleware

A

Middleware is software that’s assembled into an app pipeline to handle requests and responses. Each component:

Chooses whether to pass the request to the next component in the pipeline.
Can perform work before and after the next component in the pipeline.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

The Configure method in startup class

A

The Configure method is used to specify how the app responds to HTTP requests. The request pipeline is configured by adding middleware components to an IApplicationBuilder instance. IApplicationBuilder is available to the Configure method, but it isn’t registered in the service container. Hosting creates an IApplicationBuilder and passes it directly to Configure.

The ASP.NET Core templates configure the pipeline with support for:

Developer Exception Page
Exception handler
HTTP Strict Transport Security (HSTS)
HTTPS redirection
Static files
ASP.NET Core MVC and Razor Pages
C#
Copy
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app. UseHttpsRedirection();
    app. UseStaticFiles();

    app. UseRouting();
    app. UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });
    }
}
The preceding sample is for Razor Pages; the MVC version is similar.

Each Use extension method adds one or more middleware components to the request pipeline. For instance, UseStaticFiles configures middleware to serve static files.

Each middleware component in the request pipeline is responsible for invoking the next component in the pipeline or short-circuiting the chain, if appropriate.

Additional services, such as IWebHostEnvironment, ILoggerFactory, or anything defined in ConfigureServices, can be specified in the Configure method signature. These services are injected if they’re available.

For more information on how to use IApplicationBuilder and the order of middleware processing, see ASP.NET Core Middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Configure services without Startup

A

To configure services and the request processing pipeline without using a Startup class, call ConfigureServices and Configure convenience methods on the host builder. Multiple calls to ConfigureServices append to one another. If multiple Configure method calls exist, the last Configure call is used.

C#

Copy
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.ConfigureServices(services =>
            {
                services.AddControllersWithViews();
            })
            .Configure(app =>
            {
                var loggerFactory = app.ApplicationServices
                    .GetRequiredService();
                var logger = loggerFactory.CreateLogger();
                var env = app.ApplicationServices.GetRequiredService();
                var config = app.ApplicationServices.GetRequiredService();

                logger.LogInformation("Logged in Configure");

                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    app.UseHsts();
                }
                    var configValue = config["MyConfigKey"];
                });
            });
        });
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

launchSettings.json

A

he environment for local machine development can be set in the Properties\launchSettings.json file of the project. Environment values set in launchSettings.json override values set in the system environment.

The following JSON shows three profiles from a launchSettings.json file:

JSON

Copy
{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:54339/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    },
    "EnvironmentsSample": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:54340/"
    },
    "Kestrel Staging": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_My_Environment": "1",
        "ASPNETCORE_DETAILEDERRORS": "1",
        "ASPNETCORE_ENVIRONMENT": "Staging"
      },
      "applicationUrl": "http://localhost:51997/"
    }
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Environment in asp.net core

A

ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

Environments
ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at app startup and stores the value in IWebHostEnvironment.EnvironmentName. ASPNETCORE_ENVIRONMENT can be set to any value, but three values are provided by the framework:

Development
Staging
Production (default)
C#

Copy
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
{
    app.UseExceptionHandler("/Error");
}

app.UseStaticFiles();
app.UseMvc(); } The preceding code:

Calls UseDeveloperExceptionPage when ASPNETCORE_ENVIRONMENT is set to Development.

Calls UseExceptionHandler when the value of ASPNETCORE_ENVIRONMENT is set one of the following:

Staging
Production
Staging_2
The Environment Tag Helper uses the value of IHostingEnvironment.EnvironmentName to include or exclude markup in the element:

CSHTML

Copy

<div><environment include="Development"></div>

<div><environment exclude="Development"></div>

<div>
    <environment include="Staging,Development,Staging_2">
</div>

On Windows and macOS, environment variables and values aren’t case sensitive. Linux environment variables and values are case sensitive by default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

setting environmental variable in launchsettings.json

A

The applicationUrl property in launchSettings.json can specify a list of server URLs. Use a semicolon between the URLs in the list:

JSON

Copy
“EnvironmentsSample”: {
“commandName”: “Project”,
“launchBrowser”: true,
“applicationUrl”: “https://localhost:5001;http://localhost:5000”,
“environmentVariables”: {
“ASPNETCORE_ENVIRONMENT”: “Development”
}
}
When the app is launched with dotnet run, the first profile with “commandName”: “Project” is used. The value of commandName specifies the web server to launch. commandName can be any one of the following:

IISExpress
IIS
Project (which launches Kestrel)
When an app is launched with dotnet run:

launchSettings.json is read if available. environmentVariables settings in launchSettings.json override environment variables.
The hosting environment is displayed.
The following output shows an app started with dotnet run:
PS C:\Websites\EnvironmentsSample> dotnet run
Using launch settings from C:\Websites\EnvironmentsSample\Properties\launchSettings.json…
Hosting environment: Staging
Content root path: C:\Websites\EnvironmentsSample
Now listening on: http://localhost:54340
Application started. Press Ctrl+C to shut down.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Visual studio to set environmental variables

A

Project –> Properties –>Profiles –>select profile and update the variables
These variables override the system environmental variables

Changes made to project profiles may not take effect until the web server is restarted. Kestrel must be restarted before it can detect changes made to its environment.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Secrets in asp.net core

A

launchSettings.json shouldn’t store secrets. The Secret Manager tool can be used to store secrets for local development.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Visual studio code Environmental variables

A

When using Visual Studio Code, environment variables can be set in the .vscode/launch.json file. The following example sets the environment to Development:

JSON

Copy
{
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (web)",
        ... additional VS Code configuration settings ...

        "env": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
] } A .vscode/launch.json file in the project isn't read when starting the app with dotnet run in the same way as Properties/launchSettings.json. When launching an app in development that doesn't have a launchSettings.json file, either set the environment with an environment variable or a command-line argument to the dotnet run command.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Advantages of setting production environment variables

A

Production
The production environment should be configured to maximize security, performance, and app robustness. Some common settings that differ from development include:

Caching.
Client-side resources are bundled, minified, and potentially served from a CDN.
Diagnostic error pages disabled.
Friendly error pages enabled.
Production logging and monitoring enabled. For example, Application Insights.
Set the environment
It’s often useful to set a specific environment for testing with an environment variable or platform setting. If the environment isn’t set, it defaults to Production, which disables most debugging features. The method for setting the environment depends on the operating system.

When the host is built, the last environment setting read by the app determines the app’s environment. The app’s environment can’t be changed while the app is running.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Set Environmental variable for current session

A

To set the ASPNETCORE_ENVIRONMENT for the current session when the app is started using dotnet run, the following commands are used:

Command prompt

console

Copy
set ASPNETCORE_ENVIRONMENT=Development

PowerShell

PowerShell

Copy
$Env:ASPNETCORE_ENVIRONMENT = “Development”

These commands only take effect for the current window. When the window is closed, the ASPNETCORE_ENVIRONMENT setting reverts to the default setting or machine value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Set Environmental variable globally

A

To set the value globally in Windows, use either of the following approaches:

Open the Control Panel > System > Advanced system settings and add or edit the ASPNETCORE_ENVIRONMENT value:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Set Environmental variable globally

A

To set the value globally in Windows, use either of the following approaches:

Open the Control Panel > System > Advanced system settings and add or edit the ASPNETCORE_ENVIRONMENT value:

Open an administrative command prompt and use the setx command or open an administrative PowerShell command prompt and use [Environment]::SetEnvironmentVariable:

Command prompt

console

Copy
setx ASPNETCORE_ENVIRONMENT Development /M
The /M switch indicates to set the environment variable at the system level. If the /M switch isn’t used, the environment variable is set for the user account.

The Machine option value indicates to set the environment variable at the system level. If the option value is changed to User, the environment variable is set for the user account.

When the ASPNETCORE_ENVIRONMENT environment variable is set globally, it takes effect for dotnet run in any command window opened after the value is set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Environment variable in web.config

A

To set the ASPNETCORE_ENVIRONMENT environment variable with web.config, see the Setting environment variables section of ASP.NET Core Module.

Project file or publish profile

For Windows IIS deployments: Include the property in the publish profile (.pubxml) or project file. This approach sets the environment in web.config when the project is published:

XML

Copy

Development

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Environment variable per IIS Application Pool

A

Per IIS Application Pool

To set the ASPNETCORE_ENVIRONMENT environment variable for an app running in an isolated Application Pool (supported on IIS 10.0 or later), see the AppCmd.exe command section of the Environment Variables topic. When the ASPNETCORE_ENVIRONMENT environment variable is set for an app pool, its value overrides a setting at the system level.

Important

When hosting an app in IIS and adding or changing the ASPNETCORE_ENVIRONMENT environment variable, use any one of the following approaches to have the new value picked up by apps:

Execute net stop was /y followed by net start w3svc from a command prompt.
Restart the server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Configuration by Environment; appsettings.{Environment}.json

A

Set the environment in code
Call UseEnvironment when building the host. See .NET Generic Host.

Configuration by environment
To load configuration by environment, we recommend:

appsettings files (appsettings.{Environment}.json). See Configuration in ASP.NET Core.
Environment variables (set on each system where the app is hosted). See .NET Generic Host and Safe storage of app secrets in development in ASP.NET Core.
Secret Manager (in the Development environment only). See Safe storage of app secrets in development in ASP.NET Core.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Environment-based Startup class for fewer changes

A

Inject IWebHostEnvironment into Startup.Configure
Inject IWebHostEnvironment into Startup.Configure. This approach is useful when the app only requires adjusting Startup.Configure for a few environments with minimal code differences per environment.

C#

Copy
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        // Development environment code
    }
    else
    {
        // Code for all other environments
    }
}
Inject IWebHostEnvironment into the Startup class
Inject IWebHostEnvironment into the Startup constructor. This approach is useful when the app requires configuring Startup for only a few environments with minimal code differences per environment.

In the following example:

The environment is held in the _env field.
_env is used in ConfigureServices and Configure to apply startup configuration based on the app’s environment.
C#

Copy
public class Startup
{
    private readonly IWebHostEnvironment _env;
    public Startup(IWebHostEnvironment env)
    {
        _env = env;
    }
    public void ConfigureServices(IServiceCollection services)
    {
        if (_env.IsDevelopment())
        {
            // Development environment code
        }
        else if (_env.IsStaging())
        {
            // Staging environment code
        }
        else
        {
            // Code for all other environments
        }
    }
    public void Configure(IApplicationBuilder app)
    {
        if (_env.IsDevelopment())
        {
            // Development environment code
        }
        else
        {
            // Code for all other environments
        }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Environment-based Startup class for many changes

A

To implement environment-based Startup classes, create a Startup{EnvironmentName} class for each environment in use and a fallback Startup class:

C#

Copy
// Startup class to use in the Development environment
public class StartupDevelopment
{
    public void ConfigureServices(IServiceCollection services)
    {
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
} }
// Startup class to use in the Production environment
public class StartupProduction
{
    public void ConfigureServices(IServiceCollection services)
    {
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
} }
// Fallback Startup class
// Selected if the environment doesn't match a Startup{EnvironmentName} class
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
    }
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
} } Use the UseStartup(IWebHostBuilder, String) overload that accepts an assembly name:

C#

Copy
public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName;
return WebHost.CreateDefaultBuilder(args)
    .UseStartup(assemblyName); }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Environment-based Startup methods for many changes

A

Startup method conventions
Configure and ConfigureServices support environment-specific versions of the form Configure and ConfigureServices. This approach is useful when the app requires configuring startup for several environments with many code differences per environment.

C#

Copy
public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    StartupConfigureServices(services);
}

public void ConfigureStagingServices(IServiceCollection services)
{
    StartupConfigureServices(services);
}

private void StartupConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
    {
        app.UseExceptionHandler("/Error");
    }

    app.UseStaticFiles();
    app.UseMvc();
}
    public void ConfigureStaging(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (!env.IsStaging())
        {
            throw new Exception("Not staging.");
        }
    app.UseExceptionHandler("/Error");
    app.UseStaticFiles();
    app.UseMvc();
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Startup class conventions

A

When an ASP.NET Core app starts, the Startup class bootstraps the app. The app can define separate Startup classes for different environments (for example, StartupDevelopment). The appropriate Startup class is selected at runtime. The class whose name suffix matches the current environment is prioritized. If a matching Startup{EnvironmentName} class isn’t found, the Startup class is used. This approach is useful when the app requires configuring startup for several environments with many code differences per environment.

To implement environment-based Startup classes, create a Startup{EnvironmentName} class for each environment in use and a fallback Startup class:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Safe storage of app secrets in development in ASP.NET Core

A

This document explains techniques for storing and retrieving sensitive data during development of an ASP.NET Core app on a development machine. Never store passwords or other sensitive data in source code. Production secrets shouldn’t be used for development or test. Secrets shouldn’t be deployed with the app. Instead, secrets should be made available in the production environment through a controlled means like environment variables, Azure Key Vault, etc. You can store and protect Azure test and production secrets with the Azure Key Vault configuration provider.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Environment variables for secrets- not recommended

A

Environment variables are generally stored in plain, unencrypted text. If the machine or process is compromised, environment variables can be accessed by untrusted parties. Additional measures to prevent disclosure of user secrets may be required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Secret Manager tool

A

The Secret Manager tool abstracts away the implementation details, such as where and how the values are stored. You can use the tool without knowing these implementation details. The values are stored in a JSON configuration file in a system-protected user profile folder on the local machine:

Windows
Linux / macOS
File system path:

%APPDATA%\Microsoft\UserSecrets\secrets.json

In the preceding file paths, replace with the UserSecretsId value specified in the .csproj file.

Don’t write code that depends on the location or format of data saved with the Secret Manager tool. These implementation details may change. For example, the secret values aren’t encrypted, but could be in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Access a Secret

A

Access a secret
The ASP.NET Core Configuration API provides access to Secret Manager secrets.

In ASP.NET Core 2.0 or later, the user secrets configuration source is automatically added in development mode when the project calls CreateDefaultBuilder to initialize a new instance of the host with preconfigured defaults. CreateDefaultBuilder calls AddUserSecrets when the EnvironmentName is Development:

C#

Copy
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup();
});
When CreateDefaultBuilder isn’t called, add the user secrets configuration source explicitly by calling AddUserSecrets in the Startup constructor. Call AddUserSecrets only when the app runs in the Development environment, as shown in the following example:

C#

Copy
public Startup(IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,
optional: false,
reloadOnChange: true)
.AddEnvironmentVariables();

if (env.IsDevelopment())
{
    builder.AddUserSecrets();
}

Configuration = builder.Build(); } User secrets can be retrieved via the Configuration API:

C#

Copy
public class Startup
{
    private string _moviesApiKey = null;
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
    _moviesApiKey = Configuration["Movies:ServiceApiKey"];
}
    public void Configure(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            var result = string.IsNullOrEmpty(_moviesApiKey) ? "Null" : "Not Null";
            await context.Response.WriteAsync($"Secret is {result}");
        });
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

POCO

A

POCO (Plain old CLR Objects)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

String replacement with secrets

A

Storing passwords in plain text is insecure. For example, a database connection string stored in appsettings.json may include a password for the specified user:

JSON

Copy
{
  "ConnectionStrings": {
    "Movies": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;User Id=johndoe;Password=pass123;MultipleActiveResultSets=true"
  }
}

A more secure approach is to store the password as a secret. For example:

.NET Core CLI

Copy
dotnet user-secrets set “DbPassword” “pass123”
Remove the Password key-value pair from the connection string in appsettings.json. For example:

JSON

Copy
{
  "ConnectionStrings": {
    "Movies": "Server=(localdb)\\mssqllocaldb;Database=Movie-1;User Id=johndoe;MultipleActiveResultSets=true"
  }
}
------
The secret's value can be set on a SqlConnectionStringBuilder object's Password property to complete the connection string:

C#

Copy
public class Startup
{
    private string _connection = null;
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = new SqlConnectionStringBuilder(
            Configuration.GetConnectionString("Movies"));
        builder.Password = Configuration["DbPassword"];
        _connection = builder.ConnectionString;
    }
    public void Configure(IApplicationBuilder app)
    {
        app.Run(async (context) =>
        {
            await context.Response.WriteAsync($"DB Connection: {_connection}");
        });
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Task asynchronous programming model (TAP)

A

using System;
using System.Threading.Tasks;
using System.Windows;

// Add a using directive and a reference for System.Net.Http;
using System.Net.Http;
namespace AsyncFirstExample
{
    public partial class MainWindow : Window
    {
        // Mark the event handler with async so you can use await in it.
        private async void StartButton_Click(object sender, RoutedEventArgs e)
        {
            // Call and await separately.
            //Task getLengthTask = AccessTheWebAsync();
            //// You can do independent work here.
            //int contentLength = await getLengthTask;
        int contentLength = await AccessTheWebAsync();

        resultsTextBox.Text +=
            $"\r\nLength of the downloaded string: {contentLength}.\r\n";
    }
        // Three things to note in the signature:
        //  - The method has an async modifier. 
        //  - The return type is Task or Task. (See "Return Types" section.)
        //    Here, it is Task because the return statement returns an integer.
        //  - The method name ends in "Async."
        async Task AccessTheWebAsync()
        { 
            // You need to add a reference to System.Net.Http to declare client.
            var client = new HttpClient();
            // GetStringAsync returns a Task. That means that when you await the
            // task you'll get a string (urlContents).
            Task getStringTask = client.GetStringAsync("https://docs.microsoft.com/dotnet");
            // You can do work here that doesn't rely on the string from GetStringAsync.
            DoIndependentWork();
            // The await operator suspends AccessTheWebAsync.
            //  - AccessTheWebAsync can't continue until getStringTask is complete.
            //  - Meanwhile, control returns to the caller of AccessTheWebAsync.
            //  - Control resumes here when getStringTask is complete. 
            //  - The await operator then retrieves the string result from getStringTask.
            string urlContents = await getStringTask;
            // The return statement specifies an integer result.
            // Any methods that are awaiting AccessTheWebAsync retrieve the length value.
            return urlContents.Length;
        }
        void DoIndependentWork()
        {
            resultsTextBox.Text += "\r\nWorking . . . . . . .\r\n";
        }
    }
}

// Sample Output:

// Working . . . . . . .

// Length of the downloaded string: 41564.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

TAP Naming conventions

A

By convention, methods that return commonly awaitable types (e.g. Task, Task, ValueTask, ValueTask) should have names that end with “Async”. Methods that start an asynchronous operation but do not return an awaitable type should not have names that end with “Async”, but may start with “Begin”, “Start”, or some other verb to suggest this method does not return or throw the result of the operation.

You can ignore the convention where an event, base class, or interface contract suggests a different name. For example, you shouldn’t rename common event handlers, such as Button1_Click.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

async and await common rules

A

1:Avoid Using Async Void; instead use async Task whenever possible, where T is the return type of your method.
2:DoSomeStuff(); // synchronous method
await DoSomeLengthyStuffAsync(); // long-running asynchronous method
DoSomeMoreStuff(); // another synchronous method
This allows the compiler to split the calling method at the point of the await keyword. The first part ends with the asynchronous method call; the second part starts with using its result if any, and continues from there on.
3:This allows the compiler to split the calling method at the point of the await keyword. The first part ends with the asynchronous method call; the second part starts with using its result if any, and continues from there on.

4:In order to use the await keyword on a method; its return type must be Task. This allows the compiler to trigger the continuation of our method, once the task completes. In other words, this will work as long as the asynchronous method’s signature is async Task. Had the signature been async void instead, we would have to call it without the await keyword:

DoSomeStuff(); // synchronous method
DoSomeLengthyStuffAsync(); // long-running asynchronous method
DoSomeMoreStuff(); // another synchronous method

5:Execution difference of with wait and without wait for an async method call
DoSomeStuff(); // synchronous method
DoSomeLengthyStuffAsync(); // long-running asynchronous method
DoSomeMoreStuff(); // another synchronous method
The compiler would not complain though. Depending on the side effects of DoSomeLengthyStuffAsync, the code might even work correctly. However, there is one important difference between the two examples. In the first one, DoSomeMoreStuff will only be invoked after DoSomeLengthyStuffAsync completes. In the second one, DoSomeMoreStuff will be invoked immediately after DoSomeLengthyStuffAsync starts. Since in the latter case DoSomeLengthyStuffAsync and DoSomeMoreStuff run in parallel, race conditions might occur. If DoSomeMoreStuff depends on any of DoSomeLengthyStuffAsync’s side effects, these might or might not yet be available when DoSomeMoreStuff wants to use them. Such a bug can be difficult to fix, because it cannot be reproduced reliably. It can also occur only in production environment, where I/O operations are usually slower than in development environment.

6:Always use async task
To avoid such issues altogether, always use async Task as the signature for methods you intend to call from your code. Restrict the usage of async void signature to event handlers, which are not allowed to return anything, and make sure you never call them yourself. If you need to reuse the code in an event handler, refactor it into a separate method returning Task, and call that new method from both the event handler and your method, using await.

7:Beware of Deadlocks
In a way, asynchronous methods behave contagiously. To call an asynchronous method with await, you must make the calling method asynchronous as well, even if it was not async before. Now, all methods calling this newly asynchronous method must also become asynchronous. This pattern repeats itself up the call stack until it finally reaches the entry points, e.g. event handlers.

When one of the methods on this path to the entry points cannot be asynchronous, this poses a problem. For example, constructors. They cannot be asynchronous, therefore you cannot use await in their body. As discussed in the previous section, you could break the asynchronous requirement early by giving a method async void signature, but this prevents you from waiting for its execution to end, which makes it a bad idea in most cases.

Alternatively, you could try synchronously waiting for the asynchronous method to complete, by calling Wait on the returned Task, or reading its Result property. Of course, this synchronous code will temporarily stop your application from processing the message queue, which we wanted to avoid in the first place. Even worse, in some cases you could cause a deadlock in your application with some very innocent looking code:

private async void MyEventHandler(object sender, RoutedEventArgs e)
{
    var instance = new InnocentLookingClass();
    // further code
}
Any synchronously called asynchronous code in InnocentLookingClass constructor is enough to cause a deadlock:
public class InnocentLookingClass()
{
    public InnocentLookingClass()
    {
        DoSomeLengthyStuffAsync().Wait();
        // do some more stuff
    }
    private async Task DoSomeLengthyStuffAsync()
    {
        await SomeOtherLengthyStuffAsync();
    }
    // other class members
}
Let us dissect what is happening in this code.

MyEventHandler synchronously calls InnocentLookingClass constructor, which invokes DoSomeLengthyStuffAsync, which in turn asynchronously invokes SomeOtherLengthyStuffAsync. The execution of the latter method starts; at the same time the main thread blocks at Wait until DoSomeLengthyStuffAsync completes without giving control back to the main message loop.

Eventually SomeOtherLengthyStuffAsync completes and posts a message to the message queue implying that the execution of DoSomeLengthyStuffAsync can continue. Unfortunately, the main thread is waiting for that method to complete instead of processing the messages, and will therefore never trigger it to continue, hence waiting indefinitely.

As you can see, synchronously invoking asynchronous methods can quickly have undesired consequences. Avoid it at all costs; unless you are sure what you are doing, i.e. you are not blocking the main message loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Using statement ; using translates into try,catch and finally

A

Provides a convenient syntax that ensures the correct use of IDisposable objects.

The following example shows how to use the using statement.

C#

Copy
using (var font1 = new Font("Arial", 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}
Beginning with C# 8.0, you can use the following alternative syntax for the using statement that doesn't require braces:

C#

Copy
using var font1 = new Font("Arial", 10.0f);
byte charset = font1.GdiCharSet;

File and Font are examples of managed types that access unmanaged resources (in this case file handles and device contexts). There are many other kinds of unmanaged resources and class library types that encapsulate them. All such types must implement the IDisposable interface.

When the lifetime of an IDisposable object is limited to a single method, you should declare and instantiate it in the using statement. The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called. Within the using block, the object is read-only and cannot be modified or reassigned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

using statement and exception handling

A

The using statement ensures that Dispose is called even if an exception occurs within the using block. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the following code at compile time (note the extra curly braces to create the limited scope for the object):

C#

Copy
{
  var font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}
The newer using statement syntax translates to very similar code. The try block opens where the variable is declared. The finally block is added at the close of the enclosing block, typically at the end of a method.

Finally calls the IDisposable.Dispose() method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

using statement with multiple objects of same type

A

Multiple instances of a type can be declared in the using statement, as shown in the following example:

C#

Copy
using (Font font3 = new Font("Arial", 10.0f),
            font4 = new Font("Arial", 10.0f))
{
    // Use font3 and font4.
}
You can combine multiple declarations of the same type using the new syntax introduced with C# 8 as well. This is shown in the following example:

C#

Copy
using Font font3 = new Font("Arial", 10.0f), 
    font4 = new Font("Arial", 10.0f);
// Use font3 and font4.
You can instantiate the resource object and then pass the variable to the using statement, but this is not a best practice. In this case, after control leaves the using block, the object remains in scope but probably has no access to its unmanaged resources. In other words, it's not fully initialized anymore. If you try to use the object outside the using block, you risk causing an exception to be thrown. For this reason, it's generally better to instantiate the object in the using statement and limit its scope to the using block.

C#

Copy
var font2 = new Font("Arial", 10.0f);
using (font2) // not recommended
{
    // use font2
}
// font2 is still in scope
// but the method call throws an exception
float f = font2.GetHeight();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

Cleaning Up Unmanaged Resources

A

For the majority of the objects that your app creates, you can rely on .NET’s garbage collector to handle memory management. However, when you create objects that include unmanaged resources, you must explicitly release those resources when you finish using them in your app. The most common types of unmanaged resource are objects that wrap operating system resources, such as files, windows, network connections, or database connections. Although the garbage collector is able to track the lifetime of an object that encapsulates an unmanaged resource, it doesn’t know how to release and clean up the unmanaged resource.

If your types use unmanaged resources, you should do the following:

Implement the dispose pattern. This requires that you provide an IDisposable.Dispose implementation to enable the deterministic release of unmanaged resources. A consumer of your type calls Dispose when the object (and the resources it uses) is no longer needed. The Dispose method immediately releases the unmanaged resources.

Provide for your unmanaged resources to be released in the event that a consumer of your type forgets to call Dispose. There are two ways to do this:

Use a safe handle to wrap your unmanaged resource. This is the recommended technique. Safe handles are derived from the System.Runtime.InteropServices.SafeHandle class and include a robust Finalize method. When you use a safe handle, you simply implement the IDisposable interface and call your safe handle’s Dispose method in your IDisposable.Dispose implementation. The safe handle’s finalizer is called automatically by the garbage collector if its Dispose method is not called.

—or—

Override the Object.Finalize method. Finalization enables the non-deterministic release of unmanaged resources when the consumer of a type fails to call IDisposable.Dispose to dispose of them deterministically. However, because object finalization can be a complex and error-prone operation, we recommend that you use a safe handle instead of providing your own finalizer.

Consumers of your type can then call your IDisposable.Dispose implementation directly to free memory used by unmanaged resources. When you properly implement a Dispose method, either your safe handle’s Finalize method or your own override of the Object.Finalize method becomes a safeguard to clean up resources in the event that the Dispose method is not called.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

async and await

A

async- specifies a method is an asychrnous method
await - it specifies a suspension point, it says that async method can not pass that point untill the await task is complete.In the mean time control passes to the caller of the async method
you might have multiple await in an async method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

async with out return (Task) example - Might not work

A
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace TAPModel
{
    class Program
    {
        static  void  Main(string[] args)
        {
        Console.WriteLine("From Main before calling Async");
            //The below async method is called
             CallReadFromFile();          
            //Control is returned when the async method wait is encountered and the below command is executed
            Console.WriteLine($"From Main after calling Async");
            //If the readline does not exist the code ends with errors; as the async method 
            //has not yet returned the results
            //but with readline the console is open and waits for the async output to show up 
            Console.ReadLine();
    }
        public static int ReadFromFile()
        {
            using (StreamReader streamReader = new StreamReader("TestData.txt"))
            {
                Thread.Sleep(10000);
                string content= streamReader.ReadToEnd();
                return content.Length;
            }
        }
        public async static Task CallReadFromFile()
        {
            //Creating a task for the method that takes time to execute
            Task task = new Task(ReadFromFile);
            //starting the task
            task.Start();
            //waiting on the task response; here the control goes back to calling method 
            int count = await task;
        //once the count value is available the control returns to here and the below statements get executed
        Console.WriteLine(count);
        Console.WriteLine("The above is the result From async method");

        //after this the control returns back to the calling method 

    }
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

async method with Task returning to the main method - might not work

A
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace TAPModel
{
    class Program
    {
        static  void  Main(string[] args)
        {
        Console.WriteLine("From Main before calling Async");
            //The below async method is called; to convert Task to int = we use .Result property
            int output=  CallReadFromFile().Result;          
            //it waits for the async method to complete; all its operations and then the control is returned to here
            Console.WriteLine($"From Main after calling Async, the output is : {output}");
        Console.WriteLine("After the async method is being called and there is no dependency on waiting method");
            //No need of below line, as the code is automatically for the asyc function to complete the task
           // Console.ReadLine();
    }
        public static int ReadFromFile()
        {
            using (StreamReader streamReader = new StreamReader("TestData.txt"))
            {
                Thread.Sleep(10000);
                string content= streamReader.ReadToEnd();
                return content.Length;
            }
        }
        public async static Task CallReadFromFile()
        {
            //Creating a task for the method that takes time to execute
            Task task = new Task(ReadFromFile);
            //starting the task
            task.Start();
            //waiting on the task response; here the control goes back to calling method 
            int count = await task;
        Console.WriteLine("After the await is being called");
            //once the count value is available the control returns to here and the below statements get executed
            return count;
        //after this the control returns back to the calling method 

    }
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

async return type Task

A

for an async method that returns a value.

using System;
using System.Linq;
using System.Threading.Tasks;

public class Example
{
public static void Main()
{
Console.WriteLine(“Inside the Main method, before calling Result property on async method”);
Console.WriteLine(ShowTodaysInfo().Result);
Console.WriteLine(“Inside the Main method, after calling Result property on async method”);
}

private static async Task ShowTodaysInfo()
{
    Console.WriteLine("Inside the ShowTodaysInfo method, before await");
    string ret = $"Today is {DateTime.Today:D}\n" +
                 "Today's hours of leisure: " +
                 $"{await GetLeisureHours()}";
    Console.WriteLine("Inside the ShowTodaysInfo method, after await");
    return ret;
}

static async Task GetLeisureHours()
{
    Console.WriteLine("Inside the GetLeisureHours method, before await");
   // Task.FromResult is a placeholder for actual work that returns a string.  
   var today = await Task.FromResult(DateTime.Now.DayOfWeek.ToString());
    Console.WriteLine("Inside the GetLeisureHours method, after await");
    // The method then can process the result in some way.  
    int leisureHours;
    if (today.First() == 'S')
        leisureHours = 16;
    else
        leisureHours = 5;

    return leisureHours;
} }

/* OUTPUT
Inside the Main method, before calling Result property on async method
Inside the ShowTodaysInfo method, before await
Inside the GetLeisureHours method, before await
Inside the GetLeisureHours method, after await
Inside the ShowTodaysInfo method, after await
Today is Saturday, February 15, 2020
Today’s hours of leisure: 16
Inside the Main method, after calling Result property on async method

The before wait calls are executed in Top to Down apporach; but the after await calls are 
exeuted in bottom to Top approach; so it means the lower level async method completes first and 
then the chains up   */

Task Return Type
The Task return type is used for an async method that contains a return (C#) statement in which the operand has type TResult.

In the following example, the GetLeisureHours async method contains a return statement that returns an integer. Therefore, the method declaration must specify a return type of Task. The FromResult async method is a placeholder for an operation that returns a string.

When GetLeisureHours is called from within an await expression in the ShowTodaysInfo method, the await expression retrieves the integer value (the value of leisureHours) that’s stored in the task returned by the GetLeisureHours method. For more information about await expressions, see await.

You can better understand how this happens by separating the call to GetLeisureHours from the application of await, as the following code shows. A call to method GetLeisureHours that isn’t immediately awaited returns a Task, as you would expect from the declaration of the method. The task is assigned to the integerTask variable in the example. Because integerTask is a Task, it contains a Result property of type TResult. In this case, TResult represents an integer type. When await is applied to integerTask, the await expression evaluates to the contents of the Result property of integerTask. The value is assigned to the ret variable.

Important

The Result property is a blocking property. If you try to access it before its task is finished, the thread that’s currently active is blocked until the task completes and the value is available. In most cases, you should access the value by using await instead of accessing the property directly.
The previous example retrieved the value of the Result property to block the main thread so that the ShowTodaysInfo method could finish execution before the application ended.

C#

Copy
var integerTask = GetLeisureHours();

// You can do other work that does not rely on integerTask before awaiting.

string ret = $”Today is {DateTime.Today:D}\n” +
“Today’s hours of leisure: “ +
$”{await integerTask}”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

async return type Task

A

for an async method that performs an operation but returns no value.

Async methods that don’t contain a return statement or that contain a return statement that doesn’t return an operand usually have a return type of Task. Such methods return void if they run synchronously. If you use a Task return type for an async method, a calling method can use an await operator to suspend the caller’s completion until the called async method has finished.

In the following example, the WaitAndApologize async method doesn’t contain a return statement, so the method returns a Task object. This enables WaitAndApologize to be awaited. Note that the Task type doesn’t include a Result property because it has no return value.

using System;
using System.Threading.Tasks;

public class Example
{
    public static void Main()
    {
        Console.WriteLine("Inside the Main method, before calling async method");
        //This wait() method stops the program execution till we obtain result from async method
        //it works synchronously
        DisplayCurrentInfo().Wait();
    Console.WriteLine("Inside the Main method, after calling async method");
}

static async Task DisplayCurrentInfo()
{
    Console.WriteLine("Inside the DisplayCurrentInfo method, before calling await method");

    await WaitAndApologize();

    Console.WriteLine("Inside the DisplayCurrentInfo method, after calling await method");

    Console.WriteLine($"Today is {DateTime.Now:D}");
    Console.WriteLine($"The current time is {DateTime.Now.TimeOfDay:t}");
    Console.WriteLine("The current temperature is 76 degrees.");
}

static async Task WaitAndApologize()
{
        Console.WriteLine("Inside the WaitAndApologize method, before calling await method for delay");
        // Task.Delay is a placeholder for actual work.  
        await Task.Delay(2000);
        Console.WriteLine("Inside the WaitAndApologize method, after calling await method for delay");
        // Task.Delay delays the following line by two seconds.  
        Console.WriteLine("\nSorry for the delay. . . .\n");
    }
}
// The example displays the following output:
//       Sorry for the delay. . . .
//       
//       Today is Wednesday, May 24, 2017
//       The current time is 15:25:16.2935649
//       The current temperature is 76 degrees.

/*
* WITH OUT THE WAIT() IN THE MAIN- THE program does not wait for the async method to complete execution
*
Inside the Main method, before calling async method
Inside the DisplayCurrentInfo method, before calling await method
Inside the WaitAndApologize method, before calling await method for delay
Inside the Main method, after calling async method

*/

/*
* WITH THE WAIT() IN THE MAIN- THE program waits for the async method to complete execution
*
Inside the Main method, before calling async method
Inside the DisplayCurrentInfo method, before calling await method
Inside the WaitAndApologize method, before calling await method for delay
Inside the WaitAndApologize method, after calling await method for delay

Sorry for the delay. . . .

Inside the DisplayCurrentInfo method, after calling await method
Today is Sunday, February 16, 2020
The current time is 08:27:21.8715125
The current temperature is 76 degrees.
Inside the Main method, after calling async method

*/

WaitAndApologize is awaited by using an await statement instead of an await expression, similar to the calling statement for a synchronous void-returning method. The application of an await operator in this case doesn’t produce a value.

As in the previous Task example, you can separate the call to WaitAndApologize from the application of an await operator, as the following code shows. However, remember that a Task doesn’t have a Result property, and that no value is produced when an await operator is applied to a Task.

The following code separates calling the WaitAndApologize method from awaiting the task that the method returns.

C#

Copy
Task wait = WaitAndApologize();

string output = $”Today is {DateTime.Now:D}\n” +
$”The current time is {DateTime.Now.TimeOfDay:t}\n” +
$”The current temperature is 76 degrees.\n”;
await wait;
Console.WriteLine(output);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

async return type Void

A

for event Handler
You use the void return type in asynchronous event handlers, which require a void return type. For methods other than event handlers that don’t return a value, you should return a Task instead, because an async method that returns void can’t be awaited. Any caller of such a method must be able to continue to completion without waiting for the called async method to finish, and the caller must be independent of any values or exceptions that the async method generates.

The caller of a void-returning async method can’t catch exceptions that are thrown from the method, and such unhandled exceptions are likely to cause your application to fail. If an exception occurs in an async method that returns a Task or Task, the exception is stored in the returned task and is rethrown when the task is awaited. Therefore, make sure that any async method that can produce an exception has a return type of Task or Task and that calls to the method are awaited.

For more information about how to catch exceptions in async methods, see the Exceptions in Async Methods section of the try-catch topic.

The following example shows the behavior of an async event handler. Note that in the example code, an async event handler must let the main thread know when it finishes. Then the main thread can wait for an async event handler to complete before exiting the program.

using System;
using System.Threading.Tasks;

public class NaiveButton
{
    public event EventHandler Clicked;
public void Click()
{
    Console.WriteLine("Somebody has clicked a button. Let's raise the event...");
    Clicked?.Invoke(this, EventArgs.Empty);
    Console.WriteLine("All listeners are notified.");
} }
public class AsyncVoidExample
{
    static TaskCompletionSource tcs;
    static async Task Main()
    {
        tcs = new TaskCompletionSource();
        var secondHandlerFinished = tcs.Task;
    var button = new NaiveButton();

    button. Clicked += Button_Clicked_1;
    button. Clicked += Button_Clicked_2_Async;
    button. Clicked += Button_Clicked_3;

    Console.WriteLine("About to click a button...");
    button.Click();
    Console.WriteLine("Button's Click method returned.");

    await secondHandlerFinished;
}

private static void Button_Clicked_1(object sender, EventArgs e)
{
    Console.WriteLine("   Handler 1 is starting...");
    Task.Delay(100).Wait();
    Console.WriteLine("   Handler 1 is done.");
}

private static async void Button_Clicked_2_Async(object sender, EventArgs e)
{
    Console.WriteLine("   Handler 2 is starting...");
    Task.Delay(100).Wait();
    Console.WriteLine("   Handler 2 is about to go async...");
    await Task.Delay(500);
    Console.WriteLine("   Handler 2 is done.");
    tcs.SetResult(true);
}

private static void Button_Clicked_3(object sender, EventArgs e)
{
    Console.WriteLine("   Handler 3 is starting...");
    Task.Delay(100).Wait();
    Console.WriteLine("   Handler 3 is done.");
} }
// Expected output:
// About to click a button...
// Somebody has clicked a button. Let's raise the event...
//    Handler 1 is starting...
//    Handler 1 is done.
//    Handler 2 is starting...
//    Handler 2 is about to go async...
//    Handler 3 is starting...
//    Handler 3 is done.
// All listeners are notified.
// Button's Click method returned.
//    Handler 2 is done.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

await

A
this stops the exection of the program and returns to the calling function
-This also converts Task into Tresult like Task to int
-it gets the values of Result property but it is non-blocking which means other codes could be running
await GetLeisureHours()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

Result property of Task

A

it converts the Task to TResult but unlike await it is blocking property; so your code will stop for the result to be available.
we could use this in the Main () method ; so that we get the value from the async method before the program ends

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

Task.FromResult(TResult) Method

A

Creates a Task that’s completed successfully with the specified result.

This method creates a Task object whose Task.Result property is result and whose Status property is RanToCompletion. The method is commonly used when the return value of a task is immediately known without executing a longer code path. The example provides an illustration.

 // Task.FromResult is a placeholder for actual work that returns a string.  
       var today = await Task.FromResult(DateTime.Now.DayOfWeek.ToString());
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

Tasks could be a list of Tasks

A

List> tasks = new List>();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

Task Methods
Task.WaitAll(tasks.ToArray());
Task.Run()
Task.Add()

A

Task.Run()-Queues the specified work to run on the ThreadPool and returns a task or Task handle for that work.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

Task Properties

Task.Status

A

The current TaskStatus of this task instance.

Retrieving the value of the Task.Status property does not block the calling thread until the task has completed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

Wait method vs await operand - while similar conceptually - are actually completely different.

A

Wait will synchronously block until the task completes. So the current thread is literally blocked waiting for the task to complete. As a general rule, you should use “async all the way down”; that is, don’t block on async code. On my blog, I go into the details of how blocking in asynchronous code causes deadlock.

await will asynchronously wait until the task completes. This means the current method is “paused” (its state is captured) and the method returns an incomplete task to its caller. Later, when the await expression completes, the remainder of the method is scheduled as a continuation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

Task.Wait Method

A

Waits for the Task to complete execution.

Wait(TimeSpan) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

The task completes successfully.

The task itself is canceled or throws an exception. In this case, you handle an AggregateException exception. The AggregateException.InnerExceptions property contains details about the exception or exceptions.

The interval defined by timeout elapses. In this case, the current thread resumes execution and the method returns false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

Creating a task for async method to await is optional, you could directly await on the async method in the calling function

A
static async Task DisplayCurrentInfo()
   {
      await WaitAndApologize();
      Console.WriteLine($"Today is {DateTime.Now:D}");
      Console.WriteLine($"The current time is {DateTime.Now.TimeOfDay:t}");
      Console.WriteLine("The current temperature is 76 degrees.");
   }
   static async Task WaitAndApologize()
   {
      // Task.Delay is a placeholder for actual work.  
      await Task.Delay(2000);  
      // Task.Delay delays the following line by two seconds.  
      Console.WriteLine("\nSorry for the delay. . . .\n");  
   }

Task wait = WaitAndApologize();

string output = $”Today is {DateTime.Now:D}\n” +
$”The current time is {DateTime.Now.TimeOfDay:t}\n” +
$”The current temperature is 76 degrees.\n”;
await wait;
Console.WriteLine(output);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

Generalized async return types and ValueTask

to use use value type instead of Reference types

A

Generalized async return types and ValueTask
Starting with C# 7.0, an async method can return any type that has an accessible GetAwaiter method.

Because Task and Task are reference types, memory allocation in performance-critical paths, particularly when allocations occur in tight loops, can adversely affect performance. Support for generalized return types means that you can return a lightweight value type instead of a reference type to avoid additional memory allocations.

.NET provides the System.Threading.Tasks.ValueTask structure as a lightweight implementation of a generalized task-returning value. To use the System.Threading.Tasks.ValueTask type, you must add the System.Threading.Tasks.Extensions NuGet package to your project. The following example uses the ValueTask structure to retrieve the value of two dice rolls.

using System;
using System.Threading.Tasks;

class Program
{
    static Random rnd;
static void Main()
{
    Console.WriteLine("Inside the Main method, before calling async method");
    Console.WriteLine($"You rolled {GetDiceRoll().Result}");
    Console.WriteLine("Inside the Main method, after calling async method");
}

private static async ValueTask GetDiceRoll()
{
    Console.WriteLine("...Shaking the dice...");
    Console.WriteLine("Inside the GetDiceRoll method, before calling await method");
    int roll1 = await Roll();
    Console.WriteLine("Inside the GetDiceRoll method, after the first calling to await");
    int roll2 = await Roll();
    Console.WriteLine("Inside the GetDiceRoll method, after the second calling to await");
    return roll1 + roll2;
}

private static async ValueTask Roll()
{
    if (rnd == null)
        rnd = new Random();
    Console.WriteLine("Inside the Roll method, before the calling to await");
    await Task.Delay(500);
        Console.WriteLine("Inside the Roll method, after the calling to await");
        int diceRoll = rnd.Next(1, 7);
        return diceRoll;
    }
}
// The example displays output like the following:
//       ...Shaking the dice...
//       You rolled 8

/*
* Inside the Main method, before calling async method
…Shaking the dice…
Inside the GetDiceRoll method, before calling await method
Inside the Roll method, before the calling to await
Inside the Roll method, after the calling to await
Inside the GetDiceRoll method, after the first calling to await
Inside the Roll method, before the calling to await
Inside the Roll method, after the calling to await
Inside the GetDiceRoll method, after the second calling to await
You rolled 10
Inside the Main method, after calling async method
*/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

Random class

A
Random rnd = new Random();
 int diceRoll = rnd.Next(1, 7);
Randomly selects a value between 1 and 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

For the task to wait and await on it

A

await Task.Delay(500);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

dataset vs datable to get data from database

A

Use a DataTable.

A DataSet is an in-memory database while DataTable is an in-memory table.

DataSets are more complicated and heavier-weight; they can contain multiple DataTables and relations between DataTables

you can better use DataTable(uses less memory).

or you can try with user created value objects or DTO

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

DataReader, DataSet, DataAdapter and DataTable are four major components of ADO.NET

A

These are different objects to read data from database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

DataReader - ADO.NET

A

DataReader is used to read the data from database and it is a read and forward only connection oriented architecture during fetch the data from database. DataReader will fetch the data very fast when compared with dataset. Generally we will use ExecuteReader object to bind data to datareader.

To bind DataReader data to GridView we need to write the code like as shown below:
Protected void BindGridview() {
using(SqlConnection conn = new SqlConnection(“Data Source=abc;Integrated Security=true;Initial Catalog=Test”)) {
con.Open();
SqlCommand cmd = new SqlCommand(“Select UserName, First Name,LastName,Location FROM Users”, conn);
SqlDataReader sdr = cmd.ExecuteReader();
gvUserInfo.DataSource = sdr;
gvUserInfo.DataBind();
conn.Close();
}
}
Holds the connection open until you are finished (don’t forget to close it!).
Can typically only be iterated over once
Is not as useful for updating back to the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

DataSet - ADO.NET

A

DataSet is a disconnected orient architecture that means there is no need of active connections during work with datasets and it is a collection of DataTables and relations between tables. It is used to hold multiple tables with data. You can select data form tables, create views based on table and ask child rows over relations. Also DataSet provides you with rich features like saving data as XML and loading XML data.
protected void BindGridview() {
SqlConnection conn = new SqlConnection(“Data Source=abc;Integrated Security=true;Initial Catalog=Test”);
conn.Open();
SqlCommand cmd = new SqlCommand(“Select UserName, First Name,LastName,Location FROM Users”, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();

75
Q

DataAdapter - ADO.NET

A

DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture. Check below sample code to see how to use DataAdapter in code:
protected void BindGridview() {
SqlConnection con = new SqlConnection(“Data Source=abc;Integrated Security=true;Initial Catalog=Test”);
conn.Open();
SqlCommand cmd = new SqlCommand(“Select UserName, First Name,LastName,Location FROM Users”, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gvUserInfo.DataSource = ds;
gvUserInfo.DataBind();
}
Lets you close the connection as soon it’s done loading data, and may even close it for you automatically
All of the results are available in memory
You can iterate over it as many times as you need, or even look up a specific record by index
Has some built-in faculties for updating back to the database.

76
Q

DataTable - ADO.NET

A

DataTable represents a single table in the database. It has rows and columns. There is no much difference between dataset and datatable, dataset is simply the collection of datatables.
protected void BindGridview() {
SqlConnection con = new SqlConnection(“Data Source=abc;Integrated Security=true;Initial Catalog=Test”);
conn.Open();
SqlCommand cmd = new SqlCommand(“Select UserName, First Name,LastName,Location FROM Users”, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
gridview1.DataSource = dt;
gvidview1.DataBind();
}

Here is a detailed tutorial in DataTable: DataTable in C#

77
Q

Language Integrated Query (LINQ)

A

Language-Integrated Query (LINQ) is the name for a set of technologies based on the integration of query capabilities directly into the C# language. Traditionally, queries against data are expressed as simple strings without type checking at compile time or IntelliSense support. Furthermore, you have to learn a different query language for each type of data source: SQL databases, XML documents, various Web services, and so on. With LINQ, a query is a first-class language construct, just like classes, methods, events. You write queries against strongly typed collections of objects by using language keywords and familiar operators. The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).

For a developer who writes queries, the most visible “language-integrated” part of LINQ is the query expression. Query expressions are written in a declarative query syntax. By using query syntax, you can perform filtering, ordering, and grouping operations on data sources with a minimum of code. You use the same basic query expression patterns to query and transform data in SQL databases, ADO .NET Datasets, XML documents and streams, and .NET collections.

You can write LINQ queries in C# for SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable interface. LINQ support is also provided by third parties for many Web services and other database implementations.

Query expression overview
Query expressions can be used to query and to transform data from any LINQ-enabled data source. For example, a single query can retrieve data from a SQL database, and produce an XML stream as output.

Query expressions are easy to master because they use many familiar C# language constructs.

The variables in a query expression are all strongly typed, although in many cases you do not have to provide the type explicitly because the compiler can infer it. For more information, see Type relationships in LINQ query operations.

A query is not executed until you iterate over the query variable, for example, in a foreach statement. For more information, see Introduction to LINQ queries.

At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax. However, in most cases query syntax is more readable and concise. For more information, see C# language specification and Standard query operators overview.

As a rule when you write LINQ queries, we recommend that you use query syntax whenever possible and method syntax whenever necessary. There is no semantic or performance difference between the two different forms. Query expressions are often more readable than equivalent expressions written in method syntax.

Some query operations, such as Count or Max, have no equivalent query expression clause and must therefore be expressed as a method call. Method syntax can be combined with query syntax in various ways. For more information, see Query syntax and method syntax in LINQ.

Query expressions can be compiled to expression trees or to delegates, depending on the type that the query is applied to. IEnumerable queries are compiled to delegates. IQueryable and IQueryable queries are compiled to expression trees. For more information, see Expression trees.

78
Q

For Linq and IEnumerable

A

using System.Linq;

using System.Collections.Generic;

79
Q

IEnumerable Interface

A

Exposes an enumerator, which supports a simple iteration over a non-generic collection.
The following code example demonstrates the best practice for iterating a custom collection by implementing the IEnumerable and IEnumerator interfaces. In this example, members of these interfaces are not explicitly called, but they are implemented to support the use of foreach (For Each in Visual Basic) to iterate through the collection.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerable?view=netframework-4.8

80
Q

IEnumerable

A

IEnumerable is the base interface for collections in the System.Collections.Generic namespace such as List, Dictionary, and Stack and other generic collections such as ObservableCollection and ConcurrentStack. Collections that implement IEnumerable can be enumerated by using the foreach statement.

For the non-generic version of this interface, see System.Collections.IEnumerable.

IEnumerable contains a single method that you must implement when implementing this interface; GetEnumerator, which returns an IEnumerator object. The returned IEnumerator provides the ability to iterate through the collection by exposing a Current property.

81
Q

Linq example

A
class LINQQueryExpressions
{
    static void Main()
    {
        // Specify the data source.
        int[] scores = new int[] { 97, 92, 81, 60 };
        // Define the query expression.
        IEnumerable scoreQuery =
            from score in scores
            where score > 80
            select score;
        // Execute the query.
        foreach (int i in scoreQuery)
        {
            Console.Write(i + " ");
        }            
    }
}
// Output: 97 92 81
82
Q

ToList()

A

Creates a List from an IEnumerable.
Examples
The following code example demonstrates how to use ToList to force immediate query evaluation and return a List that contains the query results.

C#

Copy
string[] fruits = { “apple”, “passionfruit”, “banana”, “mango”,
“orange”, “blueberry”, “grape”, “strawberry” };

List lengths = fruits.Select(fruit => fruit.Length).ToList();

foreach (int length in lengths)
{
Console.WriteLine(length);
}

/*
 This code produces the following output:
 5
 12
 6
 5
 6
 9
 5
 10
*/
Remarks
The ToList(IEnumerable) method forces immediate query evaluation and returns a List that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

ToArray has similar behavior but returns an array instead of a List.

83
Q

IQueryable Interface

A

Provides functionality to evaluate queries against a specific data source wherein the type of the data is not specified.

Derived
System.Data.Linq.ITable
System.Data.Linq.ITable
System.Data.Linq.Table
System.Data.Objects.IObjectSet
System.Data.Objects.ObjectQuery
System.Data.Objects.ObjectQuery
System.Data.Objects.ObjectSet
System.Data.Services.Client.DataServiceQuery
System.Data.Services.Client.DataServiceQuery
System.Linq.EnumerableQuery
System.Linq.IOrderedQueryable
System.Linq.IOrderedQueryable
System.Linq.IQueryable

The IQueryable interface is intended for implementation by query providers. It is only supposed to be implemented by providers that also implement IQueryable. If the provider does not also implement IQueryable, the standard query operators cannot be used on the provider’s data source.

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of “executing an expression tree” is specific to a query provider. For example, it may involve translating the expression tree to an appropriate query language for the underlying data source. Queries that do not return enumerable results are executed when the Execute method is called.

84
Q

IEnumerable vs IQuerable

A

From the perspective of a Repository Pattern, you can think of it this way:

Use an eager loading IEnumerable when you want to pass an entire list to the client in one go. They can still add linq clauses, but the client does not benefit from deferred execution.

Use a lazy loading IQueryable when you want to extend deferred querying capabilities to the client, by allowing the client to add their own linq clauses. This defers the execution of the entire query until output is required.

https://www.codeproject.com/Articles/732425/IEnumerable-Vs-IQueryable

85
Q

LazyLoading

A

Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used. The opposite of lazy loading is eager loading. This makes it ideal in use cases where network content is accessed and initialization times are to be kept at a minimum, such as in the case of web pages.

There are four common ways of implementing the lazy load design pattern: lazy initialization; a virtual proxy; a ghost, and a value holder.[1] Each has its own advantages and disadvantages.

86
Q

LazyLoading example NOT CLEAR

A

Bellow is my answer but also note that Jon Skeet spoke about it today on his blog an about the fact that he is not totally ok with the MSDN meaning of “Lazy” as MSDN isn’t really clear of what lazy exactly mean when they use it in Just how lazy are you ? his post make for an interesting read.

Additionally Wikipedia assume that three rules should be maintained for lazy evaluation and third point isn’t respected in MSDN meaning as the expression will be evaluated more than once if GetEnumerator is called again (By the spec Reset is not implemented on enumerator objects generated using the yield keyword and most of linq use it currently)

Considering a function

int Computation(int index)
Immediate execution
IEnumerable GetComputation(int maxIndex)
{
    var result = new int[maxIndex];
    for(int i = 0; i < maxIndex; i++)
    {
        result[i] = Computation(i);
    }
    return result;
}
When the function is called Computation is executed maxIndex times
GetEnumerator returns a new instance of the enumerator doing nothing more.
Each call to MoveNext put the the value stored in the next Array cell in the Current member of the IEnumerator and that's all.
Cost: Big upfront, Small during enumeration (only a copy)
Deferred but eager execution
IEnumerable GetComputation(int maxIndex)
{
    var result = new int[maxIndex];
    for(int i = 0; i < maxIndex; i++)
    {
        result[i] = Computation(i);
    }
    foreach(var value in result)
    {
        yield return value;
    }
}
When the function is called an instance of an auto generated class (called "enumerable object" in the spec) implementing IEnumerable is created and a copy of the argument (maxIndex) is stored in it.
GetEnumerator returns a new instance of the enumerator doing nothing more.
The first call to MoveNext executes maxIndex times the compute method, store the result in an array and Current will return the first value.
Each subsequent call to MoveNext will put in Current a value stored in the array.
Cost: nothing upfront, Big when the enumeration start, Small during enumeration (only a copy)
Deferred and lazy execution
IEnumerable GetComputation(int maxIndex)
{
    for(int i = 0; i < maxIndex; i++)
    {
        yield return Computation(i);
    }
}
When the function is called the same thing as the lazy execution case happens.
GetEnumerator returns a new instance of the enumerator doing nothing more.
Each call to MoveNext execute once the Computation code, put the value in Current and let the caller immediately act on the result.
Most of linq use deferred and lazy execution but some functions can't be so like sorting.

Cost: nothing upfront, Moderate during enumeration (the computation is executed there)

To summarize
Immediate mean that the computation/execution is done in the function and finished once the function return. (Fully eager evaluation as most C# code does)
Deferred/Eager mean that most of the work will be done on the first MoveNext or when the IEnumerator instance is created (For IEnumerable it is when GetEnumerator is called)
Deferred/Lazy mean that the work will be done each time MoveNext is called but nothing before.
Parallel LINQ does it a little differently as the computation could be considered deferred/Lazy from the point of view of the caller but internally the computation of some number of elements begin in parallel as soon as the enumeration begin. The result is that if the next value is already there you get it immediately but otherwise you will have to wait for it.

https://stackoverflow.com/questions/2515796/deferred-execution-and-eager-evaluation

87
Q

ADONET VS Entity Framework

A

There is really no nice way to say it, so I will just say it: the guiding principle behind Entity Framework (and in fact all ORMs), is allowing people who could not learn the theory of the relational databases and did not have what it takes to learn SQL query language well to still develop complete applications.

In short EF is great for simple systems where the database is nothing more than an application-related data storage. But by the very reason for its existence it will never give you the power and the flexibility of working directly with the database (or to be more exact: if you need to use it at that level, it will be more work than just using the ado .net and the DB with T-SQL directly).

For Ultra-Large-Scale Enterprise Systems where databases have 1000s to 10s of 1000s of tables, billions of rows, implicit relationships that cannot be made explicit, and are completely business-rules and application agnostic (they store the data in the most efficient way possible, independent of any human-oriented application needs) and service 100s of completely different applications - using EF has no advantage whatsoever over just manually creating your business entities as regular classes and manually loading them in code.

public List GetDistributors()
{
List returnList;

cmdBigDB.CommandText = stringQueryThatJoins15TablesWith350_   
                       ColumnsAndWouldReturn5BillionRowsIf_   
                       UnfilteredButReturnsJust25RowsThatYou_
                       NeedFromDBServerToAppServer;
    adtBigDB = new SqlDataAdapter(cmdBigDB); 
    result = new DataSet(); 
    adtBigDB.Fill(result);
    returnList = new List();
    foreach (DataRow drwRow in result.Tables[0].Rows)
    {
        returnList.Add(new Distributor 
        { Property1 = (String)drwRow["Column1"], 
          Property2 = (String)drwRow["Column2"], 
          Property3 = (String)drwRow["Column3"] });
    }
return returnList; } Later in code:

var distributors = from m in GetDistributors() where m.Property1 < 75 orderby m.Property2 select m;

As you can see my application data model is completely abstracted from the (enormous) database. There is nothing in EF that allows you to do that in a way that would be simpler than the code above.

In fact in EF you would have to use the database-first approach to map all 15 tables, spend a ton of time to tweak the XML and the code, so you get all the implicit relationship right, since they have to be explicit in EF for LINQ to work, so later you can use LINQ-to-Entities to basically do what I did with my SQL query without any additional hassle. Because Entity Framework is just a mapping tool, it does not abstract anything away. You drag your entire DB complexity into an application that is interested just in getting some simple data from the DB and later doing “its thing” with it.

Of course you can build an entity based on a view that was built with that query, but then - what is the point of using EF?

So EF works with simple projects, where the database is 1-to-1 with single application’s data storage needs (code-first approach). In any serious environment, where you are interested in just a certain abstract subsection of a much larger database, EF does nothing for you, but creates a lot of additional hassle.

And a final thought: I have been developing using Microsoft tech for 25 years. Microsoft technologies come and go. Even the good ones get abandoned for inexplicable reasons, sometimes.

On the other hand ANSI SQL is forever. 5-pages-long queries I wrote 20 years ago for Oracle work today - in 2019 - with VS 2017 C# and SQL Server 2017, with no real changes. They work with Swift, iOS, XCode and MySQL too. At the same time Microsoft ORM from 20 years ago that came with Visual Basic 6 is long dead and forgotten.

You can’t go wrong with understanding relational databases and SQL.

If you are asking about performance then ADO.NET will always be faster then Entity framework.The difference is not much when using EF 6 but ado.net is still faster.

Actually Entity Framework is build at the top of ADO.NET so it can’t be faster. But it makes development much faster. And improves maintainability of your code. I mean the power of LinQ queries with EF requires you to write less code.
———-

88
Q

ADONET VS Entity Framework comparision

ADO.NET for big project, while EF for small ones.

A

This is very good query you have posted here.

This has been a topic of discussion for long without any strong and final decision as there is nothing called the best approach in this scenario. We follow what has been followed the most.
Web Page is Unavailable= ADO.NET
Now coming back to your first point.

1- We know that EF is called Web Page is Unavailable EF which means that EF sits on top of the Web Page is Unavailable , which tells us that it cant be faster than Web Page is Unavailable. But remember the power of LINQ which EF provides the developers. It is really powerful when comes with EF. Since EF encapsulates Web Page is Unavailable at the background it used Web Page is Unavailable only, but the question comes why EF then?? Yes if we use EF and LINQ then the maintainability and code redundancy reduces as we donot have to write the big queries anymore like SP and all.

2-See the connection and dispose of connection is a very vital thing to keep in mind. When we use EF then the context objects are used to connect to the database and for the operation in the LINQs. So when we create a new instance of the context using the “using” then it by default disposes the context when ever the use is over. Also now when we are using Dependency Injection tool like Ninject, they do the disposing internally using the IDisposable extension. So under the belt the connection gets disposed, but sometimes not immediately.

3- Async methods are good to use when you know the flow properly and sure of it. For example you need to send emails to say 50 persons, then if that is not run on the background then it would eat up a lot of time and memory. SMTP connection is not that fast. So better use async methods so that it runs on the background. But for single methods which will just return you an object which will be used then, avoid/donot use Async in this case, as we are unsure of the thread execution on the background. So better first try and know the scenario and then use async methods.

I hope I could explain something to you.

Honestly, ADO.NET is almost always better then EF in performance, for example, if you have a very huge table full of index says, Table X, you cannot and you should not simply update on X directly,

instead you should

create a temp table, says Y, with (ID, the fields that will be updated, says, A)
insert the records you wanna update into the temp table (ID, the fields that will be updated)
update X
inner join Y on X.id= Y.id
set X.A = Y.A
Ok, so… it is relatively hard to use temp table with EF, you cannot use Linq to do so (Linq is only good at query optimization)

with EF, its neither

Stored procedures (good luck passing everything as parameters…)
Dynamic Query
IQueryable classes
All of the above options will waste a lot time to implement

In contast, with ADO.NET, just put everything in a string then run ExecuteNonQuery()…

But EF does give you a better structure in data model, you will tend to find ADO.NET projects so messy.

So… I suggest ADO.NET for big project, while EF for small ones.

89
Q

LINQ to Entities

A

LINQ to Entities provides Language-Integrated Query (LINQ) support that enables developers to write queries against the Entity Framework conceptual model using Visual Basic or Visual C#. Queries against the Entity Framework are represented by command tree queries, which execute against the object context. LINQ to Entities converts Language-Integrated Queries (LINQ) queries to command tree queries, executes the queries against the Entity Framework, and returns objects that can be used by both the Entity Framework and LINQ. The following is the process for creating and executing a LINQ to Entities query:
https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/linq-to-entities

90
Q

Query Syntax and Method Syntax in LINQ (C#)

A

Most queries in the introductory Language Integrated Query (LINQ) documentation are written by using the LINQ declarative query syntax. However, the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled. These method calls invoke the standard query operators, which have names such as Where, Select, GroupBy, Join, Max, and Average. You can call them directly by using method syntax instead of query syntax.

Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition. You also must use a method call for a query that retrieves the element that has the maximum value in a source sequence. The reference documentation for the standard query operators in the System.Linq namespace generally uses method syntax. Therefore, even when getting started writing LINQ queries, it is useful to be familiar with how to use method syntax in queries and in query expressions themselves.

Standard Query Operator Extension Methods
The following example shows a simple query expression and the semantically equivalent query written as a method-based query.

C#

Copy
class QueryVMethodSyntax
{
    static void Main()
    {
        int[] numbers = { 5, 10, 8, 3, 6, 12};
        //Query syntax:
        IEnumerable numQuery1 = 
            from num in numbers
            where num % 2 == 0
            orderby num
            select num;
        //Method syntax:
        IEnumerable numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
        foreach (int i in numQuery1)
        {
            Console.Write(i + " ");
        }
        Console.WriteLine(System.Environment.NewLine);
        foreach (int i in numQuery2)
        {
            Console.Write(i + " ");
        }
    // Keep the console open in debug mode.
    Console.WriteLine(System.Environment.NewLine);
    Console.WriteLine("Press any key to exit");
    Console.ReadKey();
} } /*
Output:
6 8 10 12
6 8 10 12  */ The output from the two examples is identical. You can see that the type of the query variable is the same in both forms: IEnumerable.

To understand the method-based query, let’s examine it more closely. On the right side of the expression, notice that the where clause is now expressed as an instance method on the numbers object, which as you will recall has a type of IEnumerable. If you are familiar with the generic IEnumerable interface, you know that it does not have a Where method. However, if you invoke the IntelliSense completion list in the Visual Studio IDE, you will see not only a Where method, but many other methods such as Select, SelectMany, Join, and Orderby. These are all the standard query operators.

Screenshot showing all the standard query operators in Intellisense.

Although it looks as if IEnumerable has been redefined to include these additional methods, in fact this is not the case. The standard query operators are implemented as a new kind of method called extension methods. Extensions methods “extend” an existing type; they can be called as if they were instance methods on the type. The standard query operators extend IEnumerable and that is why you can write numbers.Where(…).

To get started using LINQ, all that you really have to know about extension methods is how to bring them into scope in your application by using the correct using directives. From your application’s point of view, an extension method and a regular instance method are the same.

For more information about extension methods, see Extension Methods. For more information about standard query operators, see Standard Query Operators Overview (C#). Some LINQ providers, such as LINQ to SQL and LINQ to XML, implement their own standard query operators and additional extension methods for other types besides IEnumerable.

Lambda Expressions
In the previous example, notice that the conditional expression (num % 2 == 0) is passed as an in-line argument to the Where method: Where(num => num % 2 == 0). This inline expression is called a lambda expression. It is a convenient way to write code that would otherwise have to be written in more cumbersome form as an anonymous method or a generic delegate or an expression tree. In C# => is the lambda operator, which is read as “goes to”. The num on the left of the operator is the input variable which corresponds to num in the query expression. The compiler can infer the type of num because it knows that numbers is a generic IEnumerable type. The body of the lambda is just the same as the expression in query syntax or in any other C# expression or statement; it can include method calls and other complex logic. The “return value” is just the expression result.

To get started using LINQ, you do not have to use lambdas extensively. However, certain queries can only be expressed in method syntax and some of those require lambda expressions. After you become more familiar with lambdas, you will find that they are a powerful and flexible tool in your LINQ toolbox. For more information, see Lambda Expressions.

Composability of Queries
In the previous code example, note that the OrderBy method is invoked by using the dot operator on the call to Where. Where produces a filtered sequence, and then Orderby operates on that sequence by sorting it. Because queries return an IEnumerable, you compose them in method syntax by chaining the method calls together. This is what the compiler does behind the scenes when you write queries by using query syntax. And because a query variable does not store the results of the query, you can modify it or use it as the basis for a new query at any time, even after it has been executed.

91
Q

The null-coalescing operator ?? and ??=

A

The null-coalescing operator ?? returns the value of its left-hand operand if it isn’t null; otherwise, it evaluates the right-hand operand and returns its result. The ?? operator doesn’t evaluate its right-hand operand if the left-hand operand evaluates to non-null.

Available in C# 8.0 and later, the null-coalescing assignment operator ??= assigns the value of its right-hand operand to its left-hand operand only if the left-hand operand evaluates to null. The ??= operator doesn’t evaluate its right-hand operand if the left-hand operand evaluates to non-null.

List numbers = null;
int? a = null;

(numbers ??= new List()).Add(5);
Console.WriteLine(string.Join(" ", numbers));  // output: 5

numbers.Add(a ??= 0);
Console.WriteLine(string.Join(“ “, numbers)); // output: 5 0
Console.WriteLine(a); // output: 0

Beginning with C# 8.0, you can use the ??= operator to replace the code of the form

C#

Copy
if (variable is null)
{
    variable = expression;
}
with the following code:

C#

Copy
variable ??= expression;

The operators ?? and ??= cannot be overloaded.

92
Q

CONSTANT

A

static by default; should be initialized at the time of declaration, value can not be changed

93
Q

READ ONLY

A

assign with non-static constructor and not even a method;There is no runtime error !! And the value can be changed again and again through a constructor.

94
Q

STATIC READ ONLY

A

A Static Readonly type variable’s value can be assigned at runtime or assigned at compile time and changed at runtime. But this variable’s value can only be changed in the static constructor. And cannot be changed further. It can change only once at runtime. Let’s understand it practically.

95
Q

LAMBDA EXPRESSION

A

LAMBDA EXPRESSION: it is like a function with arguments and the logic below it

96
Q

Func Delegate used in unit test project

A

Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter.
public delegate TResult Func(T arg);
Expression> call = x => x.Find(id);

mock.Verify(call,Times.Once); //we have once, Never, Exactly

setting up the call variable and using the same delegate in the mock.verify

the first parameter is input(IRepo) and the second parameter is output Customer

97
Q

Action Delegate

A

To reference a method that has one parameter and returns void (or in Visual Basic, that is declared as a Sub rather than as a Function), use the generic Action delegate instead.

Encapsulates a method that has a single parameter and does not return a value.

You can also assign a lambda expression to an Action delegate instance, as the following example illustrates. (For an introduction to lambda expressions, see Lambda Expressions.)

C#

Copy
using System;
using System.Windows.Forms;

public class TestLambdaExpression
{
   public static void Main()
   {
      Action messageTarget; 
  if (Environment.GetCommandLineArgs().Length > 1)
     messageTarget = s => ShowWindowsMessage(s); 
  else
     messageTarget = s => Console.WriteLine(s);

  messageTarget("Hello, World!");    }

private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
The ForEach and ForEach methods each take an Action delegate as a parameter. The method encapsulated by the delegate allows you to perform an action on each element in the array or list. The example uses the ForEach method to provide an illustration.

98
Q

Lambda and underlying Func delegates

A

The underlying type of a lambda expression is one of the generic Func delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the System.Linq namespace have Func parameters, you can pass these methods a lambda expression without explicitly instantiating a Func delegate.

Func convert = s => s.ToUpper();

string name = “Dakota”;
Console.WriteLine(convert(name));

// This code example produces the following output:
//
//    DAKOTA
99
Q

Func Delegate examples

A

The following example demonstrates how to declare and use a Func delegate. This example declares a Func variable and assigns it a lambda expression that converts the characters in a string to uppercase. The delegate that encapsulates this method is subsequently passed to the Enumerable.Select method to change the strings in an array of strings to uppercase.

C#

Copy

Run
// Declare a Func variable and assign a lambda expression to the  
// variable. The method takes a string and converts it to uppercase.
Func selector = str => str.ToUpper();
// Create an array of strings.
string[] words = { "orange", "apple", "Article", "elephant" };
// Query the array and select strings according to the selector method.
IEnumerable aWords = words.Select(selector);

// Output the results to the console.
foreach (String word in aWords)
Console.WriteLine(word);

/*
This code example produces the following output:

ORANGE
APPLE
ARTICLE
ELEPHANT

*/
Remarks
You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value.

Note

To reference a method that has one parameter and returns void (or in Visual Basic, that is declared as a Sub rather than as a Function), use the generic Action delegate instead.

When you use the Func delegate, you do not have to explicitly define a delegate that encapsulates a method with a single parameter. For example, the following code explicitly declares a delegate named ConvertMethod and assigns a reference to the UppercaseString method to its delegate instance.

C#

Copy
using System;

delegate string ConvertMethod(string inString);

public class DelegateExample
{
   public static void Main()
   {
      // Instantiate delegate to reference UppercaseString method
      ConvertMethod convertMeth = UppercaseString;
      string name = "Dakota";
      // Use delegate instance to call UppercaseString method
      Console.WriteLine(convertMeth(name));
   }

private static string UppercaseString(string inputString)
{
return inputString.ToUpper();
}
}
The following example simplifies this code by instantiating the Func delegate instead of explicitly defining a new delegate and assigning a named method to it.

C#

Copy

Run
// Instantiate delegate to reference UppercaseString method
Func convertMethod = UppercaseString;
string name = “Dakota”;
// Use delegate instance to call UppercaseString method
Console.WriteLine(convertMethod(name));

string UppercaseString(string inputString)
{
   return inputString.ToUpper();
}
// This code example produces the following output:
//
//    DAKOTA      
You can also use the Func delegate with anonymous methods in C#, as the following example illustrates. (For an introduction to anonymous methods, see Anonymous Methods.)

C#

Copy

Run
Func convert = delegate(string s)
{ return s.ToUpper();};

string name = “Dakota”;
Console.WriteLine(convert(name));

// This code example produces the following output:
//
//    DAKOTA      
You can also assign a lambda expression to a Func delegate, as the following example illustrates. (For an introduction to lambda expressions, see Lambda Expressions and Lambda Expressions.)

C#

Copy

Run
Func convert = s => s.ToUpper();

string name = “Dakota”;
Console.WriteLine(convert(name));

// This code example produces the following output:
//
//    DAKOTA      
The underlying type of a lambda expression is one of the generic Func delegates. This makes it possible to pass a lambda expression as a parameter without explicitly assigning it to a delegate. In particular, because many methods of types in the System.Linq namespace have Func parameters, you can pass these methods a lambda expression without explicitly instantiating a Func delegate.
100
Q

Expression Class or Expression> expr

A

Represents a strongly typed lambda expression as a data structure in the form of an expression tree. This class cannot be inherited.

The following code example demonstrates how to represent a lambda expression both as executable code in the form of a delegate and as data in the form of an expression tree. It also demonstrates how to turn the expression tree back into executable code by using the Compile method.

C#

Copy

// Lambda expression as executable code.
Func deleg = i => i < 5;
// Invoke the delegate and display the output.
Console.WriteLine("deleg(4) = {0}", deleg(4));
// Lambda expression as data in the form of an expression tree.
System.Linq.Expressions.Expression> expr = i => i < 5;
// Compile the expression tree into executable code.
Func deleg2 = expr.Compile();
// Invoke the method and print the output.
Console.WriteLine("deleg2(4) = {0}", deleg2(4));

/* This code produces the following output:

    deleg(4) = True
    deleg2(4) = True
*/

Remarks
When a lambda expression is assigned to a variable, field, or parameter whose type is Expression, the compiler emits instructions to build an expression tree.

Note

A conversion from a lambda expression to type Expression (Expression(Of D) in Visual Basic) exists if a conversion from the lambda expression to a delegate of type D exists. However, the conversion may fail, for example, if the body of the lambda expression is a block. This means that delegates and expression trees behave similarly with regard to overload resolution.

The expression tree is an in-memory data representation of the lambda expression. The expression tree makes the structure of the lambda expression transparent and explicit. You can interact with the data in the expression tree just as you can with any other data structure.

The ability to treat expressions as data structures enables APIs to receive user code in a format that can be inspected, transformed, and processed in a custom manner. For example, the LINQ to SQL data access implementation uses this facility to translate expression trees to Transact-SQL statements that can be evaluated by the database.

Many standard query operators defined in the Queryable class have one or more parameters of type Expression.

The NodeType of an Expression is Lambda.

Use the Lambda(Expression, IEnumerable) or Lambda(Expression, ParameterExpression[]) method to create an Expression object.

101
Q

implement interface events

A

An interface can declare an event. The following example shows how to implement interface events in a class. Basically the rules are the same as when you implement any interface method or property.

To implement interface events in a class
Declare the event in your class and then invoke it in the appropriate areas.

C#

Copy
namespace ImplementInterfaceEvents  
{  
    public interface IDrawingObject  
    {  
        event EventHandler ShapeChanged;  
    }  
    public class MyEventArgs : EventArgs   
    {  
        // class members  
    }  
    public class Shape : IDrawingObject  
    {  
        public event EventHandler ShapeChanged;  
        void ChangeShape()  
        {  
            // Do something here before the event…  
        OnShapeChanged(new MyEventArgs(/*arguments*/));  
            // or do something here after the event.   
        }  
        protected virtual void OnShapeChanged(MyEventArgs e)  
        {  
            ShapeChanged?.Invoke(this, e);  
        }  
    }  

}

102
Q

Implement interface events example

A
public interface IRepo
    {
        Customer Find(int id);
        event EventHandler FailedDatabseRequest;
    }
 [Route("api/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private readonly IRepo _repo;
        private readonly ILogger _logger;
        public TestController(IRepo repo,ILogger logger=null)
        {
            _repo = repo;
            _logger = logger;
            _repo.FailedDatabseRequest += _repo_FailedDatabaseRequest; //assign event handler
        }
    private void _repo_FailedDatabaseRequest(object sender,EventArgs e)
    {
        _logger.LogError("An error occured");
    } }
103
Q

async with return type Task

A

The Task return type is used for an async method that contains a return (C#) statement in which the operand has type TResult.

In the following example, the GetLeisureHours async method contains a return statement that returns an integer. Therefore, the method declaration must specify a return type of Task. The FromResult async method is a placeholder for an operation that returns a string.

C#

Copy
using System;
using System.Linq;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Console.WriteLine(ShowTodaysInfo().Result);
   }

private static async Task ShowTodaysInfo()
{
string ret = $”Today is {DateTime.Today:D}\n” +
“Today’s hours of leisure: “ +
$”{await GetLeisureHours()}”;
return ret;
}

   static async Task GetLeisureHours()  
   {  
       // Task.FromResult is a placeholder for actual work that returns a string.  
       var today = await Task.FromResult(DateTime.Now.DayOfWeek.ToString());  
       // The method then can process the result in some way.  
       int leisureHours;  
       if (today.First() == 'S')  
           leisureHours = 16;  
       else  
           leisureHours = 5;  
       return leisureHours;  
   }  
}
// The example displays output like the following:
//       Today is Wednesday, May 24, 2017
//       Today's hours of leisure: 5
// 
When GetLeisureHours is called from within an await expression in the ShowTodaysInfo method, the await expression retrieves the integer value (the value of leisureHours) that's stored in the task returned by the GetLeisureHours method. For more information about await expressions, see await.

You can better understand how this happens by separating the call to GetLeisureHours from the application of await, as the following code shows. A call to method GetLeisureHours that isn’t immediately awaited returns a Task, as you would expect from the declaration of the method. The task is assigned to the integerTask variable in the example. Because integerTask is a Task, it contains a Result property of type TResult. In this case, TResult represents an integer type. When await is applied to integerTask, the await expression evaluates to the contents of the Result property of integerTask. The value is assigned to the ret variable.

Important

The Result property is a blocking property. If you try to access it before its task is finished, the thread that’s currently active is blocked until the task completes and the value is available. In most cases, you should access the value by using await instead of accessing the property directly.
The previous example retrieved the value of the Result property to block the main thread so that the ShowTodaysInfo method could finish execution before the application ended.

C#

Copy
var integerTask = GetLeisureHours();

// You can do other work that does not rely on integerTask before awaiting.

string ret = $”Today is {DateTime.Today:D}\n” +
“Today’s hours of leisure: “ +
$”{await integerTask}”;

104
Q

Serialization (C#)

A

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How serialization works
This illustration shows the overall process of serialization:

Serialization graphic

The object is serialized to a stream that carries the data. The stream may also have information about the object’s type, such as its version, culture, and assembly name. From that stream, the object can be stored in a database, a file, or memory.

Uses for serialization
Serialization allows the developer to save the state of an object and re-create it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions such as:

Sending the object to a remote application by using a web service
Passing an object from one domain to another
Passing an object through a firewall as a JSON or XML string
Maintaining security or user-specific information across applications

105
Q

JSON serialization

A

JSON serialization
The System.Text.Json namespace contains classes for JavaScript Object Notation (JSON) serialization and deserialization. JSON is an open standard that is commonly used for sharing data across the web.

JSON serialization serializes the public properties of an object into a string, byte array, or stream that conforms to the RFC 8259 JSON specification. To control the way JsonSerializer serializes or deserializes an instance of the class:

Use a JsonSerializerOptions object
Apply attributes from the System.Text.Json.Serialization namespace to classes or properties
Implement custom converters

106
Q

Binary and XML serialization

A

The System.Runtime.Serialization namespace contains classes for binary and XML serialization and deserialization.

Binary serialization uses binary encoding to produce compact serialization for uses such as storage or socket-based network streams. In binary serialization, all members, even members that are read-only, are serialized, and performance is enhanced.

XML serialization serializes the public fields and properties of an object, or the parameters and return values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains classes for serializing and deserializing XML. You apply attributes to classes and class members to control the way the XmlSerializer serializes or deserializes an instance of the class.

107
Q

Making an object serializable

A

For binary or XML serialization, you need:

The object to be serialized
A stream to contain the serialized object
A System.Runtime.Serialization.Formatter instance
Apply the SerializableAttribute attribute to a type to indicate that instances of the type can be serialized. An exception is thrown if you attempt to serialize but the type doesn’t have the SerializableAttribute attribute.

To prevent a field from being serialized, apply the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and the field cannot be meaningfully reconstituted in a different environment, then you may want to make it nonserializable.

If a serialized class contains references to objects of other classes that are marked SerializableAttribute, those objects will also be serialized.

108
Q

Basic and custom serialization

A

Binary and XML serialization can be performed in two ways, basic and custom.

Basic serialization uses the .NET Framework to automatically serialize the object. The only requirement is that the class has the SerializableAttribute attribute applied. The NonSerializedAttribute can be used to keep specific fields from being serialized.

When you use basic serialization, the versioning of objects may create problems. You would use custom serialization when versioning issues are important. Basic serialization is the easiest way to perform serialization, but it does not provide much control over the process.

In custom serialization, you can specify exactly which objects will be serialized and how it will be done. The class must be marked SerializableAttribute and implement the ISerializable interface. If you want your object to be deserialized in a custom manner as well, use a custom constructor.

109
Q

Designer serialization

A

Designer serialization is a special form of serialization that involves the kind of object persistence associated with development tools. Designer serialization is the process of converting an object graph into a source file that can later be used to recover the object graph. A source file can contain code, markup, or even SQL table information.

110
Q

Serialization example:

How to write object data to an XML file (C#)

A

using System;

public class XMLWrite
{
static void Main(string[] args)
{
    WriteXML();
}
    public class Book
    {
        public String title;
    }
    public static void WriteXML()
    {
        Book overview = new Book();
        overview.title = "Serialization Overview";
        System.Xml.Serialization.XmlSerializer writer =
            new System.Xml.Serialization.XmlSerializer(typeof(Book));
        var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "//SerializationOverview.xml";
        System.IO.FileStream file = System.IO.File.Create(path);
    writer.Serialize(file, overview);
    file.Close();
} }

OUTPUT XML FILE:SerializationOverview.xml

Serialization Overview

111
Q

Serialization: How to read object data from an XML file (C#)

A

using System;

public class XMLWrite
{
static void Main(string[] args)
{
    ReadXML();
}
    public class Book
    {
        public String title;
    }
    public static void ReadXML()
    {
        // First write something so that there is something to read ...  
        var b = new Book { title = "Serialization Overview" };
        var writer = new System.Xml.Serialization.XmlSerializer(typeof(Book));
        var wfile = new System.IO.StreamWriter(@"c:\temp\SerializationOverview.xml");
        writer.Serialize(wfile, b);
        wfile.Close();
    // Now we can read the serialized book ...  
    System.Xml.Serialization.XmlSerializer reader =
        new System.Xml.Serialization.XmlSerializer(typeof(Book));
    System.IO.StreamReader file = new System.IO.StreamReader(
        @"c:\temp\SerializationOverview.xml");
    Book overview = (Book)reader.Deserialize(file);
    file.Close();

    Console.WriteLine(overview.title);

} }
112
Q

Serialization Walkthrough: persisting an object using C#

A

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/walkthrough-persisting-an-object-in-visual-studio

113
Q

Optional parameters must be present after all required parameters

A
public TestController(IRepo repo,IOptions connectionStrings, ILogger logger = null)
        {
            _repo = repo;
            _logger = logger;
            _repo.FailedDatabseRequest += _repo_FailedDatabaseRequest;
        }
114
Q

Automapper mappings in asp.net

A

https://cpratt.co/using-automapper-getting-started/

place a class in App_Start folder and call it in Global.asax in Application_Start()

AutoMapperConfig.cs
So, my suggestion, and what I personally do in all my applications, is add an "AutoMapperConfig.cs" file to the "App_Start" folder. Inside that file, add a class such as:

public static class AutoMapperConfig
{
public static void RegisterMappings()
{
AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.Author,
opts => opts.MapFrom(src => src.Author.Name));
}
}
Then, in Global.asax, in the Application_Start() method, add a call to AutoMapperConfig.RegisterMappings().

115
Q

Automapper -The CreateMap Method

one-way mapping

A

All mapping definitions for AutoMapper are created using the CreateMap method. This is actually and generic method that takes two class types in the form of:

AutoMapper.Mapper.CreateMap();
It’s important to note that this is a one-way mapping. For example, if I defined a mapping such as:

AutoMapper.Mapper.CreateMap();
I could map a Book instance to a BookViewModel instance with:

var bookViewModel = AutoMapper.Mapper.Map(book);
But, if I tried to map a BookViewModel instance back to a Book instance:
var book = AutoMapper.Mapper.Map(bookViewModel);
An exception would be raised, as AutoMapper doesn't know how to perform this mapping. We could of course just call CreateMap again with the class types reversed to define this mapping:
116
Q

Automapper for two way mapping.

ReverseMap()

A

We could of course just call CreateMap again with the class types reversed to define this mapping:

AutoMapper.Mapper.CreateMap();
AutoMapper.Mapper.CreateMap();
But, there’s actually an even easier way. If you don’t need to define any custom mapping logic for the reverse mapping, then you can just call ReverseMap() off of CreateMap():

AutoMapper.Mapper.CreateMap().ReverseMap();
With just that, you can then map back and for between Book and BookViewModel.

117
Q

AutoMapper naming convention

A

Since AutoMapper is designed to work generically with any set of classes, it relies on conventions to determine how to map from one object type to another. One of the most basic conventions is that your type members should have the same names. Take for example the following classes:

public class Book
{
    public string Title { get; set; }
}
public class NiceBookViewModel
{
    public string Title { get; set; }
}
public class BadBookViewModel
{
    public string BookTitle { get; set; }
}
If we map from an instance of Book to an instance of NiceBookViewModel, the Title property will be set as we expect. However, if you map from Book to BadBookViewModel, BookTitle will remain null. AutoMapper has no way of knowing that BookTitle should get the value of Title, because the property names are not the same. However, often you won't have total control of property names for one or both classes, so AutoMapper would quickly become useless if it couldn't handle these types of situations
118
Q

Automapper - ForMembers

A

you simply have to tell it that you want the value of one property “projected” onto the other property:

AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.BookTitle,
opts => opts.MapFrom(src => src.Title));
Now, when you map Book to BadBookViewModel, AutoMapper knows that BookTitle should be set with the value of Title.

119
Q

Automapper - convention with the types

A

Another convention AutoMapper employs relates to embedded objects. In the first post in this series, we used the following classes:

public class Author
{
    public string Name { get; set; }
}
public class Book
{
    public string Title { get; set; }
    public Author Author { get; set; }
}
public class BookViewModel
{
    public string Title { get; set; }
    public string Author { get; set; }
}
Even though Book and BookViewModel have properties named Author, the types of those properties do not match, so AutoMapper cannot map Book.Author to BookViewModel.Author because the former is a string while the later is an instance of Author. For embedded objects, AutoMapper's convention is to look for a property on the destination type named in the form of the object property's name plus the property on that object's name, in camel-case. In other words, if we altered our BookViewModel to:
public class BookViewModel
{
    public string Title { get; set; }
    public string AuthorName { get; set; }
}
AutoMapper needs no special configuration and will readily map Book.Author.Name to BookViewModel.AuthorName. That's nice, but again, we can't always control this, so you can always just spell it out for AutoMapper instead:

AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.Author,
opts => opts.MapFrom(src => src.Author.Name));

120
Q

Automapper projections

A

The nice thing about MapFrom is that you can specify more complex logic than simple 1-to-1 mappings. What if we change our Author to the following?

public class Author
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
We now have two properties that we'll still need to map to just one property on our view model. Turns out, this is not complicated at all:

AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.Author,
opts => opts.MapFrom(
src => string.Format(“{0} {1}”,
src.Author.FirstName,
src.Author.LastName)));
I’m using pretty liberal spacing to keep the code sample from wrapping inelegantly here, so the key part to pay attention to is that instead of mapping from src.Author.Name as we were previously, we’re now mapping from a string composed of the FirstName and LastName properties together on Author. Without going too far off the rails here, the parameter to MapFrom is a lambda expression. Lambdas are a fairly advanced topic that’s not appropriate to go into here, but the extremely simplified explanation is that they are essentially a function defined and called all at once. The part to the left of the => is the function’s parameter(s). In this case, that’s an object we’re calling src which AutoMapper will fill in with the the source class instance. The part to the right of the => is the return value of the function, which in this case is our string composition. In other words, you can do pretty much whatever logic you want in MapFrom as long as it can be completed in a single expression and returns the type needed by the receiving property (a string, in this case).

121
Q

AutoMapper complex projections

A

What if we need to map multiple properties on our source class into a embedded object on our destination class? For this example, we’ll use the following classes:

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}
public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}
public class PersonDTO
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}
Here, we have a DTO where all the properties are inline, but our model uses a composition Address class to contain the address-specific properties. Again, MapFrom to the rescue:

AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.Address,
opts => opts.MapFrom(
src => new Address
{
Street = src.Street,
City = src.City,
State = src.State,
ZipCode = src.ZipCode
}));
So, again, the right side of the lambda expression can be any expression that returns a valid type for the destination property. Here, the destination property is an Address instance, so we create a new instance of Address and initialize it with the properties on the source class, PersonDTO.

122
Q

AutoMapper -Nested Mappings

A

Now, what if in our previous example, instead of all the address-specific properties being inline on our PersonDTO class, they were contained in an embedded object like our Person class? If the types of both properties were Address and the names of both properties were Address, AutoMapper’s convention-based mapping would kick in and no specific configuration would be needed at all, but what if we had an AddressDTO class as well?

public class AddressDTO
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
}
public class PersonDTO
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public AddressDTO Address { get; set; }
}    
If we try to map PersonDTO to Person, we'll get an exception. We created a mapping from PersonDTO to Person, but we haven't told AutoMapper how to map between AddressDTO and Address.

When AutoMapper comes across the same property name on the source and destination classes with different types, it will helpfully automatically attempt to map from one type to the other. This means that you can map entire object hierarchies in a single Map call, which is extremely powerful. The only caveat, is that each type in the hierarchy must have a mapping defined. So, to fix our exception, we just have to add:

AutoMapper.Mapper.CreateMap();
AutoMapper.Mapper.CreateMap();
Wrap-up
As you should see by now, AutoMapper is both simple and powerful. Thanks to its convention-based mapping, many times, even in complex object graphs you don’t have to create any custom configuration at all other than the a simple call to CreateMap() for each distinct type-mapping in play. And, in more complex scenarios, where things don’t quite line up with the conventions, MapFrom makes quick work of defining custom mapping logic.

123
Q

AutoMapper - Mapping to a New Instance

A

mapping to a New Instance
As you’ve seen in the previous posts in this series, once your mappings are defined, it’s a simple matter to actually map from one object to another:

Given:

AutoMapper.Mapper.CreateMap();

Then:
var destinationObject = AutoMapper.Mapper.Map(sourceObject);
124
Q

AutoMapper - Mapping to an exiting instance

A

There’s a few special case scenarios to consider, though. First, the call above creates a brand new instance of DestinationClass, but often, especially in when doing something like an update of an existing object, you don’t want a new instance. Instead, you want to map the data onto an existing instance. Map takes a second parameter just for this purpose:

AutoMapper.Mapper.Map(sourceObject, destinationObject);
There’s two things to note here. First, we don’t have to specify the type to map to for the generic Map call. This is because, now, we’re passing the destination object instance, so the destination type can be determined by the type of that object. Second, we’re not storing the result of this call in a variable. This is because the destination object is mapped to in place and we’re not creating any new instances.

125
Q

AutoMapper -Mapping Collections - be cautious when doing update and not creating a new object

A

One very nice aspect of AutoMapper is that it has native support of working with collections of objects. You don’t need to create a custom mapping for something like a List: as long as you’ve defined a mapping for DestinationClass, AutoMapper will be able to automatically map a collection of that type. The only thing you need to do is specify that appropriate collection type for the Map call:

var destinationList = AutoMapper.Mapper.Map>(sourceList);

This works for all collection types and interfaces: List, ICollection, IEnumerable, etc. There’s one very important gotcha here, though: if you try to employ the existing instance mapping:

AutoMapper.Mapper.Map(sourceList, destinationList);
You might be surprised to find that you’ve actually clobbered your destinationList. This is because AutoMapper is actually mapping the collection, not the objects in that collection, individually. This gets particularly nasty when you consider object hierarchies. Take for example:

public class Pet
{
    public string Name { get; set; }
    public string Breed { get; set; }
}
public class Person
{
    public List Pets { get; set; }
}
public class PetDTO
{
    public string Name { get; set; }
    public string Breed { get; set; }
}
public class PersonDTO
{
    public List Pets { get; set; }
}
So, here we have a Person class with a collection of Pets, and a DTO for each type. If we then created a form to update just the Name property for each pet, we'd end up with an object graph like:

{
Pets: [
{ Name : “Sparky”, Breed : null },
{ Name : “Felix”, Breed : null },
{ Name : “Cujo”, Breed : null }
]
}
Since Breed was not posted it’s value is null for each PetDTO instance. Now, if we try to update Person with PersonDTO:

AutoMapper.Mapper.Map(person, personDTO);
We’ve actually replaced person.Pets with an entirely new collection of Pets, where each Breed property is now null. It’s easy to see how you could clobber your data in this way. The solution is unfortunately a little arduous. First, we need to use AutoMapper’s Ignore() method on our Pets collection:

AutoMapper.Mapper.CreateMap()
.ForMember(dest => dest.Pets,
opts => opts.Ignore());
This tells AutoMapper that when it maps from a PersonDTO to a Person it should not map the Pets collection. This means we’ll now have to handle this manually.

AutoMapper.Mapper.Map(person, personDTO);
for (int i = 0; i < person.Pets.Count(); i++)
{
AutoMapper.Mapper.Map(person.Pets[i], personDTO.Pets[i]);
}
What we’re doing here is looping through the list of pets on person and mapping each Pet individually. This assumes that your lists are the same on both sides though, i.e. you did not reorder the items and you did not allow the creation or removal of items in your form. To cover variance in ordering, you would want to rely on some sort of identifying property such as:

var pet = person.Pets[i];
var updatedPet = personDTO.Pets.Single(m => m.Id == pet.Id);
AutoMapper.Mapper.Map(pet, updatedPet);
Adds and removals are a bit more complicated:

var updatedPets = new List();
foreach (var pet in personDTO.Pets)
{
var existingPet = person.Pets.SingleOrDefault(m => m.Id == pet.Id);
// No existing pet with this id, so add a new one
if (existingPet == null)
{
updatedPets.Add(AutoMapper.Mapper.Map(pet));
}
// Existing pet found, so map to existing instance
else
{
AutoMapper.Mapper.Map(existingPet, pet);
updatedPets.Add(existingPet);
}
}
// Set pets to updated list (any removed items drop out naturally)
person.Pets = updatedPets;
This is probably the biggest pain point of working with AutoMapper, but as long as you keep in mind that you need to manually map list items when doing an update, it’s manageable.

126
Q

creating an anonymous object

A

var v = new { Amount = 108, Message = “Hello” };

127
Q

Creating Generic Class type

A

using System.Collections.Generic;

namespace org.cchmc.pho.core.Models
{
    public class SearchResults where T : class
    {
        public int ResultCount { get; set; }
        public List Results { get; set; }
    }
}
128
Q

Handling mulitple tables returned from stored procedure using dataset and generic class

A

creating a generic class:
using System.Collections.Generic;

namespace org.cchmc.pho.core.Models
{
    public class SearchResults where T : class
    {
        public int ResultCount { get; set; }
        public List Results { get; set; }
    }
}

public async Task> ListActivePatient(int userId, int? staffID, int? popmeasureID, bool? watch, bool? chronic, string conditionIDs, string namesearch, string sortcolumn, string sortdirection, int? pagenumber, int? rowsperpage)
{
DataTable patientListDataTable;
DataTable patientListCountDataTable;
DataSet dataSet = new DataSet();
SearchResults searchResults = new SearchResults();
searchResults.Results = new List();

            using (SqlConnection sqlConnection = new SqlConnection(_connectionStrings.PHODB))
            {
                using (SqlCommand sqlCommand = new SqlCommand("spGetPracticePatients", sqlConnection))
                {
                    sqlCommand.CommandType = System.Data.CommandType.StoredProcedure;          
                    sqlCommand.Parameters.Add("@UserId", SqlDbType.Int).Value = userId;                                      
                    sqlCommand.Parameters.Add("@PCP_StaffID", SqlDbType.Int).Value = staffID; 
                    sqlCommand.Parameters.Add("@PopMeasureID", SqlDbType.Int).Value = popmeasureID; 
                    sqlCommand.Parameters.Add("@Watch", SqlDbType.Bit).Value = watch; 
                    sqlCommand.Parameters.Add("@Chronic", SqlDbType.Bit).Value = chronic; 
                    sqlCommand.Parameters.Add("@ConditionIDs", SqlDbType.VarChar, 50).Value = conditionIDs; 
                    sqlCommand.Parameters.Add("@NameSearch", SqlDbType.VarChar, 100).Value = namesearch;
                    sqlCommand.Parameters.Add("@SortColumn", SqlDbType.VarChar, 50).Value = sortcolumn;
                    sqlCommand.Parameters.Add("@SortDirection", SqlDbType.VarChar, 100).Value = sortdirection;
                    sqlCommand.Parameters.Add("@PageNumber", SqlDbType.VarChar, 50).Value = pagenumber;
                    sqlCommand.Parameters.Add("@RowspPage", SqlDbType.VarChar, 100).Value = rowsperpage;
                await sqlConnection.OpenAsync();
                    using (SqlDataAdapter da = new SqlDataAdapter(sqlCommand))
                    {
                        da.Fill(dataSet,"PatientsDataset");
                        patientListDataTable = dataSet.Tables[0];
                        patientListCountDataTable = dataSet.Tables[1];                        
                    List patientConditions = await GetPatientConditionsAll();                     
                        foreach(DataRow dr in patientListDataTable.Rows)
                        {
                            var patient = new Patient()
                            {
                                PatientId = Convert.ToInt32(dr["PatientId"]),
                                FirstName = dr["FirstName"].ToString(),
                                LastName = dr["LastName"].ToString(),
                                PCP_StaffID = Convert.ToInt32(dr["PCP_StaffID"]),
                                PracticeID = Convert.ToInt32(dr["PracticeID"]),
                                DOB = (dr["DOB"] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr["DOB"].ToString())),
                                LastEDVisit = (dr["LastEDVisit"] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr["LastEDVisit"].ToString())),
                                Chronic = bool.Parse(dr["Chronic"].ToString()),
                                WatchFlag = bool.Parse(dr["WatchFlag"].ToString()),
                                Conditions = new List(),
                                ActiveStatus = bool.Parse(dr["ActiveStatus"].ToString()),
                                PotentiallyActiveStatus= bool.Parse(dr["PotentiallyActive"].ToString()),
                            };
                        if (!string.IsNullOrWhiteSpace(dr["ConditionIDs"].ToString()))
                        {
                            foreach (int conditionId in dr["ConditionIDs"].ToString().Split(',').Select(p => int.Parse(p)))
                            {
                                if (patientConditions.Any(p => p.ID == conditionId))
                                    patient.Conditions.Add(patientConditions.First(p => p.ID == conditionId));
                                else
                                    _logger.LogError("An unmapped patient condition id was returned by the database ");
                            }
                        }
                        searchResults.Results.Add(patient);
                    }  
                    foreach(DataRow dr in patientListCountDataTable.Rows)
                    {
                        searchResults.ResultCount = Convert.ToInt32(dr["TotalRecords"].ToString());
                    }
                }
            }

            return searchResults;
        }   
    }
129
Q

Mapper for Generic class:

A

Generic Model:
using System.Collections.Generic;

namespace org.cchmc.pho.core.Models
{
    public class SearchResults where T : class
    {
        public int ResultCount { get; set; }
        public List Results { get; set; }
    }
}

Generic View Model:
using System.Collections.Generic;

namespace org.cchmc.pho.api.ViewModels
{
    public class SearchResultsViewModel  where T:class
    {
        public int ResultCount { get; set; }
        public List Results { get; set; }
    }
}
--
Mapping:
using org.cchmc.pho.api.ViewModels;
using org.cchmc.pho.core.Models;
using AutoMapper;
namespace org.cchmc.pho.api.Mappings
{
    public class SearchResultsMappings:Profile
    {
        public SearchResultsMappings()
        {
            CreateMap(typeof(SearchResults<>), typeof(SearchResultsViewModel<>));
        }
} } --- using in the controller: var data = await _patient.ListActivePatient(int.Parse(_DEFAULT_USER.ToString()), staffID, popmeasureID, watch, chronic, conditionIDs, namesearch,sortcolumn,sortdirection,pagenumber,rowsPerPage);
            var result = _mapper.Map>(data);
130
Q

ExecuteScalar()
ExecuteReader()
ExecuteNonQuery()

A

ExecuteScalar() only returns the value from the first column of the first row of your query.
ExecuteReader() returns an object that can iterate over the entire result set.
ExecuteNonQuery() does not return data at all: only the number of rows affected by an insert, update, or delete.

http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery%28v=vs.110%29.aspx[^]
http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executereader%28v=vs.110%29.aspx[^]
http: //msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar%28v=vs.110%29.aspx[^]

131
Q

Once you go async, all of your callers SHOULD be async

A

https://github.com/davidfowl/
AspNetCoreDiagnosticScenarios/blob/master/
AsyncGuidance.md

Once you go async, all of your callers SHOULD be async, since efforts to be async amount to nothing unless the entire callstack is async. In many cases, being partially async can be worse than being entirely synchronous. Therefore it is best to go all in, and make everything async at once.

❌ BAD This example uses the Task.Result and as a result blocks the current thread to wait for the result. This is an example of sync over async.

public int DoSomethingAsync()
{
    var result = CallDependencyAsync().Result;
    return result + 1;
}
✅ GOOD This example uses the await keyword to get the result from CallDependencyAsync.
public async Task DoSomethingAsync()
{
    var result = await CallDependencyAsync();
    return result + 1;
}
132
Q

Use of async void in ASP.NET Core applications is ALWAYS bad. Avoid it, never do it.

A

Use of async void in ASP.NET Core applications is ALWAYS bad. Avoid it, never do it. Typically, it’s used when developers are trying to implement fire and forget patterns triggered by a controller action. Async void methods will crash the process if an exception is thrown. We’ll look at more of the patterns that cause developers to do this in ASP.NET Core applications but here’s a simple example:

❌ BAD Async void methods can’t be tracked and therefore unhandled exceptions can result in application crashes.

public class MyController : Controller
{
    [HttpPost("/start")]
    public IActionResult Post()
    {
        BackgroundOperationAsync();
        return Accepted();
    }
    public async void BackgroundOperationAsync()
    {
        var result = await CallDependencyAsync();
        DoSomething(result);
    }
}
✅ GOOD Task-returning methods are better since unhandled exceptions trigger the TaskScheduler.UnobservedTaskException.
public class MyController : Controller
{
    [HttpPost("/start")]
    public IActionResult Post()
    {
        Task.Run(BackgroundOperationAsync);
        return Accepted();
    }
public async Task BackgroundOperationAsync()
{
    var result = await CallDependencyAsync();
    DoSomething(result);
} }
133
Q

Prefer Task.FromResult over Task.Run for pre-computed or trivially computed data

A

For pre-computed results, there’s no need to call Task.Run, that will end up queuing a work item to the thread pool that will immediately complete with the pre-computed value. Instead, use Task.FromResult, to create a task wrapping already computed data.

❌ BAD This example wastes a thread-pool thread to return a trivially computed value.

public class MyLibrary
{
   public Task AddAsync(int a, int b)
   {
       return Task.Run(() => a + b);
   }
}
✅ GOOD This example uses Task.FromResult to return the trivially computed value. It does not use any extra threads as a result.
public class MyLibrary
{
   public Task AddAsync(int a, int b)
   {
       return Task.FromResult(a + b);
   }
}
💡NOTE: Using Task.FromResult will result in a Task allocation. Using ValueTask can completely remove that allocation.

✅ GOOD This example uses a ValueTask to return the trivially computed value. It does not use any extra threads as a result. It also does not allocate an object on the managed heap.

public class MyLibrary
{
   public ValueTask AddAsync(int a, int b)
   {
       return new ValueTask(a + b);
   }
}
134
Q

AspNetCoreDiagnosticScenarios

A

https://github.com/davidfowl/
AspNetCoreDiagnosticScenarios/
blob/master/AspNetCoreGuidance.md

135
Q

C# - Func Delegate

A

C# includes built-in generic delegate types Func and Action, so that you don’t need to define custom delegates manually in most cases.

Func is a generic delegate included in the System namespace. It has zero or more input parameters and one out parameter. The last parameter is considered as an out parameter.

The Func delegate that takes one input parameter and one out parameter is defined in the System namespace, as shown below:

namespace System
{    
    public delegate TResult Func(T arg);
}
The last parameter in the angle brackets <> is considered as the return type and remaining parameters are considered as input parameter types, as shown in the following figure.

A Func delegate with two input parameters and one out parameters will be represent as below.
The following Func delegate takes two input parameters of int type and returns a value of int type:

Func sum;
You can assign any method to the above func delegate that takes two int parameters and returns an int value.

Example: Func
class Program
{
    static int Sum(int x, int y)
    {
        return x + y;
    }
static void Main(string[] args)
{
    Func add = Sum;

    int result = add(10, 10);
        Console.WriteLine(result); 
    }
}
Output:
20
A Func delegate type can include 0 to 16 input parameters of different types. However, it must include one out parameter for result. For example, the following func delegate doesn't have any input parameter, it includes only a out parameter.

Example: Func with Zero Input Parameter
Func getRandomNumber;

136
Q

C# Func with an Anonymous Method

A

You can assign an anonymous method to the Func delegate by using the delegate keyword.

Example: Func with Anonymous Method
Func getRandomNumber = delegate()
                            {
                                Random rnd = new Random();
                                return rnd.Next(1, 100);
                            };
137
Q

Func with Lambda Expression

A

A Func delegate can also be used with a lambda expression, as shown below:

Example: Func with lambda expression
Func getRandomNumber = () => new Random().Next(1, 100);

//Or

Func Sum = (x, y) => x + y;

138
Q

Func delegates points to remember

A

Points to Remember :
Func is built-in delegate type.
Func delegate type must return a value.
Func delegate type can have zero to 16 input parameters.
Func delegate does not allow ref and out parameters.
Func delegate type can be used with an anonymous method or lambda expression.

139
Q

C# - Action Delegate

A

Action is a delegate type defined in the System namespace. An Action type delegate is the same as Func delegate except that the Action delegate doesn’t return a value. In other words, an Action delegate can be used with a method that has a void return type.

For example, the following delegate prints an int value.

Example: C# Delegate
public delegate void Print(int val);

static void ConsolePrint(int i)
{
Console.WriteLine(i);
}

static void Main(string[] args)
{           
    Print prnt = ConsolePrint;
    prnt(10);
}
Output:
10

You can use an Action delegate instead of defining the above Print delegate, for example:

Example: Action delegate
static void ConsolePrint(int i)
{
    Console.WriteLine(i);
}
static void Main(string[] args)
{
    Action printActionDel = ConsolePrint;
    printActionDel(10);
}
140
Q

Action delegate using the new keyword

A

You can initialize an Action delegate using the new keyword or by directly assigning a method:

Action printActionDel = ConsolePrint;

//Or

Action printActionDel = new Action(ConsolePrint);
An Action delegate can take up to 16 input parameters of different types.
141
Q

Anonymous method with Action delegate

A

An Anonymous method can also be assigned to an Action delegate, for example:

Example: Anonymous method with Action delegate
static void Main(string[] args)
{
Action printActionDel = delegate(int i)
{
Console.WriteLine(i);
};

printActionDel(10); }
142
Q

Lambda expression with Action delegate

A

A Lambda expression also can be used with an Action delegate:

Example: Lambda expression with Action delegate
static void Main(string[] args)
{

Action printActionDel = i => Console.WriteLine(i);

printActionDel(10); } Thus, you can use any method that doesn't return a value with Action delegate types.
143
Q

Advantages of Action and Func Delegates

A

Easy and quick to define delegates.
Makes code short.
Compatible type throughout the application.

144
Q

Action Delegates points

A

Points to Remember :
Action delegate is same as func delegate except that it does not return anything. Return type must be void.
Action delegate can have 0 to 16 input parameters.
Action delegate can be used with anonymous methods or lambda expressions.

145
Q

C# - Predicate Delegate

A

A predicate is also a delegate like Func and Action delegates. It represents a method that contains a set of criteria and checks whether the passed parameter meets those criteria or not. A predicate delegate methods must take one input parameter and return a boolean - true or false.

The Predicate delegate is defined in the System namespace as shown below:

Predicate signature: public delegate bool Predicate(T obj);

Same as other delegate types, Predicate can also be used with any method, anonymous method or lambda expression.

Example: Predicate delegate
static bool IsUpperCase(string str)
{
    return str.Equals(str.ToUpper());
}

static void Main(string[] args)
{
Predicate isUpper = IsUpperCase;

bool result = isUpper("hello world!!");

Console.WriteLine(result); }
146
Q

Predicate delegate with anonymous method

A

An anonymous method can also be assigned to a Predicate delegate type as shown below.

static void Main(string[] args)
{
Predicate isUpper = delegate(string s) { return s.Equals(s.ToUpper());};
bool result = isUpper(“hello world!!”);
}

147
Q

Predicate delegate with lambda expression

A

A lambda expression can also be assigned to a Predicate delegate type as shown below.

static void Main(string[] args)
{
Predicate isUpper = s => s.Equals(s.ToUpper());
bool result = isUpper(“hello world!!”);
}

148
Q

Predicate delegate - Points to remember

A

Predicate delegate takes one input parameter and boolean return type.
Anonymous method and Lambda expression can be assigned to the predicate delegate.

149
Q

IEnumerable vs IQueryable

A

https://medium.com/@mohamedabdeen/iqueryable-vs-ienumerable-in-net-92a15a803da3

The first important point to remember is IQueryable interface inherits from IEnumerable, so whatever IEnumerable can do, IQueryable can also do.

IEnumerable: IEnumerable is best suitable for working with in-memory collection (or local queries). IEnumerable doesn’t move between items, it is forward only collection.

Consider the below simple code which uses IEnumerable with entity framework. It’s using a Where filter to get records whose EmpId is 2.
EmpEntities ent = new EmpEntities();
IEnumerable emp = ent.Employees;
IEnumerable temp = emp.Where(x => x.Empid == 2).ToList();
This where filter is executed on the client side where the IEnumerable code is. In other words all the data is fetched from the database and then at the client its scans and gets the record with EmpId is 2.

IQueryable: IQueryable best suits for remote data source, like a database or web service (or remote queries). IQueryable is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).

But now see the below code we have changed IEnumerable to IQueryable. It creates a SQL Query at the server side and only necessary data is sent to the client side.
EmpEntities ent = new EmpEntities();
IQueryable emp = ent.Employees;
IQueryable temp = emp.Where(x => x.Empid == 2).ToList();

So the difference between IQueryable and IEnumerable is about where the filter logic is executed. One executes on the client side and the other executes on the database. So if you working with only in-memory data collection IEnumerable is a good choice but if you want to query data collection which is connected with database `IQueryable is a better choice as it reduces network traffic and uses the power of SQL language.

150
Q

Async void methods have different error-handling semantic

A

https://docs.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

sync void methods have different error-handling semantics. When an exception is thrown out of an async Task or async Task method, that exception is captured and placed on the Task object. With async void methods, there is no Task object, so any exceptions thrown out of an async void method will be raised directly on the SynchronizationContext that was active when the async void method started. Figure 2 illustrates that exceptions thrown from async void methods can’t be caught naturally.

private async void ThrowExceptionAsync()
{
  throw new InvalidOperationException();
}
public void AsyncVoidExceptions_CannotBeCaughtByCatch()
{
  try
  {
    ThrowExceptionAsync();
  }
  catch (Exception)
  {
    // The exception is never caught here!
    throw;
  }
}
151
Q

Async void methods are difficult to test

A

Async void methods are difficult to test. Because of the differences in error handling and composing, it’s difficult to write unit tests that call async void methods. The MSTest asynchronous testing support only works for async methods returning Task or Task. It’s possible to install a SynchronizationContext that detects when all async void methods have completed and collects any exceptions, but it’s much easier to just make the async void methods return Task instead.

152
Q

Configure Context - For later not clear - this is mainly when slowly converting sync to async code

A

Earlier in this article, I briefly explained how the “context” is captured by default when an incomplete Task is awaited, and that this captured context is used to resume the async method. The example in Figure 3 shows how resuming on the context clashes with synchronous blocking to cause a deadlock. This context behavior can also cause another problem—one of performance. As asynchronous GUI applications grow larger, you might find many small parts of async methods all using the GUI thread as their context. This can cause sluggishness as responsiveness suffers from “thousands of paper cuts.”

To mitigate this, await the result of ConfigureAwait whenever you can. The following code snippet illustrates the default context behavior and the use of ConfigureAwait:

XML

Copy
async Task MyMethodAsync()
{
// Code here runs in the original context.
await Task.Delay(1000);
// Code here runs in the original context.
await Task.Delay(1000).ConfigureAwait(
continueOnCapturedContext: false);
// Code here runs without the original
// context (in this case, on the thread pool).
}

153
Q

Thread vs Task

A

Thread is a lower-level concept: if you’re directly starting a thread, you know it will be a separate thread, rather than executing on the thread pool etc.

Task is more than just an abstraction of “where to run some code” though - it’s really just “the promise of a result in the future”. So as some different examples:

Task.Delay doesn’t need any actual CPU time; it’s just like setting a timer to go off in the future
A task returned by WebClient.DownloadStringTaskAsync won’t take much CPU time locally; it’s representing a result which is likely to spend most of its time in network latency or remote work (at the web server)
A task returned by Task.Run() really is saying “I want you to execute this code separately”; the exact thread on which that code executes depends on a number of factors.
Note that the Task abstraction is pivotal to the async support in C# 5.

In general, I’d recommend that you use the higher level abstraction wherever you can: in modern C# code you should rarely need to explicitly start your own thread.

154
Q

Automapper implementation

A

install the below npm packages


get data

using Microsoft.EntityFrameworkCore;
using org.cchmc.oppe.core.DbContextClass;
using org.cchmc.oppe.core.Interface;
using org.cchmc.oppe.core.Models;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace org.cchmc.oppe.core.DataAccessLayer
{
    public class Measure : IMeasure
    {      
        private readonly OPPEContext _oppeContext;
        public Measure(OPPEContext oppeContext)
        {
            _oppeContext = oppeContext;
        }
    public async Task> ListActiveMeasures(int divisionId)
    {
        List result =await _oppeContext.OdsDivMeas.AsNoTracking()
            .Include(x => x.Div)
            .Include(x => x.Meas)
            .Where(x => x.DivId == divisionId &amp;&amp; x.ActiveFlag == "1")
             .ToListAsync();

        return result;
    }
} }
----
Mapper class:
namespace org.cchmc.oppe.api.Mappings
{
    public class MeasureMappings:Profile
    {
        public MeasureMappings()
        {
            CreateMap()
                .ForMember(dest => dest.DivisionId, action => action.MapFrom(source => source.Div.DimDivIdSk))
                .ForMember(dest => dest.DivisionName, action => action.MapFrom(source => source.Div.DimDivDivisionName))
                .ForMember(dest => dest.MeasureId, action => action.MapFrom(source => source.Meas.DimMeasureIdSk))
                .ForMember(dest => dest.MeasureName, action => action.MapFrom(source => source.Meas.MeasureName));
        }
    }
}
------------
VIEW model class:
  public class DivisionMeasuresViewModel
    {
        public int MeasureId { get; set; }
        public int DivisionId { get; set; }
        public string MeasureName { get; set; }
        public string DivisionName { get; set; }
    }
---------
in the controller action method 
  [HttpGet]
        [SwaggerResponse(200,type:
typeof(List))]
        [SwaggerResponse(400,type:typeof(string))]
        [SwaggerResponse(500,type:typeof(string))]
        public async Task  ListActiveMeasures(int divisionId)
        {
            try
            {
                var data = await _measure.ListActiveMeasures(divisionId);
                var result = _mapper.Map>(data);
                if(result.Count==0)
                {
                    return BadRequest("Measures does not exist for the specified division");
                }
                return Ok(result);
            }
            catch(Exception exception)
            {
                //TODO: include logging
                return StatusCode(500, "An error occurred");
            }
        }
155
Q

Tuple value type

A

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples#code-try-5

Available in C# 7.0 and later, the tuples feature provides concise syntax to group multiple data elements in a lightweight data structure. The following example shows how you can declare a tuple variable, initialize it, and access its data members:

(double, int) t1 = (4.5, 3);
Console.WriteLine($"Tuple with elements {t1.Item1} and {t1.Item2}.");
// Output:
// Tuple with elements 4.5 and 3.
(double Sum, int Count) t2 = (4.5, 3);
Console.WriteLine($"Sum of {t2.Count} elements is {t2.Sum}.");
// Output:
// Sum of 3 elements is 4.5.

we could compare two tuples

156
Q

Use cases of tuples - value type System.ValueTuple

One of the most common use cases of tuples is as a method return type. That is, instead of defining out method parameters, you can group method results in a tuple return type, as the following example shows:

if the elements are strongly coupled use model or view model. if not use tuples instead of out parameter

A

One of the most common use cases of tuples is as a method return type. That is, instead of defining out method parameters, you can group method results in a tuple return type, as the following example shows:

C#

Copy

Run
var xs = new[] { 4, 7, 9 };
var limits = FindMinMax(xs);
Console.WriteLine($"Limits of [{string.Join(" ", xs)}] are {limits.min} and {limits.max}");
// Output:
// Limits of [4 7 9] are 4 and 9
var ys = new[] { -9, 0, 67, 100 };
var (minimum, maximum) = FindMinMax(ys);
Console.WriteLine($"Limits of [{string.Join(" ", ys)}] are {minimum} and {maximum}");
// Output:
// Limits of [-9 0 67 100] are -9 and 100
(int min, int max) FindMinMax(int[] input)
{
    if (input is null || input.Length == 0)
    {
        throw new ArgumentException("Cannot find minimum and maximum of a null or empty array.");
    }
    var min = int.MaxValue;
    var max = int.MinValue;
    foreach (var i in input)
    {
        if (i < min)
        {
            min = i;
        }
        if (i > max)
        {
            max = i;
        }
    }
    return (min, max);
}
As the preceding example shows, you can work with the returned tuple instance directly or deconstruct it in separate variables.

You can also use tuple types instead of anonymous types; for example, in LINQ queries. For more information, see Choosing between anonymous and tuple types.

Typically, you use tuples to group loosely related data elements. That is usually useful within private and internal utility methods. In the case of public API, consider defining a class or a structure type.

157
Q

Tuples vs System.Tuple

A

C# tuples, which are backed by System.ValueTuple types, are different from tuples that are represented by System.Tuple types. The main differences are as follows:

ValueTuple types are value types. Tuple types are reference types.
ValueTuple types are mutable. Tuple types are immutable.
Data members of ValueTuple types are fields. Data members of Tuple types are properties.

158
Q

Tuple Class - Reference type

A

Provides static methods for creating tuple objects.

var primes = Tuple.Create(2, 3, 5, 7, 11, 13, 17, 19);
Console.WriteLine(“Prime numbers less than 20: “ +
“{0}, {1}, {2}, {3}, {4}, {5}, {6}, and {7}”,
primes.Item1, primes.Item2, primes.Item3,
primes.Item4, primes.Item5, primes.Item6,
primes.Item7, primes.Rest.Item1);

159
Q

Tuples are commonly used in four ways: Reference types

A

https://docs.microsoft.com/en-us/dotnet/api/system.tuple?view=netcore-3.1

tuple is a data structure that has a specific number and sequence of elements. An example of a tuple is a data structure with three elements (known as a 3-tuple or triple) that is used to store an identifier such as a person’s name in the first element, a year in the second element, and the person’s income for that year in the third element. The .NET Framework directly supports tuples with one to seven elements. In addition, you can create tuples of eight or more elements by nesting tuple objects in the Rest property of a Tuple object.

Tuples are commonly used in four ways:

To represent a single set of data. For example, a tuple can represent a database record, and its components can represent individual fields of the record.

To provide easy access to, and manipulation of, a data set.

To return multiple values from a method without using out parameters (in C#) or ByRef parameters (in Visual Basic).

To pass multiple values to a method through a single parameter. For example, the Thread.Start(Object) method has a single parameter that lets you supply one value to the method that the thread executes at startup time. If you supply a Tuple object as the method argument, you can supply the thread’s startup routine with three items of data.

The Tuple class does not itself represent a tuple. Instead, it is a class that provides static methods for creating instances of the tuple types that are supported by the .NET Framework. It provides helper methods that you can call to instantiate tuple objects without having to explicitly specify the type of each tuple component.

var population = new Tuple(
“New York”, 7891957, 7781984,
7894862, 7071639, 7322564, 8008278);
// Display the first and last elements.
Console.WriteLine(“Population of {0} in 2000: {1:N0}”,
population.Item1, population.Item7);
// The example displays the following output:
// Population of New York in 2000: 8,008,278

160
Q

using General classes for similar pattern of return types

A
namespace org.cchmc.pho.core.Models
{
    public class SearchResults where T : class
    {
        public int ResultCount { get; set; }
        public List Results { get; set; }
    }
}
  SearchResults searchResults = new SearchResults();
            searchResults.Results = new List();
Mapping:
namespace org.cchmc.pho.api.Mappings
{
    public class SearchResultsMappings:Profile
    {
        public SearchResultsMappings()
        {
            CreateMap(typeof(SearchResults<>), typeof(SearchResultsViewModel<>));
        }
}
161
Q

skipping first row of a dataset if it has column names; first we convert it to ienumerable of rows and them skip

A

foreach (DataRow datarow in measureDataTable.Rows.Cast().Skip(1))
{
measureDataRow = new MeasureDataRow
{
DivMeasId = odsDivMeas.DivMeasId,
TimePeriodId=timeperiod.TimePeriodId,
UploadedBy=163

                };
                measureDataLoadList = new List();
            foreach (var (value, index) in measureColumns.Select((value, index) => (value, index)))
            {
                measureDataLoadRow = new MeasureDataLoad
                {
                    MeasureColumnId = value.MeasureColumnId,
                    MeasureColumnValue = datarow[index].ToString()
                };
                measureDataLoadList.Add(measureDataLoadRow);
            }
            measureDataRow.MeasureDataLoad = measureDataLoadList;
            measureDataRowList.Add(measureDataRow);

        }
162
Q

get the value and index with foreach using tuples

A

https://kodify.net/csharp/loop/foreach-index/
foreach (var (value, index) in measureColumns.Select((value, index) => (value, index)))
{
measureDataLoadRow = new MeasureDataLoad
{
MeasureColumnId = value.MeasureColumnId,
MeasureColumnValue = datarow[index].ToString()
};
measureDataLoadList.Add(measureDataLoadRow);
}

163
Q

Unicode - UTF-8

A

https: //www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/
https: //medium.com/@apiltamang/unicode-utf-8-and-ascii-encodings-made-easy-5bfbe3a1c45a

Every platonic letter in every alphabet is assigned a magic number by the Unicode consortium which is written like this: U+0639. This magic number is called a code point. The U+ means “Unicode” and the numbers are hexadecimal. U+0639 is the Arabic letter Ain. The English letter A would be U+0041.

Hello

which, in Unicode, corresponds to these five code points:

U+0048 U+0065 U+006C U+006C U+006F.

UTF-8 was another system for storing your string of Unicode code points, those magic U+ numbers, in memory using 8 bit bytes. In UTF-8, every code point from 0-127 is stored in a single byte. Only code points 128 and above are stored using 2, 3, in fact, up to 6 bytes.

164
Q

The Single Most Important Fact About Encodings

If you completely forget everything I just explained, please remember one extremely important fact. It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that “plain” text is ASCII.

A

The Single Most Important Fact About Encodings

If you completely forget everything I just explained, please remember one extremely important fact. It does not make sense to have a string without knowing what encoding it uses. You can no longer stick your head in the sand and pretend that “plain” text is ASCII.

There Ain’t No Such Thing As Plain Text.

If you have a string, in memory, in a file, or in an email message, you have to know what encoding it is in or you cannot interpret it or display it to users correctly.

Almost every stupid “my website looks like gibberish” or “she can’t read my emails when I use accents” problem comes down to one naive programmer who didn’t understand the simple fact that if you don’t tell me whether a particular string is encoded using UTF-8 or ASCII or ISO 8859-1 (Latin 1) or Windows 1252 (Western European), you simply cannot display it correctly or even figure out where it ends. There are over a hundred encodings and above code point 127, all bets are off.

165
Q

Content-Type: text/plain; charset=”UTF-8”

A

ow do we preserve this information about what encoding a string uses? Well, there are standard ways to do this. For an email message, you are expected to have a string in the header of the form

Content-Type: text/plain; charset=”UTF-8”

For a web page, the original idea was that the web server would return a similar Content-Type http header along with the web page itself — not in the HTML itself, but as one of the response headers that are sent before the HTML page.

This causes problems. Suppose you have a big web server with lots of sites and hundreds of pages contributed by lots of people in lots of different languages and all using whatever encoding their copy of Microsoft FrontPage saw fit to generate. The web server itself wouldn’t really know what encoding each file was written in, so it couldn’t send the Content-Type header.

It would be convenient if you could put the Content-Type of the HTML file right in the HTML file itself, using some kind of special tag. Of course this drove purists crazy… how can you read the HTML file until you know what encoding it’s in?! Luckily, almost every encoding in common use does the same thing with characters between 32 and 127, so you can always get this far on the HTML page without starting to use funny letters:

But that meta tag really has to be the very first thing in the section because as soon as the web browser sees this tag it’s going to stop parsing the page and start over after reinterpreting the whole page using the encoding you specified.

What do web browsers do if they don’t find any Content-Type, either in the http headers or the meta tag? Internet Explorer actually does something quite interesting: it tries to guess, based on the frequency in which various bytes appear in typical text in typical encodings of various languages, what language and encoding was used.

166
Q

Byte order mark - this specifies if the bytes are in which sequence

BOM tells anyone who is interpretitng the file as which encoding it is using
like UTF-8- BOM or UTF-8

for html document we could specify it with head

or xml or text - you could open in notepad ++ and define the encoding

we will not be able to see the encoding in the file but it has the BOM

Internet explorer is not able to interpret the file if there is no BOM

A

https: //www.ibm.com/support/pages/how-remove-bom-any-textxml-file
https: //www.youtube.com/watch?v=g1HWdzzuIjs

The byte order mark (BOM) is a particular usage of the special Unicode character, U+FEFF BYTE ORDER MARK, whose appearance as a magic number at the start of a text stream can signal several things to a program reading the text:[1]

The byte order, or endianness, of the text stream in the cases of 16-bit and 32-bit encodings;
The fact that the text stream’s encoding is Unicode, to a high level of confidence;
Which Unicode character encoding is used.

167
Q

C# has following standard IO classes to read/write from different souces like a file, memory, network Isolated storage etc
stream is abstract class; MemoryStream, FIle Stream,Buffered stream to

A

C# has following standard IO classes to read/write from different souces like a file, memory, network Isolated storage etc
stream is abstract class; MemoryStream, FIle Stream,Buffered stream to

168
Q

string.ToLower() and string.ToLowerInvariant()

useToLowerInvariant(), as it ignores the language culture

var ext = Path.GetExtension(fileName).ToLowerInvariant();

A

Depending on the current culture, ToLower might produce a culture specific lowercase letter, that you aren’t expecting. Such as producing ınfo without the dot on the i instead of info and thus mucking up string comparisons. For that reason, ToLowerInvariant should be used on any non-language-specific data. When you might have user input that might be in their native language/character-set, would generally be the only time you use ToLower

I think this can be useful:

http://msdn.microsoft.com/en-us/library/system.string.tolowerinvariant.aspx

update

If your application depends on the case of a string changing in a predictable way that is unaffected by the current culture, use the ToLowerInvariant method. The ToLowerInvariant method is equivalent to ToLower(CultureInfo.InvariantCulture). The method is recommended when a collection of strings must appear in a predictable order in a user interface control.

also

…ToLower is very similar in most places to ToLowerInvariant. The documents indicate that these methods will only change behavior with Turkish cultures. Also, on Windows systems, the file system is case-insensitive, which further limits its use…

http://www.dotnetperls.com/tolowerinvariant-toupperinvariant

169
Q

Enumerable.Any Method

numbers.Any();

A

https://docs.microsoft.com/en-us/dotnet/api/system.linq.
enumerable.any?view=netcore-3.1#System_Linq_Enumerable_Any__1System
Collections_Generic_IEnumerable___0__System_Func
___0_System_Boolean__

Determines whether a sequence contains any elements.

List numbers = new List { 1, 2 };
bool hasElements = numbers.Any();

Console.WriteLine(“The list {0} empty.”,
hasElements ? “is not” : “is”);

// This code produces the following output:
//
// The list is not empty.

This method does not return any one element of a collection. Instead, it determines whether the collection contains any elements.

The enumeration of source is stopped as soon as the result can be determined.

170
Q

Enumerable.Any Method

Any(IEnumerable, Func)

A
// Create an array of Pets.
    Pet[] pets =
        { new Pet { Name="Barley", Age=8, Vaccinated=true },
          new Pet { Name="Boots", Age=4, Vaccinated=false },
          new Pet { Name="Whiskers", Age=1, Vaccinated=false } };
    // Determine whether any pets over age 1 are also unvaccinated.
    bool unvaccinated =
        pets.Any(p => p.Age > 1 &amp;&amp; p.Vaccinated == false);

This method does not return any one element of a collection. Instead, it determines whether any elements of a collection satisfy a condition.

The enumeration of source is stopped as soon as the result can be determined.

171
Q

Enumerable.Take(IEnumerable, Int32) Method

The Take and Skip methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll.

A

https://docs.microsoft.com/en-us/dotnet/
api/system.linq.enumerable.take?view=netcore-3.1#System_Linq_Enumerable_
Take__1System_Collections_Generic
IEnumerable___0__System_Int32_

Returns a specified number of contiguous elements from the start of a sequence.

int[] grades = { 59, 82, 70, 56, 92, 98, 85 };

IEnumerable topThreeGrades =
grades.OrderByDescending(grade => grade).Take(3);

Console.WriteLine(“The top three grades are:”);
foreach (int grade in topThreeGrades)
{
Console.WriteLine(grade);
}
/*
This code produces the following output:

 The top three grades are:
 98
 92
 85
*/

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

Take enumerates source and yields elements until count elements have been yielded or source contains no more elements. If count exceeds the number of elements in source, all elements of source are returned.

If count is less than or equal to zero, source is not enumerated and an empty IEnumerable is returned.

The Take and Skip methods are functional complements. Given a sequence coll and an integer n, concatenating the results of coll.Take(n) and coll.Skip(n) yields the same sequence as coll.

In Visual Basic query expression syntax, a Take clause translates to an invocation of Take.

172
Q

update the Binary Stream reading position

A
using (BinaryReader b = new BinaryReader(File.Open("perls.bin", FileMode.Open)))                                                     
{
    int pos = 50000;
    int required = 2000;
    // Seek to our required position.
    b.BaseStream.Seek(pos, SeekOrigin.Begin);
    // Read the next 2000 bytes.
    byte[] by = b.ReadBytes(required);
}

// reader.BaseStream.Position = 0;

173
Q

SequenceEqual(IEnumerable, IEnumerable)

A

Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type; otherwise, false.

The following code examples demonstrate how to use SequenceEqual(IEnumerable, IEnumerable) to determine whether two sequences are equal. In the first two examples, the method determines whether the compared sequences contain references to the same objects. In the third and fourth examples, the method compares the actual data of the objects within the sequences.

In this example the sequences are equal.

C#

Copy
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void SequenceEqualEx1()
{
Pet pet1 = new Pet { Name = “Turbo”, Age = 2 };
Pet pet2 = new Pet { Name = “Peanut”, Age = 8 };

    // Create two lists of pets.
    List pets1 = new List { pet1, pet2 };
    List pets2 = new List { pet1, pet2 };
bool equal = pets1.SequenceEqual(pets2);

Console.WriteLine(
    "The lists {0} equal.",
    equal ? "are" : "are not"); }
/*
 This code produces the following output:

The lists are equal.
*/
The following code example compares two sequences that are not equal. Note that the sequences contain identical data, but because the objects that they contain have different references, the sequences are not considered equal.

C#

Copy
class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public static void SequenceEqualEx2()
{
    Pet pet1 = new Pet() { Name = "Turbo", Age = 2 };
    Pet pet2 = new Pet() { Name = "Peanut", Age = 8 };
    // Create two lists of pets.
    List pets1 = new List { pet1, pet2 };
    List pets2 =
        new List { new Pet { Name = "Turbo", Age = 2 },
                        new Pet { Name = "Peanut", Age = 8 } };
bool equal = pets1.SequenceEqual(pets2);

Console.WriteLine("The lists {0} equal.", equal ? "are" : "are not"); }
/*
 This code produces the following output:
 The lists are not equal.
*/
174
Q

If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to implement the IEqualityComparer generic interface in your class. The following code example shows how to implement this interface in a helper class and provide GetHashCode and Equals methods.

A

If you want to compare the actual data of the objects in the sequences instead of just comparing their references, you have to implement the IEqualityComparer generic interface in your class. The following code example shows how to implement this interface in a helper class and provide GetHashCode and Equals methods.

C#

Copy
public class ProductA: IEquatable
{
    public string Name { get; set; }
    public int Code { get; set; }
public bool Equals(ProductA other)
{
    if (other is null)
        return false;

    return this.Name == other.Name &amp;&amp; this.Code == other.Code;
}

public override bool Equals(object obj) => Equals(obj as ProductA);
public override int GetHashCode() => (Name, Code).GetHashCode(); } After you implement this interface, you can use sequences of ProductA objects in the SequenceEqual(IEnumerable, IEnumerable) method, as shown in the following example:

C#

Copy

ProductA[] storeA = { new ProductA { Name = “apple”, Code = 9 },
new ProductA { Name = “orange”, Code = 4 } };

ProductA[] storeB = { new ProductA { Name = “apple”, Code = 9 },
new ProductA { Name = “orange”, Code = 4 } };

bool equalAB = storeA.SequenceEqual(storeB);

Console.WriteLine(“Equal? “ + equalAB);

/*
    This code produces the following output:
Equal? True */ Remarks The SequenceEqual(IEnumerable, IEnumerable) method enumerates the two source sequences in parallel and compares corresponding elements by using the default equality comparer for TSource, Default.

The default equality comparer, Default, is used to compare values of the types. To compare a custom data type, you need to override the Equals and the GetHashCode methods, and optionally implement the IEquatable generic interface in the custom type. For more information, see the Default property.

175
Q

using var vs explicit type mentioning in the code

A

http://blog.michaelbrennan.net/2010/06/why-you-should-always-use-var-keyword.html

https://intellitect.com/when-to-use-and-
not-use-var-in-c/#:~:text=var%20is%20
shorter%20and%20
easier,descriptive%20name%20for
%20the%20variable.

How I Use var and Suggest You Do As Well

Although I agree with some of the arguments above, I have fairly specific rules that I use to determine whether I will use var or specify the type literally.

I use var any time that the initialization of the variable clearly tells me what the variable will contain.

var count = 17;
var primeNumbers = new [] { 1, 3, 5, 7, 11, 13, 17 };
var customer = new Customer();
var activeOrders = GetAllOrders().Where(o => o.Active);
foreach (var activeOrder in activeOrders) { … }
Note that in all of these cases, the variable names are descriptive and the initializer is clear. I also pluralize enumerations and arrays.

Cases where I do not use var, even though I still name the variable descriptively, are when the initializer is not clear.

decimal customerBalance = GetCustomerBalance();
CustomerStatus customerStatus = GetCustomerStatus();
I declare customerBalance as decimal to know its type for clarity. Reasonable alternatives might include double or even int or long. The point is, I don’t know by looking at the code.

I declare customerStatus as the Enum that it is. This makes it clear there are a limited number of possible values that can be referenced or tested by name.

176
Q

Static class

A
n C#, a static class is a class that cannot be instantiated. The main purpose of using static classes in C# is to provide blueprints of its inherited classes. Static classes are created using the static keyword in C# and .NET. A static class can contain static members only. You can‘t create an object for the static class.
Advantages of Static Classes
If you declare any member as a non-static member, you will get an error. 
When you try to create an instance to the static class, it again generates a compile time error, because the static members can be accessed directly with its class name.
The static keyword is used before the class keyword in a class definition to declare a static class.
A static class members are accessed by the class name followed by the member name. 
Syntax of static class
static class classname  
{  
   //static data members  
   //static methods  
}
177
Q

Static members vs non-static members

A

https://www.c-sharpcorner.com/UploadFile
tatic%20class,can%20contain%20static%
20members%20only.

Static Members

There are two types of C# static class members, static and non-static.

Non-static members

This is the default type for all the members. If you do not use the “static” keyword for the declaration of a field / property or a method, then it can be called a “Non-static member”. The main feature of a non-static member is it will be bound with the object only.

Non-static Fields / Properties
The memory is allocated when the object is created.
Non-static Methods
These methods can implement operations on non-static fields and properties

Static Members

If you use the “static” keyword for the declaration of a field / property or a method, then it is called a “Static member”. The main feature of a non-static member is that it will not be bound with any object. It is individually accessible with the class name. In other words, the static members are accessible directly, without even creating one object also.

Static Fields / Properties
The memory will be allocated individually, without any relation with the object.
Static Methods
These methods can implement operations on static fields and properties only; and can‘t access the non-static members.

178
Q

Static Constructors

A

Static Constructors
A static constructor is used to initialize the static data members, whereas the normal constructor (non-static constructor) is used to initialize the non-static data members.

Syntax

static classname()  
{  
   //some code  
}  
Rules

Static constructors can‘t contain any access modifiers.
Static constructors can‘t be defined with arguments.
Static constructors can‘t access the non-static data members.

Demo on static constructors
namespace StaticConstructorsDemo  
{  
    class MyCollege  
    {  
        //static fields  
        public static string CollegeName;  
        public static string Address;  
        //static constructor  
        static MyCollege()  
        {  
            CollegeName = "ABC College of Technology";  
            Address = "Hyderabad";  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Console.WriteLine(MyCollege.CollegeName);  
            Console.WriteLine(MyCollege.Address);  
            Console.Read();  
        }  
    }  
}
179
Q

Utility classes should not have public constructors - SONAR SOURCE RULES

A

https://rules.sonarsource.com/csharp
/RSPEC-1118

Utility classes, which are collections of static members, are not meant to be instantiated.

C# adds an implicit public constructor to every class which does not explicitly define at least one constructor. Hence, at least one protected constructor should be defined if you wish to subclass this utility class. Or the static keyword should be added to the class declaration to prevent subclassing.

Noncompliant Code Example
public class StringUtils // Noncompliant
{
  public static string Concatenate(string s1, string s2)
  {
    return s1 + s2;
  }
}
Compliant Solution
public static class StringUtils
{
  public static string Concatenate(string s1, string s2)
  {
    return s1 + s2;
  }
}
or
public class StringUtils
{
  protected StringUtils()
  {
  }
  public static string Concatenate(string s1, string s2)
  {
    return s1 + s2;
  }
}
180
Q

C# | IsNullOrWhiteSpace() Method

A

C# | IsNullOrWhiteSpace() Method
In C#, IsNullOrWhiteSpace() is a string method. It is used to check whether the specified string is null or contains only white-space characters. A string will be null if it has not been assigned a value or has explicitly been assigned a value of null.

https://www.geeksforgeeks.org/c-sharp
-isnullorwhitespace-method/#:~:text=In%20C%23%2C%20IsNullOr
WhiteSpace()%20is,assigned%20a%20value%20of%20null.

181
Q

C# | IsNullOrEmpty() Method

A

In C#, IsNullOrEmpty() is a string method. It is used to check whether the specified string is null or an Empty string. A string will be null if it has not been assigned a value. A string will be empty if it is assigned “” or String.Empty (A constant for empty strings).

https://www.geeksforgeeks.org/c-sharp-
isnullorempty-method/#:~:text=In%20C%23%2C%20
IsNullOrEmpty()%20is,is%20assigned%
20%E2%80%9C%E2%80%9D%20or%20String.

182
Q

diff nullorempty nullorwhitespace

A

https://stackoverflow.com/questions/18710644/difference-between-isnullorempty-and-isnullorwhitespace-in-c-sharp

183
Q

C# online compiler

A

https://dotnetfiddle.net/4hkpKM