|
I want to create a factory class that takes an enum, looks up a type in a dictionary (all t ypes will be from the same superclass), and returns an instance of the type. How the gently caress do I do this? I've made a Dictionary with the enum as a key and a Type as value, but how do add instances of Type representing the same class, and what's the best way to instantiate them? I can't seem to find any good examples.
|
# ? Sep 2, 2015 16:00 |
|
|
# ? Jun 7, 2024 04:04 |
|
If they're guaranteed to have a constructor with no parameters (or always the same parameters), you can use Activator.CreateInstance to instantiate objects from a Type object. I'm not sure what you mean by "instances of Type representing the same class".
|
# ? Sep 2, 2015 16:12 |
|
Bognar posted:If they're guaranteed to have a constructor with no parameters (or always the same parameters), you can use Activator.CreateInstance to instantiate objects from a Type object. OK, cool, thanks. They all have zero argument parameters. What I mean is - in my Dictionary(Of someEnum, Type), how exactly do I create Type objects to it to create the dictionary in the first place? Type itself only seems to have a zero argument constructor. edit: I see you can call GetType() on an object, but what if you don't have an instance of the object available? edit: Oh, Type is abstract. So how exactly to you build a dictionary (or any collection) of types? chippy fucked around with this message at 16:24 on Sep 2, 2015 |
# ? Sep 2, 2015 16:17 |
|
Bognar posted:What does your MailboxProcessor do? Does it have any heavyweight dependencies? If it's just an object with a few methods on it and simple dependencies, then that's not gonna be a source of performance problems and pooling them isn't gonna make a significant difference. At the moment it is really simple and I've been testing it with Apache Bench, on a modest I5 I get some 7800 Requests per second with 100.000 requests and 1000 concurrent connections. What I am trying to do is have no shared state between them and the number of threads is what the framework gives me, you can't really throttle them AFAIK. Should I have an upper limit? Or just run with it?
|
# ? Sep 2, 2015 16:31 |
|
chippy posted:OK, cool, thanks. They all have zero argument parameters. It seems like typeof works in C#, but in VB I'm getting 'xxxxx' is a type and cannot be used as an expresssion'. e: It's GetType. Panic over lads.
|
# ? Sep 2, 2015 16:36 |
|
I'm in a solution with multiple startup projects, a WCF service and the web site it serves data to, and Visual Studio (13) is only loading assemblies used by the web project, not the WCF project, so breakpoints in the service aren't working. I'm told the way to work around this is to add references to the service project to the web project to trick VS into loading them, too. Oh and of course this Isn't To Be Checked In, so it's a pain in the rear end that I get to manage myself. Is that really the best/only way to do this?
|
# ? Sep 2, 2015 19:04 |
|
Run two instances of VS, one debugging the website and one debugging the WCF service.
|
# ? Sep 2, 2015 20:19 |
|
Dr Monkeysee posted:Run two instances of VS, one debugging the website and one debugging the WCF service. This is what I do. In other news, LinqPad 5 is out. Open your wallets.
|
# ? Sep 2, 2015 22:28 |
|
I'd like to implement retry logic for deadlocks, but I'm having some trouble figuring it out. The logic I've found around the place seems simple enough, but I'm not sure how to actually check for a deadlock conflict. The examples I've seen have this: code:
|
# ? Sep 3, 2015 02:34 |
|
I suppose you'll need two if-statements then!
|
# ? Sep 3, 2015 13:17 |
|
I'm having an issue with timers. One is blocking another and I'm not sure if it's how I've set them up or if it's the terrible computer it's running on. Timer1 is polling a serial port for data every 200ms. Timer 2 is launching a print with printDocument.Print() every 30s. According to the timestamps, when the print kicks off the first timer doesn't log the next sample until 2~3s have passed. On a modern i7, this was working fine. I could see the print dialog blip up and disappear quickly. On this ancient Core2Duo the window pops up and is visible for a moment. I tried to decouple the timers by putting the .Print() call into a delegate kicked off with Invoke(), but the problem persisted. Is there some multi-threading magic I'm missing or is this PC simply not up to the task of reading a serial port while printing a document?
|
# ? Sep 3, 2015 21:17 |
|
The Wizard of Poz posted:I'd like to implement retry logic for deadlocks, but I'm having some trouble figuring it out. The logic I've found around the place seems simple enough, but I'm not sure how to actually check for a deadlock conflict. The examples I've seen have this: Check out Polly for doing a lot of that plumbing code. You could just retry on all Exceptions. JawnV6 posted:I'm having an issue with timers. One is blocking another and I'm not sure if it's how I've set them up or if it's the terrible computer it's running on. Are you running in a GUI? This Stack Overflow question says that Timer will capture the caller's Synchronization Context. I personally like to use Tasks and just making Tasks that never end. Just make sure to set ConfigureAwait(false) if you don't need to be on the UI thread. gariig fucked around with this message at 21:37 on Sep 3, 2015 |
# ? Sep 3, 2015 21:24 |
|
I am running in a GUI. It's charting the data it gets from the serial port, nice to look at but not totally necessary. The timers are System.Windows.Form.Timer , so with what's on that SO thread it makes sense they're executing in the UI thread. I'll try to break that out into a task.
|
# ? Sep 3, 2015 22:21 |
|
JawnV6 posted:I'm having an issue with timers. One is blocking another and I'm not sure if it's how I've set them up or if it's the terrible computer it's running on. windows has no guarantee as to when timers will actually fire. 2-3 sec would be pretty normal if your print driver is lovely and taking all the cpu time
|
# ? Sep 3, 2015 22:35 |
|
Use System.Threading.Timer for your data-reading code to make it independent of the GUI. A few seconds of UI freeze for printing is totally normal on old PCs.
|
# ? Sep 4, 2015 10:52 |
|
I've got some questions about caching lookup tables in ASP.NET. The basic issue here is, I imagine, an incredibly common one. We have a page with an edit form that loads its information from an SqlDataSource, and for every field that uses a lookup table, we have comboboxes tied to other SqlDataSources, so we can match the ID stored in the main table with the display name stored in the lookup table. This seems like something that every site does thousands of times, but we're running into efficiency issues. Profiling one of our pages showed our SELECT statements for our lookup table datasources were the biggest source of lag. I would have thought that these things ran once for each page and didn't run again, but it looks like, when we have these comboboxes in a grid, they run for every row. But our data hasn't changed. These things get updated once a year, if that. There's got to be a way to load the lookup table into a server-side datasource that runs once when the server starts, and can be shared by every user on every page that needs this information. But for something that seems so simple, I'm having a difficult time finding any solutions through google. Can someone help point me in the right direction?
|
# ? Sep 4, 2015 19:43 |
|
Zero The Hero posted:I've got some questions about caching lookup tables in ASP.NET. The basic issue here is, I imagine, an incredibly common one. We have a page with an edit form that loads its information from an SqlDataSource, and for every field that uses a lookup table, we have comboboxes tied to other SqlDataSources, so we can match the ID stored in the main table with the display name stored in the lookup table. This seems like something that every site does thousands of times, but we're running into efficiency issues. Profiling one of our pages showed our SELECT statements for our lookup table datasources were the biggest source of lag. It's been years since I've touched webforms, but can't you tie it to an object data source? Then implement an ODS that pulls it from something in-memory that you populate whenever. SQL data sources are awful anyway, they couple presentation and data access.
|
# ? Sep 4, 2015 20:57 |
|
EssOEss posted:Use System.Threading.Timer for your data-reading code to make it independent of the GUI. A few seconds of UI freeze for printing is totally normal on old PCs. Thanks, this sounds like how I should've built it in the first place. It's not as simple as I was hoping as the existing timer tick just grabbed things willy nilly. Am I correct in thinking that in the setup I should pass in everything I'll need in the callback through the state object? For instance it's writing things out to a file through a StreamWriter, my state has to carry at least that StreamWriter through? Or is it better to declare the logResults function as static? edit: Got it working, not proud of how. Thanks for the help! throwing static volatile on the elements from the UI thread and its recording stable samples with minimal changes to the code JawnV6 fucked around with this message at 00:23 on Sep 5, 2015 |
# ? Sep 4, 2015 23:15 |
|
Zero The Hero posted:I've got some questions about caching lookup tables in ASP.NET. The basic issue here is, I imagine, an incredibly common one. We have a page with an edit form that loads its information from an SqlDataSource, and for every field that uses a lookup table, we have comboboxes tied to other SqlDataSources, so we can match the ID stored in the main table with the display name stored in the lookup table. This seems like something that every site does thousands of times, but we're running into efficiency issues. Profiling one of our pages showed our SELECT statements for our lookup table datasources were the biggest source of lag. There is MemoryCache in .Net which has a refresh timer. You can use it to populate an Iqueryable and return from this if is not empty. I've used it in the past, works as it should. See: http://www.codeproject.com/Articles/290935/Using-MemoryCache-in-Net-4-0
|
# ? Sep 5, 2015 07:04 |
|
the roslyn team have some great ideas on nonnullable types wish them luck working out the details!
|
# ? Sep 5, 2015 08:22 |
|
I really don't think it's worth the effort. A much quicker and more appreciated fix would be the NullReferenceException mentioning either the variable name or the type of the thing that was null when it was thrown. And as they say, it creates all sorts of fun and games with generics and structs and other oddities. I also wonder if it becomes a 'virus' like async - you stick it in one place and then you've got it propagating through everywhere.
|
# ? Sep 5, 2015 11:52 |
|
more static checking is almost always better. the only oddity is that the check can't cover arrays- which are already unsound (covariance) and best avoided except in extreme performance requirement situations. there's no particular oddity with generics. if you use constraint object? - the default - you can use default(T), but with object you can't. i see this as the low-cost way to get a considerable improvement to the static type system. it's not perfect, but typechecking is not feasible to make perfect. in particular, it's a LOT more feasible than this: Milotic posted:A much quicker and more appreciated fix would be the NullReferenceException mentioning either the variable name or the type of the thing that was null when it was thrown. there just isn't enough information in IL to provide this information. the null check done by e.g. callvirt is looking at an untyped word on top of the register stack.
|
# ? Sep 5, 2015 13:04 |
|
quote:Add a notion of safe nullable reference types T? to the language, in addition to the current ones T which are from now on non-nullable. quote:From a semantic viewpoint, T and T? are mostly equivalent: they are the same type in that they have the same domain of values. Specifically, because the system is far from watertight, non-nullable reference types can contain null. The only way in which they differ is in the warnings caused by their use. I don't quite get how that would different from the existing Nullable<T> a.k.a. T? structure, which can already be used to wrap any reference type (even though that's not its intended purpose). The only new thing in the proposal seems to be the compiler warnings for lack of null-checks / null-propagation.
|
# ? Sep 5, 2015 13:47 |
|
NihilCredo posted:I don't quite get how that would different from the existing Nullable<T> a.k.a. T? structure, which can already be used to wrap any reference type (even though that's not its intended purpose). Not sure what you mean by saying that T? can wrap any reference type. T? today is always translated to Nullable<T>, which can't be used on reference types because it has a where T : struct constraint: http://referencesource.microsoft.com/#mscorlib/system/nullable.cs,27. My understanding is they won't actually be changing Nullable<T> to allow reference types. The change will be allowing ? to be used with any type, but it won't be translated to Nullable<T> when T is a reference type. So you're right in saying that this is mostly about the compiler warnings for unsafe dereferencing, but wrong in saying that that's the only change, because allowing the ? suffix on any type is a pretty major conceptual change in the language. Personally I like the proposal. It's a good compromise that allows backwards compatibility but should give much better practical safeguards against boneheaded NullReferenceExceptions. Of course we'd all rather have truly non-nullable reference types in C# if we could, but I don't think there's any realistic way to retrofit that at this point.
|
# ? Sep 5, 2015 14:13 |
|
It's certainly the first proposal I have seen that does not have massive problems and one that I can imagine relatively easily making use of in practice. I will need to do some thought exercises against everyday code that I see and try to figure out how it would work. But based on first impressions, I have a good feeling here.
|
# ? Sep 5, 2015 15:01 |
|
LOOK I AM A TURTLE posted:Not sure what you mean by saying that T? can wrap any reference type. T? today is always translated to Nullable<T>, which can't be used on reference types because it has a where T : struct constraint: http://referencesource.microsoft.com/#mscorlib/system/nullable.cs,27. You're right. I was confused by a recent discussion in another thread. quote:Personally I like the proposal. It's a good compromise that allows backwards compatibility but should give much better practical safeguards against boneheaded NullReferenceExceptions. Of course we'd all rather have truly non-nullable reference types in C# if we could, but I don't think there's any realistic way to retrofit that at this point. Compiler option maybe? I mean, if VB.Net can have Option Strict which basically switches it from a strongly-typed to a weakly-typed language, I don't see why you can't introduce some sort of Option Nullable.
|
# ? Sep 5, 2015 15:35 |
|
Yep, even suffering through "why does this have to be optional" type issues at compile time is much better than nullreferenceexceptions at runtime. And I want it to be an opt-out thing for new projects. You get nullability warnings by default. It's funny you call async a virus. I guess it does necessarily propagate through your call graph quite a bit--but how else could it work? The alternative is to block a thread at some point in the process. I find that much of the time it's easy to add the async keyword and wrap Task<> around my return type anyway--certainly easier than refactoring my function to consume a callback would be.
|
# ? Sep 5, 2015 16:35 |
|
async is one of those things which clearly isn't perfect, but is the best we have so far maybe if an *entire* language was async, coroutine based or green-threaded, you could avoid the infecting type..?
|
# ? Sep 5, 2015 16:44 |
|
Gul Banana posted:maybe if an *entire* language was async, coroutine based or green-threaded, you could avoid the infecting type..? Erlang?
|
# ? Sep 5, 2015 19:19 |
|
NihilCredo posted:Erlang? That is exactly what came to mind. Those Ericsson engineers, they knew a thing or two.
|
# ? Sep 5, 2015 23:30 |
|
that's syntactic support for CSP, not for the future monad. it works pretty nicely but it has quite different properties. a future (Task, or rather "anything with GetAwaiter") is agnostic wrt the actual concurrency model, but in erlang actors/processes are a necessity. it doesn't support something like 0-copy async io, which we have today in .net.
|
# ? Sep 6, 2015 06:51 |
|
The nullability thing seems like a good pragmatic solution to a problem that has been resisting perfect solutions for more than a decade (in this context) now!
|
# ? Sep 6, 2015 07:00 |
|
Not sure if this goes here or the SQL thread, but what's The Right Way to insert multiple rows into an SQL database, from a dataset? At present I'm just doing this, but I feel certain there must be a better method. I do fully control what goes into movieList, so I can probably get away with just string-building a command, appending new sets of values, but that still seems a bit icky.code:
|
# ? Sep 7, 2015 19:14 |
|
Well, in some sense it depends on your goals but The Default Way would be to just use Entity Framework, add your items to the DbSet and execute SaveChanges(). To gain some pefromance, save after every 1000 or whatever rows - doing either single row saves or massive list saves tends to be suboptimal with EF (though maybe your data size is so trivial it does not matter?). If you need some crazy speed for a bajillion rows of data, there might be sense in doing something different using bulk import methods specific to the database engine you are using. For SQLite, I see there is some bulk import from CSV file capability in the command-line client - that would be my first point of interest. But do you really need great performance? EssOEss fucked around with this message at 21:01 on Sep 7, 2015 |
# ? Sep 7, 2015 20:56 |
|
EssOEss posted:Well, in some sense it depends on your goals but The Default Way would be to just use Entity Framework, add your items to the DbSet and execute SaveChanges(). To gain some pefromance, save after every 1000 or whatever rows - doing either single row saves or massive list saves tends to be suboptimal with EF (though maybe your data size is so trivial it does not matter?). Will modify it to use EF; was planning to significantly refactor the data structures involved anyway, so changing them to fit EF is probably not going to be a huge inconvenience.
|
# ? Sep 7, 2015 22:05 |
|
EssOEss posted:I suppose you'll need two if-statements then! I thought it was pretty clear from my post that I can figure out how to write two if statements. I was asking if there was a better way that would be more database-agnostic, because relying on some magic error number seems like a code-smell and I assumed there would be a more abstracted way to catch the deadlock. But by all means, feel free to half-understand something and then be a dick about it, you gotta get your wins somewhere right? On another note, I posted this question on StackOverflow and I'm hoping someone here might have some advice. http://stackoverflow.com/questions/32431418/how-to-break-apart-a-list-of-nested-expressions-into-lists-of-single-level-expre/ I want to take a list like this: code:
code:
code:
code:
putin is a cunt fucked around with this message at 00:29 on Sep 8, 2015 |
# ? Sep 8, 2015 00:26 |
|
The Wizard of Poz posted:... You'll need to make a new expression using Expression.MakeMemberAccess. code:
|
# ? Sep 8, 2015 02:00 |
|
The Wizard of Poz posted:But by all means, feel free to half-understand something and then be a dick about it, you gotta get your wins somewhere right? Aww, did I hurt the boy's feelings? Get over it. Two if statements would be the normal way to do it. Don't overthink it - if you want a clean solution, structure your data access logic so you can either avoid deadlocks (can be tricky) or ignore them and fail if one does happen. Once you start "looking inside" database access exceptions, you are already down the slippery slope of special cases and nasty code.
|
# ? Sep 8, 2015 07:10 |
|
EssOEss posted:Aww, did I hurt the boy's feelings? Get over it. It's not about hurt feelings it's about wasting time. If you had posted the second part I would have very much appreciated it because even though its essentially the same conclusion, it actually helps me - and I do appreciate it, so cheers for that!
|
# ? Sep 8, 2015 08:07 |
|
|
# ? Jun 7, 2024 04:04 |
|
EssOEss posted:Aww, did I hurt the boy's feelings? Get over it. Being an rear end in a top hat serves absolutely no purpose here.
|
# ? Sep 8, 2015 08:17 |