Separate Read/Write Connection for SubSonic - subsonic

The security policy at our client's production environment requires that we use separate connection to execute writes and read to and from database. We have decided to use SubSonic to generate our DAL. So I am interested in knowing if it is possible and if yes how?

You can specify the provider SubSonic is using at runtime. So you would specify the read provider (using your read connectionstring) when loading from the database and then specify the write provider (using your write connectionstring) when you want to save to it.
The following isn't tested but I think it should give you the general idea:
SqlQuery query = new Select()
.From<Contact>();
query.ProviderName = Databases.ReadProvider;
ContactCollection contacts = query.ExecuteAsCollection<ContactCollection>();
contacts[0].FirstName = "John";
contacts.ProviderName = Databases.WriteProvider;
contacts.SaveAll();

Related

Elevated Permissions in ADO.NET

I was given job to check for possible ways of acces to SharePoint list from client-side desktop application and after some research I found that ADO.NET will be, in my opinion, best for that task.
Some code:
MySiteDataContext context = new MySiteDataContext(new Uri("http://MySite/_vti_bin/listdata.svc"));
context.Credentials = new System.Net.NetworkCredential("login", "password", "domain");
List<MyListItem> list = context.MyList.ToList<MyListItem>();
grid.ItemsSource = list;
And here is my question: Is there any way to run code like this with elevated permissions to read lists (even if credentials I gave don't have rights). Most probably there is no chance to do that because it's client-side, but I want to ensure that it is not possible.
1.U can write your code in a seperate method.
2.Then call that method in the place where u need,like SPSecurity.RunWithElevatedPrivileges(MethodName);

Multiple Auth drivers in kohana3.2

I'm working on a project where I'm trying to implement authentication against external user base for customers, this seems to be working correctly.
Recently there has been added another requirement that some people (not present in the aforementioned base) need to be able to edit parts of pages' content. First thing that comes to mind is to have separate ORM/File Auth driver enabled for those few editors to be able to authenticate them separately.
Is it possible to use two Auth drivers at the same time in Kohana 3.2?
Yes, you can use different drivers at once. Just create another instance instead of standard singleton:
// default Auth
$config = Kohana::$config->load('auth');
$auth = new Auth($config);
$user = $auth->get_user();
// special Auth for administration
$config2 = Kohana::$config->load('admin_auth');
$auth2 = new Auth($config2);
$admin = $auth2->get_user();
Restrictions:
You must use differ configs (driver and session_key values must differ). Note that some settings are defined in classes and cant be changed by config (for example, "remember" cookie named authautologin).
You cant share default ORM models (Model_User, Model_Token, Model_Role), because their names are hardcoded. But ORM driver & File driver can be used.
Kohana's Auth module does not natively support using two Drivers.
However, you can implement a new Driver yourself very easily. You can follow the instructions for creating a new Driver by copying the existing driver and modifying it, here: http://kohanaframework.org/3.3/guide/auth/driver/develop
The simple thing to do would be to put the following logic in your _login method:
Check the external user database for a valid login
If there is a valid user in the external user database, return true.
If there is no valid user in the external user database, check the local user database instead.
If the user exists in the local database, return true.

entity Framework 5 changing providers at runtime

I have an application that runs at several client sites. I have to support different DBMS at the locations (SQL Server, DB2, Oracle). I am converting the application from VC++ 6.0 with an ODBC based data layer to Visual Studio 2012 and would like to use Entity Framework (database first). I am having troubles changing the database provider at runtime in my sample application. I changed the connect string in the app.config from a SQL Server to DB2 connect string and changed the default connection factory. Now when I run the program I can connect to the database (at least there is no error) but when I iterate over the linq results I get the exception:
Unable to cast object of type 'IBM.Data.DB2.DB2Connection' to type 'System.Data.SqlClient.SqlConnection'
Here is the program code:
private void btnList_Click(object sender, EventArgs e)
{
using (var ListBill = new LB402_TestEntities())
{
var queryGroups = from Groups in ListBill.LB_Group
select Groups.GroupName;
foreach (string name in queryGroups)
{
lbGroups.Items.Add(name);
}
}
}
The modifed portions of the app.config are:
<defaultConnectionFactory type="IBM.Data.DB2.Entity.DB2ConnectionFactory, EntityFramework" />
<add name="LB402_TestEntities" connectionString="metadata=res://*/LB402.csdl|res://*/LB402.ssdl|res://*/LB402.msl;provider=IBM.Data.DB2;provider connection string="Server=db210:50000;Database=LISTBILL;uid=uuuuu;pwd=ppppp;"" providerName="System.Data.EntityClient" />
From my searching and reading it seems like I should be able to do this, but I am obviously missing something.
Changing connection string is not enough. EDMX file consist of three part and one is provider specific (that is the .ssdl reference from connection string). You need to have this part for every database you need to support and you need to have EF provider for every such database. The problem is that EDMX designer does not support modelling for multiple providers. So you must either have separate EDMX for every database = huge duplicity or you must start maintaining SSDL files for other databases manually (it is XML).
You should make some small Proof-of-concept with code first mapping and DbContext API because it doesn't have these obstacles - SSDL is generated at runtime from code mapping and provider specified in connection string.

Connections with many databases

We have a webapp where each client has their own db (approx. 700 at the moment).
In SubSonic 2, you had to wrap each call with the SharedDBConnectionScope passing in the right connection string to use, otherwise you ran the risk of one thread or client getting data from another thread or client.
In SubSonic3 is this still needed? Do I need to wrap the calls like I did in 2.x?
There are easy ways of switching the database now, but do I still have thread issues or can I do away with the call to SharedDBConnectionScope?
SubSonic 3 greatly improved the way to create a provider from scratch or just passing a name and a connectionsctring:
Some Examples:
// Linq Templates:
var db = new YourDB("connectionstring goes here", "System.Data.SqlClient");
// SimpleRepository without app.config
IDataProvider provider = SubSonic.DataProviders.ProviderFactory.GetProvider(
connectionString: "Server=localhost;Database=clientdb;Uid=root;",
providerName: "MySql.Data.MySqlClient"
);
IRepository repository = new SimpleRepository(provider,
SimpleRepositoryOptions.RunMigrations);
So basically you can create a provider or repository each time a client connects and use this in your class.

Make SubSonic use existing <connectionstring> instead of new data provider

I am adding SubSonic to a legacy application. This application already defines a ConnectionString. Is there a way I can use this connectionstring instead of creating a new Data Provider entry?
I know that one solution is programmatically setting this in the code (i.e. SubSonic.DataService.GetInstance("Name").SetDefaultConnectionString("ConnString") ). However, is there a more elegant solution?
I think that's the only way to do it. And it might throw an exception if there is no place holder SubSonicService in the config file, I don't remember.
// GetInstance just to initialize subsonic.
DataProvider provider = DataService.GetInstance(subsonicProviderName);
// Set the actual database connection string.
// Overrides config file setting.
provider.DefaultConnectionString = connectionString;
DataService.Provider = provider;

Resources