« first day (2735 days earlier)      last day (2214 days later) » 
05:00 - 15:0015:00 - 22:00

5:32 AM
Fun fact of the day: No more than 40 people live 800 feet (243 m) above the ground of New York City.
 
6:20 AM
Hi guys
why i am not able to access variable s using object of class test1?
This is really strange
 
mr5
bcoz u wrote t in capital T?
 
but i am unable to get t
I am not getting t in intellisense
 
2 mins ago, by mr5
bcoz u wrote t in capital T?
 
But i am not getting t in intellisense
 
mr5
@Learning-Overthinker-Confused ideone.com/JemIW4
 
Good morning.
 
see now when i rename t to obj i dont get obj in intellisense
 
Because you're not in a method.
You've defined obj/t as a field in your class.
But you can't just write code in the class.
 
but i have created object of class at class level so many times
 
Yes. But what is your second line trying to do?
The first line is fine. It's the second that makes no sense, so intellisense won't offer completion for it.
 
6:31 AM
Oh yes
Sorry my mistake
 
The only things you can do in the class scope is define members. obj/t aren't types.
 
I have forgotten.Morning hangover :)
 
ohayou
 
@AvnerShahar-Kashtan @mr5 Thank you guys
@RoelvanUden Thank you
 
user5500750
6:47 AM
When I run the command; dotnet run does it automatically update new changes that I make?
 
user5500750
Or do I have to stop it and then run it again?
 
It would be easy to test
In fact, so easy to test, one might argue the usefulness of waiting for someone to respond on the chat to tell you something you could find out easily on your own. ;)
 
user5500750
I am actually trying to find a solution that could automatically update the changes when they occur. Or find out whether it is possible or not.
 
user5500750
Like how it works in Spring or Angular.
 
@GSCM dotnet watch
 
6:59 AM
morning
 
@Default \o
 
user5500750
@RoelvanUden No executable found matching command "dotnet-watch"
 
You could Google what I just said and realize it's a separate tool.
 
Are you sure?
 
@RoelvanUden What's Google?
 
7:03 AM
@Neil Bing for you Microsoft twats then :-D
Like, literally the first few results will list docs.microsoft.com/en-us/aspnet/core/tutorials/…
 
Oh no, I stay away from Bing tyvm
 
7:20 AM
Morning
 
oh hai
 
good morning
 
tumbleweed.gifv
 
crickets.wav
 
7:38 AM
I believe there was a Linq instruction to convert every item in a list to string... do you guys know how? An example: I have a List<int>, let's assume it has the following values: {1,2,3}. I need a string like "The numbers are (1,2,3)".
 
You probably don't need LINQ for that. string.Join would be simpler.
var delimited = string.Join(",", numList);
 
$"The numbers are ({string.Join(",", numList)})"
Coolio, that should do the trick.
I kind of remember though, there was a way to do something along those lines with Linq, but improved. I saw it on a facebook codewars kata, someone built the whole messaging logic in 1 line with 2 linq expressions.
I really should dive deeper into Linq.
 
You can use Aggregate to do the same, but it would be denser, less clear code for no real benefit.
 
That rings a bell... I don't like the feeling of being a rookie on something I've used several times, and actually has utility.
 
8:00 AM
@HéctorÁlvarez The numbers mason, what do they mean?!
 
8:12 AM
I guess that's a Lost reference. Pun not intended, but I haven't watched it.
 
(removed)
 
Actually i got it thats why :)
 
You live, you learning. ;)
 
8:37 AM
ahoy mateys o/
 
Harr yarr landlubber.
 
landlubber?!
DO YOU TAKE ME AS SOME KIND OF SEA HATING SAVAGE?!
 
War
0
Q: Creating a simple Async Workflow Activity

WarI'm trying to get in to workflow foundation but apparently i can't seem to get even the most basic implementation of an async activity working. Could anyone point me in the right direction with this activity I have put together in order to make an async OData request using HttpClient ... Firstl...

 
@CaptainSquirrel Squirrels can't swim, thus you lost your sea cucumber powers when you turned into a nut rodent.
 
Aye, tis bad luck to know how to swim and be a pirate
 
8:45 AM
!!js
 
@HéctorÁlvarez That didn't make much sense. Maybe you meant: bs
 
!!quote get js
 
@HéctorÁlvarez 404 quote not found
 
!!quote learn :42064451 js
Coptein, is you dieded? :|
 
9:14 AM
Good morning squirrels and other people!
!!quote get hector
 
Mar 7 at 15:14, by Héctor Álvarez
Best retards
 
Not ded
 
What if I'm not a squirrel or a people
 
Construct me a situation where you are neither a person nor a people
 
9:31 AM
@Butler1233 ...then what are you
 
9:41 AM
Hi Kamil o/
Btw I chose Blazor for further web development :D
That website I built before is ready for now, waiting for input. Now I wanna build a webapp to build teams, with Noob. He does the server side.
 
@Squirrelkiller who are the users of this app?
 
Our dota2 clan
 
I hope nobody uses IE11 ;)
 
Why? Is there no support for IE11 with Blazor?
 
s/Blazor/webassembly
 
9:55 AM
ahh
 
unless you want to write a wasm polyfill for JavaScript?
 
I'm not sure people using IE would be allowed to be in the clan when we found out
 
I doubt you could compile Blazor apps in asm.js
that'd be hilarious
 
> It works in older browsers too by falling back to an asm.js based .NET runtime.
From the github page
 
O_o
 
9:57 AM
@Squiggle no thanks. I've had my fill of polyfills
 
they compiled the .net runtime in asm.js?
I wonder how large that is
 
As far as i'm concerned, there's only one reason to use IE: When your employer tells you to. (Because either some intranet site only works with that or you have to support it.)
What's asm.js?
 
AFAIK, asm.js is an interpreter for web assembly subset of JavaScript
actually no that's not true
it's just a low-level subset of JavaScript, designed to be highly optimised. I think you need to compile your code into asm.js.
which is why I was amused at the thought of doing that with the .net runtime and bundling that with your web app
I guess that's already been done to some degree with jsil.org
 
That's just as amusing as the idea that you'd bundle an entire browser and server framework with your application to run a cross platform chat application
Oh wait
 
I have to admit, @Squirrelkiller, adopting wasm right now for toy projects doesn't seem like a retarded idea
a 5MB initial download is actually fairly acceptable for PWAs
and browser support is generally good
 
10:08 AM
> While this release is alpha quality and should not be used in production, [...]
Sounds like I should use it in production
 
Clearly
Actually already using it in production, letting the clients test it out
universebrain.jpg
 
I've done it before
I did it with Xamarin forms before that was "prod ready" and it was mostly fine
 
!!quote learn :42064451 js
I think this command is broken
 
!!unbreak
 
@Neil That didn't make much sense. Maybe you meant: break
 
10:22 AM
lol
 
story of my life
 
hey nice, JSIL seems like the first try of making a web assembly
 
10:43 AM
Can I have VS automaticaly publish my webapp on build?
wait that sounds like something I'd ask google
 
@Squirrelkiller I've moved my development environment to a standard IIS for that very reason.
I want it to be deployed all the time
We had been arguing why, since IIS Express in explicitly configured to run development builds, and the standard one doesn't support edit and continue (for some reason).
 
I have IIS installed and actually like 3 sites up and running. One of them teambuilder. But every time I build and wanna try, I have to click publish, and then click publish again. Hoped there was a kind of CI for that.
 
You could simply build to an IIS app and let it do the work itself.
 
I haven't worked much with IIS, but what little I did work with it, it gave me nightmares for weeks afterwards
 
why
 
10:54 AM
I just told IIS "listen for this URL for https requests and give them the website in this folder".
Now I just throw the wbesite in that folder, and it works
 
Yeah why, the terrible setup options? The inconclusive permission system? The variety of options, all thrown into a big bucket? Or maybe the difficulty to setup an application pool correctly the first time?
This is like World of Warcraft, it doesn't start getting fun until you have dumped 200 hours into it.
 
I had today special meeting with important person in Apple but he didn't wanted to listen about closing company
 
New Website -> Folder -> Done
:)
 
@J.Doe Maybe you understated the importance of border radiuses? Let them know the corners of their phones have too much border radius and are actively spied on by the NSA.
 
They will not use border-radius in official apps cuz they will avoid PWA
Thats one of good thing
 
11:00 AM
Whats that got to do with PWA, and why would they avoid that?
 
But apple has border-radius on all the app buttons
all of them have rounded corners!!!!
 
@CaptainSquirrel My point exactly.
I bet they can read your fingerprint every time you press each button
 
Yeah but that is iOS intenal stuff - NSA have a backdoor to that anyway
 
They know exactly who opened Reddit at 13:30
 
Yeah, but not through a old exploit that got fixed years ago :P
 
11:10 AM
That's what J.Doe wants you to think!
@J.Doe Why are you against free speech again?
 
Because free speech means people can disagree with his statements
 
@CaptainSquirrel Freedom of speech means he can tell us his opinions in the first place, disagreement is beyond that.
 
dis true
Not gunna lie
im not a big fan of the fact that chat will send a notification each time a message with your name in it is edited
god damn border-radius exploits
 
I need some helpando
What is the current to-go approach for creating backend microservices in .NET Core that don't need no endpoints?

Would you simply setup an ASP.NET app and just .. not use any endpoints?
Would you hook up a server engine to a console app (like Kestrel)?

The latter makes more sense to me, but then again, there might be better solutions.
cc @RoelvanUden you are a wise man, maybe you know :D
 
11:26 AM
hey guys does anyone know if its possible to use EF Core 2 to run a Stored Proc and return the results to a model not held in the entities list int he DbContext?
 
@KamilSolecki you mean like a WCF service?
 
@CaptainSquirrel More like a system service
 
@KamilSolecki I thought the likes of your never had questions.
 
@HéctorÁlvarez I do sometimes have questions, I do :P
 
That makes me feel a bit less stupid. It's usually me asking questions.
 
11:38 AM
@Kamil Fortunately, or you'd become one of those devs who do everything right (in their opinion), can fix their stuff, but noone else can read their code and eveyone asking why something is coded like it is he gets offended
 
@KamilSolecki I would make it a service. :-)
If you don't need any end-point.. why is ASP.NET involved.
 
you can't dockerize a windows specific service
 
But you can dockerize an .NET Core CLI
 
I don't understand under what circumstance you'd need a service that has no endpoint.
Is it isolated for any reason?
 
It does backend stuff only
and it pushes out things through AMQP
 
11:47 AM
But does it run by itself? Like in the background.
 
a long running process, like a service, yes.
 
Oh I see now.
I saw ASP.NET and immediately thought RESTful web API.
 
hey, my timespan.fromminutes is returning an incorrect answer
 
hey, fix it
 
@KamilSolecki That sounds like a job, as @RoelvanUden stated, for a .NET Core console app which runs automatically when the container is loaded.
 
11:56 AM
@AvnerShahar-Kashtan it exactly is, but I need to keep it alive
Kendall suggested a never-exiting non-background thread
but Im not very in favor of making fake work to keep it alive
 
@KamilSolecki That's not fake work. That's simply work.
 
@AvnerShahar-Kashtan It needs to stay alive regardless of if there is work to do or not. It would subscribe to AMQP Buses, and only act upon incomming data
 
What you need is an application service of some sort. Something to manage the process, see it's alive and restart it if necessary. When using containers, the idea is that you simply dump the container and launch a new one, right? So what's the problem? CLI runs on startup and starts working with a while(true) loop, or better yet, listening to notifications from the message bus.
If it crashes, your container monitor spawns a new one.
If this was Windows, I'd say - use a Windows service, because that's pretty much the same thing. Your service starts up and immediately starts listening to messages from the bus. If it crahses, the Service Control Manager will restart it. In effect, the SCM is the equivalent of kubernetes or whatever container management solution you use. But architecturally it's no different.
Docker allows you to not care about cleanup. Service crashed? Just spawn a new one. The other side of this is that the service doesn't have to care about receiving signals to exit gracefully. It can block the main thread with a while (true) loop knowing that "shutdown" simply means the whole container will be destroyed.
 
I know all that, I just dont like the concept of while(true) :P
I know, I'm being picky
 
So instead of that, use a blocking collection on top of the message bus
 
War
12:07 PM
3
Q: Creating a simple Async Workflow Activity

WarI'm trying to get in to workflow foundation but apparently i can't seem to get even the most basic implementation of an async activity working. Could anyone point me in the right direction with this activity I have put together in order to make an async OData request using HttpClient ... Firstl...

 
Your logical design is "endlessly listen to bus messages and handle when they arrive". You're gonna have a while loop there, either in your own code or hidden in a library that polls the bus and raises an event.
 
War
i thought message bus implementations typically have an eventing model
pub sub
 
@KamilSolecki Eh.. it's going to happen whether or not you see it.
If you totally hate while (true) make it a while (!_isDisposed) instead!
 
I've got an EF entity, Database.Widget, and I'm writing an MVC model, Models.Widget, since I've heard from four different people that having both is a good idea. I've made the properties, and now I'm writing the constructor. Should I have it take the WidgetId int as a parameter, and then fetch the Database.Widget from the DbContext? Or should I have it take a Database.Widget object, and depend on the caller to handle the DbContext themselves? Or should the constructor take no arguments?
With the expectation that the caller will set all of the properties themselves, allowing the Models.Widget to be completely ignorant of the fact that there even is a database?
 
Models.Widget should be source-agnostic
Let Database.Widget handle the db magic
 
12:23 PM
Ok. So Models.Widget can't have any database-to-model conversion code. And Database.Widget also can't have it because the file is autogenerated and it will erase my methods every time I rebuild the edmx from the db. So I guess I need, like, a WidgetDatabaseToModelUtility class that converts one to the other
"Didn't the room collectively tell you to ditch db-first and do code-first instead, like a week ago?" you think. Yes they did, but I'm trying to only make one change at a time. In my experience doubling the number of simultaneous refactors will square the number of potential bugs
 
Models.Widget can have conversion code. It just shouldn't have anything that relies on the dbcontext directly.
Also whoever told you not to do db-first is dumb. We're exclusively db-first
Then again, that's probably due to the fact that our database 1) predates EF by quite a bit and 2) has a really dumb schema
So we hide all that weirdness behind views and stored procedures and then just tell the model builder how to translate that into entities
Our model builder class is....quite large.
 
War
@Kevin I took the approach of not having a model at all on the server (but i do MVC very differently to most)
 
I think you do most things differently to most but that's part of why we love you.
 
War
I completely separated my db & business logic from the application that consumes it
 
#microserviceallthethings
 
War
12:29 PM
I only get entities via OData calls that then get serialised and given to the client (in my case mostly browsers) where I consume using MVVM
 
War
I don't however follow a "typical microservices" stack model ... my entities are intelligent and contain all the logic they need to handle actions on them
For example I might have code like this ...
Db.Get<Invoice>(123).Approve();
await Db.SaveChangesAsync();
getting entities is mostly "typical microservices" type functionality but applying changes / performing business actions is done through calls on the entity itself
so from the browser I would issue a HTTP POST request to something like ~/Context/Invoice(123)/Approve() to perform the operation
i work on the premise that the entity graph is exactly that ... a complex graph and so an action performed on one entity may have side effects on others but that's down to the entities in the model to decide for themselves
 
Is there an idiomatic way to determine whether an object's properties have changed since its creation? It would be nice if Model.Widget had an IsDirty attribute that evaluated to true if I needed to propagate changes back to the Database.
 
EF should do that automatically unless you have AsNoTracking enabled
 
@Kevin In EF, you can access the change tracker.
 
War
12:35 PM
there is if you use an ORM and you are talking about an entity
but for a model you'll have to implement object proxying to get that
 
3
A: How to determine if an entity with reference properties in EF 4.1 is dirty?

Nelson ReisDbContext API already exposes options to check if an entity or some properties were changed. to check if an entity was modified: var myFooHasChanges = context.Entry(myFoo).State == EntityState.Modified; to check if a property was modified: var barWasModified = context.Entry(myFoo).Property(u ...

 
The changes are being made to the Model.Widget, not the Database.Widget.
 
War
^ (as i said, you'll have to implement change tracking yourself)
 
Then no, you'll have to do that yourself, or make them spit out events or something and do it yourself.
 
I wonder if the four people who told me to separate Model from Database were aware that I would have to re-implement this kind of complicated behavior when they recommended this design to me
 
12:38 PM
I honestly have no idea what it is you're doing in the first place.
 
War
probably ... most people say "do x" not considering the problems it might create
 
Is there a reason you can't commit your changes back to the DTOs to see id it's a change?
 
@RoelvanUden Kevin has an uncanny ability to get himself into the most sticky of situations
@Kevin just make it dynamic
 
I see.
 
Note, do not actually do that.
 
12:40 PM
@RoelvanUden Long story short, it's a CRUD app that lets users manipulate Widgets.
 
War
@Kevin this is part of the reason i use OData based back ends ... a patch request will update only the stuff you post back (you can post part of an entity, and it'll figure out what the changes are that need to be applied
 
Alternately, if they implement INPC, you can just set IsDirty anytime someone raises PropertyChanged - it will generate false positives if you're not careful but it shouldn't have any false negatives
 
Strong suspicion that I'm on a wild goose chase since I'm running into so many road blocks for what may be the objectively simplest use case for the MVC framework
 
War
@kevin is your client a desktop app ? e.g. WPF ?
 
@Kevin It does sound awful, whatever it is you're doing.
 
12:41 PM
Ugh. I find myself wanting to await an async operation inside an AutoMapper mapping.
 
If the recommendation was to split DB entities from MVC viewModels, that makes perfect sense. You just need to map the viewModel back to the DB entity and call save..
 
It's like I'm going to construction dot stack exchange and saying "hey I'm trying to build a shed, but my bricks keep dissolving into goo when it rains" and everybody says "weird, my bricks work just fine, have you tried this workaround?" and only a month from now I'll discover that I'm using unfired clay dug up from my backyard and everyone else is using cinderblocks from Home Depot
 
War
@RoelvanUden why do people suggest that ??? it feels like an odd thing to do to me ... adding extra resource usage on the machine for basically no net gain in functionality surely ??
@Kevin welcome to development
 
Perhaps the prudent approach is to simply assume that the Model.Widget is always dirty when I'm inside the WidgetController Event post method. If the user makes no changes and clicks "save" on the Edit page, this will create an unnecessary trip to the database, but I guess that's just too bad for the user.
 
War
@AvnerShahar-Kashtan been there ... evil as hell ... I resorted to thinking that automapper actually gains us nothing and instead I just created a reusable async expression for the entire type map
 
12:47 PM
@War Because your database and view model have different responsibilities. The view model, quite literally, models the view. At times, it will be similar to the database model, and at other times it's wildly different. That's why you don't ever use database entities in your views. You always use a view model. Then, when you've validated the view model and want to persist certain changes, you put the changed values back onto the database model and persist it. It's not weird at all.
 
War: In WPF, you don't want to (it's against the way the system is structured) to do any of your nice presentation code, string formatting, etc. on the window/page/control itself. So you create a View model that exposes a surface with those features, and it translates from your backing objects (like a database). If it's built well, when the database schema changes, the ViewModel doesn't change it's API surface
 
War
@RoelvanUden sorry bad question ... it was more ... XY, copy paste the entity, modify to fit view then have to handle mapping ... that sort of gives us nothing of value IMO.
Surely it would make more sense to just make the entity a property on the view model
I guess my issue is not understanding the value of view models but more the reason people implement them the way they do
 
In my case, the value of the view model is that it doesn't get clobbered when I regenerate my db-first database model.
 
@War Ah, like that. No, I think it's bad to model bind (the automatic MVC capability) a database entity. It has no place nor purpose. You haven't added it to the change tracker, nor should you ever. There's no validation on it, and ultimately, no purpose to have it. Thus, you explicitly choose which properties can be assigned to a view model, write your appropriate view model validation logic, and map it all back to a db entity if it's valid. That's the only thing that makes sense, imo.
That said, yes, there will be a lot of entities and view models that will start out quite similar, and possibly never diverge very much. I still think it's a good thing that they lead two very separate lives, because they are and always should be.
 
@Kevin to be honest, we don't really use db-first or code first. Yes, the database definitions are what get considered first, but we don't use auto-generated entities, either, we write the mapping code ourselves.
But as noted our database is mainly cruft at this point.
 
War
12:55 PM
@RoelvanUden hmmm ... I started out like that and found I was duplicating a lot of property declaration and making minor trivial changes but only to add further UI based constraints or to mark something as requiring a certain type of control to be bound to or similar so I took the stance that when performing databinding I needed only raw data and some form of meta data which doesn't require being done on the server.

Ultimately i ended up doing mvvm on the client and basically leaving MVC alone
it's quite a complex subject IMO ... at a glance it seems simple but then as you dig in things get problematic for one reason or another depending on each choice made
I tend to go for the solution that requires the least amount of code though with the logic being "less code, less potential bugs"
 
Sure, if you go the API route with SPA/desktop client, you can get away with far simpler code. But I was purely talking in the traditional request/response model MVC was initially written for. I agree though, that a SPA/desktop client is probably easier in the long run.
 
In which case, you still end up with viewmodels, only they're in js/ts on the client, so they're already quite different than the DB models they're based on.
 
My thing is a web app, incidentally
 
@AvnerShahar-Kashtan Pretty much, sure, but validations on GUIs are often easier because of the facilities that are included. That said, yes, there's still duplication going on.
 
I've decided I don't need an IsDirty attribute on the Model.Widget. I'll just write a Model.Widget.persistTo(Database.Widget) method that writes all the data back to the db model, and let the db model decide for itself whether it's dirty
 
1:02 PM
That sounds entirely reasonable.
 
I want to create a project for testing (AJAX). Is creating a full mvc project the only way doing it .. isn't there a little project type, or lighter option to be able sending and receiving requests?
 
Style chat: do you guys ever use this.whatever in a class definition, even if writing just whatever would be unambiguous to the compiler?
 
Yes
That's one of those things that seems innocuous until it bites you in the ass
I also use { } even when it's syntactically valid to omit them
 
I see it a lot in my workplace's legacy projects, when converting to/from similar objects. this.NumSpokes = myDatabaseWidget.NUM_SPOKES, for example.
 
Explicit is better than implicit.
3
 
1:06 PM
I never don't use curly brackets around one-line blocks, because I trust no one
 
#gotofail
 
@Kevin No. I find it to be verbose and pointless. Better naming will almost always make it unnecessary.
 
The closest I get is something like if(someCondition) { doSomething(); } on one line
 
I don't mind that.
 
@MikeTheLiar Precisely!
 
1:29 PM
Hi! A quick question: are you aware of a tool like StyleCop & FxCop, but for Visual Studio Code? I searched on the extension marketplace, but could not find anything of interest. Thank you!
 
46
A: Visual Studio Code Analysis vs StyleCop + FxCop

Ben SVisual Studio includes FxCop + more. From the developer blog of FxCop: Sorry about my ignorance, but I assume FxCop is completely separate from the Code Analysis in VSTS? More specifically, I assume that if I install the new version of FxCop, VSTS will not take advantage (no shar...

 
Hmm I have a feeling that MVC won't be able to automagically figure out what kind of html input is necessary for the Models.Widget.Owner property, since its type is a custom User class. Wasn't a problem when I was feeding Database.Widget straight to the view, because at that time it was OwnerId, representable as a text box containing an int.
But I'm getting ahead of myself, since my index page doesn't work yet. How does a cshtml template page know what type its model is? I changed @model<Database.Widget> to @model<Model.Widget>, but it's putting red underlines beneath all of my new properties, and autocomplete shows only the properties of the Database.Widget type.
 
I don't know enough about MVC specifically to be able to help. I'm guessing it's an out of date using or something like that that's was auto-refactored to reference the database widget instead of the model widget but that's just a semi-educated stab in the dark
 
@MikeTheLiar Unfortunately, that question title is misleading, it was created before VS Code existed. The wording of the title should be read along the lines of: Visual Studio "Code Analysis" vs "StyleCop" + "FxCop".
 
False alarm, the squiggles went away after I made some unrelated changes to the document. Must have flushed out the cache, or something.
 
user5500750
1:40 PM
How do I check whether AutoResetEvent is in a waiting state?
 
@Robotron TBH I just googled "vs code stylecop" and it was one of the first results
 
It definitely wasn't just surface-level weirdness with Intellisense or whatever, since trying to run the project caused a CompilationError. But whatever.
 
@MikeTheLiar Yeah, me too. Nevermind, thanks for the effort.
 
If my Database.Widget has a one-to-many relation with Database.Comments, how should I represent that on the Model side? If I do something like this.Comments = (from comment in row.Comments.ToList() select new Model.Comment(comment)).ToList(), I see two problems: 1) this wrecks any lazy loading that EF might have; 2) this creates an infinite loop if the Model.Comment constructor has this.Widget = new Model.Widget(row.Widget)
I could make Comments a read-only property that creates a new Database.Widget instance and fetches its Comments collection and converts them to a Model.Comment collection on the fly, but then I might end up creating several new instances of the same Database.Widget during the lifetime of the page.
It would be nice if the DbContext was smart enough to keep copies of all fetched rows in a cache somewhere, and check that before making another trip to the db, in case I'm making the same query more than once
 
1) don't ToList it and 2) nav properties shouldn't create an infinite loop unless I'm misunderstanding what you're saying
 
1:52 PM
Lemme see if I can construct an MCVE for #2
Regarding #1, if you mean the .ToList following row.Comments, when I tried leaving that out, it complained Only parameterless constructors and initializers are supported in LINQ to Entities.
So ¯\_(ツ)_/¯
 
Oh I see, because you're using it to construct that Model.Comment later on in the query. I would've thought Linq would've been smart enough to work around but I guess not.
Uh, I dunno, switch to query syntax?
 
Here is my MCVE. This produces an infinite loop of Initializing ModelComment. and Initializing ModelWidget.
 
@Kevin yes?
you're calling each constructor in the respective constructors
lol, maybe I should read the first of your messages
 
Yes, I am. I understand why this is occurring. I'm just wondering what the idiomatic approach is for converting cyclic-refenced objects to other cyclic-referencd objects.
I have a half-baked idea that involves additional constructors with signatures ModelWidget(DbWidget row, List<DbComment> comments) and ModelComment(DbComment row, DbWidget Widget). These would not create any new objects. Then the one-argument constructors invoke these.
 
2:07 PM
well, I guess you should reference the same class instead of creating new ones.
sounds sane to me
you said you're fetching it via a dbcontext?
wouldn't EF help you with creating the relationship then?
 
Yeah I'm not 100% sure what's going on here but typically using EF you wouldn't explicitly populate the children, you just define the relationship and let EF handle the population for you
 
EF has no problem representing the relationship on the Database object side, that works perfectly. EF is entirely ignorant of my Model classes, and I think I'm supposed to keep it that way. So it can't help me out directly.
 
then define one as the "Parent" and let that class create it's children, not the other way around.
maybe.. it's hard to say without the bigger context.
 
Here is a quick-n-dirty implementation of my half-baked idea. This successfully avoids an infinite loop.
Although I think I need to test to make sure that it still works when there's more than one comment
 
2:24 PM
@Kevin Why do you have these model classes?
They're not view models are they?
 
They are.
 
Then why do they need nested relationships and such?
 
Because the view for the Widget model contains information about its comments, and the view for the Comment model contains information about its widget.
 
Don't they have their own view models?
 
In particular, the Widget view has a "number of comments" column, and the Comment view has a "commenting on Widget # <widget identifier goes here>" header
 
2:27 PM
I would imagine to see these views then..

Comment.cshtml
Widget.cshtml

And these view models:

CommentViewModel
CommentWidgetViewModel
WidgetViewModel
WidgetCommentViewModel

... Own view models per view.
No odd cyclic dependencies needed.
Or, in this particular since you don't need anything except the id of the widget.. just tack on a WidgetId on CommentViewModel.
 
+
I still think the widget creates comments and passes along itself which comments use as their parent :)
 
... Is this not a common use case? I would think that CRUD apps would almost always need to traverse relations in this way.
 
look into master-detail I guess?
 
Well, not really, no. A view model is a model for a specific view. It's 9/10 times as flat as it can be. In your case, a CommentViewModel may be as simple as {id, text, widgetId}.
 
So now I'll have three classes for every table in my database :-I
 
2:30 PM
If you needed more information from the widget, you'd have more properties. At some point, it becomes too much organizatory-wise and you want to split it off to create a subsection of your view model, but that's really rare.
@Kevin You have one class for each table in your DB, and one class per view.
 
Provided I go for the flat design you describe, and remove the comment count from the widget view and the widget identifier from the comment view.
Otherwise I need CommentViewModel and CommmentWidgetViewModel and DatabaseComment and WidgetViewModel and WidgetCommentViewModel and DatabaseWidget, which makes six
Unless I make widgetId a property of CommentViewModel, which isn't inconceivable
 
Yes. That is if you make sub-viewmodels. In most cases, one database model and one view model/view will suffice.
In this case it sounds perfectly reasonable.
A general rule of thumb: if your view model contains more stuff than you're displaying for that particular view, your view model is wrong.
 
It just seems like a maintenance headache because now every time someone wants to add more widget information to the comment view, they have to modify the CommentViewModel class to pull in more information from the DatabaseWidget
 
@Kevin Yeah and that's EXACTLY what you want. It'll be an explicit action that has to be reasoned about. From a maintenance and non-magicness pov, this is perfect. It's not a headache at all.
See.. if you want to change the view to show more information, you have to change the view model to include that information... makes sense to me!
 
if you inject the class widget to the viewmodel it would merely be adding
public string WidgetInfo => widget.Info;
 
2:38 PM
^ And this is how I do it, too. :-)
 
Ok. I wanted my class definitions to start with everything plus the kitchen sink, and then never touch them again for as long as the database schema stays the same. But if you say that view models should be exactly as large as they need to be to display the information you currently want to display, I'll try for that.
 
that's what I usually do anyway
 
It makes a lot of sense to do it that way.
 
I do have a lingering concern that one-to-many relationships will produce some painful non-OOPy interfaces. Let's say I want my widget page to also display "most frequent commenters of the last week". Then I might add two properties to my Widget model, List<string> commentAuthors and List<DateTime> commentCreationDates. Then I have to cross-reference these lists to determine the names of all authors from the last week, then find the most frequent entry of that filtered list.
This is a contrived example, but maybe it communicates my basic concern. I don't want to ever have to write a for(int i = 0; i < someListProperty.Count; i++) loop
 
Author seems to be something attached to a comment - it shouldn't be in the widget
 
2:44 PM
But I have to embed it in the widget if I want to display it on the widget view, and if I don't want to define a WidgetCommentViewModel class.
 
if the viewmodel wants to look into it, fine. but not the model. then the viewmodel can do comments.SelectMany(c => c.Authors)./* do somthing*/
 
I think the answer is "suck it up and write a WidgetCommentViewModel class". At first glance this looks like it doubles the amount of code I have to write, but if I stick to the principle of "only write properties for what you are actually using", then the WidgetCommentViewModel can be substantially smaller than the CommentViewModel.
 
well, I wouldn't agree on the WidgetCommentViewModel + CommentWidgetViewModel.
one seems to be enough
I still get the feeling that you're describing a master-detail
 
I think Roel hinted earlier that, my current design doesn't yet require a full-fledged WidgetCommentViewModel, since I only want to display the number of comments that the Widget owns. Scalar properties representing aggregate data over a collection of child elements can simply be represented by a scalar property initialized in the constructor.
So my models could in principle be as simple as pastebin.com/kLVed4fe
 
05:00 - 15:0015:00 - 22:00

« first day (2735 days earlier)      last day (2214 days later) »