5

I'm creating a .NET MVC solution that will have two web projects, one for API and for Web. Some models, such as customer, will be the same between the two projects.

Is it best to have them kept in a common web library and referenced from the two web projects, or keep them unique to each project?

asked Jul 6, 2016 at 4:58
2
  • 3
    The most applicable principle here is probably DRY. Commented Jul 6, 2016 at 6:05
  • 2
    if you put it in a library you still will be able to create derived classes when required. So I guess that's more flexible because you can avoid duplication and allow extension. Commented Jul 6, 2016 at 11:55

1 Answer 1

5

It's tempting to ruthlessly eliminate duplication in our systems, and for very good reasons, but we must always ask ourselves whether this is real duplication or apparent duplication.

What I mean is that we'll sometimes have code that looks identical, but each will have their own reason to change, independent of each other. If you think the view model for the App will always change in tandem with the view model for the API, then they should share the same view models. However, you'll likely soon find that the view model for your app needs a property that the API doesn't need, due to needing some ephemeral UI element (like, say... a drop down list that contains data from some external system you'll be "borrowing" in your app).

I see the app and the API as two different front ends to the same business models. They should absolutely share the same business models (logic/dtos), but sharing view models is iffy at best IMO. If things are identical now, you could share the code, then inherit from your shared classes later, if you run across a situation where you need to extend the base models. Maybe the best of both worlds.

answered Jul 6, 2016 at 10:31
1
  • 2
    Since view models do not (or should not) contain logic, the only thing you are duplicating is the code required to get and set values for the object's private fields. This is so trivial that there is no benefit to sharing the class between two projects. +1 Commented Sep 27, 2018 at 13:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.