About a week ago I saw a blog post describing how you could write a set of shared Views in the Views/Shared folder that could be used by all your Models. The post also explained how to decorate the Model properties with validation rules and messages. There was information about the new Model Binding improvements. The examples take advantage of new functionality in the preview release of ASP.Net MVC 2.
For the life of me, I can't find the blog post or other information on how to create the shared views.
Can anyone offer a pointer to the post?
Ask and you shall receive.
I've been playing with this quite a bit in the past few days. In addition to generic, dynamic display of editors, details, etc., I've also implemented generic, dynamic modification of data. Essentially, Phil's sample project covers the GET side of things, and I've implemented the POST. I'm also looking into dynamically generating JavaScript in order to have a dynamic list using jqGrid. Stay tuned to my blog if you're interested in this sort of thing.
Related
I have a question regarding good practices/principles when it comes to Helper/Utility classes in OOP.
I have read online that helper/utility classes are a bad practice and go against what OOP is about (Ex: This SO Question), but I am unable to find helpful alternatives to these classes.
I will get more in depth to my problem to specify why I'm asking this question in hopes of getting some advice to go forward with. I am a new software developer who wants to develop my knowledge of design patterns and practices as that aspect of development has always been important to me (how to create good code rather than code that just works).
I am currently working on an ASP.NET MVC application. For simplicity sake, I will refer to the first two stages of the application, each with its own controller. Originally, Stage 1's controller had a bunch of methods that were to retrieve lists from SharePoint. I am implementing new functions of the application that now requires Stage 2's controller to also retrieve information from these same SharePoint lists. Following the DRY Principle, I do not want to include these methods in both stage's controller classes. Instead I was initially going to create a new helper/utility class that would include all these (static) methods to retrieve the information from the SharePoint lists that all controllers could use (helpful for the future of the application to promote code reuse).
Seeing these articles saying that this helper/utility class is going against OOP and an anti-pattern, I would like some advice on how I should go about this problem.
Thanks!
EDIT
I have realized that a solution that will come to most people's minds would be to pass the list information from Stage 1's controller to Stage 2's controller, but the list information is only necessary in specific scenarios that will be known only during Stage 2 of the application. I would like to avoid the overhead of passing the list information from Stage 1 to Stage 2 and only access the SharePoint lists when needed during Stage 2.
Thanks again!
Just to prefix this question, I've decided to take a look at moving our works old legacy systems (40+ programs from vb6, vba, vb.net to c#.net) into two separate systems using the same DAL (barcoding terminals and one web based system) as I spend most my day fixing crummy or non existant business logic in 15 year old vba programs. I've recently built an entity framework model complete with fluent validation and couldn't be happier with it after using it for a bit.
The small team is familiar with webforms (but not very) but the last few days I've explored MVC Razor. I was loving MVC Framework until I tried to start trying to add more functions onto the same page and then it seemed arbitrarily hard to replicate our a recent system I put in a webform. Before, I would eager load a customer and all it's child entities and then bind that to single page for the customer so they could access everything (which is what they wanted), it works okay and isn't slow. From this single page I could edit all their account details/contacts/emails/phones/jobs.
All the examples I've done and seen in MVC handle a single update, a single edit etc but surely you can't separate out every single action into a new view/page? I can pass a rich model through to the view in MVC, but then its a pain trying to update all the different child entities.
This is probably the exact design that MVC wasn't designed for maybe, which is okay, I'm willing to adapt it if MVC will be a better platform going forward, but how are you meant to handle adding this complexity in? Some methods I've seen:
Lots of partial views? passing child info to them (or the id and lazy loading it)?
I've seen methods that wrap multiple <forms> around everything and handle actions that way.
Separate pretty much every task out
If the solution is more lightweight and easier to maintain I'll go research whatever I need to I just wanted at an earlier stage to see if I'm wasting my time. Any pointers to the correct questions I should be asking would be greatly appreciated.
ASP.NET MVC is neither more or less better equipped to deal with complex pages than any other technology out there.
Certainly, MVC requires more low-level work than a Web Forms app, with no direct binding support, but in most cases this is a good thing and provides much more flexibility in how your page is rendered.
One of the whole ideas of MVC is to give you more control over things, but that control leads to requiring more knowledge and more effort on your part in most non-trivial cases. MVC provides a number of tooling functions to speed up trivial work (like creating standard table based CRUD) but when you have complex models, you will have to do much of the work yourself.
This is not that MVC is "ill suited" for it, but just that control and flexibility has a trade off with more responsibility on your part.
In your case, you simply create a view model with all the fields you want. Then, you create your form to edit those fields. In your controller, you will need to unflatten that view model and create or update the necessary records in the database. It's not difficult, but it's more work than WebForms databinding.
You could look into more advanced tools (commercial) for MVC, such as Telerik's tools, which have developed more of a databinding like interface, but MVC is not a drag-n-drop technology, and requires you to hook things up and write the various logic for what is done.
If you need drag-n-drop, databound functionality, then no.. MVC is not the correct technology. But then WebForms requires you to accept many compromises as well, and ties your hands in many ways.
You could use partial views, however I seldom use them. I prefer to instead use Editor/DisplayTemplates as these take care of naming your form fields correctly, even for collections and complex objects. PartialViews tend to have lots of gotchas if you aren't careful. I pretty much only use them as fancy includes, or when using Ajax.
I'm not sure what you meay by "wrap multiple <forms> around everything`. You cannot nest forms in HTML, it's not legal. If you mean place a form around each row of a table, that isn't valid html either in most cases (it's not legal to put a form in a between the table and the tr).
It would help if you had a specific problem that you could ask about, vague objections don't help us solve your issue.
You can accomplish anything in MVC that you can in WebForms. The difference is MVC will usually require you to write more code as it doesn't really offer you any "controls" to drop on your page.
In WebForms, it's easy to create a master/detail view with a GridView, FormView and then wrap everything in an UpdatePanel for automagical AJAX support.
In MVC, while you do have helpers like the WebGrid and AjaxHelpers extension methods, creating views and/or pages requires more understanding of how things work to get the desired functionality. When I start a new MVC project, here's what I include:
Backbone.js - client-side "ORM" that performs CRUD operations
against RESTful* APIs
Knockout.js - client-side view models and
real-time data-binding for your views
Knockback.js - wraps
Backbone models in Knockout view models
Using these three frameworks, you can quickly create powerful single-page apps using MVC and WebAPI.
I'm using Orchard CMS and want to be able to show sharing links (think AddThis.com or Shareaholic.com) to blog posts on a website.
Before I start I realise there is a similar modules already present in the Orchard gallery but a, I couldn't get the module to work and b, I'd like to further my understanding of Orchard.
My thought originally was simply to add a new blog widget layer and to add a new HTML widget that contains the aforementioned sharing code. Trouble with this approach was;
The layer rule would force all of my blog pages to have to be under some common url i.e. using a layer rule of something like the following. Not necessarily a bad thing but something that could be missed when the client publishes a new post.
url("~/blog*")
The sharing code contains empty html anchor elements that were getting stripped from the html when I saved in the Orchard editor.
I think #2 is down to the template used in my theme. I read a post from Bertrand Le Roy's blog about overriding templates which got me thinking. Can I just override the Orchard blog template and place the sharing code in there directly? Clearly this is less customisable in the future but I'm happy with that. I'm also thinking having fewer layers, moduels, widgets or whatever can't be a bad thing when considering Orchard performance and load times. I'd also now have my sharing icons on every blog post wherever it may be on the site.
Question is what is the view that I should override in this case? I tried editing the blog views in Modules\Orchard.Blogs\Views expecting to see some changes but I got nothing.
What would people suggest as the best way forward?
The best approach IMHO is to add the code in one of two (or both) templates:
Content-BlogPost.Summary.cshtml when the blog post is shown in a list (you can copy the code from /Core/Contents/Views/Content.Summary.cshtml) and
Content-BlogPost.Detail.cshtml when the blog post is shown in a separate page (you can copy the code from /Core/Contents/Views/Content.cshtml)
I am a little ashamed for asking so many questions, but I really want to learn.
In Sipke's blog a webshop is created. There is one specific question that boggles my mind when trying to do something similar.
Let me spell out the basic requirements:
User registration form and login, etc. This one is covered by the blog and it works nice.
Creating product parts and so on. This one is covered and no problem there.
Ordering by filling in an order form and making the payment. See down
Having the order page maintainable by customer. See down.
Viewing your own orders and their status. See down
Maintaining customers and orders from backend system. This one is covered by the blog and I need to do some work there yet.
As for items regarding creating orders and viewing your orders. I have followed the approach for creating records and using standard MVC controllers. But then I encountered problems:
Menu for orders page. This I had to do manually after installing the module.
The order page itself. I had to create the view including title and so on. But I can imagine a customer wanting the order page on another menu and with a different title. And maybe add even some own content to the ordering page. This I couldn't achieve by using standard MVC approach. So maybe I am using the wrong approach here. So I was thinking about using contentparts for creating an order and displaying them and using the drivers and handlers for that. But before I go down that road and refactor everything I want to know if that is the right approach. A downside could be that once the module follows that route it can then not so easily be reused with customers that have other cms's capable of hosting an MVC3 module.
So when to use drivers, handlers and contentparts and when to use standard controllers and views.
You should use Drivers and Parts (with Handlers if needed) when you want to create functionality for content items. E.g. if you want to display a custom media with all products, you could create a Part (together with its Driver, etc.) to handle that. Read the docs on Parts.
If the functionality is not tied to content items the most possibly you want to use the standard MVC toolbox, it's fine. Look at the built-in modules how they do that. E.g. the Blog module uses controllers and views to show the admin UI, but has parts to enhance the functionality of for example the Blog content type.
To make things more complicated you can employ ad-hoc content items to build a page that you'd normally do with simple views, but that's an advanced topic :-).
I'm hoping this isn't too subjective, but I'm new to ASP.NET MVC and I'm trying to figure out how others may have solved similar problems.
Basically, I have two entities, Customers and Orders. A customer has many orders; an order belongs to exactly one customer. I'm making an Order Search feature that should allow a user to search for orders based on order or customer information. Pretty straightforward, I think.
I've read in other posts that the search controller should use GET, but I think it makes more sense to use POST because of the large number of search params.
I'm using Entity Framework to create my models, and that's in a separate class library project and namespace.
This article talks about using binding instead of Request.Form to get at the POST data. Would it make decent sense to make a class to hold all the search data that could be materialized by the magic model binding? Otherwise I'd just be poking through the FormCollection to pull out particular values, which might be fine. Where would you recommend making such a class?
This is only a partial answer, specifically to the "FormCollection vs Class" part. It's my opinion that you should always use a class for this unless you have a very good reason not to. You get compile-time checking here which is the #1 benefit. You also get Intellisense support which is helpful too. Lastly, you might get some performance benefits as there is potentially less casting/parsing done by your code.
For the GET vs POST question, I'm still struggling with that general question myself but I do have an opinion on your specific use of it. Currently, I'm leaning towards the following rules:
Use GET if the parameters identify an entity of some sort (i.e. ~/product/id/54 = a can of Coke)
Use POST if the parameters help generate a truly dynamic page where an ungodly number of such pages can exist (a search results screen being an example where there is nearly an infinite number of possibilities).
Now I may be way off with my GET vs POST opinion here, but I think there will be a lot of agreement on the class vs FormCollection opinion.
Use a class to encapsulate the search criteria. You can make this a property of your model and then use standard model binding. That way you can pass a single object to your search method, which is much neater and more extensible than having lots of parameters.