简体   繁体   中英

A better way to implement Business Logic in MS Web Forms

I use C# Entity Framework and Asp.net Web Forms 4.

At the moment I'm developing the Administrator section for a Cms so I have to work with many GridView Controls for CRUD operations on my data.

I use EF as DAL and I implement my Business Logic inside every .aspx page; Pages are organized logically by folders.

My BL are pretty simple like: for User A takes from DB X, Y, Z and display it on a GridView instead for User B takes from DB only X and display on the same GridView.

In my little experience I found my self spending a lot of time in embedding my BL Roles inside each .aspx using Event Triggers Web Form Mechanism. My problem is that same pages could have the same BL and so I have to copy and paste my code in may different .aspx pages.

My questions:

  • How can I centralize my Business Logic and display data accordingly in many GridView without implement the BL in every page where the GirdView is codeded?
  • I understand a bit about Classes, could make sense implement BL in a Class and attach it to a GridView using the ObjectDataSource? Example here
  • In your experience what is the better approach to his kind of problems?
  • Could MVC approach be a solution?

Thanks for your time on this!

You need to layer your application - for example, in a typical scenario, you can have following projects:

  • Entities (more correctly DTO (data transfer objects)) and other classes/logic that needs to be used in all other layers
  • Data Access Layer that would handle persistence of your entities to data store
  • Business Logic Layer
  • UI Layer

You are already made choice of EF4 as your DAL. You may use POCO entities (Code-First model) to have your entity/DTO classes in a separate project. As far as your business logic goes, put that in business layer which is actually bunch of classes that would define API. For example,

public static class CustomerHelper
{
   public static Customer Get(int customerId)
   {
      // do access control security
      ...
      // use EF4 to get the customer object
   }

   public static void Update(Customer customer)
   {
      // do access control security
      ...
      // use EF4 to update the customer object
   }

}

Generally, business layer can have validations, security checks, transaction control, any pre/post processing needed for the business transaction etc.

Finally, your aspx pages will be your UI layer that would use BL classes to get/update the data. For declarative data-binding, you have to use object data source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM