logo
Welcome to our new AbleCommerce forums. As a guest, you may view the information here. To post to this forum, you must have a registered account with us, either as a new user evaluating AbleCommerce or an existing user of the application. For all questions related to the older version of Gold and earlier, please go to AbleCommerce Gold forum. Please use your AbleCommerce username and password to Login. New Registrations are disabled.

Notification

Icon
Error

Options
Go to last post Go to first unread
Joe Payne2  
#1 Posted : Wednesday, June 16, 2021 3:01:02 PM(UTC)
Joe Payne2

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??

Wanna join the discussion?! Login to your AbleCommerce Forums forum account. New Registrations are disabled.

shaharyar  
#2 Posted: : Thursday, June 17, 2021 1:49:19 AM(UTC)
shaharyar

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
Joe Payne2  
#3 Posted : Thursday, June 17, 2021 5:23:45 AM(UTC)
Joe Payne2

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.
mazhar  
#4 Posted : Thursday, June 17, 2021 5:40:26 AM(UTC)
mazhar

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;
    }
}

Joe Payne2  
#5 Posted : Thursday, June 17, 2021 5:52:34 AM(UTC)
Joe Payne2

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
}
Joe Payne2  
#6 Posted : Thursday, June 17, 2021 5:53:49 AM(UTC)
Joe Payne2

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.
mazhar  
#7 Posted : Thursday, June 17, 2021 7:36:40 AM(UTC)
mazhar

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

Joe Payne2  
#8 Posted : Thursday, June 17, 2021 7:46:10 AM(UTC)
Joe Payne2

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 Go to Quoted Post

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 :)

Users browsing this topic
Guest (2)
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.