Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:58:32+00:00 2026-06-08T20:58:32+00:00

I have a pretty well designed architecture where controllers go to services which access

  • 0

I have a pretty well designed architecture where controllers go to services which access repositories which communicate with the database.

As such, logic in controllers is kept at a minimum, but I still have very subtle pieces of code that perform some tasks like

  • validate the model
  • arrange the action method arguments
  • invoke some service with these arguments, maybe validate the result and return the view if the model is now invalid
  • finally produce a model from the service’s result, and return that.

some longer cases do different things depending on a “status” returned by the service.

here’s a couple of examples:

[HttpPost]
[AjaxOnly]
[Authorize]
public JsonResult Preview(string input)
{
    LinkResult parsed = linkService.ParseUserInput(input);
    if (parsed.Result == LinkParseResult.Used)
    {
        long? postId = parsed.Link.PostId;
        if (postId.HasValue)
        {
            Post post = postService.GetById(postId.Value, false);
            return Json(new
            {
                faulted = "used",
                link = DetailsRoute(post),
                id = postId
            });
        }
        else
        {
            return Json(new { faulted = "invalid" });
        }
    }
    else if (parsed.Result == LinkParseResult.Invalid)
    {
        return Json(new { faulted = "invalid" });
    }
    else
    {
        Link link = parsed.Link;
        if (link.Description != null && link.Description.Length > 200)
        {
            link.Description = link.Description.Substring(0, 200);
        }
        return AjaxView(link);
    }
}

and (Post comes from domain, PostModel is the view model)

private PostModel PostModelConverter(Post post)
{
    Link link = post.Link;
    if (link == null)
    {
        throw new ArgumentException("post.Link can't be null");
    }
    if (link.Type == LinkType.Html)
    {
        return new PostedLinkModel
        {
            Description = link.Description,
            PictureUrl = link.Picture,
            PostId = post.Id,
            PostSlug = postService.GetTitleSlug(post),
            Timestamp = post.Created,
            Title = link.Title,
            UserMessage = post.UserMessage,
            UserDisplayName = post.User.DisplayName
        };
    }
    else if (link.Type == LinkType.Image)
    {
        return new PostedImageModel
        {
            PictureUrl = link.Picture,
            PostId = post.Id,
            PostSlug = postService.GetTitleSlug(post),
            Timestamp = post.Created,
            UserMessage = post.UserMessage,
            UserDisplayName = post.User.DisplayName
        };
    }
    return null;
}

this raises the question about if view models really should be in the web project as a rule, or they could actual be part of the domain, or some other project.

I’m not sure I can do much about the preview action, other than maybe use a PreviewModel that receives the Link, and truncates the description, but that would save up like, two lines.

The model converter should probably be somewhere else, but I’m clueless as to where that should be.

Another point that comes to mind is if I should be splitting this controller either using the partial keyword (is it a bad practice to use this for something else than autogenerated classes?), or adding routes that use different controllers depending on what action is requested or what http method is being used, what’s the usual way to handle that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:58:33+00:00Added an answer on June 8, 2026 at 8:58 pm

    This has been asked several times:
    Business logic in the controller
    Where should I put my controller business logic in MVC3
    Keep Controllers Thin

    As well as written about elsewhere:
    ASP MVC Best Practices – Skinny Controllers
    Keep Controllers Thin

    The community seems to be at a good consensus that this kind of logic belongs outside the controllers. Generally in the model (or ViewModel), but somewhere in the business layer.

    As a final note, using partials for non-autogenerated code is not discouraged. If it makes sense to split things, do so. Just think about about what your reasons for splitting it are. This is going to be a case-by-case kind of thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question seems pretty well formulated I have a virtual machine which implements only
I have a link that look like this: www.localsite.dev/posts/332635955335419755 which works pretty well and
I have an app using SQLite3. It's running pretty well, but I would like
Well, the subject pretty much says it all. I have code to unzip and
Well, I'm gonna be pretty straightforward here, I just have a piece of code
I might have pretty basic question about regex. I have the following regex, which
I found HTMLParser for SAX and xml.minidom for XML. I have a pretty well
I'm a beginner in powershell and know C# pretty well. I have this command
OK, I have my site going pretty well here: http://www.marioplanet.com But I've realized that
I have a pretty complex but well-formatted HTML string with lots of links in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.