lundi 7 février 2022

IoC vs DIP - can you have one without the other?

Is it possible to have Inversion of Control without the Dependency Inversion Principle? The line between the two is still blurred in my mind. Is there even a line dividing the two? Some sources insist they're the same, others don't. Would like to get a conversation going around the differences, or maybe why there are differences in opinion.

Using RabbitMQ to parallel process parts

I have a service that receives a series of files from a 3rd party application. I need to process these files, and store the result in a single S3 object. Further, 3rd party application can send large number of files. So I do not want to wait until all the files are sent to start processing them. Additionally, for scaling purposes I want to process these files parallelly and then combine the results to form a single S3 objects. Hence I created a RabbitMQ exchange and bound a queue to it. I connected my consumers to this queue. These consumers will work on a set of files and store their temporary result (partial objects) to S3. Now, I want to send a final message to indicate that parts can be combined and uploaded to S3. As each consumer is processing these "sub" messages independently how do I know when the processing is complete? Are there any message design pattern I can use to solve this? Note that I can put a correlation ID for the messages if needed.

I tried to depict my setup here: RabbitMQ Setup

How to securely send request from frontend to backend and make it non replay-able?

I am trying something out in Springboot and stuck with a weird issue where I want to send some data from my frontend (react app) to backend (SpringBoot) and make that request non replay able by users (users should not be able to capture the request and make the request again and again).

Problem: I am writing some sample app which is making REST calls to a 3rd party API and I want to capture the matrices for my app. A few things about my app.

  1. Frontend calls this 3rd party API directly and this API is somewhat time consuming
  2. There is no communication between 3rd Party API and my backend and there can't be
  3. 3rd party API does not provide any way to capture matrices

Solution: I can create an endpoint at my backend and collect matrices (execution time of 3rd Party API etc) at frontend and send to backend

But with this solution there is a huge issue, any valid user can intercept that call to backend to update the matrices and play around with it and pollute the numbers.

Is there any standard way to deal with this kind of problem or any other smart way to capture the matrices for my use case?

is there any mentorship programme to learn code archetucture for senior developer position?

I am kind of a senior developer but I feel like I'm lacking skills, by observing and by gaining experience i feel like I'm lacking architecture skills to write a codebase which will support millions of user and for long term projects, although by continuous learning I'm improving but still I'm not satisfied with the progress rate.

I found a course which will I think help me grow was a developer on https://academy.essentialdeveloper.com/p/ios-lead-essentials-cohort-17-c0f4 but the prices is very high and can't afford it, can anyone suggest where I get some similar kind of mentorship with some reasonable price ?

***NOTE ***- I'm not sure if its right to post this kind of question here but I think developer can help another developer

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

I am trying to understand what the benefits of using interfaces are so that I can know when and how to use them. Most sources on the internet are relatively surface-level, explaining how interfaces work but now why to use them, and when I look up the titular question, I don't get any results that tell me whether the purpose of interfaces extends beyond polymorphism and multiple inheritances.

My reasoning is that if an interface were inherited by only one class, it would be useless, and when an interface is inherited by multiple classes, it makes no difference unless it is used for polymorphism, and the only thing that makes implementation different from extension is multiple inheritances.

If I knew for sure that their purpose was limited to this, I would have an increased confidence in my design decisions, and if I learned of a purpose outside of this, it would fill a serious gap in my knowledge. I have used the design patterns tag because there is perhaps a design pattern which makes use of interfaces in a way that is distinctly beyond mere polymorphism or multiple inheritances.

dimanche 6 février 2022

Can the view class in an MVC model be singleton design pattern?

I'm trying to build a PyQt5 application using the MVC model, is it good practice to make the view class a singleton design pattern?

samedi 5 février 2022

Calling an overridden function from a base constructor

It makes sense that I should not call an overridden function from a base constructor, since the derived class is not yet constructed.

But I want to use this design pattern, where each derived class provides methods for calculating the properties of the base class since the properties should be immutable and assigned at the constructor.

Shape.cs

public abstract class Shape
{
    protected Shape()
    {
        Area = 0f;
        Center = Vector2.Zero;
        const int n = 36;
        float du = 1/(float)n, dv = 1/(float)n;
        for (int i = 0; i < n; i++)
        {
            float u = (i+0.5f)*du;
            for (int j = 0; j < n; j++)
            {
                float v = (i+0.5f)*dv;
                float f = GetAreaElement(u, v);
                // Warning: Remove this call from a constructor to the overridable 'GetAreaElement' method.
                Area += f*du*dv;
                Center += GetPosition(u, v)*f*du*dv;
                // Warning: Remove this call from a constructor to the overridable 'GetPosition' method.
            }
        }
        Center /= Area;
    }

    public abstract Vector2 GetPosition(float u, float v);
    protected abstract float GetAreaElement(float u, float v);

    public float Area { get;  }
    public Vector2 Center { get; }
}

public class Circle : Shape
{
    public Circle(float radius)
    {
        Radius=radius;
    }

    public float Radius { get; }

    public override Vector2 GetPosition(float u, float v)
    {
        float r = u*Radius, θ = (float)(2*Math.PI)*v;
        return new Vector2(
            r*(float)Math.Cos(θ),
            r*(float)Math.Sin(θ));
    }

    protected override float GetAreaElement(float u, float v)
    {
        return u*Radius;
    }
}

public class Rectangle : Shape
{
    public Rectangle(float side1, float side2)
    {
        Side1=side1;
        Side2=side2;
    }

    public float Side1 { get; }
    public float Side2 { get; }

    public override Vector2 GetPosition(float u, float v)
    {
        return new Vector2((u-0.5f)*Side1, (v-0.5f)*Side2);
    }

    protected override float GetAreaElement(float u, float v)
    {
        return Side1*Side2;
    }
}

So what is the solution here? I want to use the base constructor to define the properties, and the calculation depends on the implementation of the derived class.

Workaround 1 - Future calculator

A workaround would be to provide a protected function that calculates the properties, each to be called from each constructor of the derived class, but there is no enforcement here. If one class forgets to call the calculator function the whole thing falls apart. And the properties are now private set which is not immutable really.

public abstract class Shape
{
    protected void Calculate()
    {
        ...
        float f = GetAreaElement(u, v);
        ...
        Center += GetPosition(u, v)*f*du*dv;
        ...
    }

    public abstract Vector2 GetPosition(float u, float v);
    protected abstract float GetAreaElement(float u, float v);

    public float Area { get; private set; }
    public Vector2 Center { get; private set; }
}

public class Circle : Shape
{
    public Circle(float radius)
    {
        Radius=radius;

        base.Calculate();
    }

    public float Radius { get; }

    public override Vector2 GetPosition(float u, float v)
    {
        ...
    }

    protected override float GetAreaElement(float u, float v)
    {
        ...
    }
}

Workaround 2 - Function delegates

Another workaround would be to supply the delegates to the required function implementations as arguments to the base class constructor.

public delegate float AreaFactor(float u, float v);
public delegate Vector2 PositionFunc(float u, float v);
public abstract class Shape
{
    protected Shape(AreaFactor a, PositionFunc p)
    {
        this.GetAreaElement = a;
        this.GetPosition = p;
        ...
        float f = a(u, v);
        this.Center += p(u, v)*f*du*dv;
        ...
    }

    public float Area { get; }
    public Vector2 Center { get;  }

    public AreaFactor GetAreaElement { get; }
    public PositionFunc GetPosition { get; }
}

public class Circle : Shape
{
    public Circle(float radius)
        : base(
              (u, v) => u*radius, 
              (u,v)=>
                {
                    float r = u*radius, θ = (float)(2*Math.PI)*v;
                    return new Vector2(
                        r*(float)Math.Cos(θ),
                        r*(float)Math.Sin(θ));
                })
    {
        Radius=radius;
    }

    public float Radius { get; }
}

This seems a bit clunky to me, and I am not sure I like the function delegate properties, instead of overridden methods.

Question/Challege

Can [SO] provide some other ways of achieving the above-stated goals

  • Base properties are immutable
  • Base properties are calculated at the constructor based on the implementation details of the derived classes.
  • Each derived class holds its own immutable properties used to describe the derived class.