I am writing an ASP.NET application which I have a UI layer, business logic layer, and data access layer. From my data layer I return business objects to the business logic layer and pass these on to the UI layer. However, I'm not sure what to do when I want to perform a save/insert with data from the UI layer.
Should I create the business object at the UI layer and pass on to the business layer or should I be creating it in the business layer?
many thanks
I agree with crunchdog-- for all but the most trivial web applications you should have a flattened form of your business objects specifically for your UI/view layer. Sometimes this is referred to as a View Model class and generally contains nothing more than several string properties that the UI layer can get from and put to directly without worry of validation. (see asp.net mvc)
For one, this keeps the UI layer cleaner and simpler, leaving it to put it's efforts toward displaying the data rather than traversing object structures, checking for and interpreting nulls, etc.
This also gives the business layer the opportunity to do validation upon those string values, returning the values as entered if they are invalid. This can save coding frustration when, for example, your server has to handle an invalid date in a date field. The business layer, recognizing the invalid values, can return them exactly as received along with the proper error messages. If all you dealt with were business/domain objects, then some values entered may not always fit the objects intended to hold them.
It can also help to have a class designated for mapping values back and forth between the business/domain objects and the UI object/view model. This helps keep the business layer concerns cleanly separated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With