DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers

Refactoring toward frictionless & odorless code: The case for the view model

Oren Eini user avatar by
Oren Eini
·
Apr. 11, 11 · News
Like (0)
Save
Tweet
Share
5.42K Views

Join the DZone community and get the full member experience.

Join For Free

remember, we are running on an action scoped session, and this is the code that we run:

public class homecontroller : sessioncontroller
{
    public actionresult blog(int id)
    {
        var blog = session.get<blog>(id);

        return json(blog, jsonrequestbehavior.allowget);
    }
}

the error that we get is a problem when trying to serialize the blog. let us look again the the class diagram:

image

as you can see, we have a collection of users, but it is lazily loaded. when the json serializer (which i use instead of writing a view, since it makes my life easier) touches that property, it throws, because you cannot iterate on a lazily loaded collection when the session has already been closed.

one option that we have is to modify the code for blog like so:

public class blog
{
    public virtual int id { get; set; }

    public virtual string title { get; set; }

    public virtual string subtitle { get; set; }

    public virtual bool allowscomments { get; set; }

    public virtual datetime createdat { get; set; }

    [scriptignore]
    public virtual iset<user> users { get; set; }

    public blog()
    {
        users = new hashedset<user>();
    }
}

i don’t like it for several reasons. first, and least among them, serialization duties aren’t one of the responsibilities of the domain model. next, and most important, is the problem that this approach is incredibly brittle. imagine what would happen if we added a new lazily loaded property. suddenly, unless we remembered to add [scriptignore] we would break everything that tried to serialize a blog instance.

i much rather use an approach that wouldn’t break if i breathed on it. like the following:

public class homecontroller : sessioncontroller
{
    public actionresult blog(int id)
    {
        var blog = session.get<blog>(id);

        return json(new
        {
            blog.allowscomments,
            blog.createdat,
            blog.id,
            blog.subtitle
        }, jsonrequestbehavior.allowget);
    }
}

by projecting the values out into a known format, we can save a lot of pain down the road.

but wait a second, this looks quite familiar, doesn’t it? this is the view model pattern, but we arrived at it through an unusual journey.

i don’t really care if you are using anonymous objects or named classes with something like automapper, but i do think that a clear boundary make it easier to work with the application. and if you wonder why you need two models for the same data, the avoidance of accidental queries and the usage of the action scoped session is another motivator.

View model

Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Building a Flask Web Application With Docker: A Step-by-Step Guide
  • Scaling Site Reliability Engineering (SRE) Teams the Right Way
  • Automating the Migration From JS to TS for the ZK Framework
  • Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: