|
Night Shade posted:Caliburn Micro uses this in its PropertyChangedBase, along with some thread marshalling stuff. The ability to refactor and not have to worry about breaking all of the property changed magic strings is totally worth the tiny overhead and the slightly obscure PropertyChanged(()=>Property) instead of PropertyChanged("Property"). Too much typing, use this instead: public event EventHandler<EventArgs> SomethingHappened = () => { }; Also it may be basically free, but it's not.
|
# ? Jul 10, 2013 17:42 |
|
|
# ? Apr 28, 2024 08:33 |
|
A coworker asked me why I write so many static classes/methods. I said that I don't want to have to create a new object every time I want to use that method. In this case the class and methods are for running SQL queries to populate DataGridViews or update tables. She is concerned that static methods are more likely to result in memory leaks or have a bigger footprint. Is that a valid concern? Should I rewrite this class so it isn't static?
|
# ? Jul 10, 2013 17:52 |
|
Uziel posted:Thanks for this. I'd heard of PostSharp before but never really looked into it and even though we aren't doing anything with MVVM/WPF, this may be a big help in simplifying some cumbersome and repetitive code. Word of warning, post sharp means post-compile stuff happens. First you build your project, then PostSharp reflects it and adds code to it. It complicates things a bit. If you've got use for some of the other stuff PostSharp offers, then it's worth looking into, but I wouldn't add it to your toolbelt just for the INotifyPropertyChanged issue. I was going the postsharp route until I found out about dynamic proxies and interceptors with Castle, and that's my preferred route now. Crazy Mike posted:A coworker asked me why I write so many static classes/methods. I said that I don't want to have to create a new object every time I want to use that method. In this case the class and methods are for running SQL queries to populate DataGridViews or update tables. She is concerned that static methods are more likely to result in memory leaks or have a bigger footprint. Is that a valid concern? Should I rewrite this class so it isn't static? Your co-worker's explanations are bullshit, but non-static classes have a lifecyle which can be valuable for managing things such as SQL connections, and you can have classes implement interfaces and be injected into the objects that call your query methods, so that you can mock the SQL call for unit testing purposes. Consider this. C# code:
Dietrich fucked around with this message at 18:10 on Jul 10, 2013 |
# ? Jul 10, 2013 17:58 |
|
Dietrich posted:you would have to know a record in your database that would satisfy the requirements of your test. And then you have a brittle test that will break for reasons other than the code being broken, which is a testing no-no. Static methods are okay when:
Basically, they're okay if they're responsible for "turning X into Y" or "mutating an instance of X in some way".
|
# ? Jul 10, 2013 18:42 |
|
Crazy Mike posted:A coworker asked me why I write so many static classes/methods. I said that I don't want to have to create a new object every time I want to use that method. In this case the class and methods are for running SQL queries to populate DataGridViews or update tables. She is concerned that static methods are more likely to result in memory leaks or have a bigger footprint. Is that a valid concern? Should I rewrite this class so it isn't static? Dietrich already covered it from a technical perspective. Lots of statics are fine as long as you aren't doing something careless like holding onto disposable objects from a static constructor or Win32 handles. However, the problem with static classes/methods is they are global and globals are evil. For example, if your data access is through static methods any part of your application could be touching your database (views, controllers, model objects, etc) creating unneeded coupling. If your data access is an object that needs to be passed into a class (inversion of control) you can see which classes are coupled to a dependency. Static classes can be helpful for items that are pure functions with no side effects or other dependencies. The usual example would be string helper functions. I don't mind parts of my application taking a dependency on my left/right/substring functions. Plus I can make them extension methods hanging off the string function.
|
# ? Jul 10, 2013 18:44 |
|
gariig posted:However, the problem with static classes/methods is they are global and globals are evil... Static classes can be helpful for items that are pure functions with no side effects or other dependencies. The usual example would be string helper functions. I don't mind parts of my application taking a dependency on my left/right/substring functions. Plus I can make them extension methods hanging off the string function. To be clear, your link about why global is evil was only about why global state is evil. It's not an argument against global methods themselves. The global methods need not even be "pure" -- for instance, Array.Clear(...) modifies its argument, so it's not pure, but it doesn't touch any global state. As long as the global methods only modify their parameters or produce a return value, they're fine. If our architecture is primarily concerned with objects (and their lifetimes and states), then sure it makes sense to have methods on objects which modify those objects. But there are a whole load of lovely program designs with plain minimal objects that have few methods on them, and instead the methods are all global methods that operate the objects passed as arguments. Especially if our architecture is primarily about processes, e.g. using a lot of async, then we'll probably adopt a functional style and have immutable objects and lots of global functions. (but still no global state).
|
# ? Jul 10, 2013 20:34 |
|
WPF question. I have a custom control similar to the following.C# code:
I'm 99% sure I need to execute DataObject.RemovePastingHandler(this, PastingHandler); somewhere. If I make MyTextBox IDisposable and put RemovePastingHandler in a Dispose method, WPF isn't going to call this automatically. I could get the window to call Dispose on all the MyTextBox instances, sounds like that would work, but is that really The Right Way? But where else could RemovePastingHandler go? The MyTextBox finalizer? I'm seeing lots of StackOverflow and MS Connect questions and answers about this, and haven't found any great answers.
|
# ? Jul 10, 2013 21:46 |
|
epalm posted:WPF question. I have a custom control similar to the following. The garbage collector will clean it up eventually because nothing will reference it. If you aren't making hundreds of these things then I wouldn't worry about it. If you are, then implement I disposable, remove the handler on disposal, and dispose of it when you close the host form.
|
# ? Jul 11, 2013 00:32 |
|
WPF does a good job managing the lifetime of its controls. You should never have to implement IDisposable. If you are subscribing to non-WPF events (DataObject.PastingEvent is a RoutedEvent) you can use Loaded/Unloaded instead of the ctor/dispose/finalizer pattern. I do check that my view-models are being disposed properly. Sometimes a UIElement will hang on to a view-model reference through binding. Usually I'll open and close the UI say 100 times, then run !dumpheap -type my.fully.qualified.ViewModel, then run !gcroot if they aren't being GC'd properly. BTW, I would use an attached property instead of extending the TextBox.
|
# ? Jul 11, 2013 01:49 |
|
uXs posted:Too much typing, use this instead: public event EventHandler<EventArgs> SomethingHappened = () => { }; It isn't totally free, but I ran some timing loops in linqpad on an i7-2600: quote:null check unsubscribed - 100,000,000 iterations: 00:00:00.3041283 (3.041283 ns/iteration) To be honest if you're trying to nickel and dime over less than ten nanoseconds, .NET is probably not an appropriate environment for whatever you're doing. Edit: and the added memory pressure is best measured in individual bytes.
|
# ? Jul 11, 2013 03:30 |
|
Still, I'm technically correct, and we all know that's the best kind of correct.
|
# ? Jul 11, 2013 09:46 |
|
In this WPF-project I'm working on I have a basic list of stuff, if you double-click an item or whatever a dialog appears that let's you edit it. Said dialog is opened from the code-behind of the view containing the list, in the dialog I attach a click handler to the Save-button which closes the dialog with DialogResult = true. I check this in the view that opened the dialog and if it's set to true (changes made) I update the list from the server. My problem is this, using TPL the Save-command called on the ViewModel of the dialog returns control super fast, too fast . The view gets the dialog result and updates itself before the ViewModel was done saving changes... I already have an ICommandViewModel interface for ICommands on ViewModels, I figured I should implement an CommandExecuted-Event and close the dialog by subscribing to it instead? But I wan't the dialog to disappear as soon as possible, hide and then close? What are your thoughts?
|
# ? Jul 11, 2013 10:19 |
|
zokie posted:In this WPF-project I'm working on I have a basic list of stuff, if you double-click an item or whatever a dialog appears that let's you edit it. Said dialog is opened from the code-behind of the view containing the list, in the dialog I attach a click handler to the Save-button which closes the dialog with DialogResult = true. I check this in the view that opened the dialog and if it's set to true (changes made) I update the list from the server. From a usability perspective, if a "save" operation is occurring that will change the data being displayed, it makes sense to notify the user that the save is occurring and close the dialog after the save is completed. Otherwise, you return to the parent form, and the user assumes that everything is saved and they continue working. Then, a few seconds later, the displayed data changes and it's disruptive and confusing. And what happens if the save fails for some reason? It would probably be easiest to just await the task and put up a message for the user that says "Saving..." or something along those lines.
|
# ? Jul 11, 2013 15:26 |
|
edit; nevermind....
|
# ? Jul 11, 2013 15:27 |
|
Don't mean to double post, but now I do have a question. We have a ComboBox control that we made that has a "Top Most Item" property (just something to appear at the top of the list, before all other actual list items). In the Design view of the control itself, we do set a default value for this property, so that way when we use the control we don't need to configure that ourselves since 9/10 times it's the same. I'm on that 1/10 instance where I don't want that default property value, so I select my combobox on the windows form and change the Top Most Item property. Then I run the program and it reverts back to the original value. What step am I missing? Unfortunately the guy who made the control is off for the next week and a half and I'm able to get the source, just the dll Sab669 fucked around with this message at 19:11 on Jul 11, 2013 |
# ? Jul 11, 2013 19:07 |
|
Sab669 posted:Unfortunately the guy who made the control is off for the next week and a half and I'm able to get the source, just the dll Uhh, why is all of your source code not in source control? You should start raising a stink about that, it's totally unacceptable. Try ILSpy and decompile the assembly, then you can figure out what it's doing wrong.
|
# ? Jul 11, 2013 19:41 |
|
Uziel posted:Thanks for this. I'd heard of PostSharp before but never really looked into it and even though we aren't doing anything with MVVM/WPF, this may be a big help in simplifying some cumbersome and repetitive code. PostSharp is really cool and powerful, but I hit some of its "free" license restrictions pretty quickly. There are other options like Fody out there. Castle Dynamic Proxy was one other.
|
# ? Jul 11, 2013 20:00 |
|
So you know how you guys told me that if I wanted unit testing in a web service, I should move all the code to a code-behind library, have the web service reference the library, and I could test that? I did that. Unit testing works awesome. Now I have a whole new set of problems. First I set the code up like this: code:
code:
code:
code:
code:
I'm at my wits' end trying to figure out just what the loving hell I'm disposing or not disposing that I should be doing the opposite. Here's literally all the code in the entire project that uses mutex. code:
By the way, sorry for all the psuedo-code and summarizing, but there's 2000 lines of code in the real project. I'm not copying it for realsies. CapnAndy fucked around with this message at 21:35 on Jul 11, 2013 |
# ? Jul 11, 2013 21:32 |
|
CapnAndy posted:So you know how you guys told me that if I wanted unit testing in a web service, I should move all the code to a code-behind library, have the web service reference the library, and I could test that? I did that. Unit testing works awesome. Now I have a whole new set of problems. This is bad. You don't want to have "live or dev" be controlled by a boolean flag. Fields in your service also sound iffy. What on earth is your service doing that it has state? The exception would be for anything injected via the constructor. What does that boolean flag control? quote:
Well, first off, you're not implementing IDisposable properly. Try to implement it properly and see if that solves the issue.
|
# ? Jul 11, 2013 21:49 |
|
Ithaqua posted:This is bad. You don't want to have "live or dev" be controlled by a boolean flag. Fields in your service also sound iffy. What on earth is your service doing that it has state? The exception would be for anything injected via the constructor. Or even better, don't implement it at all. Just wrap whatever you have to dispose in a 'using' block.
|
# ? Jul 11, 2013 22:11 |
|
uXs posted:Or even better, don't implement it at all. Just wrap whatever you have to dispose in a 'using' block. I'm assuming that some of those fields he's talking about implement IDisposable themselves. Really, the code should look like this: code:
code:
|
# ? Jul 11, 2013 22:15 |
|
Having a class with 'global variables' (I assume you mean 'static') that is Disposable where the Dispose method releases those static variables is a terrible idea. What if there were two instances of the class and the first one got Disposed and the second one was still in use?
|
# ? Jul 11, 2013 22:15 |
|
Before I go this route, I wanted to see if there was an easier way to do this. I'm exploring some ideas for a personal project and it relies on data from a site that would need to be scraped. I'll use the HtmlAgility pack to read the data, but I need to be able to post and change what the values posted are. This is the site. Yes, the official ranking site for a fringe sport is in class ASP. =( http://nasgaweb.com/dbase/main.asp Scrolling down to "Rankings", then "Select class for overall ranking" and picking Pro and 2013 as the form values. The form tag is code:
I used Fiddler to see how the values are being passed: "class=Pro&rankyear=2013&x=26&y=10" So I tried navigating to http://nasgaweb.com/dbase/rank_overall.asp?class=Pro&rankyear=2013&x=26&y=10 and I get a 404 page: http://nasgaweb.com/dbase/rank_overall5.asp?class=&year= If I fill those values in like this: http://nasgaweb.com/dbase/rank_overall5.asp?class=Pro&year=2013&x=26&y=10 I still get the 404. This page shows that I can create a session browser class and do it that way but I wanted to make sure I wasn't missing something easier/more obvious. Edit: Apparently the only reason I posted this was so I could figure it out on my own 30 mins later. The below is good enough to start with: code:
Uziel fucked around with this message at 22:45 on Jul 13, 2013 |
# ? Jul 13, 2013 21:37 |
|
If you run Windows 7 and the latest Windows Update is preventing you from loading solutions with Visual Studio 2012 Ultimate, upgrade Visual Studio to the latest service pack update and your solutions should load again.
|
# ? Jul 14, 2013 14:15 |
|
Just curious - has anyone here tried the VS2013 preview yet? http://www.microsoft.com/visualstudio/eng/2013-preview Any feedback or comments you want me to pass on to the team? (also it has "send-a-smile/frown") Personally, I'm most proud about how the callstack window now works with async... http://mdavey.wordpress.com/2013/06/28/vs2013-async-debugging/
|
# ? Jul 15, 2013 16:43 |
|
I don't normally do .NET stuff, but I'm digging into a fairly big WPF project right now, and having everything in one project is getting to be a bit much. The basic set up I have right now is: - the XAML, which is bound to some model classes - the models, using ObservableCollections and INotifyProperty changed to bind to the xaml - some controllers, which get Commands from the xaml and modify the model, as well as loading and persisting it Should these things be extracted into separate assemblies? If these are going to be separate assemblies, what's the preferred Windows way of distributing an exe and its necessary dlls? Is MVVM something I should start caring about?
|
# ? Jul 15, 2013 18:11 |
|
Jerry SanDisky posted:I don't normally do .NET stuff, but I'm digging into a fairly big WPF project right now, and having everything in one project is getting to be a bit much. quote:Should these things be extracted into separate assemblies? quote:If these are going to be separate assemblies, what's the preferred Windows way of distributing an exe and its necessary dlls? An installer of some kind (ClickOnce, MSI, WIX, whatever). quote:Is MVVM something I should start caring about? Yes. If you're doing WPF without MVVM, you're doing WPF wrong, although it sounds like you're doing MVVM or something close to it. The goal is "separation of concerns". MVVM is the easiest way to do that with WPF.
|
# ? Jul 15, 2013 18:18 |
|
Jerry SanDisky posted:Should these things be extracted into separate assemblies? If these are going to be separate assemblies, what's the preferred Windows way of distributing an exe and its necessary dlls? Is MVVM something I should start caring about? For our in house development we just put the assembly and .dlls in a folder in our share drive (one folder per app) and I have written a program that copies them all down to the user's computer then runs the executable. We distribute shortcuts that call this program with the appropriate args. The advantage of this is that we only need to copy files that are changed when we release a patch, and we don't have to deal with creating an installer or clickonce being the complete bastard it tends to be.
|
# ? Jul 15, 2013 19:13 |
|
Thanks. Are there any quick resources for doing "proper" MVVM? I know VS2010 express lacked support for MSI installer projects, is that something I can do with 2012?
|
# ? Jul 15, 2013 19:32 |
|
I'm having trouble getting a WCF web service to tell me why it can't run. The only output when I hit it is a message about an InternalServiceFault that tells me to enable IncludeExceptionDetailInFaults or turn on tracing, both of which are already enabled on the development config. Nothing is written to the trace log. The debugger isn't seeing any exceptions (Just My Code is disabled). Sometimes Session_Start gets run but sometimes it only makes it to BeginRequest (both of which are empty), but then the error gets kicked out without any of my code getting run. If it makes it to Session_Start and I leave the debugger open long enough, I'll see it hit Session_End. What else can I do to try to get any useful information out of this loving thing?
|
# ? Jul 16, 2013 17:37 |
|
Munkeymon posted:I'm having trouble getting a WCF web service to tell me why it can't run. The only output when I hit it is a message about an InternalServiceFault that tells me to enable IncludeExceptionDetailInFaults or turn on tracing, both of which are already enabled on the development config. Nothing is written to the trace log. The debugger isn't seeing any exceptions (Just My Code is disabled). Sometimes Session_Start gets run but sometimes it only makes it to BeginRequest (both of which are empty), but then the error gets kicked out without any of my code getting run. If it makes it to Session_Start and I leave the debugger open long enough, I'll see it hit Session_End. What else can I do to try to get any useful information out of this loving thing? By "trace logs" do you mean IIS logs? If not, did you check those? What about the event viewer?
|
# ? Jul 16, 2013 17:55 |
|
By trace log I mean Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener set up in the loggingConfiguration section of the web.config. There's nothing in my IIS events and all IIS is recording in its log files is the fact that someone got a 500 response when they hit the service URL.
|
# ? Jul 16, 2013 22:58 |
|
Try adding this to your web.config - it's what I use when WCF is being a pain. Don't leave it on in production though!code:
|
# ? Jul 16, 2013 23:07 |
|
Ugh, I'm an idiot - all that was set up in the config. Twice, unlike most of our projects, which is why I was expecting all of the error information to end up in a different file. Hope this makes someone laugh: all of the errors were "Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Logging..." Some day this loving project will build and run without manual intervention :\
|
# ? Jul 17, 2013 15:17 |
|
Munkeymon posted:Hope this makes someone laugh: all of the errors were "Could not load file or assembly 'Microsoft.Practices.EnterpriseLibrary.Logging..." Ahh, I love it when the error handling is causing an error.
|
# ? Jul 17, 2013 15:27 |
|
So earlier on this page, I said I was working on a hobby project to scrape data from a website. I also had contacted the owner to see if I could get read access rather than reinvent the wheel. He actually got back to me but I'm not entirely sure how to respond to this:quote:I think it’s great that you want to do this. I like your idea, if it works out maybe we could make it more general and be able to access any type of data from the database. I'm thinking I might as well just stick to scraping the data using a webclient and the htmlagilitypack than open this can of worms. :|
|
# ? Jul 18, 2013 00:32 |
|
There is a really easy out here -- see if he can setup a way for you to get a copy of the access Db. It ain't good at much, but being transportable in a single file isn't bad. With a copy of the DB you could probably turn around and hand him the scripts that spit out the XML or whatever you want. And yes I'll publicly admit I made a number of access backed web apps though I was young and needed the money and didn't know any better. PS: the model is valid, ask all the cool kids developing against sqllite. wwb fucked around with this message at 00:57 on Jul 18, 2013 |
# ? Jul 18, 2013 00:43 |
|
I'm working on a hobby project and I've done something that's either clever or dumb, but I can't decide which. The application receives messages over a network socket. The messages are in a regular, parseable format. Each message, when parsed, ultimately ends up represented by its own class that implements the IReceivableMessage interface, in order to expose the message's data as properties. There are ISendableMessages as well, but those don't come into play here. I want to have a central place where I can control the message parsing logic, so I started by creating a ReceivableMessageFactory. It looks like this: C# code:
At this point, it has to fire off the correct event. I absolutely hate the idea of doing this a million times for each type: C# code:
code:
I'm considering going back and creating a more generic Message class that exposes the properties as dictionary keys, but I'm trying to keep my public-facing API very clean and easy-to-use. New Yorp New Yorp fucked around with this message at 01:04 on Jul 18, 2013 |
# ? Jul 18, 2013 01:01 |
|
Is there some LINQ equivalent to this Mysql sorting ability? ORDER BY FIELD(FieldName,"heyo", "this is second", "third") Essentially I can make a custom sorting order based on what said field contains, can be numbers, can be strings.
|
# ? Jul 18, 2013 08:38 |
|
|
# ? Apr 28, 2024 08:33 |
|
His Divine Shadow posted:Is there some LINQ equivalent to this Mysql sorting ability? Example: http://dotnetpad.net/ViewPaste/Y8o3Nl1WYUKPO7grljNqXA
|
# ? Jul 18, 2013 10:19 |