Save a Trip to the Database with Deletes in Entity Framework

In both deletes and updates there's an assumption that you have to retrieve the corresponding entity object from the database. With an update, you pull back the object so that you can set its properties; with a delete, you retrieve the object so that you can pass it to the corresponding collection's Remove method. That delete code might look like this, for example:

More

Posted by Peter Vogel on 06/03/20160 comments


Convert a List from One Type to Another

You have a List of one object but you really want to have a List of some other object. If it was a single object, you could cast from one to the other with a single line of code:

Dim pCust As PremiumCustomer = New PremiumCustomer
Dim cust As Customer 
cust = pCust
More

Posted by Peter Vogel on 06/01/20160 comments


Organize Classes into Folders in Class View

It doesn't show often in the columns I write here, but I'm a big fan of organizing large projects into files. You can, of course, create those folders in Solution Explorer, which adds the folders to your file system.

But, if you use Class View, you know that your Solution Explorer folders don't show up there. As the number of classes in your application starts to increase, the usefulness of Class View starts to diminish. If you want, though, you can also organize Class View into folders … and those folders don't have to match your Solution View folders.

More

Posted by Peter Vogel on 05/20/20160 comments


Changing Parameter Values with out and ref

By default, any values that you pass to a method in a parameter are protected from change inside the method. In this code, for example, I know that my variable NotChanged can't be different after I call the method. In this code, the test on the last line is guaranteed to be true:

More

Posted by Peter Vogel on 05/05/20160 comments


Set Culture Information in One Step in .NET Framework 4.5

In the .NET Framework 4.0 or earlier, you have to set the culture for each thread individually. At the very least, this meant setting the CurrentThread object's CurrentCulture and CurrentUICulture properties. In the .NET Framework 4.5, you can do it in just two lines of code for every thread in your application, using the CultureInfo object.

More

Posted by Peter Vogel on 05/03/20160 comments


Get the Path to Your File in Visual Studio

One of the best things about Solution Explorer is that I can right-click on a Project, pick "Open Folder in File Explorer" and, a second later, I'm in Windows Explorer, able to work with the files that make up the project. This also works with Solutions or, really, any folder displayed in Solution Explorer (for example, if you have Show All Files turned on, you can use this with the bin folder or obj folders).

More

Posted by Peter Vogel on 04/21/20160 comments


Organizing Visual Studio Project Templates

Visual Studio has lots of templates but you probably use only a few of them. Why not create your own area with all (and only) the templates you actually use?

It’s easy to do: First, shut down Visual Studio. Then, open Windows Explorer and surf to the folder that holds your templates. This folder can move around a lot. For Visual Studio 2015 Community Edition on my computer, it’s at C:\Users\<username>\Documents\Visual Studio 2015\Templates\ProjectTemplates. For Visual Studio 2012 and earlier, I’d recommend looking in the area of C:\Program Files (x86)\Microsoft Visual Studio<version>Common7\IDE. If you can’t find the folder, probably the simplest solution is just to do a search for the ProjectTemplates folder from Windows Explorer.

More

Posted by Peter Vogel on 04/07/20160 comments


Step Out of Debugging To, Well, Wherever You Want

When you're debugging, you probably know that you can use the Debug | Step Out menu choice to exit a method without having to step through all the code in the method. But, if you want to step out and back to some higher-level method, you can do that also by using the Call Stack.

More

Posted by Peter Vogel on 04/01/20160 comments


Supporting LINQ Queries Calling Your Methods

You have a method like this that returns a collection of premium Customer objects:

Public Function GetPremiumCustomers() As List(of Customer)
  Dim db As New dbContextObject
  Return From c In db.Customers
         Where c.IsPremium = True
         Select c
End Function
More

Posted by Peter Vogel on 03/16/20160 comments


The Special Case Pattern

You have a GetCustomerById method in a factory object inside a repository that, when passed a CustomerId, returns a Customer object. Except, of course, sometimes there isn't a matching Customer object to return. What's the best way to handle this?

You have three options:

More

Posted by Peter Vogel on 03/04/20160 comments


Making Your Code More Loosely Coupled

As programmers, we often talk about how it's better for components of an application to be isolated from each other. We recognize that, with our present programming tools, some level of coupling is unavoidable. We take it for granted, for example, that we have to use "signature coupling": To call a method we must know the method's name, the format of its parameter list and the data type of its return value.

More

Posted by Peter Vogel on 02/26/20160 comments


Add Custom Data to Exceptions

When it comes to debugging problems with your code, a good Exception object is your most valuable tool. I've talked before about how why returning information about an exception using the InnerException object will let you find out what the real problem is without giving away secrets about your application.

More

Posted by Peter Vogel on 02/23/20160 comments


Subscribe on YouTube