Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a mistake by not using .Net MVC?

Microsoft web application architecture related...

Wondering if I'm making a mistake by not using .Net's MVC for my new web application? I started out web development in ASP Classic and have moved forward with each iteration of ASP.net. I've monkeyed around with ASP.net MVC the past few months and just didn't like certain parts of it. I like routing, razor, and the idea of view specific models. But it just seemed like my apps were getting overly complex after adding a few functionalities--I feel the same is true when I look at the MVC versions of apps like nopCommerce and Umbraco compared to their prior versions.

I went back and basically started writing a .Net Website/MVC hybrid. I created my own base class for "view models" that implemented Data Annotation validation; a Simple Mapper to bind form submissions to the models and to map entity properties to model properties and vice versa; created extension methods and helpers for things like paging, checked, selected, and even/odd; use controls like Repeaters, Literals, and standard HTML tags with runat="server" that don't require view state.

This approach seems to allow me to have the best of both worlds, keeps my "Controller" code close to the "View", and everything works in Medium Trust.

Here is some sample code:

public partial class Admin_Users_RoleAdd : System.Web.UI.Page
{
    protected class RoleAddModel : BaseModel
    {
        [Required, StringLength(100)]
        public string Name { get; set; }
        [StringLength(250)]
        public string Description { get; set; }

        public override bool Validate()
        {
            if (base.Validate() && Cortex.DB.Roles.Any(r => r.Name == Name))
                Errors["Name"] = "Already in use";
            return Errors.Count == 0;
        }
    }
    protected RoleAddModel model = new RoleAddModel();
    protected override void OnInit(EventArgs e)
    {
        if (Request.Form["Submit"].HasValue())
        {
            SimpleMapper.FormMap<RoleAddModel>(model);
            if (model.Validate())
            {
                var entity = new Role();
                SimpleMapper.Map<RoleAddModel, Role>(model, entity);
                Cortex.DB.Roles.AddObject(entity);
                Cortex.DB.SaveChanges();
                Response.Redirect("Roles.aspx");
            }
        }
        base.OnInit(e);
    }
}

And the "View":

<h1>Add Role</h1>

    <div id="MainForm" class="form">
        <%= model.GetErrorMessage("Error") %>
        <form action="<%= Request.RawUrl %>" method="post">
            <div class="formField">
                <label for="Name">Name</label> <%= model.GetErrorMessage("Name") %><br />
                <input type="text" name="Name" value="<%: model.Name %>" class="required" maxlength="100" />                
            </div>
            <div class="formField">
                <label for="Description">Description</label> <%= model.GetErrorMessage("Description") %><br />
                <textarea rows="8" cols="40" name="Description" maxlength="250"><%: model.Description %></textarea>             
            </div>
            <div class="buttons">
                <input type="submit" name="Submit" value="Create" class="primary" />
                <a href="Roles.aspx">Back</a>
            </div>
        </form>
    </div>

Are there things I will regret about this approach later? The main thing I can think of at the moment is test-ability, but the VWD Express doesn't support it anyway.

like image 758
Sam Avatar asked Oct 06 '22 20:10

Sam


2 Answers

I don't like Microsoft's implementation on MVC; the use of magic strings, that it seems somewhat backward to say the action then the controller, that it's like having to learn a dozen new language because it's full of microsolutions (routing, razor).

I dislike it so much so I started to write my own, but the more I wrote, the more I realized it was inflexible and the more I fixed this, the more like the Microsoft implementation it looked.

After hours and hours and hours of development on essentially a crap version of MVC, I ditched it. I've started to go back over design documents and code and write over (or at) the top of them

WRITE LESS CODE

I rewrote the UI tier of my crappy MVC app to the MS MVC in about eight hours. I'd been working on it for about eight weeks before that. Granted most of the thinking had already been done so it may not have been that quick to write an MVC from scratch.

Almost everything I used was out of the box, except the AuthorizeAttribute class, which just didn't do what I wanted it to do.

Why this relevant to your question?

It's a mistake to write code when you don't need to. If you have tried and tested libraries, reuse them, if you don't then look for them from a trusted source before writing your own. None of my problems are new and I doubt none if any of yours are too. They've all been solved by people who have more resources at their disposal than most of us.

Part of this realization that I should stop reinventing wheels and just code what doesn't come prepackaged is I'm working on a migration project at work, with Lawyers discussing the finer points of IP relating to some encryption routines. We're going to need to spend weeks writing some code to translate data to another format because we can't 'give away' our IP that was used to secure the data. If the app was just written with canned libraries, it would just be the data and the encryption keys we'd need to hand over. It would be done by now rather than a tenth draft for the lawyers to discuss.

like image 72
joocer Avatar answered Oct 10 '22 03:10

joocer


A good, well-written, maintainable and solid application is that, regardless of the technologies used to develop it. You can write good code and bad code with any language or framework. With time we gain tools that make our lives as developers easier, but if you are a good developer to begin with, it doesn't matter which MVC framework or XML parser you happen to be using.

like image 28
kprobst Avatar answered Oct 10 '22 01:10

kprobst