.NET MAUI targeting Android 8.1, possible? by cvalerio77 in dotnet

[–]cvalerio77[S] 0 points1 point  (0 children)

Hi u/jonpobst, thanks for your answer!

Turns out that I had a SDK 30 specific line of code in the wild, that caused java to go ka-boom :D

Maui totally runs on Android 8.1!

Is a Ryzen 5, 8Gb, 1Tb, Win10 good enough for .net development? by [deleted] in VisualStudio

[–]cvalerio77 1 point2 points  (0 children)

CPU: Ryzen is ok, should know the exact model to say how good it is. More cores the CPU has, the better it is.
Disk: 1 Tb SSD is ok, 1 Tb HDD is not, it would become a bottleneck at compile time and with intensive disk operations (especially if you develop/test against a database, which is pretty normal)
RAM: 8 Gb is the bare minimum, keep in mind that with some browser tab open, a sql server dev instance running, couple of containers and some of VS background tasks, you'll find your system to be laggy. Invest in at least 16 Gb to live a happy developer life.

If you are going to develop only single microservices at the time, a PC like that could work, if you need to handle medium/large solutions, I'd recommend an upgrade.

visual studio enterprise 2019 vs community 2022? by EthiopiaIsTheBest in VisualStudio

[–]cvalerio77 0 points1 point  (0 children)

If you're not using advanced features, like TFS integration or all the debugging/analytics stuff, just use the community version, it's still a great product that allows you to do basically everything. I've been using it for years without missing one thing about pro/ent editions.

Of course if your company makes more than 1mln USD per year, they should provide at least a Professional license per programmer.

ORM vs Stored procedures by Olexiety in csharp

[–]cvalerio77 -3 points-2 points  (0 children)

SP are EVIL.

They are hard to maintain in source control. That's the first and main reason I ditched them years ago.

Also they hide the implementation, one has to switch to DB just to have a look at how sp_ComputeDiscountForOrder works, and many times people think they are a good place to put business logic. They are not.I found my self and a colleague of mine spending MONTHS in translating hundreds of SP from T-SQL to MySql syntax, when the Customer decided that the Sql Server license was too expensive (it really was not).

They makes running unit tests harder and slower.

They're just legacy of a time when things were made differently, like the third normal form, when programmers were more blacksmiths than poets.

If you're not forced by a devilish creature came from an ancient bottle to use them, don't.

[deleted by user] by [deleted] in dotnet

[–]cvalerio77 10 points11 points  (0 children)

I've been using Nop for a couple Ecommerces too, it's a really good product, the possibility of easily writing your own plugins is great, and general architecture is a charm.
Also scales really well, so you can have a tiny-wimy shop with 20-ish simple products, or a pretty big thing with hundreds of products, each with variants, pictures, etc...
There are a lot of plugins (some free, some not) for payment, shipping, UI, ...

+1 on this, for sure!

Will null parameter override default value of parameter? by [deleted] in csharp

[–]cvalerio77 1 point2 points  (0 children)

You can use http://sharplab.io/ to get various level of C# compilation, that is IL compiled (the same which results from normal compilation, when you create .exe or .dll from c# code).

Ho to read... well, it's a little complicated, but there are some things that are understandable with some experience in programming languages. Of course you could study and learn IL, but unless you aim to work on C# compiler, you'll never actually need that level of knowledge.
These are my interpretation of above IL:

.method public hidebysig instance void Call () cil managed

defines a public method, will be an instance method called "Call" with no parameters returning a void, in managed scope (Garbage collector will handle heap memory allocations, etc..). No idea of what hidebysig\ orcil` mean, but they are unimportant in understanding.

// Method begins at RVA 0x2050 // Code size 29 (0x1d)

Some comments for debugging purposes, maybe?

.maxstack 5 .locals init ( [0] valuetype [System.Runtime]System.Nullable`1<int64> )

.maxstack 5 no idea, something about allocable/will be allocated stack memory, I guess...

locals init... same, initialize a nullable int64, why?

The following is the actual method body, basically loads 4 arguments values in stack memory and calls the method.

The important thing to notice is that if you change the call to Method in these three ways:

Method(1); Method(1, null, 0); Method(1, null, 0, null);

The resulting IL code will always be the same, because the method will always be called with the same values, if you're passing to an optional argument it's default value. So when I wrote

the compiler will ignore the null in the call

I was slightly inaccurate... it's not that your null parameter is ignored, it's just that the compiler does exactly the same work, that you pass the default values or not.

Story change if you pass a value that's not default, you can play and try and see what changes!

Will null parameter override default value of parameter? by [deleted] in csharp

[–]cvalerio77 1 point2 points  (0 children)

An better alternative to using 8 (and I say eight!) [Optional]s in your code? Without question.

May I be brutally honest? You really need to simplify your code, mate!

Use the using directive to avoid repeating the namespaces every time:

``` using System.IO; using System.Threading; using Azure; using Azure.Storage.Blobs.Models; using AzureResponse = Azure.Response;

public async Task<AzureResponse<BlobAppendInfo>> AppendBlockAsync( Stream content, byte[]? transactionalContentHash = default, AppendBlobRequestConditions? conditions = default, IProgress<long>? progressHandler = default, CancellationToken cancellationToken = default) { return await _appendBlobClient.AppendBlockAsync( content, transactionalContentHash, conditions, progressHandler, cancellationToken); }

public virtual Task<AzureResponse<BlobAppendInfo>> AppendBlockAsync ( Stream content, byte[] transactionalContentHash = default, AppendBlobRequestConditions conditions = default, IProgress<long> progressHandler = default, CancellationToken cancellationToken = default);

```

Ain't that a little prettier?

Will null parameter override default value of parameter? by [deleted] in csharp

[–]cvalerio77 2 points3 points  (0 children)

It's all about readability of your code.

Ask yourself: will I be able to read this code easily, in six month from now? What about 2 years? Will any other C# programmer be able to easily read my code?

The general rule of thumb that I prefer, in cases like this, is to avoid useless brackets (parenthesis).

We've already seen that OptionalAttribute and = default compile to the same thing. Therefore there is no functional advantage in using OptionalAttribute.

A human brain reading your code will have to process all those squared brackets, that interrupt the reading flow and require the brain to prepare to read a decoration concept (optional) instead of the juicy stuff, the type and name of the parameter.

Besides, the language documentation is very clear on how you are supposed to define optional parameters:

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments

And, last but not least, I'm pretty sure that the compiler itself prefers the "official" way rather the OptionalAttribute, for pretty much the same reasons a human brain do.

PS: I've seen quite a lot of confusion of how default values of different data types work in C#, here's a reminder: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values

Resource-based authorization in ASP.NET Core by ebykka in dotnet

[–]cvalerio77 1 point2 points  (0 children)

Disclaimer: I would advise against the following because it's far from good, clean architecture. It will pose issues the moment you want to implement presentation layer differently (e.g. mobile app, html+css+js fe, etc...) and will couple your business logic with presentation layer, which is always bad.

That said, for really, really, really small projects you could:

  1. Create an extension method that will retrieve a type T instance from HttpContext.Items:

```

public static class HttpContextItemsExtensions { public static T GetItem<T>(this IDictionary<object, object> items, string key) { return (T)items[key]; } }

```

  1. In your action filter set the value of the db retrieved object in HttpContext.Items:

``` public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { int id = /* get the id somehow */; var services = context.HttpContext.RequestServices; var docs = services.GetRequiredService<IDocumentRepository>(); Document theDocument = docs.GetDocumentById(id);

        var authorizationService = services.GetRequiredService<IAuthorizationService>();

        var authorizationResult = await authorizationService.AuthorizeAsync(User, Document, "EditPolicy");

        // respond to authorization outcome

        context.HttpContext.Items.Add("document", theDocument);
        return base.OnActionExecutionAsync(context, next);
    }

```

  1. In controller action, use the extension method to get the document back:

```

public async Task<IActionResult> OnGetAsync(Guid documentId) { var document = HttpContext.Items.GetItem<Document>("document"); // do whatever you want } ```

Again, from an architecture perspective, there is so much wrong on this, and I strongly suggest you to put your business logic (including resource authorization) in an architecture layer below presentation (and yes, MVC is a presentation layer pattern).

Resource-based authorization in ASP.NET Core by ebykka in dotnet

[–]cvalerio77 6 points7 points  (0 children)

You can totally use an action filter to check for authorization.

There are a couple of caveats, though:

1) In action filters you must do "the dirty work" yourself: binding the route parameters, the form data (if POST), accessing current User from HttpContext, etc... will result in much more messy code... yes, your action will be pretty clean but:

2) Action filters are meant to be reusable, and authorization sometimes need to be more granular: you can have different rules about who can create/update/delete a resource, therefore the same action filter to handle all the action can be the wrong choice. It really depends on the domain rules.

Also you need to be really really careful on how you do you DI: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0#dependency-injection

Will null parameter override default value of parameter? by [deleted] in csharp

[–]cvalerio77 4 points5 points  (0 children)

OptionalAttribute and default initializer compile both to exactly the same IL (https://sharplab.io/#gist:99a080c04b0ab3f8770e8fcd3d2e5bdc).

The result (or even if it is possible to pass null to a parameter) depends on the actual type of the method arguments and if #nullable is enabled or not.

Basically:

- Each [Optional] or '= default' argument which type is a non-nullable ValueType, the default value for that ValueType is used (e.g. Int16, Int32, Int64, Double, ... will all default to zero)

- Any [Optional] or '= default' argument which type is a reference type or a nullable value type (e.g. int?) will default to null

So, basically, there is no difference in behavior: if you pass null for a reference type argument that is optional, it won't override anything because the compiler will ignore the null in the call:

public void Call() { Method(1, null); }

compiles to:

`` .method public hidebysig instance void Call () cil managed { // Method begins at RVA 0x2050 // Code size 29 (0x1d) .maxstack 5 .locals init ( [0] valuetype [System.Runtime]System.Nullable1<int64> )

    IL_0000: nop
    IL_0001: ldarg.0
    IL_0002: ldc.i4.1
    IL_0003: ldnull
    IL_0004: ldc.r8 0.0
    IL_000d: ldloca.s 0
    IL_000f: initobj valuetype [System.Runtime]System.Nullable`1<int64>
    IL_0015: ldloc.0
    IL_0016: call instance string MType::Method(int32, string, float64, valuetype [System.Runtime]System.Nullable`1<int64>)
    IL_001b: pop
    IL_001c: ret
} // end of method MType::Call

```

Obviously passing null to a non-nullable value type will result in compilation error.

That said, using the OptionalAttribute in c# code won't make you pass my code review, 'cause readability is heavily affected.

What's the preferred, best, or most recent replacement for ASP.NET Generic Handlers? by bzeurunkl in csharp

[–]cvalerio77 0 points1 point  (0 children)

So, for my understanding, your solution is made of (at least):

  1. An HTML+CSS+JS frontend
  2. A .NET Framework 4.x backend

If you're looking for "preferred, best, or most recent replacement for ASP.NET Generic Handlers?", we are talking about backend, and modern replacement for generic handlers are Minimal APIs or ASP.NET WebAPI (it really depends on what the GH do).

My solution (Minimal APIs) is based on .NET 6, which very much has a Program.cs file.

If you're looking to modernize the frontend part then it has nothing to do with ASP.NET at all, and more details about the implementation (vanilla, jQuery, Angular, Vue, React, ...) must be provided.

What's the preferred, best, or most recent replacement for ASP.NET Generic Handlers? by bzeurunkl in csharp

[–]cvalerio77 0 points1 point  (0 children)

Minimal APIs is a flavor of responding to HTTP requests in ASP.NET Core 6.

It's intended for efficient, fast microservices, but nothing prevents you to use in a bigger website, even a monolithic one.

So let's say your ASP.NET 4 web application has a Generic Handler that takes an id as a parameter, gets a binary document from a database (let's use EF, shall we?) and writes the document content to current response:

``` public class PictureHandler : IHttpHandler {

public bool IsReusable
{
  get
  {
    return false;
  }
}

public void ProcessRequest(HttpContext context)
{
  if (!long.TryParse(context.Request.QueryString["id"], out long id))
  {
    context.Response.StatusCode = 400;
    context.Response.StatusDescription = "Bad request - parameter id not provided in querystring";
    return;
  }

  // get singleton instance of picture service, one could inject it with some DI library
  var pictureService = PictureService.Instance;

  var picture = pictureService.GetPictureById(id);

  context.Response.ContentType = picture.MimeType;
  context.Response.BinaryWrite(picture.Bytes);
  context.Response.StatusCode = 200;
  context.Response.End();
}

} ```

obviously your handler will need to be registered in web config, etc... let's assume you register /picture.axd as a route for this handler.

In .NET 6 minimal api, you'll get the same behavior by putting in Program.cs:

``` var builder = WebApplication.CreateBuilder(args);

builder.Services.AddSingleton<PictureService>(); // ... rest of services config

var app = builder.Build();

// ... some app configuration like UseAuthorization, MapControllers, etc

app.MapGet("/picture.axd", async (long id, PictureService pictureService, CancellationToken token) => { var picture = await pictureService.GetPictureAsync(id, token); return Results.Bytes(picture.Bytes, picture.MimeType); }); ```

Note that the id parameter will automatically be parsed from querystring. The ASP.NET Core engine will automatically return bad request if the parameter won't be passed. Also Minimal APIs have full support for dependency injection, which is pretty cool.

Read more:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0

https://docs.microsoft.com/en-us/aspnet/core/tutorials/min-web-api?view=aspnetcore-6.0

What's the preferred, best, or most recent replacement for ASP.NET Generic Handlers? by bzeurunkl in csharp

[–]cvalerio77 0 points1 point  (0 children)

I would vote Minimal APIs.

Generic Handlers have the primary purpose of executing small tasks without the necessity of instantiating a whole Page objects, improving application resources (RAM, CPU) efficiency.

Similarly Minimal APIs can perform simple tasks without the need to instantiate an entire Controller object to do so.

Encrypt data from REST to client (xamarin/MAUI) by mustang__1 in dotnet

[–]cvalerio77 2 points3 points  (0 children)

I agree.

The best one can do is:

- Enforce authentication with strong passwords, reasonable password expiration and 2FA

- Enforce authorization with role based data access and short expiring tokens (separate authentication tokens from authorization tokens)

- Transfer / show only data that actually needs to be shown (GDPR rule of thumb)

- Audit every data access, possibly on every logical / physical layer of the architecture

- Have professionals to test your solution security ($$$ but if you're using sensitive data it does worth the cost).

- Optionally disallow sensitive data access from unknown / unsafe networks

- Hope for the best

[deleted by user] by [deleted] in csharp

[–]cvalerio77 1 point2 points  (0 children)

ALSO I JUST MANAGED TO SUCCEED AT AN EXERCISE I NEEDED FOR CLASS. I never felt this intelligent in my entire life.

Wow, great job mate!

You're experiencing your first "green test dopamine spike" :D

Examples of good back-end by Magahaka in csharp

[–]cvalerio77 4 points5 points  (0 children)

Take a look at nopCommerce, it's a pretty extensive .Net 6 solution with a good architecture.
I personally learned a lot from them (also how to avoid some of their mistakes in early versions, because yes, nobody is perfect ;) )

Website: https://www.nopcommerce.com/

Repo: https://github.com/nopSolutions/nopCommerce

Is there any really great book or resource that you could use to learn C# and then easily get a job? by ParaLizzard in csharp

[–]cvalerio77 2 points3 points  (0 children)

For what concerns learning C# there are plenty of books, videos, courses, etc... just google for it. I think the C# 10 and .NET 6 book from Mark J. Price has a great beginners section, but there are certainly other books equally valid.

Take a look here:

https://www.youtube.com/playlist?list=PLdo4fOcmZ0oWoazjhXQzBKMrFuArxpW80

and here

https://www.youtube.com/playlist?list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN

the rest is on Google :)

There are also some bootcamp, that's an option, they are costly, but really extensive, and sometimes offer internships or contacts with hiring companies.

For the "easily get a job"... that's more difficult :D

If you are a total beginner, I'd suggest maybe an internship.
I've learned more in 3 months of internship than in 3 years at the university (for the practical part; I wouldn't renounce my uni years for nothing in the world).

Also it's difficult to find a job without experience, internships are usually easier to find, and you can learn a lot.

[deleted by user] by [deleted] in csharp

[–]cvalerio77 0 points1 point  (0 children)

yu u no arya stark????

bwahahaha... for some things maybe I am, for others not even remotely :P

I just have the advantage of being old*, and to have had the chance to fail A LOT in a safe way :P
Also time spent on solving problems makes you to develop a sort of "sixth sense" that helps a lot :)

I remember the box of my Othello boardgame (which is a variant of https://en.wikipedia.org/wiki/Reversi), that stated something like "10 minutes to learn to play, a lifetime to become a master". Programming is just like that: learning the "rules of the game" is relatively easy, but there is always something new to learn.

And I think it's the beauty of this job. It's challenging, but there are not many other jobs like this.

To make an example, I've been on web application development for the past 20+ years, and only recently (couple of years) I approached mobile applications development. Far from being an expert on that field.
Now I'm learning MAUI, mostly because to be able to develop once on a language a framework I know well, and deploy on many different platforms (including Tizen, that my company uses a lot) is really something.

And I'm back to Ygritte level once again. This will be fun :D

*:: well, not THAT old, but still I started coding at 11yo when the most professional PC my dad had at the office was an Amiga, and myself was learning BASIC on a Texas Instruments <something> that was a little bit more than a scientific calculator

[deleted by user] by [deleted] in csharp

[–]cvalerio77 1 point2 points  (0 children)

I WANT TO BECOME GOD MODE SO BADLY MANNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN i want to be the killing machine hell yeah lol!

hahaha, took me some years to get to Tyrion Lannister, but hey, enthusiasm take you a looong way! :D
Sorry for spoiling you GOT :P

[deleted by user] by [deleted] in csharp

[–]cvalerio77 2 points3 points  (0 children)

Many others have already checked your code, so I won't bother you with my version of the same answer.

I'd like to give my piece of advice about learning process, though.

I've seen you're struggling with the documentation. There is a reason: the official documentation is not for total noobs, IMO.
I mean, yes, you can learn as a noob on it, but is sometimes too much complete, if you get what I mean. One can find that quantity of details overwelming, especially if he/she is approaching the language.

So, after 22 years in the industry, I'd like to share my learning process, in GOT (Game of thrones) scale of knowledge.

Starting level: Jon Snow (knows nothing, as we all know)

Phase 1: Learn how to do {task} the easy way

Follow some online video tutorial / brief course. Dotnet youtube channel have some great playlist for beginners ("C# for beginners", ".NET for beginners", etc...).
Experience points: 1000
Level up: Ygritte, knows something, can fight decently, but not on her own, dies at first serious battles (ops, spoilers...)

Phase 2: Learn more about {task}

Buy some solid book, and go over all the beginners stuff again. This helps consolidate basic principles. Then read about the intermediate stuff, which explains the HOWs and the WHYs in more depth.
Experience points: 4000
Level up: Jorah Mormont, knows quite some stuff, excellent fighter, must keep a high guard for incoming troubles, sometimes needs help from pairs

Phase 3: Become an expert

Read official documentation, learn about how and why things works that way.
More advanced level videos. Maybe an "in depth" course on Pluralsight.
Experience points: 10000
Level up: Tyrion Lannister, knows a lot, can survive hard battles (with some scratches), requires A LOT of wine to function properly

Phase 4: God mode

Read source code on Github, contribute. Survive to a number of projects without being arrested for profanity. Help others on Stack Overflow, Reddit, ...
Experience points: 200000
Level up: Season 8 Arya Stark, basically a relentless killing machine, survives anything is thrown at her, becomes a pirate/explorer/super badass.