I'm in the process of moving our Unity configuration to the web.config file. I'm stuck on how to migrate the following code config to the xml format:
var container = new UnityContainer();
container.RegisterType<IPrincipal>(new InjectionFactory(x=> HttpContext.Current.User));
return container;
Here are the XML declartion:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="IRepository" type="Model.IRepository, Model" />
<alias alias="Repository" type="Data.Repository, Data" />
<container>
<register type="IRepository" mapTo="Repository" />
</container>
</unity>
InjectionFactory is the one thing that can't be represented in XML out of the box. In order for it to completely work, you'd have to write a C# parser that could work on the XML file, which was way more than I wanted to bite off at the time.
However, I do have a sample on bitbucket which shows a way to get a limited version of factory creation working via XML. Might give you some ideas.
Related
I'm working on a existing asp.net project. In that project I'm using unity container version 5.2.0.0 for dependency injection. Since it is big project there are plenty of types to register o the unity container and again those types have more dependencies. Its grown recursively. Is there any way to manage this hierarchically growing of my unity container or should I register all these dependencies.
//For Type1
container.RegisterType<ITypeA, TypeA>();
container.RegisterType<ITypeB, TypeB>();
container.RegisterType<ITypeC, TypeC>();
//For Type2
container.RegisterType<ITypeD, TypeD>();
//For TypeA
container.RegisterType<ITypeP, TypeP>();
container.RegisterType<ITypQ, TypeQ>();
//For TypeP
container.RegisterType<ITypeS, TypeS>();
container.RegisterType<ITypR, TypeR>();
Now my container like this. Actually worst than this. So how can I get rid of this.
You can Register all dependencies with using unity config.
First add unity to config sections in App.config and give source (MyUnityDependencies.config etc.):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity configSource="MyUnityDependencies.config" />
...
</configuration>
In MyUnityDependencies.config you can add register types for each Container:
<?xml version="1.0"?>
<unity>
<container name="MyContainer">
<register name="Dependency1"
type="ITypeA"
mapTo="TypeA" >
</register>
<register name="Dependency2"
type="ITypeB"
mapTo="TypeB" >
</register>
...
</container>
</unity>
Finally you can Register all dependencies with LoadConfiguation
IUnityContainer container = new UnityContainer();
container.LoadConfiguration("MyContainer");
I need to add a ComputedIndexField to my Solr Configuration
Doing so by creating a config patch containing the following
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultSolrIndexConfiguration>
<fields hint="raw:AddComputedIndexField">
<field fieldName="SomeFieldName">
Type, Dll
</field>
</fields>
</defaultSolrIndexConfiguration>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
But i kept getting the error. Even though this was the format the documentation was suggesting.
Could not find property 'fieldMap' on object of type: System.String
The problem:
The naming of my custom config was making it load before the nodes that it was trying to patch. I fixed it by changing the name so that they would load AFTER the node.
We have an app.config we are using with Carbonator:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="carbonator" type="Crypton.Carbonator.Config.CarbonatorSection, Crypton.Carbonator"/>
</configSections>
<carbonator defaultCulture="en-US" logLevel="1" collectionInterval="1000" reportingInterval="1000" >
<statsd server="127.0.0.1" port="8125" />
<counters>
<add path="processor_information.pct_processor_time.total" category="Processor" counter="% Processor Time" instance="_Total" />
<add path="memory.available_MBytes" category="Memory" counter="Available MBytes" instance="" />
<add path="memory.pct_commited_bytes_in_use" category="Memory" counter="% Committed Bytes In Use" instance="" />
</counters>
</carbonator>
</configuration>
We want to allow users to configure their own custom counters in an external config file that we reference from the <counters> element. For example, we would like to allow the user config file to look like:
<add path="logical_disk.pct_free_space.C" category="LogicalDisk" counter="% Free Space" instance="C:" />
<add path="logical_disk.disk_read_bytes_per_sec.C" category="LogicalDisk" counter="Disk Read Bytes/sec" instance="C:" />
<add path="logical_disk.disk_write_bytes_per_sec.C" category="LogicalDisk" counter="Disk Write Bytes/sec" instance="C:" />
I don't even know if this is possible outside of an appConfig element, but any help is appreciated.
According to this answer it should be possible. Same way is also described in this article.
But I don't think it's a good idea for one reason - if a user makes a mistake in his configuration extension, it will prevent the application from executing since the application configuration became invalid.
I would rather use the configuration in the app.config file to provide default values and implement some user configuration myself. Is such case, you can use whatever configuration format you like, for example JSON, which would be also better (easier to create and edit) for users. In your application, you simply merge both configurations (app.config values are default values which will be overwritten by the user's configuration).
This example on MSDN shows how to implement a simple dictionary inside a custom App.config section:
https://msdn.microsoft.com/en-us/library/aa719887(v=vs.71).aspx
The XML looks like this, and it employs SingleTagSectionHandler:
<sampleSection setting1="Value1" setting2="value two"
setting3="third value" />
Is this the most complicated my custom XML section can be before I have to write my own config section class? Because I wish to have a section which is slightly more complex e.g:
<mySection>
<mappings>
<mapping name="A" val1="12" val2="32"/>
<mapping name="B" val1="2" val2="2"/>
</mappings>
<add name="URL" value="http://..."/>
</mySection>
Or something along those lines at least, the exact structure isn't rigid.
How far can I push the built-in section handlers? I couldn't see a good tutorial on what is actually provided in system.configuration.
I have code that register two classes with the same names and namespaces but from different assemblies. Here's how I resolve this problem using Unity config file:
<register type="IMyService" mapTo="CoordinatingMyService" />
<register type="IMyService" mapTo="MyNamespace.MyService, MyService.Version1" name="V1"/>
<register type="IMyService" mapTo="MyNamespace.MyService, MyService.Version2" name="V2"/>
My question is how I can achieve the same in code instead of the config file.