Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Now that I've had time to fully engulf myself in the burning hell of MVC, I find myself asking the same question many times: Why not add a constructor to every model that accepts the entity it models? Code:
public OrderModel(Order o)
{
this.OrderId = o.Id;
this.OrderNumber = o.OrderNumber;
.
.
.
}
On the surface it seems like that would make life much easier setting up page models. Especially for a model that's used all over the place like Order or OrderShipment. But that's not been done anywhere in Able, and I'm curious if there was a bigger why I simply do not see? My only thought is maybe because then every page that needs an Order model has to pass the ENTIRE model and that's not as efficient as passing just the properties you need for just that page??
|
|
|
|
Rank: Advanced Member
Groups: Admin, Developers, Registered, HelpDesk, Authorized User Joined: 10/5/2018(UTC) Posts: 704
Thanks: 5 times Was thanked: 113 time(s) in 112 post(s)
|
Quote:My only thought is maybe because then every page that needs an Order model has to pass the ENTIRE model and that's not as efficient as passing just the properties you need for just that page?? Yes, this is one of the reasons. But in some cases where we have repeated code in a controller for filling the ViewModel data, we can also use the constructors as well. This link has some good discussions on the topic. https://softwareengineer...sts-for-dropdowns-in-mvc
|
|
|
|
Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Interesting discussion, thank you for sharing.
|
|
|
|
Rank: Administration
Groups: Admin, Administrators, HelpDesk, System, Authorized User, Developers, Registered Joined: 10/5/2018(UTC) Posts: 175
Thanks: 8 times Was thanked: 17 time(s) in 15 post(s)
|
MVC models are created via MVC data binding and you must provide with a default public no-argument constructor otherwise data binding will fail to create models. You can add helper methods to your model classes that can take objects and initialize the required properties. For example Code:
public class YourModel
{
string Field1 { get; set; }
string Field2 { get; set; }
public Load(DomainObject obj)
{
Field1 = obj.Field1;
Field2 = obj.Field2;
}
}
|
|
|
|
Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Ohh ok. So I could make two constructors in the model though, right? Code:
public class YourModel
{
public YourModel() { } // default constructor
public YourModel(Order o) { } // set up entire model based on order object
}
|
|
|
|
Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Heyyyyy, how did you get that color coding in your code snippet?? Mine didn't do it.
|
|
|
|
Rank: Administration
Groups: Admin, Administrators, HelpDesk, System, Authorized User, Developers, Registered Joined: 10/5/2018(UTC) Posts: 175
Thanks: 8 times Was thanked: 17 time(s) in 15 post(s)
|
Quote:So I could make two constructors in the model though, right? Yes, you can have as many constructors as you want but it must have one no-argument constructor. Quote:Heyyyyy, how did you get that color coding in your code snippet?? Mine didn't do it. In the reply editor, right next to the Quote icon there is a list icon. If you hover over it the tooltip says "Choose Language for Syntax highlighting". You can select the desired language and it will embed a code element for the selection. You can put your code inside that element and it will be formatted accordingly. Edited by user Thursday, June 17, 2021 7:37:47 AM(UTC)
| Reason: Not specified
|
|
|
|
Rank: Advanced Member
Groups: HelpDesk, Developers Joined: 11/9/2018(UTC) Posts: 564
Thanks: 122 times Was thanked: 26 time(s) in 25 post(s)
|
Originally Posted by: mazhar In the reply editor, right next to the Quote icon there is a list icon. If you hover over it the tooltip says "Choose Language for Syntax highlighting". You can select the desired language and it will embed a code element for the selection. You can put your code inside that element and it will be formatted accordingly.
LOLOL all this time and I never knew that :)
|
|
|
|
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.
Important Information:
The AbleCommerce Forums uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close