ASP.NET MVC calling Stored Procedures
I开发者_Python百科 wanted to pick your minds...
So I've got this View that allows for a small register... and a stored procedure.
I've been seeing these pseudo "repositories" when making calls... to me, it just seems like an extra hole that needs to be drilled in order to make the call. This bring up three qeustions:
1) what's the point of pseudo repositories?
2) what's the best place to make my stored procedure call: within the views controller function or referencing some other function?
3) how do I recieve the stored procedues result (i.e. success of failure)?
Here's my call:
long? tmp = 1234;
LinqToPartyDataContext lq = new LinqToPartyDataContext();
lq.spCreatePersonaParty(ref tmp,
model.FirstName,
null,
null,
null,
null,
null,
null,
null,
model.EmailAddress,
null,
null,
null,
null,
null,
model.ZipCode,
null,
null);
How do I get the result?
1) what's the point of pseudo repositories?
To hide the underlying implementation detail from your UI. E.g:
IFooRepository repo;
var foo = repo.FindSingle(1);
All the UI knows is that it is calling something that will retrieve what is wants. Why should it care about the actual details? All it cares about is getting the results it wants.
Your actual implementation could be a L2SQL repository, Entity Framework, or even a classic ADO.NET implementation.
2) what's the best place to make my stored procedure call: within the views controller function or referencing some other function?
Definetely not from the UI.
Create an interface which exposes the parameters to the stored procedure, which the Controller can call into:
var result = repository.FindSomethingSpecific(param1, param2);
return View(result);
3) how do I recieve the stored procedues result (i.e. success of failure)?
This really depends on your persistence layer (EF/L2SQL/classic ADO).
If your using EF, you can return the result of the SPROC into a POCO, which the UI can access via model binding.
If your using L2SQL (no POCO support), you'll have to manually project into a POCO (left to right copy), and bind to that.
If your using classic ADO.NET, your have to manually traverse the result set and project into a POCO.
精彩评论