I am working on a WPF application that follows an MVVM pattern. Inspite of moving the validation into services, I am ending up with a fat viewmodels that runs for several lines of code(in my case close to 1000 lines).
I have added the interface for the viewmodel here. I got a few collections exposed as combo and based on the combo selection, I have to perform validation/invoke service/apply filtering to other combos
public interface ISampleViewModel {
ObservableCollection<InstrumentDto> Collection1 { get; set; }
ObservableCollection<TenderViewConfigDetailViewModel> Collection2 { get; set; }
ObservableCollection<TenderViewConfigDetailViewModel> Collection3 { get; set; }
ObservableCollection<TenderViewConfigDetailViewModel> Collection4 { get; set; }
ObservableCollection<TenderViewConfigDetailViewModel> Collection5 { get; set; }
TenderViewConfigDetailViewModel SelectedViewConfigDetail { get; set; }
int SelectedTenderViewIndex { get; set; }
int SelectedInstrumentsViewIndex { get; set; }
SortableCollection<TenderViewToInstrumentViewModel> CurrentInstruments { get; set; }
TenderViewToInstrumentViewModel SelectedInstrumentForTenderView { get; set; }
InstrumentDto SelectedInstrument { get; set; }
bool IsAllInstrumentsFocused { get; set; }
ICommand ApplyChangesCommand { get; }
ICommand AddTenderPanelViewCommand { get; }
ICommand DeleteTenderPanelViewCommand { get; }
ICommand ModifyTenderViewVisiblityCommand { get; }
ICommand AddInstrumentsToPanelViewCommand { get; }
ICommand DeleteInstrumentsFromPanelViewCommand { get; }
ICommand MoveUpTenderListViewCommand { get; }
ICommand MoveDownTenderListViewCommand { get; }
ICommand MoveUpInstrumentsCommand { get; }
ICommand MoveDownInstrumentsCommand { get; }
bool IsValidModel { get; }
void PublishTenderViewConfigChanges(TenderViewConfigDetailViewModel viewModel,EventActionType actionType);
}
The above set of functionality make my viewmodel bulkier. How can I avoid avoid it? I cant think of breaking the functionality into smaller controls as they are dependent? Am I missing something here?
If you have stored in the ViewModel properties that can be isolated in separate classes, it is best to move them to a separate Model. Large number of properties pretty loads ViewModel, for each type of properties you should create your Model. Although on this occasion there is some debate, I believe there is nothing wrong if in the ViewModel will be links to several Models. On this subject you can see this answers:
In MVVM, is every ViewModel coupled to just one Model?
Example of using separate models:
Model
public class MainMenuModel : NotificationObject // Here also implemented INotifyPropertyChanged interface
{
private bool _buttonIsEnabled = true;
public bool ButtonIsEnabled
{
get
{
return _buttonIsEnabled;
}
set
{
_buttonIsEnabled = value;
NotifyPropertyChanged("ButtonIsEnabled");
}
}
}
ViewModel
public class MainMenuViewModel
{
private MainMenuModel _mainMenuModel = null;
public MainMenuModel MainMenuModel
{
get
{
return _mainMenuModel;
}
set
{
_mainMenuModel = value;
}
}
...
public MainMenuViewModel()
{
MainMenuModel = new MainMenuModel();
}
}
View
<Button IsEnabled="{Binding Path=MainMenuModel.ButtonIsEnabled}" ... />
The only thing that can be left on the side of the ViewModel, it Commands and IDataErrorInfo interface implementation, although implementation of IDataErrorInfo can also be moved to the side of the Model.
Also, if the implementation of Command takes a lot of space, you can create separate function / procedure that can be called such Helper and place in suitable class. Next, in Command did not write the whole implementation, it's necessary refer to this method.
For example:
private ICommand _findCommand = null;
public ICommand FindCommand
{
get
{
if (_findCommand == null)
{
_findCommand = new RelayCommand(param => this.Find(), null);
}
return _findCommand;
}
}
private void Find()
{
// Here instead of writing large code,
// moving find logic to separate static class
SomeHelper.FindPerson(MainModel.SearchName);
}
Therefore Command in this case is a wrapper for a call method in ViewModel.
Related
When editing the c# programs I have the top that includes namespaces with the using keyword. Such as using System;.
I have some classes that are redundantly implemented across my WPF application. I would like to create a single reference point to use the methods within the whole application.
for instance when I perform data binding for ComboBoxes I have a class that includes this...
public class ComboBoxItemSource : ViewModelBase
{
public ObservableCollection<Item> Source { get; set; }
public Item Selected { get; set; }
}
I would like to have this in one place and include it in a reference rather than create it in each ViewModel that will need a combobox binding.
I'd recommend to just put the class in a separate file and add it to your project.
Or even simpler:
Right click on your project in the Solution Explorer -> "Add new Item" -> "Class".
Copy/Paste your class into the file
Adjust the namespace
Add the missing usings to the file (especially the using for the "View Model Base")
The class including the namespace in the file will now look similar to this:
namespace MyNamespace
{
public class ComboBoxItemSource : ViewModelBase
{
public ObservableCollection<Item> Source { get; set; }
public Item Selected { get; set; }
}
}
You could do something like this.
Create an IComboBox interface:
public interface IComboBox
{
ObservableCollection<Item> Source { get; set; }
Item Selected { get; set; }
}
then you could create a Class that inherits from your ViewModelBase and implements your IComboBox:
public class ComboBoxVM :ViewModelBase, IComboBox
{
public ObservableCollection<Item> Source
{
get
{
// do stuff
return _source;
}
set { _source = value; }
}
public Item Selected
{
get
{
// do stuff
return _selected;
}
set { _selected = value; }
}
}
then in your ComboBoxItemSource, inherit from ComboBoxVM:
public class ComboBoxItemSource : ComboBoxVM
{
}
Hopefully this helps.
This is one of the class definitions within a DLL I use in my WCF service.
[DataContract]
public class ScenarioXML
{
[DataMember(Order = 1)]
public long? TNRScenario { get; set; }
[DataMember(Order = 2)]
public long? TNRProject { get; set; }
[DataMember(Order = 3)]
public int? Priority { get; set; }
// ...
[DataMember(Order = 19)]
public List<ScenarioAssetXML> ScenarioAssets { get; set; }
[DataMember(Order = 20)]
public List<CalendarXML> Calendars { get; set; }
[DataMember(Order = 21)]
public ScenarioTriggerCollectionXML ScenarioTriggerCollection { get; set; }
}
I'm using DataContract instead of ProtoContract, so I can expose this class to a Silverlight project through a WSDL, and still use Protobuf-net for serialization.
Now, when I use the following code in my WCF service, the original "scenario" and the "restoredModel" are identical.
MemoryStream msTestString = new MemoryStream();
Serializer.Serialize<ScenarioXML>(msTestString, scenario);
string memento = Convert.ToBase64String(msTestString.ToArray());
byte[] byteAfter64 = Convert.FromBase64String(memento);
MemoryStream afterStream = new MemoryStream(byteAfter64);
ScenarioXML restoredModel = Serializer.Deserialize<ScenarioXML>(afterStream);
However, when I use the same code in Silverlight, the TNRScenario value is null.
Similarly, the TNRScenarioAsset property of the objects in the ScenarioAssets list are null.
[DataContract]
public class ScenarioAssetXML
{
[DataMember(Order = 1)]
public long? TNRScenarioAsset { get; set; }
[DataMember(Order = 2)]
public long? TNRScenario { get; set; }
[DataMember(Order = 3)]
public string Asset { get; set; }
[DataMember(Order = 4)]
public string Action { get; set; }
}
When I make the first property a string, it completely vanishes after (de)serialization. When I put a dummy bool as a first property, the bool is there, but the second property, in this case ScenarioAssets, is still null. There's something weird going on here...
Am I doing somethign wrong, or is this a bug?
Edit:
You're right Marc! The orders get messed up in the WSDL-generated code.
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="ScenarioXML", Namespace="http://schemas.datacontract.org/2004/07/DataCollectionDLL")]
public partial class ScenarioXML : object, System.ComponentModel.INotifyPropertyChanged {
private System.Nullable<long> TNRScenarioField;
private System.Nullable<long> TNRProjectField;
private System.Nullable<int> PriorityField;
//...
[System.Runtime.Serialization.DataMemberAttribute()]
public System.Nullable<long> TNRScenario {
get {
return this.TNRScenarioField;
}
set {
if ((this.TNRScenarioField.Equals(value) != true)) {
this.TNRScenarioField = value;
this.RaisePropertyChanged("TNRScenario");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute(Order=1)]
public System.Nullable<long> TNRProject {
get {
return this.TNRProjectField;
}
set {
if ((this.TNRProjectField.Equals(value) != true)) {
this.TNRProjectField = value;
this.RaisePropertyChanged("TNRProject");
}
}
}
[System.Runtime.Serialization.DataMemberAttribute(Order=2)]
public System.Nullable<int> Priority {
get {
return this.PriorityField;
}
set {
if ((this.PriorityField.Equals(value) != true)) {
this.PriorityField = value;
this.RaisePropertyChanged("Priority");
}
}
}
//...
However, I'm not sure how to correctly implement that partial class? I created it in the WCF service, but that seems to confuse the compiler. Getting the following errors:
Error 6 'DataCollectionDLL.ScenarioXML' does not contain a definition for 'TNRScenario' and no extension method 'TNRScenario' accepting a first argument of type 'DataCollectionDLL.ScenarioXML' could be found (are you missing a using directive or an assembly reference?)
Error 2 Cannot convert type 'DataCollectionDLL.ScenarioXML [c:\Projects\Flowcontrol 1.7.1.1\flowcontrolFC.Web\Libraries\DataCollectionDLL.dll]' to 'DataCollectionDLL.ScenarioXML [C:\Projects\Flowcontrol 1.7.1.1\flowcontrolFC.Web\DAL\DataCollectionClasses\ScenarioXML.cs(31)]'
Then tried it in the Silverlight project, which compiles fine but that doesn't solve the problem. Results are the same.
The partial class I created:
namespace DataCollectionDLL
{
[ProtoContract]
[ProtoPartialMember(1, "TNRScenario")]
[ProtoPartialMember(2, "TNRProject")]
[ProtoPartialMember(3, "Priority")]
//...
[ProtoPartialMember(21, "ScenarioTriggerCollection")]
partial class ScenarioXML
{
}
}
It sounds like you used WSDL-generated proxies; that can confuse things a little bit, because protobuf-net really really cares what the numbers are, and WSDL can sometimes play fast and loose with those. It would really help if I could see the WSDL-generated proxy classes (in the .designer.cs), but I'm going to assume this is the problem. Fortunately, most code-generators use partial class, so you can add your own partial class in a separate file to add extra information into the same type, in particular: more attributes. For example:
namespace The.Same.Namespace
{
[ProtoContract]
[ProtoPartialMember(1, "TNRScenario")]
[ProtoPartialMember(2, "TNRProject")]
// ...
[ProtoPartialMember(21, "ScenarioTriggerCollection")]
partial class ScenarioXML { }
}
This will get merged by the compiler into the ScenarioXML class, and should allow protobuf-net to use the correct numeric identifiers for each property.
I'm using the following technologies: WinForms, Entity Framework 4.4 (5.0 on .NET 4.0), DBContext
I have (what I think is) a very simple Master/Details form, that actually worked just fine before I upgraded from EF 4.0 to EF 4.4. Now, for some reason, the Details DataGridView simply doesn't populate with any data!
Here's my auto-generated schema code:
public partial class RoadMapping
{
public RoadMapping()
{
this.RoadCharacteristics = new HashSet<RoadCharacteristic>();
}
public int RoadMappingID { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public virtual ICollection<RoadCharacteristic> RoadCharacteristics { get; set; }
}
public partial class RoadCharacteristic
{
public RoadCharacteristic()
{
}
public int RoadCharacteristicID { get; set; }
public int RoadMappingID { get; set; }
public string Value { get; set; }
public string Description { get; set; }
public virtual RoadMapping RoadMapping { get; set; }
}
Here's my code that was working with EF 4.0:
SATContext = new SafetyAssessmentToolEntities();
dataGridViewMappings.DataSource = bindingSourceMappings;
dataGridViewDetails.DataSource = bindingSourceDetails;
bindingSourceMappings.DataSource = SATContext.RoadMappings;
bindingSourceDetails.DataSource = bindingSourceMappings;
bindingSourceDetails.DataMember = "RoadCharacteristics";
Here's the code that isn't working with EF 4.4:
SATContext = new SafetyAssessmentToolEntities();
SATContext.RoadMappings.Load();
SATContext.RoadCharacteristics.Load();
dataGridViewMappings.DataSource = bindingSourceMappings;
dataGridViewDetails.DataSource = bindingSourceDetails;
bindingSourceMappings.DataSource = SATContext.RoadMappings.Local.ToBindingList();
bindingSourceDetails.DataSource = bindingSourceMappings;
bindingSourceDetails.DataMember = "RoadCharacteristics";
Please note that bindingSourceMappings and bindingSourceDetails are declared by the form designer.
I know there are a lot of more advanced and code-intensive ways to make this work, but I can't understand why this very simple way of doing it won't work anymore.
Any suggestions?
public partial class SafetyAssessmentToolEntities : DbContext
{
public SafetyAssessmentToolEntities()
: base("name=SafetyAssessmentToolEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<RoadCharacteristic> RoadCharacteristics { get; set; }
public DbSet<RoadMapping> RoadMappings { get; set; }
}
I am having problems databinding to EF code first. I need to be using Eager Loading, but I am running into some issues with databinding. I have the following classes:
public class Context : DbContext
{
DbSet<A> As;
DbSet<B> Bs;
DbSet<C> Cs;
}
public class A
{
public ICollection<B> Bs { get; set; }
public string Name { get; set; }
}
public class B
{
public ICollection<C> Cs { get; set; }
public string Name { get; set; }
}
public class C
{
public string Name { get; set; }
}
I am data binding Context.As to a Treeview, using the below code:
Context.As.Load();
tvItems.ItemsSource = Context.As.Local;
This works as expected, however, it does not automatically load the child properties, Bs, and subsequently, Cs. So, I found that lazy loading can help with this, like so:
Context.As.Load();
tvItems.ItemsSource = Context.As.Include(u=>u.Bs);
From my reading, this should automatically load at least the first level of child properties. However, this will not data bind, as I did not use .Local
.Include() returns IQueryable, which does not support .Local. I can use .ToList(), but this will not automatically update when I add items.
So, how the hell am I supposed to be doing this?
You could try this:
Context.As.Include(a => a.Bs).Load();
tvItems.ItemsSource = Context.As.Local;
I'm migrating from WinForms world to WPF with MVVM.
My base system works with POCO classes (NHibernate) and we use some DynamicProxy to map this POCO classes into some bindable so the ModelView can expose a proxy of POCO and save a lot of code:
public class OrderViewModel
{
public OrderPOCO Order { get; private set; }
}
public class OrderView
{
public OrderView()
{
DataContext = DynamicProxy(new OrderViewModel(new OrderPOCO()));
}
}
public class OrderPOCO
{
public virtual int Number { get; set; };
public virtual IList<OrderItemPOCO> Items { get; set; };
}
public class OrderItemPOCO
{
public virtual decimal Qty { get; set; }
public virtual decimal Price { get; set; }
public virtual decimal Amount { get; set; }
}
The collection of OrderItemPOCO is binded into a grid. The Amount is a calculated property that depends of some complex rules (I can't put it in the POCO as it's not a simple Amount = Qty * Price).
Sure I can expose in the ViewModel a custom OrderItemViewModel and a collection of OrderItemViewModel but I will need to recode my POCO classes. How I can code this kind of situation in MVVM without recode all my Model?
You are right, you need an OrderItemViewModel. But you don't have to rewrite you model classes, it will remain the same. What you need is something like this:
public class OrderViewModel
{
public OrderViewModel(OrderPOCO order)
{
Order = order;
Items = new ObservableCollection<OrderItemViewModel>(order.Items.Select(o => new OrderItemViewModel(o)).ToArray());
Items.CollectionChanged += OnItemsCollectionChanged;
}
public OrderPOCO Order { get; private set; }
public ObservableCollection<OrderItemViewModel> Items { get; private set; }
private void OnItemsCollectionChanged(object sender, CollectionChangedEventArgs e)
{
// Synchronize this.Items with order.Items
}
}
public class OrderItemViewModel
{
public OrderItemPOCO OrderItem { get; private set; }
}
public class OrderPOCO
{
public virtual int Number { get; set; };
public virtual IList<OrderItemPOCO> Items { get; set; };
}
public class OrderItemPOCO
{
public virtual decimal Qty { get; set; }
public virtual decimal Price { get; set; }
public virtual decimal Amount { get; set; }
}