T4MVC Use Extension methods in Non Web App Projects

Oct 18, 2012 at 5:42 PM

I have 3 projects in my solution, 2 web and 1 class library which has a number of extension methods. When my extension methods take a dependency on T4MVC extension methods I must move that code into a class within App_Code of each web project. This results in not being able to share extensions across multiple web apps without having to copy code between web projects.

From what I can tell it appears T4MVC creates code which could be categorized as dynamic and static - where dynamic is specific to the web project itself and static is the same code generated every time for ever web project. It is this static code which I would like to lift out of the T4MVC generated code and place into my class library.

One initial idea I had was to have the T4MVC template generate the dynamic and static code in 2 partial classes rather than just one. Then a setting could be added when someone such as myself wants to store the static in a shared library. The setting would accept a reference namespace in the external DLL and #IF EXTERNAL...#endif the static partial class so all references go to the external DLL. A developer could then pick up the #IFed out code and copy that into the external DLL.

Thoughts? Would this even work -- not sure just throwing it out there so be kind to my brainstorming here.

Coordinator
Oct 18, 2012 at 5:47 PM

If you look in the settings file, there is actually a GenerateMvcT4Extensions setting that targets this kind of scenarios. The idea is that you can put all the non-dynamic code into some shared library, and then you turn off this setting so that code doesn't get generated for each project that uses T4MVC.

But taking this yet further, I think the way to go is for T4MVC to come with that DLL in the first place, and never generate the non-dynamic parts. This is discussed here. We just need to make it happen! :)