<p>We're in the process of setting up a new framework and way of doing business for our new internal apps. Our current design dictates that all security logic should be handled by our database, and all information (and I mean all) will be going in and out of the database via stored procedures.</p>

<p>The theory is, the data access layer requests info from a stored procedure and passes over authentication to the database. The database determines the user's role/permissions and decides whether or not to perform the task (whether that be retrieving data or making an update).</p>

<p>I guess this means fewer database transactions. One call to the database. If the security was in our data access layer, this would require 1 database call to determine if the user had proper permissions, and then 1 separate database call to perform the action.</p>

<p>I, for one, find the SQL Management studio completely lacking as an IDE. My main concern is we will end up having to maintain some nasty amount of business logic in our stored procedures for some very minimal performance gains.</p>

<p>Right now, we're using LINQ for our ORM. It seems light and fast, but best of all, its really easy to rapidly develop in.</p>

<p>Is the maintenance cost worth the performance gain? Are we fooling ourselves into thinking there will even be a noticeable performance gain? Or are we just making a nightmare for ourselves?</p>

<p>Our environment:</p>

<ul>
<li>Internal, non-mission critical business apps</li>
<li>C#/ASP.NET 3.5</li>
<li>Windows 2003</li>
<li>MS SQL Server 2005</li>
<li>35 Medium sized web apps with approx 500 users</li>
</ul>

[{'answer_id': 55850, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 4, 'selected': True, 'text': '<p><strong>Don\'t do that</strong>. We recently had a <strong>VERY BAD</strong> experience when the "database guru" decided to go to another company. The maintenance of all the logic in the procedures are just horrible!!</p>\n\n<p>Yes, you\'re going to have some performance improvement, but that\'s not worth it. In fact, performance is not even a big concern in internal application. Invest more money in good servers. It\'ll pay off.</p>\n'}, {'answer_id': 55853, 'author': 'George Mauer', 'author_id': 5056, 'author_profile': 'https://Stackoverflow.com/users/5056', 'pm_score': 2, 'selected': False, 'text': '<p>You could do it but its a huge pain to develop against and maintain.  Take it from someone who is on a project where almost all business logic is coded in stored procedures.</p>\n\n<p>For security, ASP.NET has user and role management baked into it so you might be saving trips to the database but so what?  In exchange it becomes far more annoying to handle and debug system and validation errors because they have to bubble up from the database.</p>\n\n<p>Unit testing is far more difficult since the frameworks available for unit testing sprocs are far less developed.</p>\n\n<p>Proper oop and domain driven design is all but out the window.</p>\n\n<p>And the performance gain is going to be tiny if any.  We talked about this <a href="https://stackoverflow.com/questions/50346/why-is-parameterized-sql-generated-by-nhibernate-just-as-fast-as-a-stored-proce">here</a>.</p>\n\n<p>I would recommend that if you want to save your sanity as a developer you fight tooth and nail to keep the database as the persistence layer only</p>\n'}, {'answer_id': 55854, 'author': 'Grey Panther', 'author_id': 1265, 'author_profile': 'https://Stackoverflow.com/users/1265', 'pm_score': 2, 'selected': False, 'text': '<p>Unfortunately there is no "one true answer". The choice you must make depends on multiple factors, like:</p>\n\n<ul>\n<li>The familiarity of the team with the given solutions (ie if a majority of them is comfortable writing SQL, it can be in the database, however if a majority of them is more comfortable with C#, it should be in the code)</li>\n<li>The "political power" of each party</li>\n<li>etc</li>\n</ul>\n\n<p>There is no decisive advantage in any direction (as you said performance gains are minimal), the one thing to keep in mind is the DRY (Don\'t Repeat Yourself) principle: don\'t reimplement the functionality twice (in the code and in the DB), because keeping them in synch will be a nightmare. Pick one solution and stick to it.</p>\n'}, {'answer_id': 55856, 'author': 'Christian Hagelid', 'author_id': 202, 'author_profile': 'https://Stackoverflow.com/users/202', 'pm_score': 0, 'selected': False, 'text': '<p>My opinion is that the application itself should handle authentication and authorisation. On the database side you should only handle encryption of data as needed.</p>\n'}, {'answer_id': 55874, 'author': 'Peter', 'author_id': 5189, 'author_profile': 'https://Stackoverflow.com/users/5189', 'pm_score': 0, 'selected': False, 'text': '<p>I have built stored procedure based applications in the past.  In your case there maybe a way to keep authentication at the database layer and have your business logic in C#.  Use views to limit data (you only see the rows you have authority to).  These views can be used in LINQ with the same ease as tables.  You set your updates to happen with stored procedures.</p>\n\n<p>This allows linq, business logic in C#, and a common authentication layer in the database that controls access to the data.</p>\n'}, {'answer_id': 55952, 'author': 'chrissie1', 'author_id': 2936, 'author_profile': 'https://Stackoverflow.com/users/2936', 'pm_score': 1, 'selected': False, 'text': '<p>It all depends on your case it is probably better not to go the SP route and do everything the DDD way (make a Domain model in code and use that). </p>\n\n<p>However, if you have a database that is not only used by your application but by many then you should probably consider web services. In any way, the database should only be accessible via one layer that enforces the business rules else you are going to end up with "dirty" data and sanitizing your data afterward is a much bigger pain than writing a few business rules beforehand. A good database should have check-constraints and indexes set, so it will have some business rules whether you like it or not.</p>\n\n<p>And if you have to deal with millions and billions of records you will be happy to have a good DB-guy that solves the problem for you.  </p>\n'}, {'answer_id': 57392, 'author': 'tqbf', 'author_id': 5674, 'author_profile': 'https://Stackoverflow.com/users/5674', 'pm_score': 1, 'selected': False, 'text': '<p>Stored procedures are usually a win for security. Simplifying the relationship between your application and the database reduces the number of places where you can have errors; errors in code that interfaces business logic to the database tend to be security problems. So, your DBA isn\'t wrong about locking things down to stored procedures.</p>\n\n<p>Another benefit to locking the application down to stored procedures is that the app stack\'s database connection can have its privileges locked down to specific stored procedure calls and nothing else. </p>\n\n<p>A benefit to having a DBA involved in security logic for your application is that the different app features and roles can be partitioned in the database down to views, so that even if dynamic SQL and generic select statements are needed, the damage from an SQL vulnerability can be constrained.</p>\n\n<p>The flip side of this is, of course, lost flexibility. An ORM is obviously going to be faster to develop to than a constant negotiation with a DBA over stored procedure parameters. And, as the pressure on those stored procedures grows, it\'s more and more likely that the procedures themselves will resort to dynamic SQL, which will be just as vulnerable as app composed SQL to attack. </p>\n\n<p>There\'s a happy middle ground here, and you should try to find it. I\'ve worked on projects recently that were saved from pretty terrible SQL injection problems because a DBA had carefully configured the database, its connections, and its stored procedures for "least privilege", so that any one database user had access only to what they needed to know. </p>\n\n<p>Obviously, as you write SQL code in your app logic, be sure that you\'re consistently using parameterized prepared statements, that you\'re sanitizing your input, that you\'re mindful of internationalized input (there are many, many ways to say single-quote over HTTP), and that you\'re mindful of how your database behaves when inputs are too large for column widths.</p>\n'}, {'answer_id': 57400, 'author': 'Lars Mæhlum', 'author_id': 960, 'author_profile': 'https://Stackoverflow.com/users/960', 'pm_score': 2, 'selected': False, 'text': '<p>IMHO:</p>\n\n<p>Application service tier -> application logic and validation<br>\nApplication data tier -> data logic and security<br>\nDatabase -> data consistency </p>\n\n<p>You will be bitten by the sproc approach sooner or later, I have learned this the hard way.<br>\nProcs are great for one shot operations that need a lot of performance, but the CRUD part is the data tiers job</p>\n'}]
['https://Stackoverflow.com/questions/55845', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5203/']