I'm using SubSonic 3.0 (SimpleRepository) to handle database access in my ASP.NET MVC 1.0 application.
It would be nice to handle a transaction for every web request, committing if everything went smooth and rolling back in case of exception. Is this possible? If so, how?
I know this topic has been discussed many times, but I just couldn't find a satisfactory answer. I have built my own solution (create a TransactionScope in the controller, then commit/rollback in OnActionExecuted), but it turns out to be very unreliable.
The link http://ayende.com/Blog/archive/2011/04/12/refactoring-toward-frictionless-amp-odorless-code-what-about-transactions.aspx applies to NHibernate but i am quite sure you can use it as well. Basically it talks about creating custom action filter . It is also true that it does it action-wide not request wide.
Related
i've a question regarding handling of user logon while porting an application to MVC:
in the "old" WebForm days, developers simply used the SessionState object to set a user to logged-on, by -for example- simply putting the userobject into the SessionState (and this userobject holds simple properties like name/lastlogon/etc.)
this stuff worked very well for us, and i've seen lots of applications doing it that way
yes, i know there is this MembershipProvide-thingy, but i've never used it
Now, in MVC, everybody tells me that "using SessionStat for this is bad" and "apps built that way are flawed in design" and that "there are tons of security risks" and so on.
I've used this method because it worked for the app very reliable, it was simple to implement and it covered all stuff we need.
(Sure, there is the thing with recycling web worker process and emptying the session - but thats not a problem in our case, since the app runs for each country on a dedicated machine)
I've read tutorials, telling me to put that stuff in the DB and -attention- doing a request to the DB to check if the user is logged in, per EACH request? But: Under no circumstances, this is a doable way since i want to keep DB requests on a minimum.
So my question is:
A) whats wrong using this way also in the new MVC app?
B) whats the best way to handle this scenario in a newly built MVC app?
Regarding the session-in-DB-idea: instead of doing this, i'd rater setup an additional service, like a "session-manager" thats get query over the network, but such simple requests should not go to the DB - isn't that a good idea?
Any idea, hint /etc. is highly appreciated since this scenario is really confusing me :-X
A)
A fundamental principal of the asp.net mvc framework is that its stateless. Data is passed around using http requests and sent to the views in viewmodels. Web forms tried to maintain state with viewstate etc thats why you would have seen the logged in user in session approach. Thats not to say session shouldnt be used completely in asp.net mvc, there are some circumstances when it can be useful. Like maintaining a 3 step form process that has to be persisted on the last step. But generally we already have a recommended way to handle the user logins, and thats forms authentication
B)
For accessing the user object, you can create a custom identity implementing the IPrincipal interface and add the required user fields you need. Then set the custom identity in a global filter and access it in your action results. Regarding not wanting to query the database for every request, why dont you just call it for the initial request, then cache the result until the user is updated where you then can reload the object and set it in the custom identity again.
I'm writting an application that has many Ajax widgets (Kendo-UI to be percise). It's starting to get messy to have all those Ajax responses without the standard controllers so I was starting to consider making each entities their own controller. If I'm taking the time to do this, I figured I might as well go foward and do those as WebAPIs since I was planning to do this in a not so close future, but hey, it would be done already...
So my question is: Is it a good practice to use an MVC application's own Web API as a Ajax Widget feeds or is there any reason to stick with standard Controllers?
I've seen some arguments about performance, but I don't think this applies to this situation. I believe it was more of a "Controller calling WebAPI" situation which has obvious performance hits. But since it's already a client side Ajax call, weither it goes into a standard MVC Controller or a WebAPI controller shouldn't change a thing, would it?
Edit
Additional information regarding the project:
I am using Entity Framework for the data access.
I have a repository pattern going on with UnitOfWork.
I am using proper a MVC structure (EF POCOs AutoMapped to DTO POCOs in the repository and fed into View Models by the controllers)
This is a MVC 4 project on .NET 4.0
There is a lot of database relationships (specially for the object I'm working with at the moment)
I don't know about "good practice", but it's certainly not "bad practice". I see no difference whether you do it in the app or a different one.
I think its a good thing but only if what you are doing in the API is kept as generic as possible to other applications and services can reuse the API.
Both the applications I have written and continue to maintain use pretty much the exact same stack as your app.
I have recently re-factored one of the applications to use the API for all the common things like lists that I'm binding to Kendo ComboBoxes etc. in my views. Its a fairly large application that re-uses a lot of the same lists such as states, priorities, complexities across various Entities and views so it makes sense to put those in the API.
I haven't gone as far as going the whole hog through. I draw the line with things like this:
public ActionResult GetAjaxProjectsList([DataSourceRequest]DataSourceRequest request)
{
return Json((DataSourceResult)GetProjectsList().ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
}
That is very specific to how the Kendo Grid wants the data back. Nothing else I have connecting to this app will use this data in this format so I keep it in the controller.
In short... I use the API for common things within the same MVC app and things that I allow to be used by other applications or services, like Excel.
I'm a .Net desktop applications developer trying to switch to ASP.Net MVC (3 or 4 doesn't matter). We have a .Net library which notifies any GUI you want to put on this assembly by Property Changed Events (or any other custom event we deemed necessary for that matter).
This might not even be a relevant question, because perhaps it's done totally different in ASP.Net MVC, but how do you update your View in the browser on your client after receiving a Property Changed Event from an assembly on your server? We generate these events ourselves by the way. The assemblies are our own.
I've read about Partial Views, but I need to look into that. Whatever I came across seemed so cumbersome. Because it's really straight forward and simple in desktop development. So I'm starting to wonder if Property Changed Events are the way to go when developing for the web.
Thoughts? Links? Sources? I'm interested.
p.s.: See tags for a further understanding of what I'm going for.
HTTP world is entirely different
you request something from the server(Asp.Net MVC) and you get a response back from the server.
there is no way in which the server can contact the user back** as the asp.net server wouldnt keep anything in memory after the response
so here you are wrong in 2 aspects
1. property changes - because after the response there is nothing in the memory of the server there are no models(Objects) so there is nothing whoz property is actually changing
2. even if u maintaining something static if that changes there is no way for you to contact the client back
**there are some frameworks available that allow you to contact the client back and they do this by sockets or comet
You need to move away from the Event-driven thinking of desktop (and WebForms for that matter)-development.
MVC is a designpattern for separation of concerns, and no tightly bound events will exist, unless you do some hack-ish work of your own.
You can, however, accomplish this by using frameworks like SignalR that rely on Javascript and open connections, but the notion of events is not present in MVC.
i want to use asp MVC with the Embedded RavenDB but it looks like that the DB don't react when debug the MVC application (using VS 2010 and IIS 7.5 Express). For Example in a MVC action i write an object an try to read it from the db in the next step, but the object is not found until the next request. For me it looks like the embedded RavenDB can only listen for request if the MVC application is on idle.
If i changed from embedded to the normal Client/Server it works without problems. I would prefer to use the embedded RavenDB. Maybe someone has some experience with this problem?!
The in memory database takes time to do things, so you might be getting stale results.
The nostalequerieslistener from this question should help: RavenDB how to flush?
I had this issue with unit tests, bascially while RavenDB in memory is fast, the small amount of code between inserting and querying for the object is faster.
You can use RavenDB embedded without any disadvantages, except that your application startup will take a little longer, when you create the EmbeddableDocumentStore, but nothing to worry about.
The only thing that you should keep in mind is that while you can seemlessly switch from embedded to client/server, there are (very few) cases where you can't go the other way without modifying your code. For example, the lazy-features aren't available in the embedded session because just don't make sense there.
Easily you can integrate. this might help.
http://msdn.microsoft.com/en-us/magazine/hh547101.aspx
Please mark as answer if solved your issue.
RavenDB implement the Unit Of Work pattern. Do you call SaveChanges on the document session when you are done?
Yep.
Check this post for a complete sample using best practices:
RavenDB, UnitOfWork and MVC - revisited
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