What if i don't store connectionstrings in the web.config?

Oct 10, 2007 at 9:06 AM
The Microsoft.Practices.Repository constructor requires a property "Database".

My problem is that i have a main database with users and each user has his own database connectionstring.
That situation isn't gonna change, the connectionstrings are stored in the maindatabase.

Does anyone has an idea how to solve this?

PS: I don't want to save the connectionstring in the web.config for each user.
Oct 10, 2007 at 5:23 PM
Let me try to clarify this a little; I think I understand your scenario, but I'm not sure.

You have database A, which is shared across all your users, and who's connection string is in config.

You connect to database A, and retrieve a second connection string from it, which is used to connect to a per-user database B.

You then want to connect to that second database using an explicit connection string.

The current issue is that last part: the Repository class right now doesn't allow you to pass a plain connection string; you need to have the key in configuration.

If you're up to changing the package slightly, it would be trivial to add a new constructor to the Repository base class that took an Entlib Database object. Then you could construct that using whatever connection string you want and pass it into the repository. You'd then tweak the template to add the new constructor to your derived

Does that make sense?
Developer
Oct 10, 2007 at 6:06 PM
I have a solution for this already built, but it is done a bit different.

I created a new method on RepositoryFactory where you can pass in an instance of ConnectionStringSettings, like:

ConnectionStringSettings settings = new ConnectionStringSettings("MyConnectionString", "server=...", "System.Data.SqlClient");
IProductRepository repository = RepositoryFactory.Create<IProductRepository>(settings);

It does require a new constructor on a Repository Class which is a very simple change to the templates:
public class ProductRepository(ConnectionStringSettings settings) : base(settings) {}

The problem is more difficult than one would think, because the Data Access Application Block DatabaseFactory.CreateDatabase(...) method is very restricting. In essence you have to create a CustomDatabaseFactory Class to handle some of the stuff that DatabaseFactory.CreateDatabase() does internally.

I can certainly add the functionality to the source code if people are interested.

Best Regards,

Dave

_____________________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Coordinator
Oct 10, 2007 at 10:43 PM
Hi all,

I am interested in this functionality! I have a similar situation described by rfcdejong: a main database for the application itself, and specific databases for each group of users belonging to the same company.

Cheers,
Luis
Oct 11, 2007 at 7:35 AM
I made a fast workaround, just to get further..

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection csSection = config.ConnectionStrings;

List<User> users = Repositories.Users.GetAllUsers();
foreach (User user in users)
{
ConnectionStringSettings connStringsSettings = new ConnectionStringSettings(user.Naam, user.ConnectionString, "System.Data.SqlClient");
csSection.ConnectionStrings.Add(connStringsSettings);
}
config.Save(ConfigurationSaveMode.Minimal, true);


Strange enough this doesn't always work in debug mode because of the ...vhost.exe.config :)

Anyway, thanks for the reply's and i hope there'll be a nice solution build into the repository package.
Maybe DavidHayden his code, i'm intrested at this moment.

Greetings,
Raymond
Oct 11, 2007 at 10:04 AM
With my workaround, the error i get in debug

The requested database ..... is not defined in configuration.
Developer
Oct 11, 2007 at 4:45 PM
Raymond,

I think you would be much better off just being able to pass in a connection string at runtime as with my example.

I will start working on adding the ehancement into source code tonight.

Regards,

Dave

_______________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Oct 12, 2007 at 9:47 AM

DavidHayden wrote:
Raymond,

I think you would be much better off just being able to pass in a connection string at runtime as with my example.

I will start working on adding the ehancement into source code tonight.

Regards,
Dave



Thank you Dave
Developer
Oct 13, 2007 at 6:43 PM
Hey Guys,

I was thinking that since this is probably a niche request I would just post the code on PnPGuidance for you to review as opposed to adding it to the source code. You can view it here:

Repository Factory Enhancements to Use ConnectionStringSettings

Regards,

Dave

_____________________________________

David Hayden
Microsoft MVP C#
PnPGuidance
Coordinator
Oct 18, 2007 at 3:32 AM
Dave,

Thanks for implementing this! The code looks great and it is simple to use it.

Regards,
Luis
Jan 2, 2008 at 8:35 AM
Can someone please implement this code

David's code is working flawless.
After getting new source i have to implement this code manualy, that is stopping me from getting new sources.

Repository Factory Enhancements to Use ConnectionStringSettings