Dynamic ConnectionString using Adapter

Posts   
 
    
Lain
User
Posts: 13
Joined: 19-Mar-2009
# Posted on: 17-Feb-2010 13:59:47   

I am aware that one approach to dynamically set the ConnectionString for the Adapter is to set the ConnectionString property of each DataAccessAdapter object being use.

What I'm not clear about is when the DataAccessAdapter object is within a method in a class generated by GenPro. If I set the ConnectionString property in this generated code, the next time GenPro regenerate the code my modification will be removed and I will need to set the ConnectionString property again (and again) for each DataAccessAdapter object in the generated code. Is there a better approach?

I know that one way to centralize the ConnectionString is by putting it in the appSettings section of the application config file. As a matter of fact, I have implemented a solution where we dynamically add a new appSetting entry for GenPro in our application start-up method (using ConfigurationManager, etc.) to have an entry like the following: <add key="Main.ConnectionString" value="DYNAMICALLY generated..."/> This approach works. The problem is sometimes the user or the process running our application doesn't have write permission for the config file or the directory where the config file is located.

For this reason, I'm ruling out the use of appSettings. But if I use the approach above of setting the ConnectionString property of the DataAccessAdapter, what about GenPro generated code that create its own DataAccessAdapter object?

I guess what I am getting at is somekind of static property to set the ConnectionString property of DataAccessAdapter only once w/out having to set for every DataAccessAdapter object instance used.

I know that you can set the 'ConnectionStringKeyName' static property of DataAccessAdapter. But that only specify the key of an appSetting stored in the config file. This is an approach that I want to avoid due to 'write permission' problem and others as I explained above.

Am I missing something here? Is there a better way to set the ConnectionString of the Adapter object w/out resorting to changing every Adapter object's ConnectionString property and also w/out using the appSettings apporach?

Thanks in advance for your help.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 17-Feb-2010 14:46:53   

I guess what I am getting at is somekind of static property to set the ConnectionString property of DataAccessAdapter only once w/out having to set for every DataAccessAdapter object instance used.

This is available in SelfServicing (just for the records).

Anyway, as far as I can remember, the only thing that can use a built-in adapter is the LLBLGenProDataSource, and yet you can set LivePersistence Off and handle the events yourself to use your own instantiated adapter.

methodman
User
Posts: 194
Joined: 24-Aug-2009
# Posted on: 17-Feb-2010 15:05:36   

Just use an IoC container for getting your adapter and set the container to inject the connection string in the constructor. Very simple.

A call for an adapter


var adapter = ObjectFactory.GetInstance<IDataAccessAdapter>;

And the configuration looks like:


ForRequestedType<IDataAccessAdapter>()
                .TheDefault.Is.OfConcreteType<DataAccessAdapter>()
                .WithCtorArg("connectionString")
                .EqualTo("connstring");

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 17-Feb-2010 15:28:58   

Methodman,

Dependency inversion....good point, thanks for sharing.

Lain
User
Posts: 13
Joined: 19-Mar-2009
# Posted on: 19-Feb-2010 11:53:24   

Thanks a lot for your prompt response and help.

I like the DI approach since I am familiar w/ the IoC and DI concepts. However, I'm still not clear about how to get the 'ConnectionString' property of the DataAccessAdapter assigned to a string value that I will provide dynamically.

Let's use the example provided in the 'Generated code - Setting up and using Dependency Injection' section of the help manual.

In the case of assigning the 'Validator' property of IEntity2 object, the Validator is of type IValidator. The custom Validator class extends the ValidatorBase which in turn implements IValidator. In this case it is clear to me that we have the following attribute in our custom Validator class:

[DependencyInjectionInfo(typeof(MyDataTableEntity), "Validator")] public partial class MyDataTableEntityValidator_1 : ValidatorBase { ... }

Here we simply add the DependencyInjectionInfo attribute to the custom validator class for MyDataTableEntity and each instance of MyDataTableEntity will gets its 'Validator' property assigned automatically to an instance of this validator type.

What I'm not clear about is in the case of the 'ConnectionString' property of DataAccessAdapter. Because the 'ConnectionString' property is a string type. How do I add DependencyInjectionInfo attribute to a 'string' class???

Am I missing something here?

Thanks in advance for your help.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Feb-2010 18:41:00   

I think by IoC, methodman means the concept of IoC and some DI framework like Spring.Net. You can't with LLBLGen DI framework as it is meant for Entities. So, the options I see are:

  • Pass the connectionString into the DataAccesAdapter's constructor
IDataAccessAdapter adapter = new DataAccessAdatper( DBHelper.GetMyConnectionString() );
David Elizondo | LLBLGen Support Team