Developer forum

Forum » Development » Difference between Model-First and Code-First ItemTypes

Difference between Model-First and Code-First ItemTypes

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I am trying to understand the main differences/benefits between Model-First and Code-Firts ItemType.

When it is appropriate to use one or the other, what implications (if any) they have on performance and extensibility of the ItemTypes.

I have found some details in the Training section on this website but I feel that the information is not that comprehensive to understand the benefits and how to start developing them.

Thank you,
Adrian


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Adrian,

1) You can get intellisense in Visual Studio when using code first items.

So instead of doing this with a regular item...

string myText = item.GetString("MyText");

... you can do this with a code first item...

string myText = item.MyText;

With intellisense you can easily see all the available fields (properties) on an item. With regular items you will have to remember the system names of the item fields - or use TemplateTags to see what is available.

If you need to implement a lot of advanced logic for handling your items then code first can make it a lot easier. You can avoid problems caused by missing fields or misspelling of field names because you get full intellisense and compile time errors. With regular items you can only work with key/value pairs and errors caused by missing or misspelled field names would only show up at runtime.

2) Code first items can only be changed by a developer. Regular items can be changed by any user that has access to Settings > Item Types.
- It can be an advantage to use code first items if you want to ensure that item type definition is "locked", but it can also make it more cumbersome to make changes in the future.

3) In general it doesn't have any considerable effect on performance whether you use regular items or code first items. However, it really depends on your code.

4) With code first items you can override the save and delete logic, but you can achieve the same with any type of item by implementing notification subscribers for ItemNotification.Saving, ItemNotification.Saved, ItemNotification.Deleting, ItemNotification.Deleted.

Other than that I think it is mostly about what the developers prefer.

You can start out with model first items and then replace them with code first items later on if needed.

Best regards,
Morten

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Morten,

Thank you very much for the comprehensive answer. It makes a lot of sense.

Is there any documentation on how to get started? Or best practice?

Thanks,

Adrian

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

I think the best documentation is currently found in the training section and api docs...

https://doc.dynamicweb.com/training-certification/t3-platform-developer/t3-platform-developer/3-4-items

https://doc.dynamicweb.com/api

 

You must be logged in to post in the forum