Creating the controller using MVC3 - asp.net-mvc

I'm trying to get a UI using MVC. I'm currently battling with MVC3. There does’nt seem to be much documentation.
Here’s what I’m thinking. I believe I need to create stored procs for each query. I’m creating the the LINQ to SQL for the model. I’m trying to figure out how to create the cotroller. Any suggestion on how to input data to the stored proc and return results to the view? The default controller classes dont seem to do anything
Am I doing this right? Or MVC2/MVC1 the easier way to go?
I'm new to MVC on .NET. Thanks in advance.

There is plenty of documentation, if you look for it... Also, http://www.asp.net/mvc is a good place to start.
What it seems like you really need documentation for is LINQ-to-SQL, not MVC, which is here: http://msdn.microsoft.com/en-us/library/bb425822.aspx
There's also a great ScottGu tutorial here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
The whole series is available here, including a PDF download: http://scottonwriting.net/sowblog/archive/2010/07/27/links-to-scott-guthrie-s-using-linq-to-sql-tutorials.aspx

I believe I need to create stored procs for each query.
You could do this but it might be an overkill. I would recommend you watching the tutorials here. Paragraph 5. covers tutorials about using Entity Framework for data access. Try them out and if you encounter some specific issues don't hesitate to ask a question by showing what you have tried and what didn't work for you.

If you're using Linq to SQL, you don't need to use Stored Procs at all.
If you want more information on MVC generally, try doing the NerdDinner example.

Related

Proper ASP.NET MVC 5 Approach

Looking for some guidance as I am struggling here.
As someone who spent many years developing ASP.NET applications - that has been out of the game for 3-4 years, I am struggling to pick up the latest approaches. Probably as a result of a decade of experience.
My ASP.NET in the past was built using stored procedure calls, and the GUI was built using hand-coded HTML and JavaScript. I used JavaScript to emulate Ajax type functionality (before it was really a well known thing), and also would at times use XSLT to separate presentation from code.
Now, I am trying to move to MVC 5. It's not intuitive to me, and the few popular tutorials out there seem to be doing things that I don't want to do. For example, they are connecting to a simple table through EF, and allowing a user to view, edit or create items.
I'm looking for some basic things:
How do I get data from a stored procedure into some form of a view (GridView, or ListView, etc) - Using a controller, a View, a stored procedure as the data source (using Entity Framework? When i try DB First, and select to include procedures, I can't find them anywhere)
I'm going to want to allow them to override the value in one column. This would be stored down in the database as a different field.
This stuff was always very simple using basic web forms, but I can't find a tutorial that allows me to do anything other than just edit an existing table using EF - which isn't what I want to do. So tempted to just go back to the old fashioned way ...
This is one tutorial that was making sense to me, until it jumped into the code first approach on the database, where it went off the path from what I was looking to do:
http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
Here was another one:
http://www.asp.net/mvc/overview/getting-started/database-first-development/generating-views
But this one was also different than what I was hoping to do.
I wish I wasn't so short on time with this project.
Thanks for any guidance.
You may better wait for ASP.NET 5/vNext/MVC6 release and don't waste your time on MVC5. If your project relies on SQL and works well do not use Entity Framework at all.

Rails MVC concept in practice

I am not actually asking a question but rather an suggestion(or recommendation) on how to write code to fit nicely into Rails MVC pattern. Hope rails veteran or anyone familiar with MVC can give me some feedbacks.
I have an web app that talks to a RESTful api app via ActiveResource. It can fetch and update contents using API calls. It works perfectly. However, the web app does not have any models. The way it works is when user triggers an action(index,view,edit etc), the controller will directly call the REST api to fetch/update data.
My question is: Is it a good practice to do it this way or should I create models and populate data in there instead of directly calling the api? I was wondering if it is just a pragmatic compromise to MVC. I have just started working with Rails(and MVC) so I am open to any ideas, comments or recommendations on this
It's a bit of a catch-22 question. (I did wrote a huge answer but then deleted because it will be too tedious to read)
If you mean, can you implement the MVC pattern without a model, then the answer is no. The M means Model.
If they mean, can you use the MVC without using a model, then the answer is "yes", but it is no longer MVC, you have obliterated the the M i.e. Model.
I would recommend you to read MVC pattern in detail and then try to understand what your application actually trying to do.
http://c2.com/ is a very good place if you want to understand the design patterns.
A model is an object representing data or even activity, e.g. a
database table or even some plant-floor production-machine process.
A view is some form of visualization of the state of the model.
A controller offers facilities to change the state of the model.
Now in your case (it seems): you do have a data coming through via api so I would suggest populate the model properties and propagate it across.
Also Considering Pragmatic Compromise in MVC Dealing with things sensibly and realistically in a way that is based on practical rather than theoretical considerations. Omitting the use of Model in MVC do-not sound like a good idea, and it no longer remains MVC.
Having said that It seems from your point of view you are trying to say that Rails isn't necessarily strictly MVC hence why not use the way you want to :) but I will suggest to keep the integrity of MVC (and follow the purist approach).
http://c2.com/cgi/wiki?ModelViewController
Good read: jeff Atwoods: http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html (Feel free to skip the asp.net part)
https://stackoverflow.com/questions/1242908/in-english-what-really-is-model-view-controller
sums it all :) source is mentioned above.
"The model consists of application data and business rules" (wikipedia)
A model is essentially a table in your database locally.
If you're not storing any data, not validating any data, then you don't need a model.
If you want to clean up your code, maybe put some functions in a helper or in /lib

jqGrid ASP.NET MVC3 + EF: Which is the best method to implement search?

I'm still trying to implement research in jqGrid + MVC3, so far without success.
Meanwhile someone could solve me this, I was wondering what was in your opinion between these two articles the best search implementation and whether there are valid alternatives:
Using jqGrid’s search toolbar with multiple filters in ASP.NET MVC by Ilya Builuk
jqGridDemo by #Oleg
Personally I feel the second solution more elegant and complete, but I'm still having problems. Moreover, in its code is used JavaScriptSerializer and I read in this blog that "The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1"...and this discussion get me more confused about this...
Personally, I am still having a problem with the ObjectQuery because I calculated properties of the entities, so they do no reside into the dataset ...
Thank you and I apologize if my questions can be trivial to most, but I am just a beginner...and I'm trying to learn...THX

asp.net mvc LINQ TO SQL handling database events (alerts/feed)

Recently I did one web site (www.ramtajogi.com) using asp.net mvc, linq to sql .
I have used repository pattern to get data from the database. Now everything is working fine.
But the new requirement is to record all the events (on poetry book created, on poem added or when a new comment is posted to any poem. What is the best way to implement it.
Should I change my existing classes and do or is there any better way to do it.
Regards
Parminder
I think what you are looking for is a way to log across different actions:
Try this: http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filters.aspx
Maybe you want to go the "SQL Trigger" road? I know it's a much debated subject, but it allows you to log without changing any of your existing code.
I'm not sure about its impact, but if the triggers only do some inserts into a log table, I guess it should be quite low.
Thanks everyone,
Actually I created another table for alerts and added one record for every event.
Regards
Parminder

Asp.net Mvc: Creating Model Classes with LINQ to SQL

I am trying to learn Asp.net Mvc so I am trying out
this Tutorial.
They talk about the Repository Pattern and how it is easy to change to another data access technology instead of just calling Linq to Sql directly.
Using LINQ to SQL within a controller class makes it difficult to switch data access technologies in the future. For example, you might decide to switch from using Microsoft LINQ to SQL to using the Microsoft Entity Framework as your data access technology. In that case, you would need to rewrite every controller that accesses the database within your application.
Note: I never really understood how an interface worked before reading this tutorial and it's still not 100% clear. I see it now as some sort of 'template' for a class.
After successfully using Linq to Sql I thought it would be nice to try out Ado.net Entity Framework since I've been reading a lot about this. They said using the Repository Pattern would make it easy to switch so I thought I would test that.
My question is: what should I do to change to Ado.net EF?
This is what I think I should do.
Add the Movie.edmx file and configure it(add my movie table).
Write a new class based on the IMovieRepository and maybe call it MovieEFRepository.
Change the parameter in the controller constructor to MovieEFRepository. This is the first thing I find strange because in the tutorial they say that not using the repository will force you to change all the controllers if you change to an other datasource. Don't I need to change every controller anyway since I am specifying the MovieRepository class?
The last adjustment I think I need to do is to change the View. Because it's using the Product class which was created by the Linq to Sql designer. I am not sure how I am going to do this. I guess I should have used some object that wasn't dependent on the dbml file?
Forgive me if I have a slightly simplistic view of Asp.net Mvc. I am webdesigner with a lot of interest for Asp.net webdevelopment.
So after a few days of reading and a lot of googling I got it to work. First I tried to find out what IoC (Inversion of Control) actually meant.
One of the first sites I found was a website with a screencast about Unity. Which is a DI/IoC framework for .Net.
Link: David Hayden screencast on Unity.
Looking at it now this is actually a very good screencast and example on how easy it is to use Unity and IoC/DI. At the time I didn't understand it completely so I went on and kept googling.
One website I kept running into was the one from Martin Fowler.
Link: Martin Fowler - IoC Container and the DI pattern
For me, a person that is a coding novice this website is a little to abstract. Also this might sound weird but the font, line-height and typography on that website was really awful which made it even harder to read.
The next website I read was about Windsor Castle since Alfredo Fernández said it was easy to use.
Link: Castle Project- Windsor Container
The documentation wasn't to bad but I had some problems converting their "getting started" basic example to my Asp.net Mvc application. Also part 2 and 3 were missing from their getting started.
After this I started looking for the different frameworks to see if i could find a really basic example. If I just looked at the first screencast again I would have fixed it a lot sooner but somehow I lost track of it.
Link: Scott Hanselman: List of .NET DI Containers(IOC): very good blog post with most of the .NET IOC solutions.
Link: Phil Haack: TTD and DI using Structure Map: Using the xml configuration file was to complicated for me and i couldn't get it to work.
Link:
Andre Loker: ASPNET-MVC-with-Windsor-programmatic-controller-registration: Didn't try this example. Looking at it now I might have been able to get it to work.
Link: MvcContrib: This adds functionality to Asp.net Mvc. It also has 3 or 4 IOC ControllerFactories build in. I couldn't get it to work I also didn't find a lot of documentation about this.
I had a lot of problems with xml configuration files and I couldn't seem to get them to work. I tried Windsor, Structure Map and Spring.net but I always got stuck with the xml files.
So I decided to go to the Asp.net Mvc site because that's where I started learning about Asp.net Mvc. I found the first screencasts and MIX09 presentations very clear and I understood most of what people were talking about. I got stuck at the second screencast by Rob Conery when building the Storefront application. Because I knew a little more about repository and IOC/DI now I thought it would a be a good idea to start watching Rob Conery's screencasts again. In one of the screencasts he talks about uploading all the samples to codeplex.
Link: Codeplex: Mvc sample apps
I went to codeplex and found out you can browse through the source files without downloading them. I tried to find out how Rob Conery handles IOC/DI with his repositories. So I was glad to see he was using Structure Map but instead of using a xml configuration file he was using a bootstrapper class that registers all the interfaces to their concrete class.
After trying this with my webapplication I finaly was able to get Structure Map to work in my application (Hooray).
He also showed me how to fix the dependency on my Product class that comes from Linq to Sql. He creates an extra object that then gets called by "select new product { }" in the Linq queries.
Wow, this answer is a little longer than I planned but I hope this helps other people like me who are very novice in coding and Asp.net Mvc.
You might have your repository decoupled because of injection, not if you followed just the examples because of
public MoviesController() : this(new **MovieRepository**())
I recomend you to read about IOC, is easy and very interesting, you can use and ioc container like castle windsor.
With that, your contoller will have only one constructor, the one with the interface, and not will need to be changed.
With your entities you can do the same that with the controllers, create an interface for each entity and use the ioc pattern too, with tha you will only have to change your configuration file for your ioc container.
If you don't do these things, your right, you will need to change all you said.
I hope that help! sorry about my english!

Resources