|
It's not executed on each iteration, so the reference pointer changes to the last variable to be set as the predicate.(It really hoists the variable outside the loop and you reassign it every iteration). Assigning a variable inside the loop creates a new pointer for each iteration. Closures! Funking Giblet fucked around with this message at 15:10 on Sep 16, 2014 |
# ¿ Sep 16, 2014 15:07 |
|
|
# ¿ May 3, 2024 06:35 |
|
I use NHibernates ISession directly in the controllers. It's awesome!
|
# ¿ Sep 24, 2014 15:09 |
|
wwb posted:Do you get paid by the hour? It works quite well, The session is already an abstraction. I use query objects and extension methods to help out here and there, but otherwise I just pull the data I need.
|
# ¿ Sep 25, 2014 11:35 |
|
crashdome posted:So I am looking for a bit of advice because I seem to be running around in circles from over-thinking. This is only my third WPF app and I want to be simple yet I don't want forced/bastard code ruining it this time. This current app is like others I have to build... simple (6 data tables), mostly just transactional data access (assign, add, spit some output into a table), and will have about 4-5 Views/ViewModels. I'm using EF6 because it has a designer - it's a client thing for some reason - AND I like the change tracking and the transaction/rollback features. I use the above with the mediator pattern. I leave repositories well alone, they create constantly changing interfaces and implementations. A mediator can figure out which query handler to use for each query, each query handler is a class that exists on its own. code:
In MVC, I use the Ayende style session and transaction UOW management (with nHibernate). I can mock data to test for each query if I feel it requires it.
|
# ¿ Oct 29, 2014 16:23 |
|
You seem to be thinking of them wrong. Imagine I said that I wanted a function to multiply a number by 2. code:
Which is basically a function which operates on x which returns 2*x, or as in our case above f(input) = 2input. A lambda in C# would be the same as the above, the input variable is returned as 2 * input input => 2 * input Most people just write x => 2 * x. "With x as an input, return 2 * x" What you are probably thinking then, is why didn't Console.Write work with my lambda function. Well, a lamba is an anonymous function definition, it doesn't actually call (unless you explicitly do so). Passing it to Console.Write, you could expect Console.Write to call the function in it's body and pass it's own x, but as Console.Write doesn't take a delegate as an overload, this isn't possible. The way you declared x as an int didn't make sense in this context. Think of it this way instead. code:
|
# ¿ Nov 18, 2014 08:17 |
|
RICHUNCLEPENNYBAGS posted:You should probably pick one but in my experience the kind of EF that makes sense for readonly stuff and the kind that makes sense for CRUD are different (I usually select into intermediate representations for readonly pages with multiple entities or whatever to avoid lazy loading or neverending queries but for updating an entity I work with the entity classes directly). If you don't use Lazy Loading, use a basic ORM. We use Lazy Loading here, problems arise when there is a fundamental misunderstanding of how the ORM works. A little knowledge and it's no longer a problem. You should be using projections on reads, lazy loading on writes. (Left outer joins will prevent lazy loading if you know what you need ahead of time).
|
# ¿ Nov 25, 2014 10:15 |
|
Che Delilas posted:Oh, the View (the .cshtml file in my case) isn't what the client sees, is it? It describes how to generate the final HTML, which is then sent to the client. That's never really clicked in my head until now. I would still use an object for the view that contains only what you want, as you may run into trouble later if you say, need to post values back and are trying to save the user back to the database, or a junior decides to output a property by accident. Look at AutoMapper which will help you create viewmodels from models using conventions and some configuration, which are just representations of your data which is just used for the view.
|
# ¿ Nov 27, 2014 08:59 |
|
Destroyenator posted:I'm not convinced by it. I've seen it in projects before and you either don't get any information when you rename/delete fields on either side, or you use it's validator which involves writing out either all the mappings or all the differences for each pair of classes at which point why not just write the mapping yourself? Unless I'm missing something major I don't see the value there? I think you're missing something major! You can validate the configuration at runtime which will find unmapped fields, or write a unit test to do as much. Not to mention injectable value and type resolvers which can be reused across types. Funking Giblet fucked around with this message at 11:03 on Nov 27, 2014 |
# ¿ Nov 27, 2014 10:59 |
|
Destroyenator posted:Yeah I've seen the validation as unit tests thing, but the places I've seen it usually involve writing a bunch of exceptions for the missing/extra/renamed fields. That's when I get to: why bother, you're effectively writing the maps anyway at this point? Mapper.AssertConfigurationIsValid is all you need.
|
# ¿ Nov 29, 2014 09:07 |
|
bpower posted:Im learning linq at the moment. This is a good quick reference.. Look at writing a ValueResolver.
|
# ¿ Dec 13, 2014 19:54 |
|
chippy posted:Can I use a List of Strings as a the key for a dictionary? You could extend the list and override GetHashCode to order the strings, join them and call string.GetHashCode, or use an int as the key. It really depends on how important the hash is. If it's only used in memory, then you should be fine. Storing the hash would mean you should use a consistent hash instead of GetHashCode for string.
|
# ¿ Dec 23, 2014 09:10 |
|
For a truly ordered list, the order would need to be explicitly saved in the database in a column. In an ORM when an entity is loaded, then the list property accessed lazily, the order stored in the database should be reflected in the list without having to issue an "order by". This is a feature standard in nHibernate, not sure about Entity Framework. When writing a projection though, you would need to specify your own ordering.
|
# ¿ Jan 7, 2015 11:43 |
|
The Wizard of Poz posted:I've got a really, really basic question that is a bit about personal style perhaps, or best practice. If I have a method that's going to be used to say, update a person's details, what is the best approach: 1) include the details in the signature of the method as parameters OR 2) use some kind of an purpose-made object to hold all of the possible update fields? Second one, just make sure you are using a dedicated viewmodel for updates, and not a model that might expose some fields you don't want updated as it may be possible is certain circumstances to update them (bad automapper usage etc).
|
# ¿ Mar 26, 2015 11:02 |
|
RICHUNCLEPENNYBAGS posted:I'm kind of curious how people use Automapper in real projects. I thought I'd try it out and I quickly found that it was just making more work for me and it seemed like unless your view models were like, almost exactly like your domain models, it wouldn't save you time. It does a lot of things which save time beyond basic mapping. You can validate mappings so that if properties change on the domain model, you will know if you missed them in your view. You can also write generic ValueResolvers which allow you to share methods of mapping specific values (such as localized values / currencies which I've found it very useful for). It can be frustrating if all you are doing is mapping two or three properties each time but it's been a life saver for me multiple times.
|
# ¿ Apr 10, 2015 13:38 |
|
Iverron posted:If I'm understanding what you're suggesting correctly, this was discussed at length back around page 45: You just made the lives of everyone easier. I strongly recommend using the context directly, or through a light service layer to abstract common queries all while avoiding repositories, which most people get wrong anyway! I also recommend handling the transactions in an actionfilter so each action begins a transaction and commits when finished. Funking Giblet fucked around with this message at 21:19 on Apr 26, 2015 |
# ¿ Apr 26, 2015 20:56 |
|
Bognar posted:I don't have much time at the moment to elaborate, but I will say I strongly disagree with the general patterns described here. If you need a context, then instantiate one - don't keep the same one around for the lifetime of a request. Due to the way EF handles object caching, strange and unexpected things can happen. Similarly, you shouldn't just automatically set up a transaction per request. Even if you ignore the performance concerns, you still should reason about which requests actually require a transaction and only use one where it's necessary. I don't use EF, but NHibernate, which will always create a transaction no matter what, and it's bad for performance to not handle it yourself as it will create one per modification, so you might get a few transactions per action. My current setup allows override the isolation level or skipping transactions if required, but will fall back to the default NHibernate behaviour. As for newing up a context, I tend to share the session in the same request, and allow the IOC container to do the work. The session cache is useful when shared also, any weirdness should either be accounted for or is a bug. Having everything in one transaction for a command or request means it will behave the same as SQL does anyway, meaning any changes will be accessible within the session while inside the transaction.
|
# ¿ Apr 27, 2015 16:32 |
|
Bognar posted:Ah, I can't say much for NHibernate - most of the advice I was giving was EF specific. Wait, so EF uses query caching? This is optional in NHib (and should be off for most cases). It should use entity caching per id, so queries skip it. Includes would hit it if only id's are used for the joins, but the above example should lazy load the users if they aren't in the query so nothing should break, And the second example shouldn't work because the transaction should be isolated away from change in the first place. Does EF support clearing the cache before a query in that case? I'm just trying to wrap my NHib knowledge around EF because the above sounds bad.
|
# ¿ Apr 28, 2015 17:19 |
|
This brings me back to my original point, don't encapsulate queries at all, and use projections only, even using those projections directly if possible. (Can't believe I'm about to do this..) code:
Nhibernate can actually batch multiple queries in one round trip like this if you use the Future modifer, which acts like a promise for all other queries marked with future within the same transaction, which is why reusing the transaction and session actually helps. Funking Giblet fucked around with this message at 19:09 on Apr 28, 2015 |
# ¿ Apr 28, 2015 19:05 |
|
https://github.com/jbogard/MediatR This is what I use to simplify controllers. The added benefit is you could mix NHib and EF and NoSql behind the scenes, you only expose IMediator to the controllers. This also helps in horrible cases such as code:
This could be changed to the following. code:
Now each of thesse requests/commands has an associated handler, they themselves are simple pocos. The handler looks like this. code:
This is obviously getting complex, but this is the next step whenever a controller becomes complicated. You quickly see that controllers become nothing more than wrappers around these commands, which with some clever ActionInvoker implementation, might not even need to exist at all... I would even call my queries directly inside the command handlers, no repos or services. Funking Giblet fucked around with this message at 08:58 on Apr 30, 2015 |
# ¿ Apr 30, 2015 08:53 |
|
The idea is that queries and query handlers are added to the solution and just work, there is no adding to a repository interface and implementation class, and then updating the dll in the client etc. It also means you don't need to change the constructors to inject different repos etc, you just have a generic mediator which figures it out for you. Each query can be implemented using any method you like, using any data access you like.
|
# ¿ May 29, 2015 15:37 |
|
Humphrey Appleby posted:Thanks for the reply. I agree on not using Jenkins to deploy. We are not keep binaries in Mercurial. Jenkins is making a commit with the version number of the DLL after the build. We then store the DLL(s) and deploy to one of the testing servers. When someone uses one of the test servers or production the DLL version number is in our logging. We then can always use that version number to look up the commit Jenkins did right after it made a build. The key part for us is being able to tie any log message to a specific commit/point in time. Why not write the commit id of the build to the assembly as the Product Number or something, instead of committing from Jenkins.
|
# ¿ Jun 1, 2015 21:54 |
|
Uziel posted:Regarding C# 6.0, does anyone know if Resharper picks up the new language features and recommends using them? For example, "Hey, use the null conditional operator here instead!" or "Convert this to use auto property initializers". It sure does, even in projects in the old framework...
|
# ¿ Jun 20, 2015 22:26 |
|
uncurable mlady posted:When is MVC6 coming out, anyway? We have a project we're starting soon and would like to use it. I imagine the same time as Windows 10, would make sense. Try developing using OWINn to get a taste that can easily be ported.
|
# ¿ Jun 28, 2015 21:52 |
|
Are any of the async tasks fire and forget? Or can you defer the response via a websocket or something? Using an ESB might help.
|
# ¿ Jul 9, 2015 09:44 |
|
Sedro posted:You need to implement GetHashCode, and yours will throw exceptions when comparing to null. The comparison I highlighted above can caused issues with dynamic proxies, so ensure either you skip it, or unwrap the proxy before comparison.
|
# ¿ Jul 30, 2015 20:03 |
|
Remember. Post = new item Put = replace an item (either insert in existing position and move the surrounding indexes, or replace record so old one no longer exists / is archived. Patch = update fields on item.
|
# ¿ Aug 4, 2015 07:54 |
|
A base ViewModel with an ActionFilter could do it in a nice transparent way, everything would need to extend from it though. An interface could do the same job also.
|
# ¿ Sep 1, 2015 09:44 |
|
Manslaughter posted:Is there a way in visual studio to check a project for all uses of any enums? I'm having serialization problems with them, but its not just one specific enum, and I have a few hundred of them to sort out (tool generated). Write a unit test or something with the below code. code:
|
# ¿ Sep 24, 2015 00:02 |
|
Cryolite posted:Does anyone have any examples of good, well-designed REST API wrappers written in C#? Oh hey, it's my Twitch wrapper (that someone branched) One thing I've noticed from that API is that, I have no way to version the APIs, so I couldn't support Twitch V3 easily. I would probably use automapper behind a versioned API layer or MediaType mapping to improve it.
|
# ¿ May 5, 2016 00:04 |
|
bobua posted:If I try to context.AddRange(myList); I'll get an exception for trying to add existing items(matching primary keys) when I savechanges. How would the context have any reference to the new items I've added to do that change tracking? Have you tried using a HashSet and implementing GetHashCode and Equals? Funking Giblet fucked around with this message at 12:59 on Nov 29, 2016 |
# ¿ Nov 29, 2016 12:55 |
|
chippy posted:Thoughts on the Unit Of Work/Repository pattern? I'm considering implementing repositories to use in my service layer, mostly to assist with unit testing/mocking, but as the UoW pattern's primary use seems to be to make sure the repos all use the same context, this seems redundant as I'm using Autofac to inject my context with an instance-per-request lifetime. How are you handling transactions? UoW will help with this, and you can also wrap the requirement for events on commit / rollback. Handy when using something like a message bus to delay send a message on commit. Don't use repos (it's a DAL, repositories are something else entirely, related to DDD). With repos you tend to lose some ability to optimize queries.
|
# ¿ Mar 10, 2017 10:03 |
|
mortarr posted:Another gotcha is that the validators are single-instance, so if you need to inject stuff like your DbContext, then inject a Func<DbContext> in the constructor and not DbContext itself. Don't do Func<DbContext>, as it resolves from the scope the Func<> is registered into, i.e: root scope, unless you are using the static DependencyResolver inside a delegate registration to help (don't do this). Use a UoW and IDbContextFactory instead to create a new instance and dispose when done.
|
# ¿ Aug 17, 2017 12:02 |
|
mortarr posted:You know how you figure out a way to do a thing, then copy/paste that way into all future projects forever and ever amen, until it bites you in the arse? That's where I am right about now with autofac. Autofac scopes are a pain in the rear end. Basically, never register anything as single instance ever, unless it's truly depends on nothing at all really. Automapper can really bite you on this, as it's config needs to be SingleInstance, but it's instance should not be at all (It can depend on the SingleInstance config, but the tendency is to register the whole thing as SingleInstance, which breaks a lot.) InstancePerLifetimeScope if generally what you want for everything, and just manage the lifetime scopes themselves.
|
# ¿ Aug 21, 2017 13:26 |
|
EssOEss posted:I would use a separate DataContext for the Edit dialog and throw away this DataContext (along with any data) once the dialog is closed (regardless of whether with a save or cancel action). I would go further that if you find any re-use of a DataContext, stop what you are doing right now and eliminate it and make sure you have a new one per operation, that is one for read (and hopefully, you map to a ViewModel), and a separate one for writing. Otherwise you are introducing a multitude of potential issues.
|
# ¿ Nov 24, 2017 15:08 |
|
chippy posted:I'm all for one per request but what's the rationale behind separate contexts for reading and writing? I don't mean all reads and writes are separated, but you should definitely have a separate context per business transaction. Sometimes you need to read before you write (say, if you manage your own primary keys and can have accidental duplicate requests), and you want some transaction integrity too, but storing a context for reuse can cause issues, horrible horrible issues. Funking Giblet fucked around with this message at 09:53 on Nov 27, 2017 |
# ¿ Nov 27, 2017 09:50 |
|
LongSack posted:What’s the overhead for this? gently caress all!
|
# ¿ Nov 27, 2017 09:50 |
|
quote:Claim storage This depends on how you have it configured. You could have a reference token stored in a cookie which then calls to an IDP to retrieve claims on every request.
|
# ¿ Jan 10, 2018 10:55 |
|
LongSack posted:I think I’m missing something with EF. I have a Title class which has a foreign key into the Genres table, so the the Title class has a navigation property of type Genre. Hrmm, I would always create a new context for a new operation, but you seem to have a different problem. You shouldn't need to reload everything, you have it already, just add the new entry to the Observable when SaveChanges is successful, dispose the context. If you need to delete, create a new context and on success, remove it from the Observable. You need decouple your Observable from the DBContext. your ViewModel should probably be a simple POCO, not an EF Entity. This should also make it easy to test
|
# ¿ Jan 11, 2018 10:57 |
|
Ok, this is what I was talking about, your view model is capturing a context, this is not correct at all and is going to cause a lot of issues (as it is already). You are also relying on a destructor to dispose your context, this may never be called. Let's try something small to give you an idea of how first to fix this. In each of your methods, create a new context. code:
Remove the _context variable. Figure out a different way to Populate your observable, and maybe, try not using an EF entity, but a DTO of some sort. This is not the way to do it, but should show some improvements, and show you the way a context should be used (short lived as possible) (I haven't much time now, but we can drill into a proper method of doing it)
|
# ¿ Jan 12, 2018 13:23 |
|
|
# ¿ May 3, 2024 06:35 |
|
LongSack posted:If there is, it would be some deeply weird poo poo involving reflection, but even then I am dubious, because typeof(“foo”) and typeof(string) are almost certainly the same. Wonder what nameof does in this scenario
|
# ¿ Jan 18, 2018 13:28 |