Mocking / facade for SQL Server with a service? - c#

Is it possible to mock, or create a facade, to replace a SQL Server database in-place, currently being used directly by a website via ADO.net, with a service (ideally WCF or other .NET technology)?
Example:
Consider an ASP.NET website with a lot of features which use SQL Server 2008 over ADO.NET. The SQL transactions involve many stored stored procedures, though there are some direct T-SQL transactions. It would be powerful to be able to replace the (singular) SQL server with a more scalable implementation, without having to re-implement the website features.
This could be a shim for ADO.NET, or an ODBC driver to a custom service framework, or something else. The "right" answer may seem to be "reimplement the website to use a more abstracted data interface", but let's consider that the website code is immutable (though configuration could be altered).

Maybe simply provide an data access layer that specifies well designed interfaces. Your website will only ever have a handle on an interface, and you can use dependency injection/IoC to provide your concrete implementation of your interface on an as needs basis.
It's not an "existing technology" as such, but probably just a prudent way forward in your design I'd suggest.

Related

How can I model webservice and/or Oracle DB?

I'm learning MVC. There are plenty of sample codes working with SQL Server. The coder has the database created on the fly from his/her classes, which enables a very clean and rapid development workflow.
I'm working with Oracle DB.
Can I also abstract data from tables, that I already have? I don't need to abstract all columns(i.e. i need only two of 50). I need only read access and wanna use either web service or oracle as input.
You know any sample code, so I can see how can abstract data for web-service or oracle as data source?
You could take a look at Entity Framework. It allows you to abstract your data access code from the underlying database. This really is not MVC specific and you could use it in any .NET application you wish.
As far as web services are concerned I would recommend you designing a data access layer that will be called from your MVC controllers and which would delegate the calls to the underlying web service. An abstraction over this web service would be beneficial if you want to unit test your controllers in isolation.

Direct Table Access Security with Entity Framework

Greetings!
I have recently built out a template for one of my clients that includes a lot of new features, not the least of which is EF/WCF RIA.
Deployment time is greatly reduced when I can connect via EF directly to a table. No more CRUD SPROCs for all 50 tables. I've read that EF, by default, doesn't allow for standard SQL Injections, but I am wondering if anyone can provide me with a comprehensive security overview, or at least enough data where I can sit down with the senior DBA/Security guy here, and convince him that writing all of the CRUD SPROC's aren't necessary.
I've read MSDN Security Considerations (Entity Framework), Security for WCF RIA Services, and a slew of other articles, but I thought I'd give SO a try and ask for some real world implementations & hard evidence in terms of security & EF.
Your thoughts are greatly apprecaited
The senior DBA/Security guy here had a lot of questions about how
Your client program never access SQL directly, instead they are calling RIA Service and RIA Service is inside your ASP.NET, which is completely under your control.
You can intercept your ObjectContext by overriding methods like SaveChanges and you can limit your IQueryable in your RIA Services Template classes.
C# or VB.NET code is very easy to read and understand instead of stored procedures.
Maintenance of stored procedure is sure a pain.
RIA Service provides you method templates where you can intercept logic and manipulate and monitor actions taken against database.
The one and the only difference is, your logic of monitoring and manipulation for CRUD executes inside ASP.NET Application Pool in case of WCF RIA Services and in case of Stored Procedure, it executes within your Database server.
In both the cases, your client has no direct access.
EF already validates your data before storing in DB against the model you have created.
Future edition of SQL Server is coming with inbuilt modelling tools which will anyway deprecate stored procedures for CRUD in some way.
I had to deal with this as well. I'm not sure if your situation is the same in the sense you can still use EF with proc function exports. I can't say I did any better but I did educate the DBA person on the WCF RIA security boundaries. Meaning if someone can get authenticated they'll still be able to call the service methods that call the procs. So I guess in my opinion there isn't a huge difference to exposing the entities you need vs exposing service methods that call procs. I was able to get the person to loosen up their restrictions when I showed them how easy it was to do CRUD operations with Linq to entities, which meant less work for them.

Are there existing data layers I can use for an application I'm building?

I'm writing a .NET application and the thought of implementing a data layer from scratch is icky to me. (By data layer I'm referring to the code that talks to the database, not the layer which abstracts the database access into domain objects [sometimes called the data access layer and used interchangeably with data layer].)
I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.
It doesn't matter to me if the library is in C# or VB.NET and I don't care if it's LINQ or ADO.NET. As long as it works.
** I want to emphasize that I'm not looking for data access technologies or mechanisms (e.g. LINQ, ORM tools, etc.) but rather existing libraries.)
If you are talking to only SQL Server the Linq to SQL is your best option. It is pretty easy to get up and running. You will get both the Data Layer and the Abstraction. All you have to do is provide a connection string to Linq to SQL and it will handle the rest.
If you are going to connect to other database than SQL you would want to with NHibernate.
NHibernate takes a little more work than Linq to SQL to get up and running. MS provided in Visual Studio a nice tool that can get you reading from a SQL database pretty quick.
Honestly as much of a fan as I've always been with NHibernate. With the latest release of Enterprise Library 5 Data Access Block that they added in the dynamic mapping support natively. I would have to strongly consider not using NHibernate on a project and instead use a forward database generation tool from my domain objects to create my database (perhaps even use NHibernate solely for the scheme export) or something like CodeSmith and use EntLib.
You can use easyobjects has a very small learning curve, and is very extensible.
From their web:
EasyObjects.NET is a powerful data-access architecture for the .NET Framework. When used in combination with code generation, you can create, from scratch, a complete data layer for your application in minutes.
I'd like to find an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works. I'll be talking to SQL Server only.
Might want to check out Subsonic. Though I personally find it quite limited, it's certainly not an ORM, but a "query tool." It will make CRUD operations easy and straightforward, and it generates partial POCO classes for every table in your database, rather than trying to map from a database to a domain layer.
Microsoft's Entity Framework might be what you are looking for to releave you from writing "the code that talks to the database".
The best things are that it already ships with Visual Studio and - depending on your requirements - you can use most functionality out-of-the box or manually adjust it to your custom business logic via T4 templates.
You can use it for forward and reverse engeneering and being a microsoft technology it integrates well with other MS products like SQL server.
I started using it 3 months ago in my current project at work which is composed of several windows and WCF services to convert third party data into our own database scheme. From the experiences we made with it, we'll be using the EF in future project a lot more.
What would you expect this framework to do with your exceptions? If it can't connect to your database, what should it do - crash the application, show an error message (winforms or WPF or ASP)... the questions are endless.
An ORM such as those suggested elsewhere in these answers is likely to be the closest you're going to get. Expecting a third party framework to provide all your exception handling isn't realistic - how would a third party know how your application is supposed to behave?
The direct answer to your question asking for "an existing generic data layer implementation which provides standard crud functionality, error handling, connection management - the works" is simple: use ADO.NET. The answers everyone else have provided actually go beyond that functionality, but your responses suggest that you think that there's something even further beyond - something that implements your data layer for you. My suggestion is that what you're looking for probably doesn't exist.

Best way to query different database engines in a uniform way?

I work on a C# client application (SlimTune Profiler) that uses relational (and potentially embedded) database engines as its backing store. The current version already has to deal with SQLite and SQL Server Compact, and I'd like to experiment with support for other systems like MySQL, Firebird, and so on. Worse still, I'd like it to support plugins for any other backing data store -- and not necessarily ones that are SQL based, ideally. Topping off the cake, the frontend itself supports plugins, so I have an unknown many-to-many mapping between querying code and engines handling the queries.
Right now, queries are basically handled via raw SQL code. I've already run into trouble making complex SELECTs work in a portable way. The problem can only get worse over time, and that doesn't even consider the idea of supporting non-SQL data. So then, what is the best way to query wildly disparate engines in a sane way?
I've considered something based on LINQ, possibly the DbLinq project. Another option is object persistence frameworks, Subsonic for example. But I'm not too sure what's out there, what the limitations are, or if I'm just hoping for too much.
(An aside, for the inevitable question of why I don't settle on one engine. I like giving the user a choice of the engine that works best for them. SQL Compact allows replication to a full SQL Server instance. SQLite is portable and supports in-memory databases. I can imagine a situation where a company wants to drop in a MySQL plugin so that they can easily store and collate an application's performance data over the course of time. Last and most importantly, I find the idea that I should have to be dependent on the implementation details of my underlying database engine to be absurd.)
Your best bet is to use an interface for all of your database access. Then for each database type you want to support to do the implementation of the interface for that database. That is what I've had to do for projects in the past.
The problem with many database systems and storage tools is that they aim to solve different problems. You might not even want to store your data in a SQL database but instead store it as files in the App_Data folder of a web application. With an interface method you could do that quite easily.
There generally isn't a solution that fits all database and storage solutions well or even a few of them well. If you find one that claims it does I still wouldn't trust it. When you have a problem with one of the databases it's going to be much easier for you to dig through your objects than it will be to go dig through theirs.
Use an object-relational mapper. This will provide a high level of abstraction away from the different database engines, and won't impose (many) limitations on the kind of queries you can run. Many ORMs also include LINQ support. There are numerous questions on SO providing recommendations and comparisons (e.g. What is your favorite ORM for .NET? appears to be the most recent and has links to several others).
I would recommend the repository pattern. You can create a class that encapsulates all the actions that you need the database for, and then create a different implementation for each database type you want to support. In many cases, for relationional data stores, you can use the ADO.NET abstractions (IDbConnection, IDataReader, IDataAdapter, etc) and create a single generic repository, and only write specific implementations for the database types that do not provide an ADO.NET driver.
public interface IExecutionResultsRepository
{
void SaveExecutionResults(string name, ExecutionResults results);
ExecutionResults GetExecutionResults(int id);
}
I don't actually know what you are storing, so you'd have to adapt this for your actual needs. I'm also guessing this would require some heavy refactoring as you might have sql statements littered throughout your code. And pulling these out and encapsulating them might not be feasible. But IMO, that's the best way to achieve what you want to do.

Are there any database-like persistence layers available for .NET?

I'm trying to tackle the problem of disconnected operation for an application with a relatively rich data layer, and it occurs to me that the most natural way to make this work is with a client-side database. I don't want to have to install a separate product, however, and I'm left to wonder if there are any layers out there where you can essentially link a database-like persistence layer into an application. Has anyone had any experience with this? Are there any good frameworks that cover this area?
I would recommend SQLite. It's a full SQL database engine wrapped in a single dll with no installation or maintenance that just ships with your app and runs in-process. There's a great .NET wrapper that integrates nicely and allows you to create custom functions in .NET.
http://sqlite.phxsoftware.com/
If you don't need the power of a relational database and want to simplify translation of your object model for persistence, you should look into DB4O - it's an object database that can run on your client and transparently persist your classes.
You can use NHibernate with sqlite or sqlce database. We use sqlce.
.Net has strongly typed datasets, which work great for this purpose.
http://msdn.microsoft.com/en-us/library/esbykkzb%28VS.71%29.aspx
Even thought you don't want to install another product, you might want to consider SQL Server Compact Edition. Although you do need to install it, it's free, and installs no new Windows services.
The databases themselves are simply a single file per database. LINQ to SQL and LINQ to Entities are still supported, and you can even get a Windows Mobile version.
Are you looking for a database-like persistence layer because you want the query power of a database on the client side, or for persistence between application runs, or both?
If you need both, or just the persistence, then any one of the other answers showcasing integrated DB libraries will do (like this one for SQL Lite).
However, if the only thing you need is the ability to perform complex queries against in-memory data then I would highly recommend using plain-ol LINQ-to-Objects, assuming the option is available to you.

Categories