Post-Mortem: Using the Repository Pattern With Entity Framework

Dear all future developers who may get stuck dealing with a couple of applications I’ve written over the past year or so.  I know that much future cursing at me is on the way.  Much to my regret, I confess I used repositories with Entity Framework Code-First.


…and I didn’t just use one or two,  I added at least four or five — on top of a BASE repository.  To make it worse, I even created a BaseController to deal with all this so that now I have inheriting controllers that have to deal with ramifications. 


The severity of the above could have been significantly reduced if I had written proper unit tests.  In one app, I actually DID write (improper — with very little mocking) unit tests first but because the tests now need to run on an independent build server, I have to suffer to re-write them quickly to meet a deadline but, the reality is, the apps need re-architecture.   


I’m working on a prototype with better architecture  but that will probably not have time to go through the the whole integration testings, etc. 


Thanks @hoagieland for your post.  It might be a little extreme but with many valid points.


All who have to deal with this particular flavor should read:


You’re All Doing Entity Framework Wrong

.NET Framework vs .NET Core vs .NET Standard ?!?

I think get a grip on the latest trends only to learn that I’m still steps behind.  Thanks to Immo Landwerth for this clear article and cool tricks.  It’s ten months old but new to me.  😑

.NET Standard – Demystifying .NET Core and .NET Standard

“NET Standard is a specification. Each .NET Standard version defines the set of APIs that all .NET implementations must provide to conform to that version. You can think of it as yet-another .NET stack, except that you can’t build apps for it, only libraries. It’s the .NET implementation you should use for libraries that you want to reference from everywhere.”

Descriptions of .NET
Implementations
OS
Open Source
Purpose
.NET Framework
Windows
No
Used for building Windows desktop
applications
and ASP.NET Web apps running on IIS.
.NET Core
Windows, Linux, macOS
Yes
Used for building cross-platform console
apps
and ASP.NET Core Web apps and cloud
services.
Xamarin
iOS, Android, macOS
Yes
Used for building mobile applications for
iOS
and Android, as well as desktop apps for
macOS.
.NET Standard
N/A
Yes
Used for building libraries that can be
referenced
from all .NET implementations, such as
.NET Framework, .NET Core and Xamarin.

Visual Studio Version History With Toolset Paths

Product name Codename Version Number Supported .NET Frameworks Supported .NET / CORE Versions Release date
Visual Studio 97 Boston 5 N/A N/A Feb-97
Visual Studio 6.0 Aspen 6 N/A N/A Jun-98
Visual Studio 2002 Rainier 7 1 N/A 13-Feb-02
Visual Studio 2003 Everett 7.1 1.1 N/A 24-Apr-03
Visual Studio 2005 Whidbey 8 2.0, 3.0 N/A 7-Nov-05
Visual Studio 2008 Orcas 9 2.0, 3.0, 3.5 N/A 19-Nov-07
Visual Studio 2010 Dev10, Rosario 10 2.0 – 4.0 N/A 12-Apr-10
Visual Studio 2012 Dev11 11 2.0 – 4.5.2 N/A 12-Sep-12
Visual Studio 2013 Dev12 12 2.0 – 4.5.2 N/A 17-Oct-13
Visual Studio 2015 Dev14 14 2.0 – 4.6 1 20-Jul-15
Visual Studio 2017 Dev15 15 3.5 – 4.7 1.0-1.1, 2.0 7-Mar-17
Visual Studio 2019 Unknown 16 TBA TBA TBA

Product name .NET Toolset Path
Visual Studio 2005   Windows installation pathMicrosoft.NetFrameworkv2.0.50727
Visual Studio 2008 .NET 3.5 Windows installation pathMicrosoft.NETFrameworkv3.5
Visual Studio 2010 .NET 4.0 Windows installation pathMicrosoft.NETFrameworkv4.0.30319
Visual Studio 2012 .NET 4.5 Windows installation pathMicrosoft.NETFrameworkv4.0.30319
Visual Studio 2013 .NET 4.5.1 %ProgramFiles%MSBuild12.0bin
Visual Studio 2015 14 %ProgramFiles%MSBuild14.0bin
Visual Studio 2017 15 %ProgramFiles%MSBuild15.0bin
Visual Studio 2019 TBA TBA

Logging MethodBase.GetCurrentMethod().Name Results in “MoveNext”

Using log4net, the following code example has always (that I’ve paid attention to) successfully written the method name to the log:

CODE:
catch (Exception ex)
{
  globalMgr.Logger.Debug($”Exception thrown in  {MethodBase.GetCurrentMethod().Name} {ex.Message}  {ex.StackTrace} “);
  return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);

LOG TEXT:
Starting HttpClient in GetStoreData()

Lately, however, the method base is logged as “MoveNext.”

LOG TEXT:
Starting HttpClient in MoveNext

The reason for this is that I’m placing the GetCurrentMethod call inside an async method.   Therefore, the final method that the code lives is indeed in “MoveNext.” 

If I don’t get back with a solution soon enough, hard-code your method names into the logging but do not tell anyone I told you to do such a thing.

.NET: ASP.NET CORE 2.0

After a long patient wait, it has finally happened – I’m using ASP.NET CORE 2.0 on all my new projects at work and converting some of my old.   I’m very excited!

Why is this an important advancement for us Microsoft developers?   Simply put,  I can now write applications that will run cross-platform.   And I can do it easily.  I’m not so limited to operating systems or mobile vs. Desktop.  I can keep writing in my favorite flavor – C# — but I also get to stretch out my JavaScript muscles.   I can run Visual Studio on my Windows PC, my Mac, or my Linux box In fact, though Visual Studio makes life easier and faster, I don’t really even need it.   

I can download the .NET CORE SDK on either OS and I am ready to go.  I can use the same templates used in Visual Studio but just initialize them via the command prompt.  Code can be built and ran from this same prompt.  I can edit the code via any text editor.   To make it sweeter; however, downloading the free editor – Visual Studio Code – I can also walk through code and debug!    

I know, it’s been around a while, but in the corporate world, I’ve gotten started earlier than allot of folks.  I cannot say what this does to my efforts at continuous delivery – I actually started to think that I might permanently end up in the Java world without choice. 

You can read about ASP.NET CORE here!