burnt1ce
burnt1ce

Reputation: 14897

ASP.NET MVC: Passing context to a custom class

What's the best way of passing context to a custom business/helper class from an action method? The helper/business class that i'm creating will need information such as the currently logged in user, an instance of a UrlHelper class (to generate urls) and any other data that's related to ASP.NET MVC's infrastructure.

I was thinking of just passing the instance of a controller. Is this advisable? What's the best practice?

Upvotes: 1

Views: 1211

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

The helper/business class that i'm creating will need information such as the currently logged in user.

Unlikely. IMHO It will suffice the username => User.Identity.Name, which could be passed as a string parameter to this business tier.

an instance of a UrlHelper class (to generate urls)

Unlikely. Generating urls is not the responsibility of the business tier. Pass the urls directly from the controller which has an Url property allowing you to generate urls at free will (again as string parameters).

Conclusion: decouple your business logic from your infrastructure. Basically if you are calling something a business logic the sole notion of URL helper hardly makes sense. Once you cross the boundaries of the web tier there are no more url helpers.

Upvotes: 5

John Farrell
John Farrell

Reputation: 24754

To reduce coupling pass value types or parameter/dto classes that already exist in your business helper layer.

Don't pass anything related to the Web/UI tier.

Upvotes: 1

Related Questions