Custom database configuration

Topics: Data Access Application Block
Jul 19, 2010 at 3:25 PM

Hi,

 

I have created a custom database and registered it like so

 

<dataConfiguration>  

  <providerMappings>

    <add name="System.Data.SqlClient" databaseType="RM.LP.Data.LPDatabase, RM.LP.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2258c5292d433ac6" />  

  </providerMappings>

</dataConfiguration>

 

and I have two databases

 

<connectionStrings>

  <add name="Logging" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Logging" providerName="System.Data.SqlClient" />    

  <add name="Agent" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Agent" providerName="System.Data.SqlClient" />

</connectionStrings>  

 

As I understand it, the dataConfiguration section registers my database class with both connections because they both use the System.Data.SqlClient provider.  Is it possible to configure my database object differently per database connection i.e. set a property on my custom database object to true when access?

 

Cheers,

 

Paul

Jul 20, 2010 at 12:42 AM

I'm afraid I didn't get your question clearly.  What do you mean configure each database object differently and set a property to true when access? 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 20, 2010 at 5:40 AM

Hi Sarah, thanks for responding and sorry for not being clear enough.

 

I would like to add an Audit flag to my database object and set it to true for all access to the Agent database but set it to false for all access to the logging database.  I don't want to set this property in code so my question is can it be done in configuration?

 

Something like 

 

<connectionStrings>

  <add name="Logging" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Logging" providerName="System.Data.SqlClient" audit="false" />    

  <add name="Agent" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Agent" providerName="System.Data.SqlClient" audit="true" />

</connectionStrings>

 

I appreciate that you can set properties in configuration via the dataconfiguration section but those settings seem to apply to connections to all databases.  Am I missing something?

 

Thanks,

 

Paul

Jul 20, 2010 at 6:58 AM

You can configure properties for each connection string as long as it is a valid attribute.  There's no audit attribute for a connection string thus, it is invalid.  What is the exact property of a connection string which you want to configure? 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 20, 2010 at 7:23 AM

Yes, I know that audit is not a valid connection string property.  It is a property on my RM.LP.Data.LPDatabase class which inherits from GenericDatabase.  I want to check the audit property in an override of the ExecuteNonQuery method and if true, to log the command being executed to a text file for auditing purposes.  However, I do not want to log commands run against the logging database (because this would result in an infinite loop).  Is it possible to configure the audit property differently for individual databases?

Jul 20, 2010 at 8:04 AM
Edited Jul 20, 2010 at 8:40 AM

Are you using version 5.0 of Entlib? I think configuring the registration of the database in a unity container will solve your problem.  Are you familiar with Unity? 

Anyway, what you will do is create a registration of the database you want and configure a property injection for that registration.  You will inject the value you want for the Audit property.   

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 20, 2010 at 9:03 AM

Yes I am using 5.0.  I'm familiar with Unity but could you provide an example, I'm still not clear how the registration could be different for each connection?

Jul 20, 2010 at 9:22 AM
Edited Jul 20, 2010 at 9:22 AM

The registration is differentiated thru their names. Here's the sample configuration for this:

<unity>
    <namespace name="Microsoft.Practices.EnterpriseLibrary.Data" />
    <assembly name="Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <container>
      <register type="Database" mapTo="RM.LP.Data.LPDatabase, RM.LP.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2258c5292d433ac6" name="Logging">
        <property name="Audit" value="false" />
      </register>
      <register type="Database" mapTo="RM.LP.Data.LPDatabase, RM.LP.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2258c5292d433ac6" name="Agent">
        <property name="Audit" value="true" />
      </register>
    </container>
</unity>

Your code then for resolving each database instance would be:

var container = new UnityContainer()
    .LoadConfiguration()
    .AddNewExtension()
var configurator = new UnityContainerConfigurator(container); 
        
EnterpriseLibraryContainer.ConfigureContainer(configurator, ConfigurationSourceFactory.Create());
EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);
 //this would have a value of  Audit = false
Database loggingDb = EnterpriseLibraryContainer.Current.GetInstance("Logging");
 //this would have a value of  Audit = true
Database agentDb = EnterpriseLibraryContainer.Current.GetInstance("Agent");

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jul 20, 2010 at 9:52 AM

Thanks Sarah, that makes sense.  Just one more question (please excuse my ignorance), but how do I associate the database instance with a connection string?

Jul 20, 2010 at 9:58 AM
Edited Jul 20, 2010 at 9:59 AM

I think this is what you're asking:

Database loggingDb = EnterpriseLibraryContainer.Current.GetInstance("Logging");

This line of code creates a Database instance based on the connection string defined in your config named "Logging" which to point out exactly, is this line:

<add name="Logging" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Logging" providerName="System.Data.SqlClient" audit="false" />    

The loggingDb.ConnectionString property thus contains the value "Server=localhost;Integrated Security=SSPI;Initial Catalog=Logging" providerName="System.Data.SqlClient".

Does this answer your question?

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jul 20, 2010 at 10:03 AM

Ah, so it all ties up because the registration name

 

<register type="Database" mapTo="RM.LP.Data.LPDatabase, RM.LP.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2258c5292d433ac6" name="Logging">


matches the name of the connection string

 

<add name="Logging" connectionString="Server=localhost;Integrated Security=SSPI;Initial Catalog=Logging" providerName="System.Data.SqlClient" />

 

Is that correct?



 

Jul 21, 2010 at 1:03 AM

Yes, that's correct.

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com