Jaliya's Blog

Pages

Tuesday, August 7, 2018

I wanted to have a docker image with Windows Server 2016 having ASP.NET Core 2.1 Runtime and Node installed, and didn’t find any official image that has them all (maybe there is and it’s just I couldn’t find).

Tuesday, July 31, 2018

In TypeScript, Rest Parameters are often used in functions to specify the parameters when the number of parameters is unknown.

Let’s go by a sample scenario. Imagine we need to have a method which will return us the full address (nicely separated by “,”) when different parts of the address are supplied. Some addresses have Address Line 1, Address Line 2 etc., and for some, there is no Address Line 2, likewise.

In this case, there is no fixed number of parameters. To cater such a requirement, we can have the following function with Rest Parameters.

Sunday, July 1, 2018

I am humbled, pleased and honored to receive the precious Microsoft Most Valuable Professional (MVP) Award for the fifth consecutive year. In the last couple of years received the award on 1st of January, but this time it's on 1st of July due to changes in Microsoft MVP Program. The expertise category which I received the award for is same as the last year, that is Visual Studio and Development Technologies. In the first two years, I have been awarded MVP for Visual C# and then .NET.

As always looking forward to another great year on top of Microsoft Development Stack.

So when we decorate the parameter with FromServices attribute, the framework will look up the services container and inject the matching implementation. No need to do constructor injection at all. After all parameter injection is one of the ways we can do DI.

Thursday, June 14, 2018

Delivered an hour-long session at Sri Lanka Developer Forum titled ASP.NET Core 2.1: ActionResult<T> and HttpClientFactory. Actually, this was a cover-up session, the original speaker couldn’t make it to deliver the session. And this is the very first session that I did using someone else's laptop as my laptop had trouble connecting to the projector.

There was no time to prepare the slides, it was purely a hands-on session. There I demoed the use of ActionResult<T> and HttpClientFactory which was released with ASP.NET Core 2.1. And there were a lot of questions in overall .NET Core and ASP.NET Core.

Thursday, May 17, 2018

ASP.NET Core 2.1 is almost released, we now have access to ASP.NET Core 2.1 RC1. Among other nice features like SignalR (Yes, SignalR is included in ASP.NET Core 2.1) which is getting shipped with ASP.NET Core 2.1, one of my favorites is the introduction of ActionResult<TValue>.

So this is very simple, and we are searching for an Employee by Id and returning an Employee. Having Employee is the return type here is very useful for things like documenting the API using Swagger for instance.

But this looks like a bad code, what if there is no Employee by given Id. In that we don’t usually return null, we return HTTP Status Code: NotFound.

But if we are to return that, we no longer can return an Employee type, we are returning an IActionResult. And the code changes to following.

Now when using this, you don’t need to have UseLazyLoadingProxies and navigation properties doesn’t need to be virtual. EF Core will be injecting ILazyLoader where it’s required, and he is going to load the related data.

This will also give us the following output.

Output

And before winding up, note, Lazy Loading should be used with care. It doesn’t matter whether it’s EF or EF Core, it can affect the performance big time.

Tuesday, May 15, 2018

In my last post, I wrote about how you can get yourself started on Blazor. If you are new to Blazor, maybe you can read it first, so you can easily catch along with this. In this post let’s see how we can use Dependency Injection within Blazor.

DI is a first class citizen is Blazor meaning Blazor has DI built in.

If you have a look at the Program.cs of an Blazor application, you should be seeing this.

Sunday, May 13, 2018

Blazor is web UI framework based on C#, Razor, and HTML for building single-page applications. Yes, with Blazor, we can use C#, which is a server-side language and it’s going to be executed on the browser. That is done via WebAssembly (shortened wasm). Blazor provides all of the benefits of a client-side web UI framework using .NET on the client and optionally on the server.

As of today, Blazor is still in its experimental phase and the latest version is 0.3.0. Even though it’s in its early stages, you, of course, can create a basic to average size web application. So let’s see how you can get started with Blazor.

This is the root component. A component is a piece of UI, such as a page, dialog, or data entry form. Components can be nested, reused, and shared between projects.

<!--
Configuring this here is temporary. Later we'll move the app config
into Program.cs, and it won't be necessary to specify AppAssembly.
--><RouterAppAssembly=typeof(Program).Assembly />

Next let's have a look at the content on Pages folder. This is where we have the pages. Pages are also components. First let’s examine _ViewImports.cshtml file inside Pages folder.

_ViewImports.cshtml

@layoutMainLayout

Here What it says is, we are using MainLayout for all the pages inside Pages folder. Technically layouts are also components. Alternatively you can specify the @layout in individual pages as well. And if you have another folder inside Pages folder, you can have a _ViewImports.cshtml file there and place a @layout, so for all the pages in that folder, that particular @layout is being used. Basically _ViewImports.cshtml is used to place common usings.

So here the MainLayout is residing inside Shared –> MainLayout.cshtml.

You can see it uses NavMenu component which is residing in Shared folder. When this layout is being used in pages, pages’ content will get rendered where we have @Body.

Let’s open up Index.cshtml.

Index.cshtml

@page"/"<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPromptTitle="How is Blazor working for you?"/>

So this is the page, which will get rendered when we hit “http://{url}/” on the browser. And you can see the page contains another component SurveyPrompt. And this component has a Parameter named Title.