views:

848

answers:

3

I am using web forms, C#, Asp.net. As we all know, in this model UI and business logic are often mixed in. How do I separate these effectively?

The example I would like to use is: I have a GridView and a DataTable (GridView binds to the DataTable and DataTable is fed from the stored procedure).

I would like the GridView (UI) and DataTable (business logic) to be decoupled.

Is it worth it to write an wrapper for DataTable? Are there practical patterns that have been proved and tested that you could recommend to be followed?

If someone with experience could shed some light, that would be awesome. And, as a final note I would like to say that ASP MVC is not an option right now, so don't recommend it.

My database access layer returns a DataTable. Note that I HAVE to use this database layer as this is a company policy.

+1  A: 
George Stocker
I am having difficulty following your links. It may be because your situations are specific portions of the process, while for me this is the first time and I would like to start at the beginning, not with Dictionary.
gnomixa
Ok; fair enough. I can expand my answer, if you'd like.
George Stocker
I am going to correct myself. I HAVE to use DataTable object as this is what my data layer returns which is a company policy - we all have to use this data access class. How would I go about decoupling then?
gnomixa
thanks, hmmmm, in my case I guess i will have to convert DataTable to List<T>. At this point, I will have to ask what is the advantage of converting an object that is made for GridView to a generic List?
gnomixa
i think in my scenario creating a class that inherits from DataTable is more applicable. Then, I can manipulate it as a response to UI events. Actually, maybe not - that's pretty much what I am doing now. I am not sure if it's worth it to decouple given that I have a constraint to use DataTable.
gnomixa
no i don't have to transport DataTable to UI layer. I do have to use dataTable when getting data (through data access layer). So the next step after getting data would be, transporting the data to the Business layer object (such as List<t>, dictionary, or whatever)?
gnomixa
If you keep the data as a datatable (or a class that derives from Datatable) then you'll always be tied to your current implementation. What happens if you add columns to the database and now have to get those out (it gets hairy). What happens if they change how you get this data?
George Stocker
The advantage of using a List<T> or a business object is that you still use the DataTable to get the information from the database, but if that changes later on, you only need to make changes in your Data Access Layer, and not in both your DAL and your UI layer.
George Stocker
Gortok, one more question about this. Now I save the data table inside the session btwn postbacks. After separating I should still load List<T> to and from Session inside the UI, correct? As far as I know, business layer should not be accessing Sessions object. Please clarify.
gnomixa
A: 

I'd start by decoupling the data table right into the trash can. Build a domain layer, and then some type of data access layer which deals with the DB (ORM recommended).

Then build a servicing layer which provides the data to the UI. All business logic should be within the service or the entities themself.

JoshBerke
no go for me. I HAVE to use DataTable object as this is what my data layer returns which is a company policy - we all have to use this data access class.
gnomixa
A: 

Consider implementing MVP (model view presenter) pattern. It gives you separation of biz logic through presenter interface, which also allow better unit testing capabilities. Your codebehind of aspx page is then just connector of events and getter/setter of properties. You can find it in MS pattern&practices enterprise application blocks (CAB - composite application block - if i'm not mistaking).
You can read more about it here: http://msdn.microsoft.com/en-us/magazine/cc188690.aspx
But also going from DataTable/DataSets to objects (POCO) is preferred.

Hrvoje