mercredi 15 décembre 2021

What Architecture is Best Suited for running scalable browsers

I am trying to build a saas product that servers as a helper for a bigger product.

Ideally, I should be able to allow users connect their accounts using OAuth and then make API calls on their behalf to the parent product.

But the product does not have a public API.

My basic solution is -> To to build an server that runs browsers in docker containers, using each users auth-token from the main app to make API calls on their behalf. There are obviously some serious problems with this but is there a better way of achieving my goal?

how can i convert this switch-case example to command pattern?

enter image description here[structure to convert to command pattern]

There are a lot of decision structures in this code, but I don't want this crowded image in my codes. I want to apply the command pattern to this build. How to apply command pattern to this structure to remove this switch-case complexity?

Would you use a 'Repository' pattern to communicate with a Web Service?

I have a web service that supports RESTful CRUD operations: Add, Delete, Update. On my client (Xamarin) I obviously I want to hide this behind an interface for mocking and possibly using a different technology to store my objects.

Would you use a Repository Pattern, Service Layer Pattern or another pattern? It seems like there's little difference whether you're persisting to a flat file, a database or a web service.

REST service design with DTOs, composition and schema validation

For some time I am trying to figure out most suitable pattern(s) to use with REST services and with regards to composite objects, DTO and schema validation.

Consider trivial pseudo code:

class UserDto:
  id: primary key, auto inc, unique
  salutation: string, can be null
  email: string, unique

class PhoneDto:
  id: primary key, auto inc, unique
  number: string
  user_id: foreign key pointing to a user record

Each user object can have zero or more phone records attached to it.

Say we are developing CRUD methods for User:

  1. When creating user, id should not be passed at all, since it will be assigned by the DB. In all other cases, id is mandatory:
  2. when deleting user object, we pass the id and return deleted object to the caller (all fields from user table, but not from the phones table).
  3. When updating, id is again mandatory, but all other fields are optional: caller maybe wants to set salutation since forgot earlier, or wants to change email. Either way this call makes sense with id set and at least one parameter to be altered. Finally,
  4. reading also needs user id but also optionally to load assigned phones.

Now, the question(s):

  1. Shall DTO objects in general be separated/decoupled from business model? Since mainly ORMs specifics reside in DTOs, looks like logical move to decouple, but would like to here opinions on this and what are some recommendations regarding which pattern to use, etc.
  2. When creating an user object, shall we send nested address-es in the same call or is it recommended to make second call and send list of addresses to appropriate endpoint. For more complex cases, like user is having a profile or some other tables related, number of additional calls raises accordingly, but still, maybe the code will remain cleaner if we do this instead making composite payloads at the expense of more traffic (assume objects are not huge blobs of data)
  3. Now, how 1. and 2. are affecting usage of Swagger? I would like to cover endpoints with nice Swagger but not sure how complicated it would be
  4. What pattern(s) are recommended to use to achieve first 3? Swagger is optional, but it really would be nice to have it. Decorator is the first to come to my mind

I don't target any specific framework so I made the questions as generic as I could. But for the sake of complete information, my main candidate is oat++.

Thanks in advance

Design patterns in c# [closed]

I apologize in advance for such a stupid question, but can someone suggest which design patterns are best used for CRUD operations, role management (conditionally boss, manager, worker). I work with windows forms in visual studio and our teacher made it a prerequisite to use some kind of design pattern to implement the above tasks. Thank you in advance!

Create singleton when private constructor have parameters

I would like to implement singleton pattern in StudentProvider and then access method through interface. StudentProvider constructor accepts few parameters. Here's the sample working code without singleton.

public interface IStudentProvider
{
    Task<StudentViewModel> GetStudentAsync();
}

public class StudentProvider : IStudentProvider
{
    private readonly HttpContext httpContext;
    private readonly IActionContextAccessor actionContextAccessor;
    private readonly IConfiguration configuration;
    private readonly IUnitOfWork unitOfWork;
    private readonly string host;

    public StudentProvider(IHttpContextAccessor _httpContextAccessor, IActionContextAccessor _actionContextAccessor, IConfiguration _configuration, IUnitOfWork _unitOfWork)
    {
        httpContext = _httpContextAccessor.HttpContext;
        
        actionContextAccessor = _actionContextAccessor;
        configuration = _configuration;
        unitOfWork = _unitOfWork;
        host = _httpContextAccessor.HttpContext.Request.Host.Host;
    }

    public async Task<StudentViewModel> GetStudentAsync()
    {
        var std = new StudentViewModel();

        // httpContext, actionContextAccessor, configuration, unitOfWork and host uses here

        return std;
    }
}

Now i converted this into single, here's the code:

public interface IStudentProvider
{
    Task<StudentViewModel> GetStudentAsync();
}

public sealed class StudentProvider : IStudentProvider
{
    private readonly HttpContext httpContext;
    private readonly IActionContextAccessor actionContextAccessor;
    private readonly IConfiguration configuration;
    private readonly IUnitOfWork unitOfWork;
    private readonly string host;

    private static StudentProvider instance = null;

    public static StudentProvider GetInstance
    {
        get
        {
            if (instance == null)
            {
                instance = new StudentProvider();
            }

            return instance;
        }
    }

    private StudentProvider(IHttpContextAccessor _httpContextAccessor, IActionContextAccessor _actionContextAccessor, IConfiguration _configuration, IUnitOfWork _unitOfWork)
    {
        httpContext = _httpContextAccessor.HttpContext;

        actionContextAccessor = _actionContextAccessor;
        configuration = _configuration;
        unitOfWork = _unitOfWork;
        host = _httpContextAccessor.HttpContext.Request.Host.Host;
    }

    public async Task<StudentViewModel> GetStudentAsync()
    {
        var std = new StudentViewModel();

        // httpContext, actionContextAccessor, configuration, unitOfWork and host uses here

        return std;
    }
}

The issue with above singleton code is instance = new StudentProvider(); is expecting parameters which i'm not able to pass.

How do i pass parameters to constructor from singleton instance ?

mardi 14 décembre 2021

A way to warn the user about a little freeze by a function doing calculations in a NSIS installer

I'm doing my first NSIS script but i found a bump in the road. In fact i think is hard to explain only in the title, (maybe someone can help with that to) so let me explain fully:

I'm creating a installer that uses a few custom pages because i want the user select some options first (it uses nsDialogs), and depending of that do some tweaks in the with installation path (mostly autodetect it because it could depend to other things). All of this is working fine.

In some cases of that options, between checking if some files exists, it hash a file to look if the file is the one it expects (because it's going to patch it with a delta later). I used Crypto plugin or MD5 plugin, both are fine and both do what i want, but they hang the installer for a while (a second or so), i suppose because the file is a little big (it's about more than 100MB) and near that is the issue.

Normally in these cases, you select the option, goes to the next (custom) page, and in the creator function of the custom page autodetects the folder, and it directly do the file checks and when is checking the file hash, it hangs for a second and continues, but all this time hanged it only shows a blank page, because it didn't reach yet in the creator function the nsDialogs::Show instruction to show the window content. In that page you can change the folder, and if it's the case, once is changed it runs the checks again (it's a dedicated function that was called in both cases) and hangs again for a bit, but then the window shows everything and i can set a text to say something (in fact, it is what i did first), but with that automatic first time i can't do this.

That's the point: how to show something to the user to aware them about the installer is doing the hash calculations, instead showing only a blank window.

What I have tried or thought to do:

  • With nsDialogs, because it did the calculations first and didn't reach the nsDialogs::Show until later, i can't display anything in the window at that point (or, at least, is what i read in all the documentation i found about that). And, like the documentation says and it was tested, everything you put after the nsDialogs::Show instruction is executed when you push the next or back button.
  • Seeing that with nsDialogs at first not seems the way to go, i was searching if it's possible to show a window above the installation window (something like a MessageBox) and close it automatically, before and after the hash calculation respectively, showing only a text with a "Please wait" or kind of. But i didn't find a way to do it.
  • Maybe with a timer and do the checks a few miliseconds after could be done, but it seems to me a very cheap way to do it with some issues waiting to happen, mostly because depends of the machine speed, something i could do only in a last resort if it makes to show first the window with nsDialogs::Show, and later execute the check files with a timer. But, i want to do the checks as the folder is set because that function enables the "Next" button and i want that as soon as is possible, and adding timers to this doesn't look right.
  • Or is other more stylish way to do this but i didn't figure it out yet.

if it is not well understood the topic, i could add a little example tomorrow created from scratch to show this, because my main test is so big that is not point to paste all of that here.

Thanks!