6952
Comment:
|
14545
|
Deletions are marked like this. | Additions are marked like this. |
Line 4: | Line 4: |
---- 2019-06-12 Got a little frustrated with the IoC -> Dependency Injection stuff in ASP.NET core while trying to retrieve configuration settings and use them in the {{{ConfigureServices}}} method. Here is what I learned. It seems quite common in the constructor to save off {{{IConfiguration}}} and {{{IHostingEnvironment}}} instances for later use. {{{#!highlight csharp public Startup(IConfiguration configuration, IHostingEnvironment environment) { Configuration = configuration; Environment = environment; } }}} Inside of {{{ConfigureServices}}} (which does exactly what it says) we can configure a service related to our configuration and then use it later using dependency injection. But what if I want to use it here? How do I get the value now? That was the question that really bothered me. The prevailing internet opinion is do it using DI or noway. Almost every documentation page or stack overflow discussion centered around injecting it into a controller. There was even some prickly remarks that you shouldn't do it any other way as that is an anti-pattern. '''Point 1: {{{ConfigureServices}}} is to configure services not consume them!''' '''Point 2: I learned the order - Constructor first (no surprise there), {{{ConfigureServices}}} method second and {{{Configure}}} method last.''' '''Point 3: You can use dependency injection on {{{Startup}}}, the constructor, and on the {{{Configure}}} method, but NOT ON {{{ConfigureServices}}} hence the saving off of the two items in the constructor above.''' From point #1 above, it becomes a matter of creating a settings object and binding it to a section in appsettings.json. This can be completely accomplished by the following code in {{{ConfigureServices}}}: {{{#!highlight csharp ... services.Configure<Uris>((settings) => { Configuration.GetSection("UriSettings").Bind(settings); }); ... }}} Of course the Uris object was defined as a Plain Old C# Object (POCO) with public properties that match the settings I want to use (yes they will match string, string[], etc.). It implements the {{{IUris}}} interface that will be used to access the service. This will become a little clearer in the next example, so stick with me a moment. Then using the knowledge gained from Points #2 and #3, I injected {{{IUris}}} service into {{{Configure}}} as follows: {{{#!highlight csharp private void InitializeDatabase(IApplicationBuilder app, IUris uris) { ...//use uris here somewhere for something } }}} '''Whew, take a deep breath. Because this framework hides so much, what is going on amounts to magic, and as a programmer ''magic is totally unacceptable!''.''' So here is another way to do it that exposes a bit of the magic to the light of day. Suppose instead that I had done the following in {{{ConfigurationServices}}}: {{{#!highlight csharp //This method allows us to create a service that is the Settings Object that we want. It does not rely on the IOptions<...> class. Yahoo! //1. Loads the section in to an IConfigurationSection IConfigurationSection sec = Configuration.GetSection("UriSettings"); //2. This translates the section json into the a new object of type Uris services.Configure<Uris>(sec); //3. Builds the service provider so that we can use it. ServiceProvider sp = services.BuildServiceProvider(); //4. Uses the Service provider to get an object of type IOptions whose value is a Uris object built from the appsettings.json. IOptions<Uris> iop = sp.GetService<IOptions<Uris>>(); //5. Get the Uris object from the iop.Value. Uris uris = iop.Value; //6. Registers a service with the Dependency Injection container using the object we retreived as the singleton. services.AddSingleton<IUris>(uris); }}} If I use this code instead, it is clear what is happening. 1. We get a section {{{sec}}} from appsettings.json. 1. We configure a new service using {{{sec}}} and the class {{{Uris}}}, based on {{{IUris}}}. 1. Since we are configuring services, I don't have a service provider here, so we build one. 1. Now we can actually get the service (which uses the OptionsPattern to return our object) 1. Finally we can actually get the settings object we created. 1. Optionally we can create a service directly that will return us the actual object instead of the IOptions<IUris> object. Less magic? Yes, but not the standard way to do this, just much more understandable. AND, I have a reference to the actual object that is the service in the Dependency injection container. |
|
Line 8: | Line 83: |
Looking at the C# 8 features. See: [[https://mybuild.techcommunity.microsoft.com/sessions/77032?source=sessions#top-anchor&ocid=AID747785&wt.mc_id=CFID0415&MC=Vstudio&MC=CSHARP&MC=MachLearn&MC=.NET&MC=AppDnDTool|The video presetnation]] or see [[https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8|the webpage]] | Looking at the C# 8 features. See: [[https://mybuild.techcommunity.microsoft.com/sessions/77032?source=sessions#top-anchor&ocid=AID747785&wt.mc_id=CFID0415&MC=Vstudio&MC=CSHARP&MC=MachLearn&MC=.NET&MC=AppDnDTool|The video presentation]] or see [[https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8|the webpage]] |
Line 38: | Line 113: |
Line 51: | Line 127: |
_ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof( | _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle) |
Line 55: | Line 131: |
Or even with nested switch expressions {{{#!highlight csharp public static decimal CalculateToll(object vehicle) => vehicle switch { Car c => c.Passengers switch { 0 => 2.00m+0.50m, 1 => 2.00m, 2 => 2.00m-0.50m, _ => 2.00m-1m, //Note they called the "_" the discard (symbol) which matches any. }, Taxi { Fares: 0 } => 3.50m + 1.00m, ... DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m, ... _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(... }; }}} '''IAsyncEnumerable is a new type. ''' It allows you to produce and consume realtime data. See the video above at abut 35 Minutes. {{{#!highlight csharp private static async Task ChartTollsAsync() { await foreach (var t in TollSystem.GetVehiclesAsync()) { await TollSystem.ChargeTollAsync(t.vehicle, t. time, t.inbound, t.license); } } }}} Default implementation in Interfaces. Allow you to have an implementation in an interface. Then you can override the interface and create the implementation, but you don't have to. Interesting. .Net 4.8 is the last implementation of the .NET framework that will be available. (0:42 in the video). This begs the question what are the different .Net frameworks and what are they for? Where do they belong? Where should I use them? === .NET Framework vs. .NET Core vs. .NET Standard === 1. Libraries that target .NET Standard will run on any .NET Standard compliant runtime, such as .NET Core, .NET Framework and Mono/Xamarin (smaller surface area, but wider compatibility) 1. libraries that target .NET Core can only run on the .NET Core runtime. 1. Libraries that target .NET Framework can only run on Windows. Ignoring libraries for a moment, the reason that .NET Standard exists is for portability; it defines a set of APIs that .NET platforms agree to implement. Any platform that implements a .NET Standard is compatible with libraries that target that .NET Standard. One of those compatible platforms is .NET Core. To summarize: 1. A .Net Core Class Library is built upon the .Net Standard. If you want to implement a library that is portable to the .Net Framework, .Net Core and Xamarin, choose a .Net Standard Library 1. .Net Core will ultimately implement .Net Standard 2 (as will Xamarin and .Net Framework) 1. .Net Core, Xamarin and .Net Framework can, therefore, be identified as flavours of .Net Standard To future-proof your applications for code sharing and reuse , you would rather implement .Net Standard libraries (since portable class libraries are now deprecated). Sources: * https://stackoverflow.com/questions/42939454/what-is-the-difference-between-net-core-and-net-standard-class-library-project for a nice discussion. * https://www.davidyardy.com/blog/net-standard-vs-net-core/ * https://devblogs.microsoft.com/dotnet/introducing-net-standard/ |
Current Events in CS & IT
I'll try to update this with events that are relevant for at least a semester or even a year. Some of these will be lists that relate to CS and CSA majors
2019-06-12
Got a little frustrated with the IoC -> Dependency Injection stuff in ASP.NET core while trying to retrieve configuration settings and use them in the ConfigureServices method. Here is what I learned.
It seems quite common in the constructor to save off IConfiguration and IHostingEnvironment instances for later use.
Inside of ConfigureServices (which does exactly what it says) we can configure a service related to our configuration and then use it later using dependency injection. But what if I want to use it here? How do I get the value now? That was the question that really bothered me. The prevailing internet opinion is do it using DI or noway. Almost every documentation page or stack overflow discussion centered around injecting it into a controller. There was even some prickly remarks that you shouldn't do it any other way as that is an anti-pattern.
Point 1: ConfigureServices is to configure services not consume them!
Point 2: I learned the order - Constructor first (no surprise there), ConfigureServices method second and Configure method last.
Point 3: You can use dependency injection on Startup, the constructor, and on the Configure method, but NOT ON ConfigureServices hence the saving off of the two items in the constructor above.
From point #1 above, it becomes a matter of creating a settings object and binding it to a section in appsettings.json. This can be completely accomplished by the following code in ConfigureServices:
Of course the Uris object was defined as a Plain Old C# Object (POCO) with public properties that match the settings I want to use (yes they will match string, string[], etc.). It implements the IUris interface that will be used to access the service. This will become a little clearer in the next example, so stick with me a moment.
Then using the knowledge gained from Points #2 and #3, I injected IUris service into Configure as follows:
Whew, take a deep breath. Because this framework hides so much, what is going on amounts to magic, and as a programmer magic is totally unacceptable!.
So here is another way to do it that exposes a bit of the magic to the light of day. Suppose instead that I had done the following in ConfigurationServices:
1 //This method allows us to create a service that is the Settings Object that we want. It does not rely on the IOptions<...> class. Yahoo!
2 //1. Loads the section in to an IConfigurationSection
3 IConfigurationSection sec = Configuration.GetSection("UriSettings");
4 //2. This translates the section json into the a new object of type Uris
5 services.Configure<Uris>(sec);
6 //3. Builds the service provider so that we can use it.
7 ServiceProvider sp = services.BuildServiceProvider();
8 //4. Uses the Service provider to get an object of type IOptions whose value is a Uris object built from the appsettings.json.
9 IOptions<Uris> iop = sp.GetService<IOptions<Uris>>();
10 //5. Get the Uris object from the iop.Value.
11 Uris uris = iop.Value;
12 //6. Registers a service with the Dependency Injection container using the object we retreived as the singleton.
13 services.AddSingleton<IUris>(uris);
If I use this code instead, it is clear what is happening.
We get a section sec from appsettings.json.
We configure a new service using sec and the class Uris, based on IUris.
- Since we are configuring services, I don't have a service provider here, so we build one.
Now we can actually get the service (which uses the OptionsPattern to return our object)
- Finally we can actually get the settings object we created.
Optionally we can create a service directly that will return us the actual object instead of the IOptions<IUris> object.
Less magic? Yes, but not the standard way to do this, just much more understandable. AND, I have a reference to the actual object that is the service in the Dependency injection container.
2019-05-31
Looking at the C# 8 features. See: The video presentation or see the webpage
Range calculations: Syntax gets shorter. Consider the following license plate number and state, that is then separated into two variables, plate and state.
can become
Property pattern: allows you to use switch more easily. Consider the following
Becomes:
1 public static decimal CalculateToll(object vehicle)
2 => vehicle switch
3 {
4 Car { Passengers: 0 } => 2.00m+0.50m,
5 Car { Passengers: 1 } => 2.00m,
6 Car { Passengers: 2 } => 2.00m-0.50m,
7 Car _ =>2.00m-1m,
8
9 Taxi { Fares: 0 } => 3.50m + 1.00m,
10 ...
11 DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,
12 ...
13 _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(vehicle)
14 };
Or even with nested switch expressions
1 public static decimal CalculateToll(object vehicle)
2 => vehicle switch
3 {
4 Car c => c.Passengers switch
5 {
6 0 => 2.00m+0.50m,
7 1 => 2.00m,
8 2 => 2.00m-0.50m,
9 _ => 2.00m-1m, //Note they called the "_" the discard (symbol) which matches any.
10 },
11 Taxi { Fares: 0 } => 3.50m + 1.00m,
12 ...
13 DeliveryTruck t when t.GrossWeightClass > 5000 => 10.00m + 5.00m,
14 ...
15 _ => throw new ArgumentException(message: "Not a known vehicle type", paramName: nameof(...
16 };
IAsyncEnumerable is a new type. It allows you to produce and consume realtime data. See the video above at abut 35 Minutes.
Default implementation in Interfaces. Allow you to have an implementation in an interface. Then you can override the interface and create the implementation, but you don't have to. Interesting.
.Net 4.8 is the last implementation of the .NET framework that will be available. (0:42 in the video). This begs the question what are the different .Net frameworks and what are they for? Where do they belong? Where should I use them?
.NET Framework vs. .NET Core vs. .NET Standard
- Libraries that target .NET Standard will run on any .NET Standard compliant runtime, such as .NET Core, .NET Framework and Mono/Xamarin (smaller surface area, but wider compatibility)
- libraries that target .NET Core can only run on the .NET Core runtime.
- Libraries that target .NET Framework can only run on Windows.
Ignoring libraries for a moment, the reason that .NET Standard exists is for portability; it defines a set of APIs that .NET platforms agree to implement. Any platform that implements a .NET Standard is compatible with libraries that target that .NET Standard. One of those compatible platforms is .NET Core.
To summarize:
- A .Net Core Class Library is built upon the .Net Standard. If you want to implement a library that is portable to the .Net Framework, .Net Core and Xamarin, choose a .Net Standard Library
- .Net Core will ultimately implement .Net Standard 2 (as will Xamarin and .Net Framework)
- .Net Core, Xamarin and .Net Framework can, therefore, be identified as flavours of .Net Standard
To future-proof your applications for code sharing and reuse , you would rather implement .Net Standard libraries (since portable class libraries are now deprecated).
Sources:
https://stackoverflow.com/questions/42939454/what-is-the-difference-between-net-core-and-net-standard-class-library-project for a nice discussion.
https://devblogs.microsoft.com/dotnet/introducing-net-standard/
2019-02-11
Security: Installed Suricata at home today as part of pfSense. I noticed that (unamed cell company) is using IKEv2 with weak parameters... Hmmm. Turns out it is the Galaxy Note 5 (Android 7.0) that is the instigator of this particular alert. Apparently all email coming from unamed cell company goes through their hosts (even if it is using the outlook app) using outdated VPN connection configurations. The next question would be, is it hackable. Perhaps more later.
2019-02-07
I'm playing with Docker Containers. I didn't have a full concept of what it does, so I started here on youtube. This video covers high level concepts related to efficiency, performance and security.
2018-12-29
I've been playing around with several things this break and I've been able (amid being sick) to catch up on a bunch of stuff.
I re-created my website using bootstrap. Don't judge to harshly, I just started playing with it. But at least now it will be visible on my phone.
- I updated all my infrastructure at home - updated the firewall, got backups running correctly again, updated plex etc.
In an effort to create a dashboard that would monitor the various processes and backups etc, I started playing around with nodejs and express. And what did I find? A new tutorial resource that I think I'm going to love! I highly recommend taking a look at the Mozilla Developer Network tutorials at https://developer.mozilla.org/en-US/
2018-01-23
Java - Pervasive |
Python - Web, Scientific Programming and Data Science |
Javascript - Web |
C++ - "C, C++ and C# will still be the most popular languages in 2018 because they power so much software" |
C# - Games, Enterprise, Web |
PHP - Web |
Perl - glue code |
Swift - Apple of course, but increasing in popularity |
R - Data mining... |
Rust - Systems language from Mozilla |
2016-11-30
2016-11-16
10 Tech Jobs Set For Big Pay Raises In 2017
10 Hot Programming Languages to Build Web Apps 10 Programming Languages That Will Keep You Employed
10 Hot Prog. Languages to Build Web Apps (11/14/2016) |
10 Prog. Languages that will keep you employed (5/1/2015) |
HTML 5.0 |
Java |
CSS |
SQL |
Javascript |
Python |
Java |
Javascript |
PHP |
C++ |
Ruby |
C# |
ASP.NET (C#) |
PHP |
Cold Fusion* |
Swift |
Perl* |
GO* |
Python |
R |
*These langauges seem to me to be a bit iffy for the list. Cold Fusion, may be around, but I don't see it much. Perl is aptly described in the article as "the chewing gum and baling wire of programming languages." Go seems trendy, but not popular yet.
Nine Data center tools IT Pros Should Know About
CloudStack OpenStack derivative now computing with OpenStack.
OpenNebula aims at simplicity in Data Center Automation.
Eucalyptus For use with AWS style services
Ansible Tower is RedHat's cloud management software.