Question

I am pretty new to MVC and I am currently working on an MVC 3 project in visual studio and I want to create a method or variable that is accessible globally. When I say globally I mean available in my web project, service layer project, and data layer project.

I guess when I say global I mean global to the entire solution.

I tried creating a class in the solution items folder and referencing in my web project but its not letting me add a reference to the class since it is not a DLL.

I am a little confused with how to do this. Any suggestion would be appreciated. Also keep in mind that though I am a programmer I am still somewhat new to MVC and programming.

Edit: I have also tried adding a method in the global.asax file but was unable to call it

Was it helpful?

Solution

You should create a shared assembly where you define the class. You can then add a reference to the shared assembly from all projects that need the feature.

OTHER TIPS

The class that you want to be "global" sounds like some sort of service. I suppose this is the kind of thing you may want to do with a logging service for example.

Using a logging service as an example it is generally best practice for the interface to the logging service be defined in a lightweight contracts type assembly. Then any of your assemblies that require an implementation of ILoggingService should inject the necessary implementation using an IoC container such as Autofac or MEF.

This pattern is pretty common and allows you to share common services while keeping implementations loosely coupled. Also this pattern will lead to highly testable code as fake implementations can be injected with Moq

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top