Is there differences (performance, behavior) between these ways of properties binding?
private string _texto;
public string Texto
{
get => _texto;
set
{
Set(ref _texto, value);
NomeHome = _texto;
}
}
public string Texto
{
get => _texto;
set
{
_texto = value;
NotifyOfPropertyChange(() => Texto);
}
}
I doubt there is a difference in performance but it could be argued using the Set method is using code that has been tested to perform correctly and therefore less prone to bugs. At some point you may well forget to put the NotifyOfPropertyChange call into a property.
Related
I'm building a c# class that works with two different data sources. It will load a data source and take a configuration set from a function. Then I want to do several tasks on all properties within the object.
for example.
public String StreetAddress
{
get { return _streetAddress; }
set
{
if (value.Length <= 64)
_streetAddress = value;
else
_streetAddress = value.Substring(0, 1024).Trim();
}
}
public String City
{
get { return _city; }
set
{
if (value.Length <= 128)
_city = value;
else
_city = value.Substring(0, 128).Trim();
}
}
public String State
{
get { return _state; }
set
{
if (value.Length <= 128)
_state = value;
else
_state = value.Substring(0, 128).Trim();
}
}
So that holds the data from one side. I was hoping to be able to store and set a change flag on each property. So if we take State for example. If the person is moved from Texas to Illinois I want to set a bool within that property to note the change then be able to loop over all changes before saving the object to the DB. But I don't see any way to assign another state variable within that property. Is the best way to write another object on top of this to control it or is there another more creative way to store multiple strings within the one property?
If you'd like an OOP way of doing the thing, you can:
Define an interface and a class for holding your property, such as:
interface IPropertySlot
{
bool IsDirty { get; }
void ResetIsDirty();
object UntypedValue { get; }
}
class PropertySlot<T>:IPropertySlot
{
public T Value { get; private set; }
public bool SetValue(T value)
{
if (!Equals(_value, Value))
{
Value = value;
IsDirty = true;
return true;
}
return false;
}
public bool IsDirty { get; private set; }
public void ResetIsDirty()
{
IsDirty = false;
}
public object UntypedValue
{
get { return Value; }
}
}
Store your properties inside your class in a dictionary from String (for name of property) to IPropertySlot and get/set them through a pair of methods:
void SetProperty<T>(string name, T value)
{
IPropertySlot property;
if (!_properties.TryGetValue(name, out property))
{
property = new PropertySlot<T>();
_properties[name] = property;
}
((PropertySlot<T>)property) .SetValue(value);
}
T GetProperty<T>(string name)
{
IPropertySlot property;
if (!_properties.TryGetValue(name, out property))
{
property = new PropertySlot<T>();
_properties[name] = property;
}
return ((PropertySlot<T>)property).Value;
}
Finding the changed properties later is just a matter of going over the _properties.Values and finding which of them are IsDirty.
This approach also gives you a way to add more functionality to your properties in an OO manner (such as raising PropertyChanged/PropertyChanging events, mapping it to DB fields, etc.).
In such a situation I'd prefer an approach external to the Dto implementation.
Implement some unit that would take two instances of a class, and determine all the differences.
Map each property to compare:
static PropertyManager<Dto> manager = new PropertyManager<Dto>()
.Map(x => x.City)
.Map(x => x.StreetAddress);
Use two instances to compute difference:
var a = new Dto{ StreetAddress = "Foo", City = "Bar" };
var b = new Dto{ StreetAddress = "Foo", City = "Baz" };
var differences = manager.ComputeDifferences(a,b).ToList();
if( differences.Any() )
{
Console.WriteLine("Instances differ");
}
foreach (var diff in differences)
{
Console.WriteLine(diff);
}
This sample code prints out:
Instances differ
x.City
Here is a complete code example:
https://dotnetfiddle.net/4sNeoN
How do I use a combination of booleans values, to set the enabled property on a MvxBind button?
For example:
Using one boolean value, the binding is achieved with:
<Button
android:text="Next"
local:MvxBind="Enabled IHaveDoneEverything"/>
But how do I implement this using multiple boolean values?
Things I've tried that didn't work:
Using an OR statement in axml. local:MvxBind="Enabled (IHaveDoneThis | IHaveDoneThat)"
Using an extra property in my ViewModel. This didn't work due to the property not being 'set' and thus not being updated in the view.
public bool IHaveDoneAtleastSomething
{
get { return (IHaveDoneThis | IHaveDoneThat); }
}
Using a custom valueconverter.
local:MvxBind="Enabled [IHaveDoneThis , IHaveDoneThat], Converter=MultipleBooleansToOneBooleanUsingORValueConverter"/>
Using || instead of | actually resolved this issue.
local:MvxBind="Enabled (IHaveDoneThis || IHaveDoneThat)"
For future reference, in order to get the extra ViewModel property working you would have to make sure that the ViewModel calls the RaisePropertyChanged method for the extra property whenever IHaveDoneThis or IHaveDoneThat changed otherwise the binding won't trigger.
What I usually do is something like:
private bool _internalIHaveDoneThis;
public bool IHaveDoneThis
{
get{return _internalIHaveDoneThis;}
set
{
if(_internalIHaveDoneThis != value)
{
_internalIHaveDoneThis = value;
RaisePropertyChanged(() => IHaveDoneThis);
RaisePropertyChanged(() => IHaveDoneAtleastSomething);
}
}
}
private bool _internalIHaveDoneThat;
public bool IHaveDoneThat
{
get{return _internalIHaveDoneThat;}
set
{
if(_internalIHaveDoneThat != value)
{
_internalIHaveDoneThat = value;
RaisePropertyChanged(() => IHaveDoneThat);
RaisePropertyChanged(() => IHaveDoneAtleastSomething);
}
}
}
public bool IHaveDoneAtleastSomething
{
get { return (IHaveDoneThis | IHaveDoneThat); }
}
I am having problems getting and setting a value because I have introduced a third aspect into my code.
Previously I would do this to get/set in the record:
public virtual string MyString { get; set;}
and then in my part:
public string MyString
{
get { return Record.MyString; }
set { Record.MyString = value; }
}
and NHibernate would save my values in the DB (obviously my other code is not presented here for the sake of brevity).
Now I am trying to do a checkbox with a list. I have this for a checkbox:
public class MyPart : ContentPart<MyPartRecord>
{
public MyPart()
{
MyList = Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>().Select(x =>
{
return new SelectListItem { Text = x.ToString().ToUpper(),
Value = ((int)x).ToString() };
}).ToList();
}
public IList<SelectListItem> MyList { get; set; }
private string myCheckBox;
// Record class contains the following commented code:
// public virtual string MyCheckBox { get; set;}
// Trying to do this now here in MyPart class:
public string MyCheckBox
{
get
{
if (!string.IsNullOrEmpty(myCheckBox))
return myCheckBox;
// Tried the following commented code to get value:
// Record.MyCheckBox = myCheckBox;
return string.Join(",", MyList.Where(x => x.Selected)
.Select(x => x.Value).ToArray());
}
set
{
myCheckBox = value;
// Tried the following commented code to set value:
// Record.MyCheckBox = myCheckBox;
}
}
}
I just don't know how to assign the values in this case (getting/setting myCheckBox into MyCheckBox. It's being saved in the DB as null.
Thanks in advance for any help.
It seems to me you'd be hiding the base virtual implementation of MyCheckBox.
I think instead you'd rather override the base:
public override String MyCheckBox
{
get
{
if (!string.IsNullOrEmpty(myCheckBox))
return myCheckBox;
// Tried the following commented code to get value:
// Record.MyCheckBox = myCheckBox;
return string.Join(",", MyList.Where(x => x.Selected)
.Select(x => x.Value).ToArray());
}
set
{
myCheckBox = value;
// Tried the following commented code to set value:
// Record.MyCheckBox = myCheckBox;
}
}
Thereby becoming the variable instead of obfuscating it?
I wound up just getting rid of the part and sticking with only a record - this allowed me to do the get and set in a view model, which worked out better (and was less confusing).
Suppose I have the following class:
public class Person : ReactiveObject, IEditableObject
{
private string name;
private string nameCopy;
public string Name
{
get { return this.name; }
set { this.RaiseAndSetIfChanged(ref this.name, value); }
}
public void BeginEdit()
{
this.nameCopy = this.name;
}
public void CancelEdit()
{
this.name = this.nameCopy;
}
public void EndEdit()
{
}
}
Now suppose I want to create an observable sequence (of Unit) that "ticks" whenever a change is committed to Name. That is, I only care about changes to Name that occur between a call to BeginEdit and and a subsequent call to EndEdit. Any changes prior to a call to CancelEdit should be ignored and the sequence should not tick.
I'm struggling to get my head around how I would do this with Rx. It seems I would need state in the pipeline somewhere in order to know whether the change occurred during the window of BeginEdit/EndEdit calls. I suppose I could timestamp everything and compare timestamps, but that seems a nasty hack.
I came pretty close using a dedicated Subject for edit actions along with Observable.Merge:
public class Person : ReactiveObject, IEditableObject
{
private readonly Subject<EditAction> editActions;
private readonly IObservable<Unit> changedDuringEdit;
private string name;
private string nameCopy;
public Person()
{
this.editActions = new Subject<EditAction>();
var nameChanged = this.ObservableForProperty(x => x.Name).Select(x => x.Value);
var editBeginning = this.editActions.Where(x => x == EditAction.Begin);
var editCommitted = this.editActions.Where(x => x == EditAction.End);
this.changedDuringEdit = nameChanged
.Buffer(editBeginning, _ => editCommitted)
.Where(x => x.Count > 0)
.Select(_ => Unit.Default);
}
public IObservable<Unit> ChangedDuringEdit
{
get { return this.changedDuringEdit; }
}
public string Name
{
get { return this.name; }
set { this.RaiseAndSetIfChanged(ref this.name, value); }
}
public void BeginEdit()
{
this.editActions.OnNext(EditAction.Begin);
this.nameCopy = this.name;
}
public void CancelEdit()
{
this.editActions.OnNext(EditAction.Cancel);
this.Name = this.nameCopy;
}
public void EndEdit()
{
this.editActions.OnNext(EditAction.End);
}
private enum EditAction
{
Begin,
Cancel,
End
}
}
However, if several changes are cancelled, and then one is committed, the observable ticks several times on commit (once for each prior cancellation, and once again for the commit). Not to mention the fact that I get a List<Unit> which I don't actually need. In a way, this would still satisfy my use case, but not my curiosity or sense of code aesthetic.
I feel like Join should solve this fairly elegantly:
var nameChanged = this.ObservableForProperty(x => x.Name).Select(_ => Unit.Default);
var editBeginning = this.editActions.Where(x => x == EditAction.Begin);
var editCommitted = this.editActions.Where(x => x == EditAction.End);
var editCancelled = this.editActions.Where(x => x == EditAction.Cancel);
var editCancelledOrCommitted = editCancelled.Merge(editCommitted);
this.changedDuringEdit = editBeginning
.Join(nameChanged, _ => editCancelledOrCommitted, _ => editCancelledOrCommitted, (editAction, _) => editAction == EditAction.End)
.Where(x => x)
.Select(_ => Unit.Default);
But this doesn't work either. It seems Join is not subscribing to editCancelledOrCommitted, for reasons I don't understand.
Anyone have any ideas how to go about this cleanly?
Here's how I'd do it:
IObservable<Unit> beginEditSignal = ...;
IObservable<Unit> commitSignal = ...;
IObservable<Unit> cancelEditSignal = ...;
IObservable<T> propertyChanges = ...;
// this will yield an array after each commit
// that has all of the changes for that commit.
// nothing will be yielded if the commit is canceled
// or if the changes occur before BeginEdit.
IObservable<T[]> commitedChanges = beginEditSignal
.Take(1)
.SelectMany(_ => propertyChanges
.TakeUntil(commitSignal)
.ToArray()
.Where(changeList => changeList.Length > 0)
.TakeUntil(cancelEditSignal))
.Repeat();
// if you really only want a `Unit` when something happens
IObservable<Unit> changeCommittedSignal = beginEditSignal
.Take(1)
.SelectMany(_ => propertyChanges
.TakeUntil(commitSignal)
.Count()
.Where(c => c > 0)
.Select(c => Unit.Default)
.TakeUntil(cancelEditSignal))
.Repeat();
You have a timing problem that I don't think you have articulated yet; when are you hoping for the changes to tick?
either as they occur
once the commit happens
The clear and obvious problem with 1) is that you don't know if the changes will be committed, so why would you raise them. IMO, this only leaves option 2). If the change is cancelled, then no event is raised.
Next question I have is, do you want each change raised? ie. for the process
[Begin]-->[Name="fred"]-->[Name="bob"]-->[Commit]
Should this raise 1 or 2 events when the Commit is made? As you are only pushing the token type Unit, it seems redundant to push two values. This now leads me to think that you just want to push a Unit value when EndEdit() is executed and the values have changed.
This leaves us with a painfully simple implementation:
public class Person : ReactiveObject, IEditableObject
{
private readonly ISubject<Unit> changedDuringEdit = new Subject<Unit>();
private string name;
private string nameCopy;
public string Name
{
get { return this.name; }
set { this.RaiseAndSetIfChanged(ref this.name, value); }
}
public void BeginEdit()
{
this.nameCopy = this.name;
}
public void CancelEdit()
{
this.name = this.nameCopy;
}
public void EndEdit()
{
if(!string.Equals(this.nameCopy, this.name))
{
changedDuringEdit.OnNext(Unit.Default);
}
}
public IObservable<Unit> ChangedDuringEdit
{
get { return this.changedDuringEdit.AsObservable(); }
}
}
Is this what you are looking for? If not can you help me understand the complexities I am missing? If it is then I would be keen to flesh this out so that I wasn't recommending using Subjects :-)
Consider these properties,
double _temperature;
public double Temperature
{
get { return _temperature; }
set { _temperature = value; }
}
double _humidity;
public double Humidity
{
get { return _humidity; }
set { _humidity = value; }
}
bool _isRaining;
public bool IsRaining
{
get { return _isRaining; }
set { _isRaining = value; }
}
And now I want to make a list/collection/container of properties like this,
PropertyContainer.Add(Temperature); //Line1
PropertyContainer.Add(Humidity); //Line2
PropertyContainer.Add(IsRaining); //Line3
I want to make this such that later on I may be able to access the current values of properties using index, something like this,
object currentTemperature = PropertyContainer[0];
object currentHumidity = PropertyContainer[1];
object currentIsRaining = PropertyContainer[2];
But obviously, this is not going to work, since PropertyContainer[0] will return the old value - the value which Temperature had at the time of adding Temperature to the container (see the Line1 above).
Is there any solution to this problem? Basically I want to access current values of properties uniformly; the only thing that can change is, the index. The index however could be string as well.
PS: I don't want to use Reflection!
Well, you could use Lambdas:
List<Func<object>> PropertyAccessors = new List<Func<object>>();
PropertyAccessors.Add(() => this.Temperature);
PropertyAccessors.Add(() => this.Humidity);
PropertyAccessors.Add(() => this.IsRaining);
then you could to this:
object currentTemperature = PropertyAccessors[0]();