Which option best suits the MVP model - android-mvp

While I was refactoring some code I had a doubt.
If some error happens in the model class, would it be better to call a method of the model class from the presenter class to check if something went wrong, or instead call a method of the presenter class from the model class when the error happens?
For example: Call a method of a model class (from presenter class)
if (modelClass.isWhateverRight())
//Do something
Or Call a method of the presenter class (from model class)
if (something)
presenterClass.showMessage();

Related

Sylius get TaxCategory from ShippingMethod

To get simple I want to call a method of a child class on a parent class.
Sylius get two interfaces for one model: Shipping\Model\ShippingMethodInterface is the parent interface and I will use the term of BaseShippingMethodInterface. Core\Model\ShippingMethodInterface extends of the parent interface. This interface add some methods including the getTaxCategory() one.
My problem is that I got an object of type Shipping\Model\ShipmentInterface and this object have a method call getMethod(). But this method return an instance of Shipping\Model\ShippingMethodInterface and not Core\Model\ShippingMethodInterface so I can't call the getCategory() method on this object.
If someone know how to bypass this conception problem let me know.
Thank you

How do I check if a class is a model class in MVC

I started learning ASP.NET MVC and I have got some doubt. How do I check if a class is a Model class in MVC. I have PHP Codeigniter background, where all models inherit CI_Model. It was easy to figure out whether a class is a model class or not by checking instanceof operator but in .NET MVC Model class do not extend any class.
So how do I figure out whether a class is a model class through C# Code? How does MVC framework figure out whether the class is model or not. I have renamed folder from "Models" to "Modelss" but still model binding works with ModelState.IsValid. Any help is greatly appreciated.
Most models in an MVC application are plain old CLR objects (POCOs), that often don't have a base class because it isn't needed. You can change that, if you need to.
In the following examples, lets assume you have a object called param coming in from somewhere.
In C#, you can check if an object is of a certain type in a few ways. You can cast the object to the type, and if you don’t get an exception, the cast was successful. This is not the preferred method any longer, but I wanted you to know if was an option.
try {
var myType = (MyModel)param; // cast happens here
// do something with myType
}
catch{
// cast failed
}
Another way is to use the as operator. This is a much better way to do this because no exception is thrown if the cast fails, you just get null in the variable.
var myType = param as MyModel;
if (myType != null) { // you have what you need.
...
}
Another technique is the is operator (another good way). This works similar to as, but returns a Boolean rather than the object, or null, and you can inline it in an if statement to do the cast, and assign to a variable all in one line of code.
if (param is MyModel myType){
// do something with myType
}
If you do add a base class to your models, you can use that type rather than the class name in the examples above. If you want, you can forego the base class and use a marker interface (an interface with no properties, or functions declared in it), and check for that type.
public interface IModel {}
public class MyModel : IModel {
...
}
if (param is IModel myType){
// do something with myType
}
BTW, changing the folder name in the project didn't make any difference because C# works based on namespaces, and not folder structure, for most application types. As long as the folder and class files are included in the project, and the namespace is referenced, all is good.
Hope you find this information useful!

How to gain control of what is returned after a route matches?

With ASP.NET MVC, when a route matches, the framework will determine the Controller type and then use the ControllerActivator class to create a new instance of it and then execute the Action method and finally return it's result to the request caller.
Just out of curiosity, what if I wanted to intervene exactly between the route and the Controller. Let's say controllers don't fit my needs. Let's say I just need to execute a random method in a class and return it's result.
What would I do then? What would I have to override? Can you please provide the path? A sample code would be appreciated too.
Create your own controller factory which implements IController factory. Something like below.
public class MyControllerFactory : IControllerFactory
Implement IControllerFactory interface
Register your new controller factory in Global.asax Application_Start event.
ControllerBuilder.Current.SetControllerFactory(typeof(MyControllerFactory));
You can check code of DefaultControllerFactory as reference for implementing your own factory.

mvc uppercase Model vs lowercase model

I'm working on a MVC 5 project, very new to MVC. I noticed this line in the code:
#Html.DropDownListFor(model => model.ContractorId, Model.Contractors)
the directive on the top of the page is:
#model Project.Models.ContractViewModel
The ContractViewModel class does have a Contractors object in it.
public IEnumerable<SelectListItem> Contractors
I'm just a little confused on the capital M (Model.Contractors), does Model always refer to the object passed in the #model? Then what is the difference between using Model.Contractors and model.Contractors?
Model actually represents an instance of the class that is your model and model is an alias for lambda expression.
When you write #Model in your view you are using the object of ContractViewModel which is passed from Controller action, if it is not passed from View it can be null and accessing any property of Model can throw Null Reference Exception and writing Model.Contractors you basically mean ContractViewModel.Contractors
and when you write a html helper you need to write an alias for it, its not mandatory to write model you can write anything.
for example:
#Html.DropDownListFor(m=> m.ContractorId, Model.Contractors)
It is just an alias to access the model properties inside Html Helper.
When you write #model Project.Models.ContractViewModel at the top of View that is a different thing, in this case we are defining the model of view that it will take instance of Project.Models.ContractViewModel , actually our view is now strongly typed to instance of class Project.Models.ContractViewModel.

All viewmodels inherit from 'BaseViewModel', can I set this up in OnActionExecuting?

If all my actions have a model that inherits from BaseViewModel, is it possible to initialize this model from the OnActionExecuting method?
Currently in all my actions I do this:
var model = new SomeModel();
model.User = Users.Get(...);
Now I am loading the user object in OnActionExecuting, so I was hoping I could setup my model from there somehow.
You can't do it in OnActionExecuting, since at that point you don't know which subclass of BaseViewModel to use. However, you can update the model in OnActionExecuted, which runs after the action method but before the view is rendered.
Most likely you are populating these properties to use in the view only, but if you do need access to them in the action method you can put them into ViewBag or a controller property in OnActionExecuting and add them to the model in OnActionExecuted

Resources