jeudi 3 mars 2022

Postgres architecture for one machine with several apps

I have one machine on which several applications are hosted. Applications work on separated data and don't interact - each application only needs access to its own data. I want to use PostgreSQL as RDBMS. Which one of the following is best and why?

  1. One global Postgres sever, one global database, one schema per application.
  2. One global Postgres server, one database per application.
  3. One Postgres server per application.

Feel free to suggest additional architectures if you think they would be better than the ones above.

C# Creating instances dynamically based on database keys mapped to class names

I have the following database table, which is related to the class below.

Id      Description        Category     Level
---     ---                ---          ---
C_0001  "This is a class"  CategoryA    High
C_0003  "Hello"            CategoryA    Med
C_0057  "Blah blah blah"   CategoryB    Low


public class C_0001_Check : CalculationCheck
{
    public override void Calculate()
    {
          // DoChecks
    } 
}

I also have a factory class used to create instances of whatever "checks" I need (class simplified):

public class CalculationCheckFactory()
{
     public IEnumerable<ICalculationCheck> GetCalculationChecks(List<Category> categories)
     {
          var checks = _calcCheckRepo.GetChecks(categories);
          var calcChecks checks.Select(x => CreateCheckInstanceFromId(x.Id);
          // Assign level & message from check to calcCheck
          return calcChecks
     }

    private static ICalculationCheck CreateCheckInstanceFromId(string id)
    {
        try
        {
            var className = $"{_calcCheckNameSpace}.{id}_Check";
            var type = Type.GetType(className);
            var(ICalculationCheck)Activator.CreateInstance(type);
        }
        catch (Exception e)
        {
            throw new NotImplementedException($"Cannot create instance with ID: {id}.", e);
        }
    }

}

The issue I am now running into is that I would like to inject a repository into one of my CalculationCheck classes (using DI/autofac), and the Activator doesn't like this.

I know there is likely a way of doing this with the Activator, but it has made me re-think the entire design of this code. Given that the database cannot change (so I need to retrieve some properties from the database items), is there a better approach to this problem in general?

Java vending machine strategy [closed]

I am trying to create a vending machine scenario in Java where it makes coffee based on the below options

size - small, medium, large
decoction - light, medium, strong

Based on the options provided by the user, milk, sugar, decoction quantities are decided to make the final product.

eg:
1)  size = small and decoction = strong then 
milk = 2.5 units; sugar = 1unit; decoction = 2.5 units
2) size = small and decoction = light then 
milk = 4 units; sugar = 1unit; decoction = 1 units

Could you please suggest a proper way to implement this

mercredi 2 mars 2022

Pattern for inheriting and using a member variable in derived classes without casting each time

I have a base class:

class Base{
protected:
    Storage* _storage;

    virtual void createStorage(){
        delete storage;
        _storage = new Storage();
    }

    void exampleUseOfBaseStorage(){
        _storage->baseData++; //some complex calculation
    }
}

struct Storage{
    int baseData;
}

Each derived class has their own kind of storage:

struct DerivedStorage : Storage{
    int derivedData;
}

In the derived class,

class Derived{
protected:
    virtual void createStorage() override{
        delete storage;
        _storage = new DerivedStorage();
    }
}

Hence _storage can be used for all the base class members and the derived class members. The downside is, because for each derived class, I would have to cast the type of storage to DerivedStorage, or whatever type of storage the derived class uses, it is very tedious to type out the cast statement each time. Is there an elegant way around it ?

My solution is to just have one more variable of type DerivedStorage, and just use that in the derived classes member functions. Eg:

class Derived{
protected:
    DerivedStorage* _ds = nullptr;

    virtual void createStorage() override{
        delete storage;
        _storage = new DerivedStorage();
        _ds = _storage; // Use _ds in all member functions of Derived instead of _storage
    }

    void exampleUseOfDerivedStorage(){
         _ds->derivedData++; //some complex calculation
    }
}

Is there a more elegant pattern to use for this use case?

how to avoid frequent file writing?

I have a small project which crawls data from the internet consistently. After every achievement of a single HTTP request, I save the data to a local file immediately. That causes a problem that the program white too frequently, it's low in performance.

So, is there a mature solution that will maintain the integrity of business and also avoid frequent file writing?

mardi 1 mars 2022

Using Kafka as a Source of truth (async)

I would like to see opinions about using kafka how source of truth in a microservices architecture (async).

Case:

1 - Customer do a order

2 - Customer is redirect to a page with order's details

Architecture:

1 - Event is send to kafka topic

2 - Order microservice consume the event and save in the database

Doubt: When the customer is redirect to order details page can be a delay and my microservice could not consume the event yet. What's the best practices?

  • keep tryng get the order detail (wait in the interface) until the order is returned by order microservice?

  • Workaround in the user interface with cache (client side)?

  • is better write in the database first and catch the event with CDC (for example) to ingest in the kafka topic and distribute to anothers consumers(microservices)?

Refactoring nested each loop in Ruby

I need to do a search comparing two hashs

I use an each to iterate over a hash and b_search to do the binary search:

 @player.each do |player|
  player_support_search =  players_support.bsearch { |player_support| player_support[:score] >= player[:score] }
  number_player_support_search [player_support_search [:id]] += 1 unless player_support_search .nil?
 end

Is there any pattern where I can refactor to use only one loop?