Posts

ToMethodObject Refactoring

Image
I love refactoring code. To refactor code safely you need automated tests. Some code, especially code not written using Test Driven Development(TDD), make it difficult to write tests for the part of the code you are looking to change. Usually what you face are a class that has many dependencies, so in your test setup, you have to create all these dependencies to inject into the constructor even though many or perhaps all of these dependencies don't even have anything to do with the part of the code you are looking to change. Or the method that you want to write a change for is private and the calling public method has a bunch of dependencies which again many will not have anything to do with the part of the code you are looking to change. You are thinking: "If only this code was in a public method it would be so much easier to test". But you may have heard somewhere that changing the code just to make it easier to test is bad.  This is a terrible line of thought that has ...

Visual Studio "You are not authorized to access {username}.visualstudio.com." error

Image
 I have gotten this error once before and ended up having to call Microsoft support and have someone walk me through the steps to fix this. We went through multiple things to fix it and I didn't write them down. Now when I try to sign into Visual Studio I get this error:  I then tried to sign in with my GitHub account and got a bunch of Error Script popups. Did some googling and found the solution to my problem here . Thanks to BigOine I went to Tools -> Options -> Accounts and changed "Add and reauthenticate accounts using:" to "System web browser" and I was able to login with my regular account and the {username}.visualstudio.com error went away.

The program has exited with code 4294967295

 I had gotten a new laptop at work and had gotten everything transferred and working except when I tried to debug a web application in Visual Studio it would immediately stop and in the output window I could see the message "The program has exited with code 4294967295". I searched the internet and most of the solutions involved Docker issues but I wasn't running Docker. I discovered that I could run it in release mode and then attach a debugger to the process. It was clunky but at least I could debug again. A couple of weeks later my laptop got a Windows update and then I couldn't launch a browser when clicking on URLs in e-mails or in Teams. Searching for a solution on the web for that suggested looking at my default browser settings. Turns out my default browser was set to Internet Explorer 🤯. I use Edge and Chrome from links on my Windows Taskbar.  I changed the default browser to Edge and I could launch a browser from URL links again and finally debug a web appli...

Interface Oriented Code Organization(IOCO)

Image
When I started experimenting with TDD  and specifically writing the test first, I had a sudden realization as I began to contemplate where should I place this new test I was about to create.  I realized that the simplest place for the test would be in a folder structure that matched the UI for a web app or the API for a web API, and then quickly realized it would be helpful to have the production code folder structure match as well. I had been working on several large complex code bases, some web apps, and some web APIs, and trying to find the code that needed to change or where to add new code wasn't obvious from the code organization. The code was organized in the layered architecture style with UI layer, Service Layer, Repository Layer, etc. kind of like the structure Microsoft uses in  "Traditional N-Layer architecture applications" . With folders for Controllers, Views, ViewModels, etc. This architecture-oriented code organization(AOCO) is really good at telling you ...

DbEntityValidationException

When calling SaveChanges  on an entity context object you can sometimes get a DbEntityValidationException  error but it doesn't give you a convenient message as to what the error(s) were.  Here is a code snippet to get a convenient message from the DbEntityValidationException  object: Try context.SaveChanges() Catch ex As DbEntityValidationException 'Capture all default Entity Framework Validation errors like required fields, text size checks etc. Dim errorMessage As New StringBuilder For Each validationResult As DbEntityValidationResult In ex.EntityValidationErrors errorMessage.AppendLine( String .Format( "Entity of type [{0}] in state [{1}] has the following validation errors:" , validationResult.Entry.Entity. GetType .Name(), validationResult.Entry.State)) For Each item As DbValidationError In validationResult.Vali...

Page.ClientScript.RegisterStartupScript not working

Had a website with a master page and in the the page_load event I was calling  Page.ClientScript.RegisterStartupScript to run some javascript based on the current state of the application. In the default.aspx page the script worked fine but in this other page the script was not getting called at all.   After much research I have learned that  Page.ClientScript.RegisterStartupScript only works if the page has a form element that has the  runat="server" set.  Once I added a form element with runat="server" to the other page the script finally started getting called the same as it did on the default.aspx page which of course had already had a form element.

ASP.NET 2.0 DropDownList EnableViewState

In ASP.NET 2.0 the  DropDownList   EnableViewState  doesn't work the same as other controls like GridView . Fortunately Anson Goldade created one that does. You can learn how here:  http://www.tech-archive.net/Archive/DotNet/microsoft.public.dotnet.framework.aspnet.webcontrols/2006-02/msg00149.html