Problem with Custom Configuration Settings - winforms

I added a custom section to my app.config file for a Windows Forms Application. I created the class to extend the configuration file:
CustomFields myCustomFields = (CustomFields)System.Configuration.ConfigurationManager.GetSection("CustomFields");
I specify the section name:
<section name="CustomFields" type="Application.Core.CustomFields, ATMCardRequest.Core" allowLocation="true" allowDefinition="Everywhere" />
Now here is where I think the issue is. The above has worked fine before but I need a lot of properties for this section and instead of doing this:
<CustomFields setting1='hello' setting2='world'/>
I am doing this:
<CustomFields>
<property name="setting1">hello</property>
<property name="setting2">world</property>
...
</CustomFields>
Code:
/// <summary>
/// Settings file which holds the name of the XML Fields
/// </summary>
public class setting1: ConfigurationSection
{
/// <summary>
/// Name of the setting1 Field
/// </summary>
[ConfigurationProperty("setting1", IsRequired = true)]
public String setting1
{
get
{
return (String)this["setting1"];
}
set
{
this["setting1"] = value;
}
}
/// <summary>
/// Name of the setting2 Field
/// </summary>
[ConfigurationProperty("setting2",IsRequired = true)]
public String setting2
{
get
{
return (String)this["setting2"];
}
set
{
this["setting2"] = value;
}
}
}
}
Which isn't working. Apparently it doesn't understand the 'property' syntax.
Any ideas what I am doing wrong? Thanks.

If would define the properties that you need this way:
<CustomFields>
<property name="setting1">hello</property>
<property name="setting2">world</property>
...
</CustomFields>
than each "property" becomes child node for CustomFields and your properties are now attributes/values for those child nodes, not attributes of the CustomFields node as in the first example.
If you have lot of properties and you want to set them more elegantly here are two options that might consider:
1) Use the following structure for the custom section (slightly changed):
<CustomFields>
<setting1 value="hello"/>
<setting2 value="world"/>
...
</CustomFields>
and the following code to define the properties used to retrieve the values:
public class CustomFields: ConfigurationSection
{
[ConfigurationProperty("setting1")]
public PropertyElement Setting1
{
get
{
return (PropertyElement)this["setting1"];
}
set
{ this["setting1"] = value; }
}
[ConfigurationProperty("setting2")]
public PropertyElement Setting2
{
get
{
return (PropertyElement)this["setting2"];
}
set
{ this["setting2"] = value; }
}
}
public class PropertyElement : ConfigurationElement
{
[ConfigurationProperty("value", IsRequired = false)]
public String Value
{
get
{
return (String)this["value"];
}
set
{
this["value"] = value;
}
}
}
Then, to retrieve the values:
string setting1value = myCustomFields.Setting1.Value;
string setting2value = myCustomFields.Setting2.Value;
For details please see How to: Create Custom Configuration Sections Using ConfigurationSection on MSDN.
2) Take a programmatic approach instead of relying on attributes and reflection. ConfigurationSection class or IConfigurationSectionHandler could be used in this case. As a result you will have access from code to the xml node containing the custom section data and will be able to load any kind of XML structure.

Related

WPF binding Datasource is updated through reference

is it possible to set a Datasource through a reference?
public partial class GraphView : UserControl
{
public ObservableCollection<ChartCollection<long>> signals { get; set; }
public GraphView()
{
UCGraph.DataSource = this.signals;
}
}
and if I set the signals property should it update the Datasource?
MyGraphUC.signals = mySignals;
It doesn't seem to be working for me. Why?
No you can't directly because the variables UCDataGraph.DataSource and signals are not connected by any means. They just happen to point to the same instance after you assign them in your constructor (actually they will both point to null which is not an instance at all). That being said, you can leverage the setter to do your bidding like so:
public partial class GraphView : UserControl
{
private ObservableCollection<ChartCollection<long>> _signals
public ObservableCollection<ChartCollection<long>> signals
{
get
{
return _signals;
}
set
{
this._signals = value;
UCGraph.DataSource = this._signals;
}
}
public GraphView()
{
UCGraph.DataSource = this.signals;
}
}
Alternatively you can just .Clear() the observable collection and refill it with the elements instead of changeing the collection itself if that is an feasible scenario for you.

MVVM : how to pass parameter to ViewModel's constructor

I'm using L. Bugnion's MVVM Light Framework.
What are some of the recommended approaches to pass parameters such as Customer's ID to ViewModel's constructor?
Edit:
The parameter I need for each ViewModel is not something that is shared across models. it is something unique to each viewmodel instance.
//Create a container class to pass via messenger service
public class CarSelectedArgs
{
#region Declarations
public Car Car { get; set; }
#endregion
#region Constructor
public CarSelectedArgs(Car car)
{
Car = car;
}
#endregion
}
//example of view model sending message.
public class SendingViewModel : ViewModelBase
{
private Car _car;
public Car SelectedCar
{
get { return _car; }
set
{
_car = value;
if (value != null)
{
//messenger will notify all classes that have registered for a message of this type
Messenger.Default.Send(new CarSelectedArgs(value));
}
}
}
}
//Example of ViewModel registering to recieve a message
public class SampleViewModel : ViewModelBase
{
#region Constructor
public SampleViewModel()
{
Messenger.Default.Register<CarSelectedArgs>(this, OnCarSelected);
}
#endregion
#region LocalMethods
void OnCarSelected(CarSelectedArgs e)
{
var NewCar = e.Car;
}
#endregion
}
For me the whole point of using MVVM Light is to avoid injecting anything into the constructor of a View Model. MVVM Light provides a Messaging facility that allows you to send your parameters to a listener registered inside of the View Model.
For example, this is my View Model from my WordWalkingStick project using VSTO and WPF:
using System;
using System.Xml.Linq;
using GalaSoft.MvvmLight.Messaging;
namespace Songhay.Wpf.WordWalkingStick.ViewModels
{
using Songhay.Office2010.Word;
using Songhay.OpenXml;
using Songhay.OpenXml.Models;
using Songhay.Wpf.Mvvm;
using Songhay.Wpf.Mvvm.ViewModels;
/// <summary>
/// View Model for the default Client
/// </summary>
public class ClientViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ClientViewModel"/> class.
/// </summary>
public ClientViewModel()
{
if(base.IsInDesignMode)
{
#region
this._flatOpcSourceString = ApplicationUtility
.LoadResource(
new Uri("/Songhay.Wpf.WordWalkingStick;component/PackedFiles/FlatOpcToHtml.xml",
UriKind.Relative));
this._xhtmlSourceString = ApplicationUtility
.LoadResource(
new Uri("/Songhay.Wpf.WordWalkingStick;component/PackedFiles/FlatOpcToHtml.html",
UriKind.Relative));
#endregion
}
else
{
this._flatOpcSourceString = "Loading…";
this._xhtmlSourceString = "Loading…";
//Receive MvvmLight message:
Messenger.Default.Register(this,
new Action<GenericMessage<TransformationMessage>>(
message =>
{
var tempDocFolder =
Environment.ExpandEnvironmentVariables("%UserProfile%/Desktop/");
var inputPath = tempDocFolder + "temp.docx";
var outputPath = tempDocFolder + "temp.html";
var flatOpcDoc =
XDocument.Parse(message.Content.TransformationResult);
OpenXmlUtility.TransformFlatToOpc(flatOpcDoc, inputPath);
this.FlatOpcSourceString = flatOpcDoc.Root.ToString();
var settings = new SonghayHtmlConverterSettings()
{
PageTitle = "My Page Title " + DateTime.Now.ToString("U"),
UseEntityMap = false
};
OpenXmlUtility.WriteHtmlFile(inputPath, outputPath, settings);
var xhtmlDoc = XDocument.Load(outputPath);
this.XhtmlSourceString = xhtmlDoc.Root.ToString();
}));
}
}
/// <summary>
/// Gets or sets the flat opc source string.
/// </summary>
/// <value>The flat opc source string.</value>
public string FlatOpcSourceString
{
get
{
return _flatOpcSourceString;
}
set
{
_flatOpcSourceString = value;
base.RaisePropertyChanged("FlatOpcSourceString");
}
}
/// <summary>
/// Gets or sets the XHTML source string.
/// </summary>
/// <value>The XHTML source string.</value>
public string XhtmlSourceString
{
get
{
return _xhtmlSourceString;
}
set
{
_xhtmlSourceString = value;
base.RaisePropertyChanged("XhtmlSourceString");
}
}
string _flatOpcSourceString;
string _xhtmlSourceString;
}
}
You can see that MVVM Light is messaging (not injecting) values into the constructor (Messenger.Default.Register) with its Messenger.
Request anything you want, via injection, using interfaces.
If you have settings shared across models, instantiate a singleton containing the values and expose them via ISomethingProvider and ISomethingEditor interfaces.
Here is what I do:
ViewModel needs to show a car window with car id passed as parameter:
ViewModel -> message to codebehind for view to open window. Message sends id.
Essentially in code behind:
var vm = new viewmodel(id);
var view = new view();
view.datacontext = vm;
view.show();
my viewmodel has a constructor that takes in an id.
In the case of writing tests against the viewmodel I sometimes create an overload of the viewmodel constructor that takes an ISomething as a parameter. I have the default constructor call the second one with a default implementation of ISomething. In case of the test I call the constructor with a test implementation. I know it's not the best method, because it creates a dependency between the two classes... but sometimes you'll have to take the easy path...
public class SomeViewModel
{
private ISomething internalSomething;
public void SomeViewModel():this(new Something()){}
public void SomeViewModel(ISomething something)
{
this.internalSomething = something;
}
}
Update
Creating a view in xaml can be like this:
<UserControl xmlns="...."
xmlns:Example="SomeNamespace">
<UserControl.DataContext>
<Example:SomeViewModel />
</UserControl.DataContext>
<Grid>
...
</Grid>
</UserControl>

Set hidden AttachedProperty through Style

I've got a problem using System.Windows.Interactivity.Interaction attached behavior class (from Expression Blend SDK 4). I'd like to define a pair of triggers for System.Windows.Window class in XAML Style element. But as the TriggersProperty field of System.Windows.Interactivity.Interaction class is private and there is no SetTriggers method in this class, I've got an error 'Set property System.Windows.Setter.Property threw an exception. -> Value cannot be null. Parameter name: property' when running the following code.
I really want to use the triggers and actions in styles, because I'd like to use them for my window-descendant control. Of course I can use my custom behavior or directly code my window-descendant class with triggers-analogue logic, but I'd like to use already existent triggers and actions of the expression library and my own, not declining them, simply because the TriggersProperty of Interaction class is hidden and I can't set it through style.
Is any workaround for the problem? With Reflection or someway other?
PS. I already tried to declare custom static class with TriggersProperty attached dependency property, registered with the help of AddOwner method, but no help - at the end it still tries to access the same TriggersProperty in the same System.Windows.Interactivity.Interaction class.
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:windowsInteractivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity">
<Window.Style>
<Style TargetType="Window">
<Setter Property="Title" Value="WindowStyleTest"/>
<Setter Property="windowsInteractivity:Interaction.Triggers">
<Setter.Value>
<windowsInteractivity:EventTrigger EventName="MouseDown"/>
</Setter.Value>
</Setter>
</Style>
</Window.Style>
</Window>
!!!Update!!!
Okay I took it a bit further. I extended the extension to perform all the work including setting the Triggers collection.
TriggerCollectionExtension
The Extension That does all the heavy lifting.
Note: The first time ProvideValue is called it will be from loading the style so the TargetValue is a Setter.
[ContentProperty("Triggers")]
public class TriggerCollectionExtension : MarkupExtension
{
public string EventName { get; set; }
public string CommandName { get; set; }
public object CommandParameter { get; set; }
public System.Windows.Interactivity.TriggerCollection Triggers { get; private set;}
public TriggerCollectionExtension()
{
var trigCollectionType =
typeof(System.Windows.Interactivity.TriggerCollection);
var triggers = (System.Windows.Interactivity.TriggerCollection)
trigCollectionType.GetConstructor(
BindingFlags. NonPublic | BindingFlags. Instance,
null, Type.EmptyTypes, null).Invoke (null);
// Cheat to get around this problem.
// must have IsFrozen set to false to modify
var methCreateCore = trigCollectionType.GetMethod("CreateInstanceCore",
BindingFlags.NonPublic | BindingFlags.Instance);
var cloneTriggers =
(System.Windows.Interactivity.TriggerCollection)
methCreateCore.Invoke(triggers, null);
this.Triggers = cloneTriggers;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as
IProvideValueTarget;
// The first time this is called is when loading the style.
// At that point the TargetObject is of type Setter.
// Return this (The MarkupExtension) and it will be reevaluated when the style
// is applied.
var hostcontrol = target.TargetObject as Control;
if (hostcontrol != null)
{
var cloneTriggers = this.Triggers;
var eventTrigger = new EventTrigger(this.EventName);
var trigbase = eventTrigger as TriggerBase;
trigbase.Attach(hostcontrol);
var commandAction = new CommandAction(hostcontrol, this.CommandName,
this.CommandParameter);
eventTrigger.Actions.Add(commandAction);
cloneTriggers.Add(eventTrigger);
Interaction.SetShadowTriggers(hostcontrol, this.Triggers);
return null;
}
else
{
return this;
}
return null;
}
}
Interaction
The re-ownership/exposure of the TriggersCollection.
<!-- language: c# -->
/// <summary>
/// Helps workaround the bug in the deployed interaction DLL.
/// The DependencyProperty is registered as ShadowTriggers and the Setter Getter is
/// SetTriggers() GetTriggers().
/// The result is compile error for XAML if anything but Shadowtriggers is used and
/// runtime error.
/// </summary>
public static class Interaction
{
static Interaction()
{
var interActionType = typeof(System.Windows.Interactivity.Interaction);
var triggersProperty = (DependencyProperty)interActionType.InvokeMember(
"TriggersProperty",
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.GetField,
null, null, null);
ShadowTriggersProperty = triggersProperty.AddOwner(typeof(Interaction));
}
public static readonly DependencyProperty ShadowTriggersProperty;
public static System.Windows.Interactivity.TriggerCollection
GetShadowTriggers(DependencyObject d)
{
return
(System.Windows.Interactivity.TriggerCollection)
d.GetValue(ShadowTriggersProperty);
}
public static void
SetShadowTriggers(
DependencyObject d,
System.Windows.Interactivity.TriggerCollection value)
{
d.SetValue(ShadowTriggersProperty, value);
}
}
CommandAction
A custom TriggerAction that looks up the Command on the DataContext.
<!-- language: c# -->
public class CommandAction : TriggerAction<FrameworkElement>
{
FrameworkElement control;
private string commandName;
object commandParameter;
private ICommand actualCommand;
public CommandAction(FrameworkElement control, string commandName,
object commandParameter)
{
this.control = control;
this.commandName = commandName;
this.commandParameter = commandParameter;
object datacontext;
if (this.FindDataContext(this.control, out datacontext))
{
var datacontextType = datacontext.GetType();
var propCommand = datacontextType.GetProperty(this.commandName);
this.actualCommand = propCommand.GetValue(datacontext, null) as ICommand;
}
}
private bool FindDataContext(FrameworkElement control, out object datacontext)
{
datacontext = default(object);
var parent = VisualTreeHelper.GetParent(control);
while (parent != null)
{
var parentFrame = parent as FrameworkElement;
if (parentFrame != null)
{
datacontext = parentFrame.DataContext;
if (datacontext != null)
{
return true;
}
}
var parentFrameContent = parent as FrameworkContentElement;
if (parentFrameContent != null)
{
datacontext = parentFrameContent.DataContext;
if (datacontext != null)
{
return true;
}
}
parent = VisualTreeHelper.GetParent(parent);
}
return false;
}
protected override void Invoke(object parameter)
{
if (this.actualCommand != null)
{
if (this.actualCommand.CanExecute(parameter))
{
this.actualCommand.Execute(parameter);
}
}
}
}
Wow long time reader first time posting code. I finally learned why the code doesn't always cut and paste so well. It took so many tries to submit this update.
I am sure there are reasons like disk space, parsing, or rendering speed, and the editor maintains state on failure to submit excellently.
I got it, why the error appears. That's because at runtime it's searching an Attached Dependency property by string name, that is "ShadowTriggers" (as it specified in System.Windows.Interactivity assembly, Interaction static constructor). So I created the own custom static class and inherit the Triggers Dependency Property from System.Windows.Interaction there (via Reflection and AddOwner, just exposed the property as ShadowTriggersProperty). It worked! But... Now I have to provide a TriggerCollection instance to the Style's Property Value Setter, and the constructor of the class is internal. Suppose it is a no way further.

AutoMapper Ignore an item in a collection based on a property value in the source

I'm mapping an ApplianceViewModel to a ApplianceDTO. Each Appliance has a collection of ActionViewModels which are mapped to ActionDTO. What I'd like to do is configure the mapper to ignore ActionViewModels whose IsPersisted value is False.
My ViewModel classes ...
public interface IApplianceViewModel : INotifyPropertyChanged
{
ObservableCollection<IActionViewModel> Actions { get; set; }
// other properties removed for simplicity
}
public interface IActionViewModel : INotifyPropertyChanged
{
bool IsPersisted { get; set; }
// other properties removed for simplicity
}
My DTO classes ...
public class ApplianceDTO
{
public IEnumerable<ActionDTO> Actions { get; set; }
// other properties removed for simplicity
}
public class ActionDTO
{
// properties removed for simplicity
}
I set up my mapping like this ...
Mapper.CreateMap<IApplianceViewModel, ApplianceDTO>();
Mapper.CreateMap<IActionViewModel, ActionDTO>()
var appliance = new ApplianceViewModel {
Actions = new ObservableCollection<IActionViewModel>(
new List<IActionViewModel> {
new ActionViewModel { IsPersisted = true },
new ActionViewModel { IsPersisted = false }
}};
var applianceDTO = Mapper.Map<IApplianceViewModel, ApplianceDTO>(applianceDTO);
Currently my applianceDTO will have two items in it's Actions collection, but I'd like to set up my mapping so that the ApplianceActionViewModel with the IsPersisted property set to false isn't mapped. Can I do this?
Update
Omu's comment lead me to a solution using a ValueResolver to map the collection of Actions. I'm not really happy with this solution but its the best option available.
First I created a custom ValueResolver.
public class IsPersistedCollectionResolver : ValueResolver<IApplianceViewModel, IEnumerable<ActionDTO>>
{
protected override IEnumerable<ActionDTO> ResolveCore(IApplianceViewModel source)
{
return Mapper.Map<IEnumerable<IActionViewModel>, IEnumerable<ActionDTO>>(source.Actions.Where(x => x.IsPersisted));
}
}
Then I modified my code to use it in the mapping configuration.
Mapper.CreateMap<IApplianceViewModel, ApplianceDTO>()
.ForMember(dest => dest.Actions, opt => opt.ResolveUsing<IsPersistedCollectionResolver>());
Mapper.CreateMap<IActionViewModel, ActionDTO>();
have you tried doing something like :
Mapper.map(objects.Where(o => o.IsPersisted == true))

Validating method arguments with Data Annotation attributes

The "Silverlight Business Application" template bundled with VS2010 / Silverlight 4 uses DataAnnotations on method arguments in its domain service class, which are invoked automagically:
public CreateUserStatus CreateUser(RegistrationData user,
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[RegularExpression("^.*[^a-zA-Z0-9].*$", ErrorMessageResourceName = "ValidationErrorBadPasswordStrength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
[StringLength(50, MinimumLength = 7, ErrorMessageResourceName = "ValidationErrorBadPasswordLength", ErrorMessageResourceType = typeof(ValidationErrorResources))]
string password)
{ /* do something */ }
If I need to implement this in my POCO class methods, how do I get the framework to invoke the validations OR how do I invoke the validation on all the arguments imperatively (using Validator or otherwise?).
We have approached it like this:
We have a ValidationProperty class that takes in a RegularExpression that will be used to validate the value (you could use whatever you wanted).
ValidationProperty.cs
public class ValidationProperty
{
#region Constructors
/// <summary>
/// Constructor for property with validation
/// </summary>
/// <param name="regularExpression"></param>
/// <param name="errorMessage"></param>
public ValidationProperty(string regularExpression, string errorMessage)
{
RegularExpression = regularExpression;
ErrorMessage = errorMessage;
IsValid = true;
}
#endregion
#region Properties
/// <summary>
/// Will be true if this property is currently valid
/// </summary>
public bool IsValid { get; private set; }
/// <summary>
/// The value of the Property.
/// </summary>
public object Value
{
get { return val; }
set
{
if (this.Validate(value))//if valid, set it
{
val = value;
}
else//not valid, throw exception
{
throw new ValidationException(ErrorMessage);
}
}
}
private object val;
/// <summary>
/// Holds the regular expression that will accept a vaild value
/// </summary>
public string RegularExpression { get; private set; }
/// <summary>
/// The error message that will be thrown if invalid
/// </summary>
public string ErrorMessage { get; private set; }
#endregion
#region Private Methods
private bool Validate(object myValue)
{
if (myValue != null)//Value has been set, validate it
{
this.IsValid = Regex.Match(myValue.ToString(), this.RegularExpression).Success;
}
else//still valid if it has not been set. Invalidation of items not set needs to be handled in the layer above this one.
{
this.IsValid = true;
}
return this.IsValid;
}
#endregion
}
Here's how we would create a Validation property. Notice how the public member is a string, but privately I am using a 'ValidationProperty.'
public string TaskNumber
{
get { return taskNumber.Value.ToString(); }
set
{
taskNumber.Value = value;
OnPropertyChanged("TaskNumber");
}
}
private ValidationProperty taskNumber;
Now, whenever the value is set, the business layer will validate that it's a valid value. If it's not, it will simply throw a new ValidationException (in the ValidationProperty class). In your xaml, you will need to set NotifyOnValidationError & ValidatesOnExceptions to true.
<TextBox Text="{Binding TaskNumber, Mode=TwoWay, NotifyOnValidationError=True, ValidatesOnExceptions=True}"/>
With this approach, you would probably have a form for creating a new 'User' and each field would valitate each time they set it (I hope that makes sense).
This is the approach that we used to get the validation to be on the business layer. I'm not sure if this is exactly what you're looking for, but I hope it helps.

Resources