2

I have been building a greenfield application. It is three layer (data-access, domain-models, presentation) with the presentation layer being VMVC. This is the cleanest application I've ever built by far, and everything has fallen into place in the architecture very well.

However, there is one fly in my soup aggravating my code OCD... How do I deal with the basic information that is passed around in every page? For example, the name of the currently logged in user, their reputation points, and whatever is in his or her application clipboard. That is to say, how do I get "Top Bar" View-Model passed to common layout view (_Layout.cshtml)?

I have the domain-model user object of the current user referenced in the AppContext object, which has worked well for authorization, ect. But where do I turn that into a view-model and get it to the layout view in a MVC way? Putting that stuff in every single view-model seems a bit ridiculous. Having the AppContext generate the view-model seems... really really dirty.

Sorry if this question is a little petty, but I'd really like to see if you people have some sort of elegant solution that I've missed; especially when the application has come together so cleanly so far.


Clarification of architecture:

Data-Access (Repositories) references domain-model. Presentation references data-access and domain-models. The controller makes a call to the repository which returns the domain-models; these domain-models are mapped to view-models which are then passed to the views.

I am following the one view-model to view rule.

Robert Harvey
201k55 gold badges469 silver badges682 bronze badges
asked Mar 21, 2016 at 2:03
3

1 Answer 1

1

Still would be interested in someone else's solution, but here's what I did:

public static class LayoutController
{
 public static LayoutModel Model
 {
 get
 {
 var model = new LayoutModel();
 var user = AppContext.User;
 model.Name = user.PrettyName;
 model.HasSystemAccess = user.HasAccessTo.TheSystem;
 return model;
 }
 }
}
answered Mar 22, 2016 at 12:14

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.