edpollitt / nerdle.autoconfig Goto Github PK
View Code? Open in Web Editor NEWConvention-based automagical C# .config to object mapper
License: MIT License
Convention-based automagical C# .config to object mapper
License: MIT License
Wanted to use the property of type Type ...
Interface:
public interface IMyConfiguration
{
Type DeclaredType { get; }
}
Config
...
But got an exception
Tried to map element ... using 'RecursingMapper', and barfed. See inner exception for details.
Workaround:
declare property as string
in the code use:
var mytype = Type.GetType(myconfig.DeclaredType)
Hi there,
Thanks for this library - I've rolled my own configuration classes before and this is much less boilerplate code to write - I much prefer your library.
However, I would like to use an schema XSD file alongside my configuration section so that I can benefit from intellisense in Visual Studio and document the configuration settings.
To do this I added an XSD describing my custom config section and decorated the my config section that will be processed by AutoConfig with an xsi:noNamespaceSchemaLocation attribute and xmlns:xsi - something like this:
However the namespace declaration attributes are picked up in the automatic mapping so I errors because AutoConfig cannot map these attributes to my config class.
I cloned the repo and debugged and found that MappingFactory.CreateMapping() includes namespace declarations - so I ingored the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" declaration by adding a where clause to ingnore it :
var attributeList = sectionElement.Attributes().Where(x => !x.IsNamespaceDeclaration).ToList();
Of course, it still tries to match the xsi:noNamespaceSchemaLocation attribute. To get around this I added a NoNamespaceSchemaLocation to my config interface and defined it as optional with AutoConfig.WhenMapping<> but the code in MappingFactory.TakeAttribute is not namespace aware.
So I changed MappingFactory.TakeAttribute() to use LocalName instead:
static XAttribute TakeAttribute(ICollection attributeList, string name)
{
return TakeSingleMatching(attributeList, attr => attr.Name.LocalName == name,
count => string.Format("Found {0} attributes with name '{1}' but only expected to find one.", count, name));
}
This works for my purposes, but you may wish to add optional logic to ignore attributes for specific namespaces and namespace prefixes as a better solution. If you could configure via AutoConfig.WhenMapping() to ignore the a given namespace that would be perfect.
Something like:
AutoConfig.WhenMapping(mapping =>
{
mapping.IgnoreNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
});
Then you could change element/attribute mapping to see if the namespace is in the ignore list and ignore any that are not part of the actual config for the project.
Thanks,
Darryl
Hi,
Just wondering what the reasoning is for banning custom mappers when mapping from attributes?
Cheers,
Gareth
Hello --
I see a commit for updating the assembly to support .NET Standard 2.0. Would you mind publishing a new version of the Nuget package that supports this? Thank you so much for your work on this project!
Joe
@edpollitt, I would like to add Section Group like below. How do i achieve this? Below is my sample xml and C# code
var mappings = AutoConfig.Map<IBaseConfigurations>();
public interface IBaseConfigurations
{
IApiSettingsConfiguration ApiSettings { get; set; }
ITestConfiguration TestConfiguration { get; set; }
}
<baseConfigurations>
<apiSettingsConfiguration>
<baseUrl>http://localhost:62954</baseUrl>
<endPoint></endPoint>
<pollingIntervalInSeconds>10</pollingIntervalInSeconds>
</apiSettingsConfiguration>
<testConfiguration>
<name>Testing</name>
</testConfiguration>
</baseConfigurations>
Could you please sign the assemblies published at nuget.org so that they become strong named?
Thanks in advance!
Hi,
Could you please add an ability to read configuration from not default configuration file?
For instance, it could look like this:
AutoConfig.SetPathToConfigFile(string configFile)
or like this
AutoConfig.Map(string sectionName = null, string fileName = null)
Thanks!
when map to a KeyedCollection<Tkey,TItem> will get a exception, but List was well.
when i use a class(with get and set property) and deserialize from xml, the KeyedCollection<Tkey,TItem> will run well
$exception {"Tried to map element 'themeConfig' to property 'ThemeConfig' of 'AutoConfig.DynamicTypes.Implementation_Of_ConsoleTest_IWebsiteConfiguration' using 'RecursingMapper', and barfed. See inner exception for details."} Nerdle.AutoConfig.Exceptions.AutoConfigMappingException
<section name="websiteConfiguration" type="Nerdle.AutoConfig.Section, Nerdle.AutoConfig" />
....
<websiteConfiguration>
<siteUrl><![CDATA[http://my.domian.com/]]></siteUrl>
<themeConfig default="Red">
<themes>
<theme name="Blue" />
<theme name="Red" />
<theme name="Black" />
</themes>
</themeConfig>
</websiteConfiguration>
public interface IWebsiteConfiguration
{
string SiteUrl { get; }
IThemeConfig ThemeConfig { get; }
}
public interface IThemeConfig
{
string Default { get; }
ThemeCollection Themes { get; }
}
public interface ITheme
{
string Name { get;}
}
public class ThemeCollection : KeyedCollection<string, ITheme>
{
protected override string GetKeyForItem(ITheme item)
{
return item.Name;
}
}
Configuration manager use caching to save configuration values from config file and use it next time. Is it possible to update config values by any concrete class instance and use those updated values if config loaded in memory next time (app restart)?
Hi! How I can use mapper for nested fields? E.g.:
interface IA {
IB property1;
}
interface IB {
someEnum property2;
}
If I am trying like that
AutoConfig.WhenMapping<IA>(
mapping =>
{
mapping.Map(x => x.property1.property2).Using<Property2Mapper>();
});
I'll get nothing.
But if I am trying as following
AutoConfig.WhenMapping<IB>(
mapping =>
{
mapping.Map(x => x.property2).Using<Property2Mapper>();
});
I'll get exception ....... A custom IMapper cannot be specified when mapping from an attribute
.
Why ?
We'd like to be able to compose this library's awesome config deserialization code with things that aren't on disk (our existing config library supports loading config from a file system abstraction library for in-memory testing and long-path support).
Your public API currently requires that we write the content out to disk so it can be loaded in by the library (your public API optionally takes a configFilePath
). Would you consider adding an overload to AutoConfig.Map
that takes a Stream
or a TextReader
, or alternatively making the internal method that takes an XElement
public?
Thanks for the good work on this library!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.