Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating UI and logic in C#

Does anyone have any advice on keeping logic out of my GUI classes? I try to use good class design and keep as much separated as possible, but my Form classes usually ends up with more non-UI stuff mixed in than I'd like, and it tends to make maintenance a real pain.

(Visual Studio 2008 Professional, C#, Windows apps).

Many thanks.

like image 937
Andy Avatar asked Jun 25 '10 12:06

Andy


People also ask

How do you separate UI and logic?

Separating the UI from Business Logic The concept is simple: divide a computer program into sections that each address a separate and distinct concern of the application. This so-called "concern" can be something as simple as naming a class that is instantiated or as broad as platform specific details (Figure 1).

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 do we separate business logic?

Separating the logic from the data solves lots of issues surrounding sharing data in spreadsheets. Version controlling spreadsheets is a nightmare, collaborating is hard, and the data sprawl that comes from emailing spreadsheets inevitably leads to privacy and security concerns.

What is UI logic?

UI logic is how the content is been showing on the screen,(build method), business logic is you provide input to the code returning the output you require. (Methods/classes etc.). Setstate apparently updates the value in UI.


2 Answers

Put your logic in a separate assembly; and, build that assembly without its referencing any GUI packages (e.g. System.Drawing, System.Windows.Forms, etc.).

like image 91
ChrisW Avatar answered Oct 11 '22 00:10

ChrisW


It's really just a matter of practice and self discipline. I mean, we've all done it. And we all continue to do it from time to time under the wrong conditions (manager/customer yelling to get something done "right now" vs. "right", etc.).

One thing I do when writing code to drive the UI (more on the web side, but the same thing applies) is to ask myself with each unit of code (a single line, a conditional, a loop, etc.) whether that piece of code depends on the presence of the UI. If I'm writing to a text box, that's UI-dependent, so it goes there. But if I'm calculating the result that will go in that text box, that's probably business logic.

Another approach (as ChrisW alluded to out while I'm typing) is to develop the logic first in a non-UI class library. Put as much logic in there as you can (use your judgment as to what defines "logic" though) that doesn't depend on UI-based libraries. Then build the UI to make use of that logic. There are different approaches to allow the concurrent development of these two pieces, such as stubbing out the logic assembly behind interface classes and just coding the UI pieces to those interfaces (then using dependency injection to plug the assembly classes into the interfaces), etc.

like image 10
David Avatar answered Oct 11 '22 01:10

David