Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Friday, March 17, 2023

A short list of C# coding issues.

Injecting services into entities

It been bugging me for a while now that to use services in a entity can be a bit of pain. It usually starts with a little entity class to do X. Then, oh, I'd like to be able to do some logging, so take an ILogger<X> as constructor problem..

Woops; now I need a wrapper or some outer factory style XFactory to provide the logger service... It's not too bad I suppose, just a bit cumbersome. 

Entities with async construction

Ok, yes, I know; a constructor should be a fairly dumb operation. The constructor's goal is (mor or less) to validate and preserve input parameters. 

But some times.. particularly if your trying to break out of a Service + DTO view of the world, an entity may need to call an async service method during creation.

Years ago this problem presented with C++ on windows, generally solved with a 2-phase construction.

2-phase is a valid pattern for richer entities, but id does introduce:
  • Created entity is NOT immediately usable.
  • Entity code now generally needs safety code to throw if not fully initialised 

Typed strings with serialization

Not quite sure what to call this. 

Often data is expressed as a string, but with some constraints. For instance a PathString will always start with a '/'. This can be a very useful constraint, particularly for string concatenation etc.

 I also find myself writing constrained string classes for jobs like ensuring a valid Kubernetes entity name.

But as I've done a few times now, I then need patterns to serialize / deserialize these typed-strings, and to cleanly use them with other code.


I'm hoping to soon publish a package for these issues; currently trialling a pattern, we'll see.

Wednesday, March 15, 2023

Introducing kwd.CoreUtil

 I built this a while ago; others may find it handy.


It's a small library focussing primarily on syntax sugar  for C#

kwd.CoreUtil v1.1.0:  Git / Nuget

The main code is a set of extensions to make working with FileInfo and DirectoryInfo more natural.

In addition there are some other utilities for strings and file streams.


Originally the design was to restrict this to ONLY depend on standard .NET Core packages. 

But more recently I stumbled onto System.IO.Abstractions; since the package is lite, and provides significant benefit; I've take it as a dependency


 So what's it good for?

Well, my favourite is the Ensure* extensions. 
Super handy to ensure a directory exists before used.

Have a look and provide some feedback :)

Tuesday, October 2, 2012

Least-Concept-Method

Often when writing code a developer will use member overloading to provide an alternate convenient call interface on a class. Assuming the developer has at-least a passing interest in quality, they will try and stay DRY. To this end, one of the method overloads will contain the core 'work' for the member, whilst overloads will perform only simple tasks, and then hand-off to the core method to do the real work. But which overload is the best for the core?

I like to use a least-concept method for the core. By this I select a set of parameter(s) that are

  1. Not native types, or simple constants.
  2. Don't require the method to navigate deeply into the parameter(s) (one-level in is ideal).
Why not native types? 
Often I've seen code that uses a core method that extracts the most-basic, or native data from more complex objects, and implements the core with this. For example, say I have a CountLines() method that needs a file, the developer may go for the most basic CountLines(string fileName) style. This approach has the benefit of being flexible for the caller, but it sacrifices clarity. The next developer has to know that what I really mean is the full path to the file, not just its file name. You could argue the parameter is badly named (and I'd agree), but I still would prefer a stronger type. C# is a typed language, a better idea would be to leverage it. In this case I would go for a CountLines(FileInfo file) method.

Why not the most complex object as a parameter?
Sometimes you need to implement a method that takes a complex object, particularly when implementing an interface (plug-in), but the method only uses a small part of the complex object. Lets say you're implementing a RejectIfTooBusy(HttpApplication context) method. You want to reject if the current request is from a set of black-listed hosts. The context has the data you need via context.Request.Url.Host, but to reach this you need to check for nulls along the way. Rather than having this parameter conversion code in your main implementation, I prefer to pull out a core RejectIfTooBusy(Uri whoReqested) method, and have this overload called by the first. By separating out the core member, it becomes clearer that all I'm checking is the request url. It can also be useful to split a complex object, to highlight the bits that the method really uses.

A short list of C# coding issues.

Injecting services into entities It been bugging me for a while now that to use services in a entity can be a bit of pain. It usually starts...