joren
Using base class as relationship definition in model ASP.net WEB API
So I've written this model that I used to save my credentials to authenticate my users with:
Now as you can see I have a
BaseProfile
here, each credential has its connected profile attached. It is a one to one relationship. However, I have multiple types of profiles, that have a few things in common so I wrote a base class like this:
and then I have different profiles that inherit from this, would this relationship still work as intended when using Baseprofile
? Or do I need to have a field for each profile and have them as nullable/have some enum to decide the profile type and use that as a way to differentiate3 replies
How are DTO utilized
So I've been writing some end points for my learning project, and I stumbled on DTO. The concept I understand, limit view you return for privacy reasons, security reasons, having a smaller payload, etc.
How does it work in practice though, I saw that
auto mapper
is a thing used, which is awesome, that I can use and understand easily. However, what's best practice, when you have a DTO for a model, does the endpoint always return the DTO? Or do you have two endpoints, one that return the actual model, and one the DTO?177 replies
Data seeding database
So currently in my
program.cs
I have the following code:
Now, seeding only makes sense in a development environment and only has to be executed once. So would this be a proper way to go about it? My DataSeeder class basically creates objects of all my models, gives them mock values, and that I save to the database.74 replies
❔ ✅ Verifying the purpose of Moq usage
So, as you can see I use
It.IsAny<int>()
to mimic a value that would be passed, since GetUserName()
requres an integer passed as argument.
However this value is not used in the actual mock, but rather the
the 42
that is passed here, which then calls GetUserName()
inside of it.67 replies
❔ Clarification on Async and Task.Run
So I've been trying out the Async and
Task.Run
, now I need some clarification about the two and their differences.
Now Async is basically one worker, that shifts its attention from Task to Task while another task is being processed (without needing workers attention). Now I read that C# achieve this by using the Task.Scheduler
, which has a thread pool and what not.
Now Task.Run
would be parallelism in C#, it creates a thread with the function you pass to it.
Now, granted the above information is correct, Async using a thread pool wouldnt that be considered utilizing parallelism?214 replies
❔ => operator
So I read while looking into LINQ documentation that
=>
is the syntax for a lambda expression. Now in the context of LINQ it makes sense, I would've guessed already. However in a class:
I would assume it is not seen as a lambda, it should just be a class method as its just syntax sugar for:
26 replies
✅ Deferred Execution or Forcing Immediate Execution
So I was reading the documentation on LINQ and I came across the two concepts i wrote in the title, now lets look at:
Now its pretty clear its deferred execution, we declare the query but its actually executed when the
foreach
is called. What I dont understand is why
Is considered Immediate execution, it still declares it and then triggers the query, in this case a foreach
internally but conceptually this is pretty much the same.
This though:
Calling .ToList
or To.Array
makes sense, you'd be executing the query in place and returning the result of the execution rather than saving the query to be executed later. Am I misinterpreting the documentation?346 replies
❔ Covariance and Contravariance C#
So I stumbled on this topic and I dont seem to grasp it properly, though I saw the following example somewhere:
Both now can call
func()
, but b
cannot call fuc()
too as its treated as a Base
(reference) type. Now in C++, you'd just cast b
to Derived
and it'd work and be perfectly valid code. The sources I looked at, mentioned that this is why covariance/contravariance exists?136 replies
❔ Confusion as to what to default to when it comes to accessibility modifiers
So I come from a extensive C++ background, in which we use the keywords:
public
and private
a bit different. They say everything about accessibility but nothing about assemblies and whether we are allowing external assemblies to access our classes, methods and even members.
Now to my understanding internal
is basically public
but within the assembly im working in, meaning that is how I say: this func()
method is public and can now be called on its respective object outside of the class. And public
is used to say: hey, this is allowed to be called form another assembly and is public
within its own assembly too.
Now, knowing this, I concluded that best practice, granted we work within one assembly, the internal
keyword is the default one should use however I'd love to get some assurance on it. Since even my IDE defaults to public
when auto-generation ctors, methods, etc.
261 replies