GregoryConrad / rearch-dart

Re-imagined approach to application design and architecture
https://pub.dev/packages/rearch
MIT License
92 stars 4 forks source link

Tutorial Graphic #95

Closed jtkeyva closed 9 months ago

jtkeyva commented 9 months ago

Was trying to think of ways to simplify the reading and explanation to make it easier for people to "wrap their heads around" ReArch. I don't even think this code is correct but I was just playing around to see how shapes and color can help visualize what's going on even in the most basic example

image

Do you plan on using @capsule or something like that in the future to make things more concise?

GregoryConrad commented 9 months ago

Your example is almost correct, it'd actually be:

int appleCapsule(CapsuleHandle _) => 2;
int orangeCapsule(CapsuleHandle _) => 5;
int fruitCapsule(CapsuleHandle use) => use(appleCapsule) + use(orangeCapsule);
int pizzaCapsule(CapsuleHandle use) => 4;
final food = container.read(fruitCapsule) + container.read(pizzaCapsule);

I wasn't ever planning on a @capsule for regular capsules because I think it will just bring confusion without enough gain ("when should I use the macro?" or "when should I write a function?"). Eventually, though, if Dart gets const closures, folks could perhaps define capsules as:

const myCapsule = (CapsuleHandle use) => use.state(1234);

Although I am not sure I like this; the code itself loses type information (making it harder for readers).

I originally did have separate macros planned for factories, async capsules, and actions, although I do frankly think its better to just make one all encompassing macro (that just checks the return type of the function to handle async-specific stuff) like:

@ReArch() // generates fruitCapsule
int fruit(
  @appleCapsule int apple,
  @orangeCapsule int orange,
) {
  return apple + orange;
}

@ReArch() // generates delayedFruitAsyncCapsule, delayedFruitCapsule
Future<int> delayedFruit(@fruitCapsule int fruit) => Future.delayed(const Duration(seconds: 1), () => fruit);
jtkeyva commented 9 months ago

Got it thanks. Ah ya a read... that's why it kept spitting out a function. Maybe I'll just actually take the time to learn by fully reading the docs until it clicks... rather than try to make things up in a graphic ha.

I look forward to some more examples, videos or interactive codelab style resources. I want to get off BLoC, I just need to have the reassurance that ReArch can and will do everything BLoC does

GregoryConrad commented 9 months ago

I look forward to some more examples, videos or interactive codelab style resources.

I plan to eventually make a code lab or a "code as you read" guide based on https://github.com/GregoryConrad/rearch-dart/tree/main/examples/scorus. I think that particular example app (I added it within the last week or two) does a fairly good job showcasing modeling data in ReArch.

The issue is the building blocks (containers, capsules, side effects) are all pretty basic, but the things you can do with them can go really in depth. It's hard to introduce all of it at once, since there are so many different specializations of different ideas/paradigms that you can make.

jtkeyva commented 9 months ago

Great to hear. Good documentation and several examples are the hallmarks of what I consider stable & safe to use packages.

Riverpod got very popular with sub-par docs likely because he created the venerable Provider package and has developed a good reputation. Unfortunately, you're the new kid on the block and you don't have such luxuries!

I wish I could help you more but I am still trying to get out of my BLoC mindset. Actually, there would be a great little tutorial example right there... how to convert a feature that uses BLoC/Cubit to one that uses ReArch. Would you create it if I sponsored it? I learn through reverse engineering and if I could get an example that shows the BLoC way and then the ReArch way, I could way more easily figure it out.

GregoryConrad commented 9 months ago

how to convert a feature that uses BLoC/Cubit to one that uses ReArch

Pretty easy! For BLoCs, make a new capsule that uses the use.reducer side effect. For Cubits, make a new capsule that uses the use.state side effect.

Here's the catch: I wouldn't normally recommend migrating existing projects to ReArch unless they are fairly small (less than a few thousand LoC). ReArch is significantly different than other libraries and only plays nicely when you model things entirely immutable and functionally.

Would you create it if I sponsored it?

Sure! If you let me know exactly what you might want it to look like (such as an idea on what they should do, or even better, an already-written BLoC), I'd be happy to produce the ReArch equivalent

jtkeyva commented 9 months ago

Got it thanks. Yeah, well I have a project that I have been working on for quite a while that used BLoC but have been re-writing it for production. There's not a lot going on but with so much BLoC boilerplate & using the repository pattern it's pretty cumbersome to rewrite even basic features.

I am nervous about going all in on ReArch even though it feels right. One of the things I like most about BLoC is it's forced structure and diciplined pattern. Do you have a suggested ReArch way? I mean, your big claim is that you have re-imagined the way apps are architected...but I don't see any examples or docs that walk you through a suggested structure or pattern.

I want to be sure I am structuring things "right" but currently there is not blueprint or suggested structure/pattern.

GregoryConrad commented 9 months ago

ReArch’s approach to “architecture” moreso applies to how applications are designed (especially with regards to data and flow) rather than laid out (what code goes where). You’re welcome to use any categorization/hierarchy you choose. Now as far as the how applications are designed bit, there is a good chunk of docs on that, which is covered under the different “paradigms” sections at ReArch.gsconrad.com