2

I have a solution which contains 2 MVC Website Applications (Website & Website Admin System). As I am progressing with the build there is a lot of common code especially in my View Models, HtmlHelpers etc. Is it good practice in MVC to treat these two sites as separate entities and keep code in one site specific to that site and manually copy code across to second site? Or is there a practice to share this common code across web apps?

It is just natural for me to refactor common code when I see it? However I am not so sure how this works within an MVC environment. Any suggestions or comments welcome.

Cragly
  • 3,554
  • 9
  • 45
  • 59

2 Answers2

1

You can create a class library project and push that code to that project. Then have both website projects reference that DLL. I believe views are about the only thing in MVC that you can't do that with. I believe you can do it with controllers too, but I haven't tried it.

Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • 2
    You can do it with views via embedded resources. – John Farrell Dec 31 '10 at 15:23
  • Thanks Brian, I did think of this as have done it with other WebForms projects. However in my mind I think this tips the scales a little when trying to get the fine balance of maintainability/readability. Umm... maybe I have answered my own question – Cragly Dec 31 '10 at 15:23
  • I just posted code for the views on another question - http://stackoverflow.com/questions/19746/views-in-separate-assemblies-in-asp-net-mvc/4573845#4573845 - there aren't too many maintainability issues if you give your shared library the same structure as a regular mvc project with local controllers as subclasses of the shared controllers - much less than copying files between projects at least. – Tom Clarkson Jan 02 '11 at 00:56
1

If your sites are sharing many ViewModels and HtmlHelpers I'd take a step back and examine why they are two separate sites. Is the second one an Admin area of the first site, or is it a completely separate entity altogether? If the first, I'd probably have everything in one site. If the second, I'd do as Brian Ball suggested and create a library project that the two sites share.

Hc5kphUXpR
  • 155
  • 1
  • 8
  • The second site is going to be more than an admin of the first. However it does have a lot of admin tasks such as a lot of CRUD work for Users etc which is where most of the crossover starts. I have taken Brian's approach and created a separate class library which the two sites reference. All common Web related objects will reside here. Thanks all. – Cragly Dec 31 '10 at 19:02