Hot answers tagged

87

If your class has 20 parameters in the constructor, it doesn't sound like your team quite knows what SRP is. If you have a class that does only one thing, how does it have 20 dependencies? That's like going on a fishing trip and bringing along a fishing pole, tackle box, quilting supplies, bowling ball, nunchucks, flame thrower, etc.... If you need all that ...


33

I think it's in Martin Fowler's Refactoring that I once read a counter-rule to SRP, defining where it's going too far. There is a second question, as important as "does every class have only one reason to change?" and that is "does every change only affect one class?" If the answer to the first question is, in every case, "yes" but the second question is "...


24

Just because a system is complex doesn't mean that you have to make it complicated. If you have a class that has too many dependencies (or Collaborators) like this: public class MyAwesomeClass { public class MyAwesomeClass(IDependency1 _d1, IDependency2 _d2, ... , IDependency20 _d20) { // Assign it all } } ...then it got way too complicated ...


21

If the data never changes and is read only, then just put it in a code file as a list of constants. public readonly string AppStartUpData = "MyAppNeedsThis"; If this data is different per deployment, then an external file is fine. .Net comes with built in .config files (App.Config). One should use those as there are standard ways (built in to the ...


18

I have tried CaliburnMicro and MVVMLight and when using Caliburn I really feel what you feel, sure it feel really magical able to bind control to property just by using Name="PropertyName" instead of old Text="{Bind PropertyName}" but in the end Caliburn goes way overboard to do this magical thing, when something goes wrong it really hard to debug, to make ...


16

To answer the question, Yes, each view should have its own View Model. But there is no need to model the entire hierarchy. Only what the view needs. The problem I had with most online resources regarding MVVM: In most examples, the View is almost 1-to-1 mapping of the Model. But in my scenario, where there are different views for different facets of the ...


16

Use one DbContext object per data access or transaction. DbContext is a lightweight object; it is designed to be used once per business transaction. Making your DbContext a Singleton and reusing it throughout the application can cause other problems, like concurrency and memory leak issues. DbContext essentially implements a Unit of Work. Treat it ...


15

You know when you are violating MVVM when: The ViewModel is aware of the View. The ViewModel should never know or care if or what is sitting on top of it. It simply is. Sometimes, the ViewModel might need to generate events that should be handled on the View. When I was working with MVVM, we used the Mediator pattern to handle those cases. The Model is ...


15

You can always add a file into your project and set its build type to Embedded Resource so that it is embedded directly into the application itself. Alternatively a file that is encrypted and placed in an accessible location.


14

A binary file would be the obvious answer, but it depends on how you are loading it - you might as well make life easy for yourself if you can. XML might be a good choice as there are built in methods in C# for reading this. You could add a checksum to your data, so that if the user alters it, the checksum will no longer match (you need to add a check to ...


13

It looks like you've found yourself in perfect learning situation. Currently your application works, and I'm sure you're very familiar with all of the 20k lines of code. Making changes isn't that difficult, and, so far, there doesn't seem to be any reason to change your development approach. You are a cowboy! And being a cowboy is a lot of fun! But ...


11

It is important to realize what MVVM is. It is not some shared bit of functionality that you do not have to reimplement (parsing a JPEG file or connecting to a given SQL database server), it is a pattern--a pattern for how one may choose to implement a rich GUI. So, if your implementation of the pattern is simple and straightforward, I do not think you need ...


10

My perspective is from years of experience working with Winforms, the "old fashioned way," with events and code-behind. So I can tell you with absolute certainty that, once you get beyond the simplest of applications, your code quickly becomes a big ball of mud. It was inevitable, because that's the way applications were written back then. Webforms is just ...


10

Break the problem into two parts. Learn the basics of F# by excluding the use of WPF. Learn how to write functions without using mutables and while statements and using function composition. Once you have this done learn how to make use of mutables to update state. Learn how to use WPF in F# by reading Learning WPF through F#, and vice versa, by John Liao....


9

One way to avoid refreshing too often would be to use a dirty_flag + a Timer; you set the Timer interval to something like 200ms or whatever value you consider optimal. When en event that requires a refresh happens, you just set the dirty_flag to true, and reinitialize the Timer(that way you make sure that Timer.Tick will be raised at "x" milliseconds after ...


9

There are different tools and different patterns. Tools ADO.NET ADO.NET is much more than DataSet and DataTable. I would not use those classes in any project but instead use the IDataReader to populate pocos as I describe here: http://blog.gauffin.org/2013/01/ado-net-the-right-way/ Data mappers The next step is to use a data mapper. What they do is to ...


9

I think I know what you mean. Basically you solve the problem by adding either a 'controller' or a 'master' viewmodel (excuse psudocode) ie public class MasterVM { public ChildVM View1 {get;set;} public ChildVM View2 {get;set;} private Data data; public MasterVM() { View1.OnEvent += updateData; } private Action<int&...


8

I've been a WPF/Silverlight programmer for years building huge applications, such as trading systems, on MVVM. For me, as the years have gone by, I've learned that strict MVVM eats time and costs money. By strict, I mean rules such as "no code behind". It's impossible in anything but the most basic form/database app, not to have code-behind. Your designer ...


8

I use dependency injection and a IViewFactory injected into the view model to respect both constraints. A ProductViewModel (for example) calls this.viewFactory.Show("Details", this) to open ProductDetailsView with itself as ProductViewModel. It could also open a view based on another view model with this.viewFactory.Show<ClientViewModel>(). The ...


7

Personally I see nothing wrong with binding directly to the Model instead of exposing properties through the ViewModel, however if you plan on binding to your EF POCO classes, you need to have them implement INotifyPropertyChanged so the UI knows when they update. EF should have a setting that will make it generate the classes with INotifyPropertyChanged ...


7

MVVM isn't outdated, but it was overplayed to begin with. I never liked it and it kept me in WinForms for too long; failing to see the forest for the trees, I threw the baby out with the bathwater. I get WPF now, and I get the idea of not wanting to mix code with markup, but I prefer the Android style of sticking the markup in one place and dereferencing ...


7

I've sorted it out and in the end it went pretty well so I'll give here my recommendations according this painful experience. (these tips apply for WPF) Validation : use INotifyDataErrorInfo instead of rules in XAML, your validation takes place in the object instead and even it's sort of complicate to setup it is really worth it. This interface is ...


7

The command should be on the ViewModel. In MVVM, the ViewModel is the thing that should contain all of the logic that drives the view. The view is the simple class - just a collection of UI controls whose state is bound to properties on the ViewModel. Josh Smith wrote a really good intro to MVVM that should clear things up for you.


7

My first experience with WPF has been using Caliburn.Micro so this is probably quite different from most developers. I have found both WPF and Caliburn.Micro to be quite a steep learning curve, coming from WinForms, however after some experience with both I have found them a pleasure to use as a pair. Currently working in a different organization where ...


7

This may not answer everything you are asking, but it's too much for a comment. Your solution's organization (I use the term organization rather than architecture) should reflect how you use it. It should be efficient for your use. For instance, when you want to make a change to a Menu does a developer have to open files from View/Menus and also from ...


7

Theoretical answer If you have a ViewModel, actions that have cosmetic effects (e.g. highlight an item on mouseover) are the job of the View, while actions that have "real" effects (e.g. spawning a new window) are the job of the ViewModel. As such, creating a new window is a job for the ViewModel. However, neither the View nor the ViewModel should know ...


7

In WPF the XAML UI control is bound to the 'low level' ViewModel via IObservable properties or classes, NotifiyPropertyChanged events and the like. When the view model changes, either due to a UI control action or a background thread the UI will automagically update. This can involve a whole load of complicated nested ViewModels though. A general ...


7

Absolutely yes. It is a good idea in the same way that it is a good idea to apply separation of concerns on classes to put each in its own in order to perform a single task. You may only have one instance of that class, but it matters not. The optimization is not in terms of program performance but in terms of organization and overall program "health". ...


6

You can call from C# to C/C++ directly using a technology known as P/Invoke. With P/Invoke, a C++ function can be made to look just like a C# function. Here's a simple example from this article in MSDN Magazine: C Method Definition BOOL MessageBeep( UINT uType // beep type ); P/Invoke Definition in C# of method in C [DllImport("User32.dll")] static ...


Only top voted, non community-wiki answers of a minimum length are eligible