Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply SRP to user interface classes?

My application uses a DI framework and follows "program to an interface" practice wherever necessary.

I use constructor injection since I'd like to see the dependencies explicitly. But now the constructors of my form classes take too many parameters (>=4 e.g).

Question: Since UI design doesn't usually adhere to SRP, a Winform class might have n constructor dependencies. Do you like to leave them as they are, pass a proxy object instead, use a service locator...? Also do you inject "aspects" (logger, etc) in every constructor, given that no aop-framework is used?

like image 799
henginy Avatar asked Nov 30 '25 15:11

henginy


1 Answers

There's no reason why UI implementations should not adhere to SRP. Using patterns such as MVP, MVC or MVVM, a UI class' single responsibility is to to render and collect data through the UI. This is often best done through rather passive data structures such as View Models.

The issue about constructor over-injection should be addressed by refactoring to Aggregate Services.

Aspects are best addressed through application of the Decorator design pattern.

like image 86
Mark Seemann Avatar answered Dec 03 '25 05:12

Mark Seemann



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!