Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update UI from business layer?

I have a three layer application in C#. In business layer i have many threads that do same job . I want to show the progress of each thread on UI , but i don't have the reference of presentation layer .

How can i do this ? What's the best way for this ?

Thanks .

like image 224
Mahdi Amrollahi Avatar asked Jun 20 '10 07:06

Mahdi Amrollahi


People also ask

How do I separate business logic from presentation layer?

When separating the business and presentation logic, you need to consider the following: Avoid affinities between the two parts of the application. Be aware of the DPL-restricted API; see Exception conditions for LINK command for details. Be aware of hidden presentation dependencies, such as EIBTRMID usage.

Why to separate UI from logic?

UI components are sometimes messy. If the business logic is tightly coupled with the UI it is often very hard to write proper tests. The only way to make those components testable is by decoupling UI elements from the business logic.


2 Answers

The most appropriate answer here is probably to expose an event somewhere on your business layer. Your UI code can subscribe to the event, and handle the event by switching to the UI thread (if necessary) and update itself.

Then the business code doesn't need to know about the UI, and can work the same without any UI (but as with all delegates / callbacks, you need to check for null before attempting to invoke the delegate).

like image 64
Marc Gravell Avatar answered Sep 28 '22 19:09

Marc Gravell


The simplest way is for the UI to pass the business layer a delegate to call (or an interface) so that it can indicate progress.

This is also really easy to test, because it separates the concerns: in your business layer tests, you can pass in a test delegate and make sure it gets called. In your UI tests, you can fake out the business layer, and pretend that there's progress, calling the appropriate delegate and checking that the UI updates appropriately.

like image 40
Jon Skeet Avatar answered Sep 28 '22 19:09

Jon Skeet