Posts

Showing posts with the label Mvvm

AndroidViewModel Vs ViewModel

Answer : AndroidViewModel provides Application context If you need to use context inside your Viewmodel you should use AndroidViewModel (AVM), because it contains the application context. To retrieve the context call getApplication() , otherwise use the regular ViewModel (VM). AndroidViewModel has application context . We all know having static context instance is evil as it can cause memory leaks!! However, having static Application instance is not as bad as you might think because there is only one Application instance in the running application. Therefore, using and having Application instance in a specific class is not a problem in general. But, if an Application instance references them, it is a problem because of the reference cycle problem. See Also about Application Instance AndroidViewModel Problematic for unit tests AVM provides application context which is problematic for unit testing. Unit tests should not deal with any of the Android lifecycle, such as con...

Bind Command In WPF Using MVVM

Answer : You can bind the Command property of the button to any property that returns ICommand. Prism implements a nice convenient command called DelegateCommand that is very easy to use (here is a knock-off of it): public ICommand MyButtonClickCommand { get { return new DelegateCommand<object>(FuncToCall, FuncToEvaluate); } } private void FuncToCall(object context) { //this is called when the button is clicked } private bool FuncToEvaluate(object context) { //this is called to evaluate whether FuncToCall can be called //for example you can return true or false based on some validation logic return true; } <Button x:Name="myButton" Command="{Binding MyButtonClickCommand}" /> The CodeProject example How to use Commands in WPF has a very similar example with code that you can easily work through. The previous Stack Overflow question has an example using RoutedCommands that are statically bound to: How to bind Close command...

Best Practice For Android MVVM StartActivity

Answer : The answer to your question is what is your goal? If you want to use MVVM for separation of concerns so that you can unit test your Viewmodel then you should try to keep everything that requires a Context separate from your Viewmodel . The Viewmodel contains the core business logic of your app and should have no external dependencies. However I like where you are going :) If the decision which Activity is opened lies in the View, then it is very very hard to write a JUnit test for it. However you can pass an object into the Viewmodel which performs the startActivity() call. Now in your Unit test you can simply mock this object and verify that the correct Activity is opened The way I do it is, in your ViewModel: val activityToStart = MutableLiveData<Pair<KClass<*>, Bundle?>>() This allows you to check the class of Activity started, and the data passed in the Bundle. Then, in your Activity, you can add this code: viewModel.activityToStart....

Blazor Project Structure / Best Practices

Image
Answer : I just created a new ASP .NET Core 3.1 project with 3 web apps: MVC, Razor Pages and Blazor. NetLearner: https://github.com/shahedc/NetLearnerApp I’m developing all 3 in parallel so that you can see similar functionality in all of them. I’ve extracted common items into a Shared Library for easy sharing. The shared library includes: Core items (Models and Services) Infrastructure items (Db context and Migrations) Here’s the corresponding blog writeup, which will be followed by an A-Z weekly series, which will explore 26 different topics in the next 6 months. blog post: https://wakeupandcode.com/netlearner-on-asp-net-core-3-1/ Hope the current version is useful for what you’re asking for. Stay tuned and feel free to make suggestions or provide feedback on the project structure. So I was diving into looking for more example projects and I came across a SPA Server Side Dapper application (https://www.c-sharpcorner.com/article/create-a-blazor-server-spa-wit...