RecordVisitors–code–part 2

Now , after the idea is complete, let’s see the code. ( Yes, I know that TDD will be better … )

For the implementation :

1. Problems with the manual test-  how to create a Fake User – see below the solution.

2. Problems of how to let user define his own data

You can find the original code at https://github.com/ignatandrei/RecordVisitors/releases/tag/just-Code

I do not want to bother with details about implementation – so some statistics ( obtained from VS with Analyze => Calculate Code Metrics)

Those are the classes  / Interfaces:

  1. Extensions – used to easy registration in Startup
  2. IRecordVisitorFunctions – to let the user define his function to get the name of the user
  3. IUsersRepository – to let the user define his function to save / retrieve from database
  4. RecordVisitorFunctions – implementation
  5. RecordVisitorsMiddleware
  6. UserRecorded – what to save in the default implementation
  7. UserRecordVisitors –  the default DBContext to save
  8. UsersRepository – default database implementation with EF in memory

Created also an ASP.NET Core WebAPI project to test . The following was added:

1. A MockAuthenticatedUser (  credits to https://visualstudiomagazine.com/Blogs/Tool-Tracker/2019/11/mocking-authenticated-users.aspx )

2.  To the startup:

services.AddAuthentication(“BasicAuthentication”)
     .AddScheme<AuthenticationSchemeOptions,
                   MockAuthenticatedUser>(“BasicAuthentication”, null);

app.UseAuthentication();
//put AFTER authentication
app.UseRecordVisitors();

app.UseEndpoints(endpoints =>
{
     endpoints.MapControllers();
     endpoints.UseVisitors();
});

The number of lines are aproximatively 200 ( the total number of lines written by VS is 1000 )