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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You can do it with views via embedded resources.
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
I just posted code for the views on another question - stackoverflow.com/questions/19746/… - 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.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.