1

I began to work on some small project and currently thinking about best approaches in architecture. Assume I have a class with a lot of properties that I retrieve in controller from my database.

Building building = BuildingsBLL.GetBuilding(buildingID);

Buildings are of different types with different sets of properties.
I see two options:

  1. I can pass Bulding to common view with a lot of code and show / hide specific blocks on the page according to the building type (using if building type blablabla...). Or

  2. I can cast building to another type (School or Hospital or smth else) and pass it to certain view with minimal amount of code.

So in the first case I need just one view and no models(?) and in the second case I need separate model and view for each building type.

In my opinion the second case is preferable. But maybe I'm missing something and aforementioned approaches are not usable at all?

1
  • Generally speaking, upcasting is a bad idea. If you had to address your items as a more general type (i.e. the interface / abstract base class), that usually means you should keep referring to them that way. But technically, nothing prevents you from doing if(item is Skyscraper) { Html.RenderPartial("SkyscraperDetail.ascx", item); } iirc. Pardon the old MVC syntax, I haven't worked in newer versions yet. Commented Jan 29, 2014 at 12:17

1 Answer 1

1

So in the first case I need just one view and no models(?) and in the second case I need separate model and view for each building type.

I would do it in following way - Have abstract Building Model, then inherit SchoolBuilding and HospitalBuilding models from Building Model.

Then in the frontend, if I have lot of operations on both School and Hospital buildings, then I will have separate controllers for both. But if operations are considerably less, then I will have only Building Controller in which different actions corresponding to School and Hospital models. Based on the amount of re-usability, I will extract some partial views for both hospital and school actions.

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

Comments

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.