jeudi 23 janvier 2020

SQL Pattern name of using multiple tables and referencing with ID

I'm looking for a name of the following pattern:

Let's say, you have a database table with a price and the amount of purchased goods. You want to calculate and save the overall price. You could save the price directly in the original table. But I I learned it is good practise to create a different table and reference the price table by ID.

For my thesis I need to cite and explain this pattern. However, I don't know which name to look for. Can you give me a hint?

enter image description here

Event sourcing, CQRS with Axon Server / Framework - Event Sourcing the entire application a good idea?

This question is very loosely related to Axon Server / Framework, since I'm learning it specifically while studying and trying to get into learning how to build microservices. Since it's difficult to learn about all the architectural patterns without actual hands on experience (and difficult to get hands on experience to begin with without a large application to actually test/build with), I'm theorizing a lot here (my questions might just be plain stupid, sorry, I'm still learning).

I downloaded Axon Server and successfully built and ran the included giftcard demo in three separate microservices. It works great, I see the events and all that on the Axon Dashboard, but I can't help but think about the "what ifs" of if I really built a very large enterprise application.

Consider this :

I'm building a streaming platform like Twitch. There are the basic, obvious services such as Customer service with its profile data, an order service (for subscriptions, donations), etc.

But there are other services such as the chat system. Hypothetically, I feel like event sourcing a chat system would be pretty beneficial, because you would have timestamps built in for replaying the chat if viewers are watching a VOD. However, I also feel like I shouldn't be mixing events for a chat system with an event store that deals with transactions; the two are completely separate systems and just storing every event from every service in a single event store feels... Monolithic?

I'm still unclear what the best practices are. All the samples and examples from talks always show very simplistic systems with the typical Customer, Order, Item aggregates and doesn't go into detail how this simple application would grow to a large service like Twitch.

So my questions are,

1) Should you event source an entire application, or can you choose which ones to event source and not?

2) Does Axon Server support two different event stores, such as one for a chat system and one for other services? Would I just run two different instances of Axon Server in this case? Should an application ever have two different event stores, or do I dump every event into a single event store, regardless of if something like a chat system sending far more events than anything else (would event sourcing a chat system even be a good idea, given the use case that I specified where viewers might want to "replay" the chat log?)?

3) The whole idea of a single event store being the single source of truth feels monolithic. Am I wrong in thinking this, and if I'm not, what are the ways to circumvent this if I had a really large application?

Software Architecture Design

We will start a new project and need to make architecture design decision. The architecture is basically Domain driven architecture. Our layers : App.Entities, App.Common, App.UI, App.BLL, App.DAL. We have our interfaces in BLL layer. We need to know what is the optimal way to locate interfaces in application. I mean, is it ideal interface contracts to be in App.BLL layer with implementations? Or is it better to be in seperate App.BLLContract layer. Do you have any suggestions and what is the reason for it?

mercredi 22 janvier 2020

How to design class property containing a collection of translations? For patterns gurus

I am design pattern newbie, please help to properly organize classes.

My entity has a property 'name' that should have several translations:

class Entity {
   private int id;
   private TranslationCollection name;
   ...
}

'name' - should contain a set of translations:

{
  'en': 'english word',
  'sp': 'spanish word',
  ... 
}

So I decided to make class 'Translation':

class Translation {
  private string locale;
  private string value;
  public constructor (locale, value) { ... }
  ...
}

and class 'TranslationCollection':

class TranslationCollection {
   private translationArray[];
   public constructor() { 
     this.translationArray = new array() 
   }
   public addTranslation(Translation translation) {
      translationArray.push(translation);
   }
   ...
}

and there is requirements:

1 locales could be only from 'listOfLocales' which is available only at runtime.
2 Translation collection could not have repeating locales.
3 Unit testable.

Is it normal to inject 'listOfLocales' into 'Translation' constructor to check locale is in the list? In that case I will have three parameters in 'Translation' constructor. And I think it is not good design.

Or should I have some 'TranslationFactory' with 'listOfLocales' injected in the constructor to create Translation with proper locale?

Or may be I am on the wrong way, and there is a better solution?

An how to organize check that 'TranslationCollection' has no repeating locales? Should I check in addTranslation method that there is no array items with locations matching new Translation.location programmatically, which seems not best solution. Is it a way to organize non repeating collection more declaratively?

Program for this number pattern using for loop in java 1 22 3 4444 5 666666 7 88888888. Not getting the desired pattern

public static void main(String[] args) {
   int noOfRows = 8;

    printPattern(noOfRows);
}

private static void printPattern(int num){
    for(int i = 1; i <= num; i++){
        for(int j = 1; j<=i;j++){
            System.out.print(i);
        }            
        System.out.println();            
    }        

Program for this number pattern using for loop in java 1 22 3 4444 5 666666 7 88888888. Not getting the desired pattern

What is the best practice to avoid putting an "initialize" method in an interface for a data service?

I have an interface for a data service and object repositories in my domain layer, but as I write the implementation for these interfaces, I find that I am needing an "initialize" or "fill" method in the case of an XmlDataService implementation. I need to read the xml, then "load" the repositories from the xml. This feels a lot like a leaky abstraction. What is the best practice here or I am way off base?

My Interface:

public interface IDataService
{
    event EventHandler<string> StatusUpdated;
    IRepository<Points.Core.Point> GetPoints();
    ...

    void Fill();
    Task FillAsync();
}

XmlDataService implementation:

    public class XmlDataService : IDataService
{
    public event EventHandler<string> StatusUpdated;
    public void OnStatusUpdate(string s) => StatusUpdated?.Invoke(this, s);

    public IRepository<Point> GetPoints() => _pointsRepo;
    ...

    public void Fill()
    {
        using (var ds = new DataSet())
        {
            OnStatusUpdate("Reading dataset...");
            ds.ReadXml(_reader, XmlReadMode.ReadSchema);
            ds.EnforceConstraints = false;
            OnStatusUpdate("Loading points...");                
            _pointsRepo.Initialize(ds);
            ...
        }
    }

    public XmlDataService(XmlReader xmlReader)
    {
        _reader = xmlReader ?? throw new ArgumentNullException("xmlReader");
        //instantiate repos
        _pointsRepo = new PointsRepository();
        ...
    }
}

Is there a more functional implementation of this Writable Stream using RxJS that waits for a promise to resolve before processing?

I've been living under a rock for a few years so I haven't touched RxJS yet and I'm curious how it handles a situation like below.

const {Readable, Writable} = require('stream')

class DelayedStream extends Writable {
  constructor(options = {}){
    options.objectMode = true;
    super(options);

    this.promise = new Promise((resolve, reject) => {
      // open connection
      setTimeout( function() {
        console.log('resolved')
        resolve('with client')
      }, 2000) 
    }) 
  }
  _write = function(chunk, encoding, done){
    console.log(`_write("${chunk}") called`)
    // waiting for connection.
    this.promise.then((client) => {
      console.log(`processing "${chunk}"`)
      done() 
    });
  }
  _final = function(done) { //...close connection }
}

var readable = Readable.from(['one', 'two', 'three'])
var test = new DelayedStream();
readable.pipe(test)

The usage of the above is for a scenario where I am waiting for a connection to establish before processing the incoming Readable stream.

I'm not fond of having to create a new class that handles this but it works. It seems like a more functional approach would be optimal but RxJS is lower on my list of things to learn coming back to dev after a very, very long hiatus..

Thanks!