Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Akka.net / Generics and Database & Entity Framework

Tags:

c#

akka.net

Hi there I am fairly new to akka.net! so appologize any strange questions :-)

I am working on actors which are accessing a database. One is reading and one is writing. Both are goverend by a router creating them as needed based on workload.

In order to avoid to create for every type in the database I want to use Generics in combination with Expressions. The message along the the following lines:

public class Msg<T> 
{
   public Expression<Func<IEnumerable<T>, T, IEnumerable<T>>> Expr { get; }

        public MsgExprObjBool (Expression<Func<IEnumerable<T>, T, IEnumerable<T>>> expr)
        {
            Expr = expr;
        }
}

within the actor I'd like to use the Expression defined and retrieve the entities as requested for further processing or send them back at least - like so:

public class MyActor : ReceiveActor
{
    public MyActor ()
    {
      Receive<MsgExprBool<Foo>> (s => Console.WriteLine ($"Result<Material> == {s.Expr.Compile () (_foos)}"));

      Receive<MsgExprBool<Boo>> (s => Console.WriteLine ($"Result<Boo> == {s.Expr.Compile () (_boos)}"));

      Receive<MsgExprObjBool<Qoo>> (s =>
        {
            foreach (var r in s.Expr.Compile () (_Qoos, qoo))
                Console.WriteLine ($"Result<Qoo> == {q.Id}, {q.Name}");
        });
    }
}

My question is now, has anybody been using this approach to access a database and if so, what are the experiances with that?

REM: Since the actor system is NOT communicating with any foreign requestors all share a set of common object definitions - like the database objects.

like image 634
mph Avatar asked Dec 12 '25 20:12

mph


1 Answers

With all due respect, I think your approach is completely wrong.

This means passing expressions which not only will not work but even if it did you would be referencing objects/instances outside of the actor which breaks 100% the rule of self-containment and only passing immutable messages.

You are not passing messages here, you are passing lines of code.

You may need to bite the bullet and wrap your operations in immutable messages and pass those, so that everything that happens within the actors does not touch external objects and therefore will be able to scale and not have resource issues like locks.

like image 168
Karell Ste-Marie Avatar answered Dec 15 '25 09:12

Karell Ste-Marie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!