Posts

Showing posts from June, 2024

EF Weirdness - invalid column of XXXXX

 Some days there is just weirdness that makes no sense.  My error message was "invalid column of StoreMenuId".  The error message did not make any sense.  I had no column called that in any table or view in my database.   Also wrong was when I looked at the properties on the table after I eventually found them in a weird place under EntityType in Intellisense.  I had all the correct columns PLUS this mysterious column.   EntityType: StoreMenuDevices   Properties:      Id (int) Required PK AfterSave:Throw ValueGenerated.OnAdd IdentityColumn     CreatedOn (DateTime) Required      CurrentMenu (int?)        DeviceName (string) MaxLength(50) Annotations: MaxLength: 50      LastUpdatedOn (DateTime) Required      SkusMissing (bool) Required      Status (string) MaxLength(50) Annotations: MaxLength: 50      StoreMenuId (no...

Random DateTime for testing

 private readonly Random _gen = new();         private DateTime RandomDay()         {             DateTime start = new DateTime(1995, 1, 1);             int range = (DateTime.Today - start).Days;             return start.AddDays(_gen.Next(range));         }

Find a column named XXXX in tables or views

Find a column named XXXX in tables or views: SELECT      COLUMN_NAME AS 'ColumnName', TABLE_NAME AS  'TableName' FROM        INFORMATION_SCHEMA.COLUMNS WHERE       COLUMN_NAME LIKE '% XXXX %' ORDER BY    TableName, ColumnName; 

Empty Content check on HttpResponseMessage

Checking HttpResponseMessage for empty content is strange since it is internal.  Even a ToString() will not do what you expect.  So easiest thing is the code below although there are a lot of ways to get the answer.   private static bool IsContentEmpty(HttpResponseMessage response) {     return response.Content.GetType().Name == "EmptyContent"; }

Building a configuration

  IConfiguration config = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory)     .AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)     .AddEnvironmentVariables().Build();

Http Response Message

 Microsoft does not make it easy to mock the HttpResponseMessage .  It is an abstract, but still. private static HttpResponseMessage BuildFakeResponse() {    HttpResponseMessage response = new()    {        Content = new StringContent("Test Content ", Encoding.UTF8, "text/csv")    };    response.Content.Headers.Add("Content-Disposition", "attachment; filename = yourname.csv");    return response; } Here is one that worked with a model and using Json. private static HttpResponseMessage BuildFakeResponse() {    XXXXX xxxx = new();    string jsonXXXX = JsonConvert.SerializeObject(xxxx);    HttpResponseMessage response = new()    {        Content = new StringContent(jsonXXXX, Encoding.UTF8, "application/json")    };    response.Content.Headers.Add("Content-Disposition", "attachment; filename = yourname.csv");    return...

Model Testing

 I like this Model Tester for unit testing. using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace QT.RemoteOrder.API.Tests; [ExcludeFromCodeCoverage] internal static class ModelTesting {     public static void TestMockData<T>(T model) where T : class     {         var type = typeof(T);         foreach (var propertyInfo in type.GetProperties())         {             var propertyTypeName = GetTypeFromPropertyInfo(propertyInfo);             switch (propertyTypeName)             {                 case "System.String":                     Assert.IsNotNull(propertyInfo.GetValue(model), $"Class: {type.Name}  Property: {propertyInfo.Name}");                   ...

Backpressure

  Backpressure   Backpressure occurs because inflow tasks are outstripping our ability to process the data. Perhaps a denial-of-service attack is underway, rendering lots of data without pagination, or constrained shared resource such as a network printer.  Backpressure occurs more often in web/cloud environments when scaling challenges are encountered.     Strategies Better Design -    a) Minimize Rendering  - ex: design your customer grid to be on a tabbed notebook page or page that can be pinned, so does not need to populate the grid if not seen. ex2: add collapsible areas so user can control. ex3: have menus to take you to a different screen. ex4: pagination of grids; b)  Minimize Data Traffic  - ex: design your web page to only download to client first time and only send out updates when updates occur or user does action such as key strokes.   ex2: Filtering as specifically as possible, before retrieving data. Bu...

Reactive Extensions (Rx) and .NET

Reactive Extensions (Rx) and .NET    The other day I came across a namespace I had not delved into - that of System.Reactive.  ReactiveX or Rx is an older Microsoft technology that a ton of Javascript frameworks spun out from.  It is kinda weird that the origin technology was Microsoft and no one really did anything with it or promoted it.      Rx is perfect for the distributed cloud-based applications and web of course.  Rx is focused around the push data propagation rather than the pull.                                                                      Focus                                       Pull                     ...

API Best Practices

API Best Practices Rule 1: Don't have API endpoint calling another API endpoint if you can help it.   Example 1:  A web page of a website that has a button that calls to first API's endpoint that then calls another API's endpoint. Reasons:   1) Call that second API directly since it is in control of that process.  2) Now you have doubled your failure points (servers, API interfaces that could change, security, etc.) for this process.  3) Obscuring the purpose for your first API by mixing in other API's purposes unless the first API is a clearing house or manager API.