Constructor Injection for System.Data.Linq.DataContext?

Jul 28, 2008 at 9:27 PM

I must be missing something.  I am trying to wire up my Data Access layer with the Unity container and I want to pass a Linq DataContext to my DAO objects when I resolve them from the container.  However I cannot seem to map a DataContext because it errors saying it cannot create one.  The reading I have been doing suggests when a class has multiple constructors that it uses an attribute to identify which constructor the container should use, but my question is:  What about classes I don't have control over? 

My configuration is below.  I have a Linq to SQL data model with a data context named RMSDataContext.  I do have control over this object, however I don't want to add an attribute to the constructor of this class because it would be wiped out whenever I update the model in the designer.  I was able to work around this by defining the DataContext typeAlias and then using RegisterInstance<DataContext>(new RMSDataContext());  but I also don't want one global DataContext for my entire application.

Please help.


<

 

configuration>

 

<

 

configSections>

 

<

 

section name="unity"

 

 

 

type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,

 

Microsoft.Practices.Unity.Configuration, Version=1.1.0.0,

Culture=neutral, PublicKeyToken=31bf3856ad364e35

 

"/>

 

</

 

configSections>

 

<

 

unity>

 

<

 

typeAliases>

 

 

<

 

typeAlias alias="IPurchaseOrderAccessor"

 

 

 

type="SafetyNet.RMS.Data.IPurchaseOrderAccessor,

 

SafetyNet.RMS

 

" />

 

<

 

typeAlias alias="ISupplierAccessor"

 

 

 

type="SafetyNet.RMS.Data.ISupplierAccessor,

 

SafetyNet.RMS

 

" />

 

<

 

typeAlias alias="DataContext"

 

 

 

type="System.Data.Linq.DataContext,

 

System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

 

" />

 

 

</

 

typeAliases>

 

<

 

containers>

 

<!--

 

Data Access Container -->

 

<

 

container name="DataAccess">

 

<

 

types>

 

<

 

type type="DataContext"

 

 

 

mapTo="SafetyNet.RMS.DataContext">

 

<

 

typeconfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,

 

Microsoft.Practices.Unity.Configuration

 

">

 

<

 

constructor>

 

<

 

param name="connection" type="string">

 

<

 

value value="Data Source=XXXX;Initial Catalog=XXXX;Integrated Security=true" />

 

</

 

param>

 

</

 

constructor>

 

</

 

typeconfig>

 

</

 

type>

 

 

<

 

type type="IPurchaseOrderAccessor"

 

 

 

mapTo="SafetyNet.RMS.Data.PurchaseOrderAccessor,

 

SafetyNet.RMS

 

">

 

<

 

typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,

 

Microsoft.Practices.Unity.Configuration

 

">

 

<

 

constructor>

 

<

 

param name="dataContext" parameterType="DataContext">

 

<

 

dependency />

 

</

 

param>

 

</

 

constructor>

 

</

 

typeConfig>

 

</

 

type>

 

<

 

type type="ISupplierAccessor"

 

 

 

mapTo="SafetyNet.RMS.Data.SupplierAccessor,

 

SafetyNet.RMS

 

" />

 

</

 

types>

 

</

 

container>

 

</

 

containers>

 

</

 

unity>

 

</

 

configuration>

 

Jul 28, 2008 at 9:53 PM
You can specify the injection details for the DataContext in configuration or via the API as well. This is specifically for the case you're talking about - when you don't have control over the code you're injecting.

Your configuration looks ok at first glance. What specific error are you getting? What's the injected class look like?

Could you post a small unit test that reproduces the problem?

Thanks,

-Chris
Jul 29, 2008 at 2:25 AM
Chris,

Thanks for your thoughts.  I tore down my original mapping file and created a new one and it works now for some reason.  Thanks for your help.
Apr 16, 2009 at 12:59 PM
I am experiecing the exact same problem, did you ever figure out what the problem was.

My config...

<?

 

xml version="1.0" encoding="utf-8" ?>

 

<

 

configuration>

 

<

 

configSections>

 

<

 

section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

 

</

 

configSections>

 

<

 

unity>

 

<

 

typeAliases>

 

<

 

typeAlias alias="datacontext" type="NICore.Domain.NIDataMappingDataContext, NICore" />

 

</

 

typeAliases>

 

<

 

containers>

 

<

 

container>

 

<

 

types>

 

<

 

type type="System.Data.Linq.DataContext, System.Data.linq" mapTo="datacontext">

 

<

 

typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">

 

<

 

constructor>

 

<

 

param name="connection" parameterType="string">

 

<

 

value value="Data Source=XXXX;Initial Catalog=XXXX;Persist Security Info=True;User ID=XXXX;Password=XXXX" />

 

</

 

param>

 

</

 

constructor>

 

</

 

typeConfig>

 

</

 

type>

 

</

 

types>

 

</

 

container>

 

</

 

containers>

 

</

 

unity>

 

 

</

 

configuration>


 

extract from my class is as follows...

 

 

 

 

. . . . . . .

IUnityContainer container =

new UnityContainer();

 

 

 

//Get the "DataLayer" container definition from the config file and

 

 

//configure our container with it

 

 

UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");

 

section.Containers.Default.Configure(container);

 return container.Resolve<DataContext>();

 

 

. . . . . . .