1

so i am in a hastle here , in the the presentation layer I want to pass data through the bloc events to the bloc state and finally to a page and I found that I am repeating the same params everywhere , so I though if I can use the entities in the domain or make a new entity that has the things that I need and avoid boilerplate code .

to make it clearer for you I have an entity in the domain layer with multiple field called order

I have an add new order page it is split to two layers a first page and a second page , the first page contains general information about the order , and the second page contains specefic details . when the operation in the first page is successful it uses bloc to emit a transition to the second page with the general information then the second page adds to these general info and initiates the add order use case through the bloc . i hope that was clear enough to understand my issue

this is the code to help you understand more :

Widget build(BuildContext context) {
return BlocConsumer<OrdersBloc, OrdersState>(
  listener: (context, state) {
    if (state is AddNewOrderMoveToSecondPagePermitted) {
      Navigator.of(context).pushReplacement(PageRouteBuilder(
          pageBuilder: (context, animation, secondaryAnimation) {
            return AddNewOrderMapPage(
                state.issuerId, state.orderDescription ... etc);
          },
          transitionsBuilder:
              (context, animation, secondaryAnimation, child) {
            return FadeTransition(
              opacity: animation,
              child: child,
            );
          },
          transitionDuration: const Duration(milliseconds: 600)));
    }
    if (state is OrderUploaded) {
      showSnackBar(context, "success", SnackBarType.success);
    } else if (state is OrderFailure) {
      showSnackBar(
          context, "something wrong happened", SnackBarType.failure);
    }
  },
  builder: (context, state) {
    return const AddNewOrderFirstPage();
  },
);

}

 void _onAddNewOrderSecondPageRequested() {
on<AddNewOrderMoveToSecondPage>(
  (event, emit) {
    emit(AddNewOrderMoveToSecondPagePermitted(
        orderType: event.orderType,
        orderTitle: event.orderTitle,
        orderImages: event.orderImages,
        orderId: event.orderId,
        offerPrice: event.offerPrice,
        orderDescription: event.orderDescription,
        issuerId: event.issuerId));
  },
);

}

the event and state share the same parameters which are these

 orderType: event.orderType,
        orderTitle: event.orderTitle,
        orderImages: event.orderImages,
        orderId: event.orderId,
        offerPrice: event.offerPrice,
        orderDescription: event.orderDescription,
        issuerId: event.issuerId

please guide me because I am new to clean architecture . thank you

1
  • I recommend to just create an Entity for Order named OrderEntity for example and then pass just this Entity in to your Events and Blocs. Commented Nov 23, 2024 at 22:06

1 Answer 1

0

I would recommend using new entity you can called it CreateOrderEntity and put all your properties together there , there is a lot of details about the entity and page I don't know about ,

class CreateOrderEntity{
double price = 0;
string orderType = "";
string orderTitle = "";
//etc

}

you will now pass one object then use it across your two pages or the way you need to fill the entity , I hope you found what was your looking for, also let me know if you need further information

Sign up to request clarification or add additional context in comments.

5 Comments

firstly thank you for the response ,should createOrderEntity contain the exact same fields that the order entity has? because the create order in the presentation layer contains 2 pieces a first page that only has the general info of the order and the second the page that has specific info like coordinates the entity that I have in the domain layer has all those info so how will i handle it ?
Dear no worries at all , if you don't need any extra properties you could use OrderEntity in this case no need to create extra entity , the only case that you will need extra entity that the properties differ , and yes it could contains the two pieces I don't see any issue with that
okay sir, other thing that I am little bit confused on is wouldn't this contradict the clean architecture principles of keeping the presentation layer separate from the domain layer? and that a higher layer is not to be used by a lower one?
In clean architecture, the presentation layer should be separate from the domain layer, usually using DTOs to map entities. However, for simplicity in smaller projects, you might use entities directly. Using Entities: - Pros: Simpler, fewer layers. - Cons: Tighter coupling, harder to scale. Using DTOs: - Pros: Keeps layers decoupled, more maintainable. - Cons: Adds complexity. For small apps, using entities directly is fine; for larger apps, DTOs are better.
can you please explain further and provide an example ? In a real world application that could scale big how would i handle it , i am not entirely familiar with dtos in the presentation layer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.