daveharney (15) [Avatar] Offline
#1
Chapter 9 cleared up a lot of questions I had about the Web API. This isn't a priority for me right now, but this chapter made the concepts very clear.

Chapter 10 was of great interest because I've run into Dependency Injection a number of times and just stumbled along without really understanding how it worked - despite having read several other explanations. Now I feel I've got a pretty good handle on the subject.

A few comments:

The SOLID link 18 point to the wrong reference

Page 282 - I got worried I was in over my head when you mentioned "anonymous delegate" - fortunately, understanding not required! But lambda is good!

The example code was a very big help - much appreciated. I did find an error when I commented out this code in startup.cs. At least this was a problem in my VS2017 - not sure if some update would make this a moot point:

      services.AddMvc();

            // Can also remove explicit configurations below and use extension method
            services.AddEmailSender();

            //services.AddScoped<IEmailSender, EmailSender>();
            //services.AddSingleton<NetworkClient>();
            //services.AddScoped<MessageFactory>();
            //services.AddSingleton(provider =>
            //    new EmailServerSettings
            //    (
            //        host: "smtp.server.com",
            //        port: 25
            //    ));
        }


EmailSenderService....cs had the namespace references switched around:

using Microsoft.Extensions.DependencyInjection;
//using DependencyInjectionExample.Services;

//namespace Microsoft.Extensions.DependencyInjection.Services
namespace DependencyInjectionExample.Services
{
    public static class EmailSenderServiceCollectionExtensions
    {
        public static IServiceCollection AddEmailSender(this IServiceCollection services)
        {
            services.AddScoped<IEmailSender, EmailSender>();
            services.AddSingleton<NetworkClient>();
            services.AddScoped<MessageFactory>();
            services.AddSingleton(
                new EmailServerSettings
                (
                    host: "smtp.server.com",
                    port: 25
                ));
            return services;
        }
    }
}

Andrew Lock (51) [Avatar] Offline
#2
Hi Dave,

Thanks for the comments as alwayssmilie

The SOLID link 18 point to the wrong reference


This looks correct to me? https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

I got worried I was in over my head when you mentioned "anonymous delegate"


You got it - a lambda! Good to know that "anonymous delegate" is more confusing rather than providing a definition! Technically correct but clearly doesn't add anything there smilie

I did find an error when I commented out this code in startup.cs


You are correct, there is a namespace issue, but (by convention) the extension methods are usually added to the Microsoft.Extensions.DependencyInjection namespace. That makes the extensions easily visible in the Startup.cs file, without needing to import additional namespaces first. This is just a convention though, so adding to the DependencyInjectionExample.Services is also perfectly acceptable smilie

using DependencyInjectionExample.Services;

//namespace Microsoft.Extensions.DependencyInjection.Services
namespace Microsoft.Extensions.DependencyInjection
{
    public static class EmailSenderServiceCollectionExtensions
    {
        public static IServiceCollection AddEmailSender(this IServiceCollection services)
        {
            services.AddScoped<IEmailSender, EmailSender>();
            services.AddSingleton<NetworkClient>();
            services.AddScoped<MessageFactory>();
            services.AddSingleton(
                new EmailServerSettings
                (
                    host: "smtp.server.com",
                    port: 25
                ));
            return services;
        }
    }
}

daveharney (15) [Avatar] Offline
#3
I should have explained the SOLID link better:
10.1 Introduction to dependency injection
The ASP.NET Core framework has been designed from the ground up to be modular and to
adhere to “good” software engineering practices. As with anything in software, what is
considered best-practice varies with the time, but for object-oriented programming, the
SOILD18 principles have stood the test of time.

If i click on the little "18" in the above paragraph on page 265, it takes me to the "17" reference. The actual "18" reference itself is correct. Hope this makes sense!
Andrew Lock (51) [Avatar] Offline
#4
Oh I see, how odd! Thanks for letting me know smilie
Nikolai Tsepalov (1) [Avatar] Offline
#5
Hi Andrew,
I think the highlighted word in the attached file should be "scoped"
Nikolai
Andrew Lock (51) [Avatar] Offline
#6
Hi Nikolai,

You're quite right, I'll get that amended

Thanks