|
Marc Clifton wrote: MVC, like MVVM is a great guideline, but the religiosity of it I can leave behind.
I completely agree with you. I love MVVM, but I'm not over the top. There are some real fanatics out there who jump up on the table and scream if you so much as open a WPF code-behind.
The only concern I always keep in mind is... when you start regressing away from the pattern, at some point it ceases to be MVC/MVVM/[insert pattern name here].. Then what do you have left? Some kind of hybrid? Or is it just code all over the place? Hard to say.
If it's not broken, fix it until it is
|
|
|
|
|
Coder For Hire wrote: Or is it just code all over the place?
Ideally, you'd have some loosely coupled, not tied to a framework, potentially re-usable, probably more easily unit-testable, code.
Marc
|
|
|
|
|
Just a personal preference but I tend to steer clear of jquery UI stuff and use Bootstrap instead - I think things work nicer, especially for mobile devices, and (and in my opinion) it's a lot easier to customise/extend than jquery UI.
I like ASP.NET MVC, but the thing I didn't like was having to create a controller for every view (seemed a bit too much like the old webforms code behind files), so I created a NuGet package which intercepts the routing to use a built in "standard" controller if no controller or action is defined for a route.
It means you can just add views without a controller. If you find you need a controller for a specific view/action then you can add it and it gets picked up automatically
NuGet: https://www.nuget.org/packages/ControllerLess/[^]
Source: https://github.com/brentj73/ControllerLess[^]
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Brent Jenkins wrote: I like ASP.NET MVC, but the thing I didn't like was having to create a controller for every view (seemed a bit too much like the old webforms code behind files),
[shameless plug] Exactly the point I made in my just finished e-book on web servers, coming out soon! [/]
Marc
|
|
|
|
|
It's odd that I don't really hear that many devs complain about it. Especially when a lot of view controllers in AJAXified apps often have no logical code in them apart from:
protected void ActionResult Index()
{
return View();
}
I've seen projects with dozens (even hundreds) of view controllers with nothing but that in them.. If it was any other class, everyone would be ranting about re-usability, but call it a "view controller" and everyone just accepts that that's the way it is
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Brent Jenkins wrote: I've seen projects with dozens (even hundreds) of view controllers with nothing but that in them..
Gads, isn't the controller class re-usable?
I tried my hand at Razor and hated the MVC approach, so I ditched it, and yes, especially useless on the server-side for AJAX'd / SPA implementations.
Marc
|
|
|
|
|
Marc Clifton wrote: Gads, isn't the controller class re-usable?
It is - with a big compromise in that you'd need to have a common set of action names across the application. But then obviously you're going to start returning different models for each view too..
You could make each inherited class or the base class generic too, but then you're still having to maintain 100 different controllers with code that's 99% the same..
Razor's handy at times, but I wouldn't use it for model binding or generating HTML personally, more just for helpers here and there.
AJAXified apps and SPA's certainly seem to make better use of the framework.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
A common set of action names?
how many views have you in the system if you have 100's of controllers?
and how many index's?
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
A common set of action names, like: View, Edit, Save, Delete - if you like that kind of thing (I personally don't). But then some views might need other actions: ConfirmDelete, SaveAddress, AddItemToBasket, etc..
Basically, what we end up with is a view controller for every view (isn't that just the same as webform's code behind?)..
In enterprise web applications, there can be hundreds or even thousands of views (including partial views). Every one (in general) will have repeated code like using "standard" ASP.NET MVC:
protected void ActionResult Index()
{
return View();
}
Any action that does nothing except return a view is extraneous code in my book. You would't write code this way (multiple unique classes with repeated, near identical method bodies) in any other context, so why do we accept it with view controllers?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
why would you have different controllers for each action? unless each of the above action does exactly the same thing, in which case have one controller for all the similar actions
ie one controller to do all the saves, one to do all the deletes etc
but normally you have different information to save etc on each view, so group all the actions for that view in one controller, even if you have a single class that actually does the save it can be called from multiple controllers
you can even have multiple views using the same controller, so all your customer views go to one controller, all your sales views to another etc
as for your example you are not actually writing the same code what you are doing is using an inbuilt functionality to control flow, you don't seem to have a problem with using multiple @html.helpers functions, what you have I n
protected void ActionResult Index()
{
return View();
}
is a server side helper using its default functionality, that you are not using its full capacity does not make it invalid.
the system is called Model - View - Controller and is designed to pass data to the html by use of a model, have that data changed by the UI and then the Model is passed back to the controller for processing, that you are using it for what sounds like a different process does not make the concept invalid
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: why would you have different controllers for each action? unless each of the above action does exactly the same thing, in which case have one controller for all the similar actions
You can group actions together for a view, but it is still one view controller for each view, isn't it?
Bergholt Stuttley Johnson wrote: that you are not using its full capacity does not make it invalid
I think that perhaps you're missing my point? Let me give a clearer example:
I have an application with a header, nav bar and a footer. I use a MVC layout file to bring the basic structure of my application to life. So far so good.. So I create a number of pages which look up data and present it to the user. The need for the controller code that I write is clear here - I'm performing some logic.
However, I have a set of pages - let's call them "help" pages, that do no database lookups and have no real logic. I still want to keep my header, nav bar and footer (with content appropriate to the logged in user, perhaps)..
In standard ASP.NET MVC I still have to add a view controller for each help page (view). I need to write a C# action. Whenever I add a new page (view) or rename pages (views) I need to update my C# code and recompile and publish the entire application.
How does that make sense? It's not a case of under-using the framework, it's day-to-day development. Some pages (views) need their own logic, others don't - the point is that with ASP.NET MVC, I have to write logic for every page (view) whether it's needed or not.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I can see what you are getting at I think, firstly still not sure why you need a new controller for each view? if its only initialization then you can put them all in one controller, unless you are calling the views the same name!
whist I can see that three lines of code repeated numerous times may seem like a silly overhead, these three lines are initialising the MVC process, think of it as similar to initialising a variable, you do that hundreds of times too but accept that as part of the process.
as for updating well that is how things work, if you object to that then don't use MVC if you want the flexibility of MVC then you accept that its compiled and will need recompiling when you change code, if you don't want that then use a scripting language and HTML
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: I can see what you are getting at I think, firstly still not sure why you need a new controller for each view? if its only initialization then you can put them all in one controller, unless you are calling the views the same name!
Not quite, as it's also relevant to actions within controllers. An example:
- Views
- Home
- Index.cshtml (pretty much static content)
- UsagePolicy.cshtml (pretty much static content)
- Customers
- Index.cshtml (pretty much static content)
- List.cshtml (a list of customers)
- Edit.cshtml (the "edit customer" form)
- Shared
With ASP.NET MVC as it currently stands, I need the following controllers and actions:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult UsagePolicy()
{
return View();
}
}
public class CustomerController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult List()
{
..Customer list logic
}
[HttpGet]
public ActionResult Edit()
{
..Form initialization logic
}
[HttpPost]
public ActionResult Edit()
{
..Form relevant logic
}
}
What I'm suggesting reduces the code down to this (HomeController is no longer needed):
public class CustomerController : Controller
{
public ActionResult List()
{
..Customer list logic
}
[HttpGet]
public ActionResult Edit()
{
..Form initialization logic
}
[HttpPost]
public ActionResult Edit()
{
..Form relevant logic
}
}
Surely reducing the amount of code down in any project makes sense more than writing the same code in different classes over and over again?
Bergholt Stuttley Johnson wrote: hink of it as similar to initialising a variable, you do that hundreds of times too but accept that as part of the process.
Not quite (again). I only add a variable where I have a use for it - it's optional. Actions (and controllers) are mandatory whether or not I have any logical code to add.
Common methods would normally be placed within a base class, or you might decide to add in a route, but that still means recompiling and re-publishing the entire application every time someone adds a new view/action name.
What I'm getting at is this: why not (in ASP.NET MVC) make actions (and controllers) optional, in that I only write them if I actually need to include some functional code? The default behaviour should be (in my opinion) that if a route is requested that goes through to a view without an action and/or controller, then it simply returns the view.
Bergholt Stuttley Johnson wrote: f you object to that then don't use MVC
I love ASp.NET MVC, but even the MVC guys themselves work on improving it. If we just accept things as they are, we'll never move forward
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
so what you are saying is that it should create the controller/action code automatically if it isn't in your code.
ok but this allows the runtime exception if the view doesn't exist when the existing method will not compile if the view is missing
whilst it is a possible enhancement it does break the idea of the model-controller-view process, and whilst your example does show it may be a nice to have it is not the perceived flow and may have little traction in the future development of MVC, it will depend on exactly how much this is seen as an advantage over other enhancements
if you love it then you must except that their are something that are inherent to the way the architecture is and if you want this to change then perhaps MVC.net may not be the platform for you, MVC itself is not restricted to .net and there are flavours that are script code based and does not require compiling, however they don't have the razor engine
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Bergholt Stuttley Johnson wrote: so what you are saying is that it should create the controller/action code automatically if it isn't in your code.
No, that isn't what I'm saying. Perhaps it's easier if you take a look at the example project here..
Using ASP.NET MVC Without Controllers or Actions for Each View[^]
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I don't know MVC real well, but isn't there a way to create a "base" controller with
protected void ActionResult Index()
{
return View();
}
in it and use that as default for all views, and the use specialized controllers when needed?
If it's not broken, fix it until it is
|
|
|
|
|
But what about when you have another view called "About" for example with static text? You now need to add the "About" action to one or more controllers, recompile, republish..
Even to add a single static HTML page I'd still need to create a new view controller - that doesn't seem logical to me..
And you need to know up-front what the names of your views will be.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Brent Jenkins wrote: Even to add a single static HTML page I'd still need to create a new view controller
No you wouldn't. Just add the page (or, more ideally, the folder it's in) to an ignore route and let IIS serve the page natively.
|
|
|
|
|
And if I still want to use the shared layout or Razor?
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
Then it's not an html page anymore.
|
|
|
|
|
So I have a MVC layout with a header, nav bar and footer and the content contains just this:
<p>Hello world</p>
In the world of standard ASP.NET MVC, I still have to create a view controller with an action to return my view.
That's what I'm talking about - the content of the view is static but I still need to code, recompile and publish.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
I've never dynamically deployed cshtml, but I believe it's compiled on the fly.
Couldn't you create a catch-all route that passes the viewname as a parameter instead of the action name?
routes.MapRoute(
"my-route",
"{controller}/{viewname}",
new { action = "GetView" });
|
|
|
|
|
Surely better to just re-route views without an action (or controller) through to a default controller? For anything else, leave the "standard" behaviour.
How do you know so much about swallows? Well, you have to know these things when you're a king, you know.
modified 31-Aug-21 21:01pm.
|
|
|
|
|
you could but why?
surely that the views are different, if they are purely html and not requiring a model you could just have a case statement and a redirect to view
I can see that having an action for every view is extra code but if you have so little in a controller maybe you should be thinking is MVC the right technology!
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
you don't need to create a new controller if you don't wish, you could just use one controller for everything (yeuk) and use route mapping to wire it together
not sure I'd like to have to debug/fix it though
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|