Three-layer web application

Is it good, good practice, to use the second layer to redirect the user?

For instance:

public static void ForceLogin() { HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName]; if (cookie != null) { if (Regex.IsMatch(cookie.Value, "^[0-9]+\\.[a-f0-9]+$")) { using (EibxDataContext db = new EibxDataContext()) { int count = db.Logins.Count(l => l.Password == cookie.Value); if (count == 1) { return; } } } } HttpContext.Current.Response.Redirect("~/Login.aspx"); } 

On the last line, I use the Business / Service logic level to redirect the user to the login page.

Should this be done at presentation level?

+4
source share
3 answers

Absolutely not. The business logic level must make the decision, the user interface layer must do the redirection. The business layer should not know anything about HttpContext and should not directly read cookies. Pass the relevant information to the business layer so that the business layer can make a decision and transfer the decision to the user interface layer so that it can work on the resulting solution.

Here's the reason ... what if the business layer is used from a web service? How can the business layer redirect in this case? Or suppose it is used with a non-web client? Redirecting does not make sense in this context. If you change your user interface level, which should not affect the level of your business logic, and mixing in the forwarding and reading cookies to the business layer will require this with the proposed design.

+8
source

It depends on how you define your layers; for example, my "business logic" is usually related to a problem that I am trying to solve and does not know anything about the user interface. Therefore, it cannot redirect, since it does not have access to the request / response.

Personally, I would do it at the user interface level; working with raw interactions, such as the gatekeeper and the keeper, is part of setting the user interface level for the web application. IMO For example, through an http module, which is (by definition) a component of the UI layer.

+5
source

I would say that you are doing it right in business logic. The presentation layer does not have to make routing decisions.

0
source

All Articles