开发者

MVC - where to put connection initialization code?

开发者 https://www.devze.com 2023-03-20 14:22 出处:网络
I\'m writing a simple measurement application which will be use bluetooth to talk to the device. My question is where to put bluetooth connection initialization code? To Model or to Controller?

I'm writing a simple measurement application which will be use bluetooth to talk to the device.

My question is where to put bluetooth connection initialization code? To Model or to Controller?

From my understanding of MVC, Model is what an application is - data models and all logic which applies to 开发者_高级运维the data. And the Controller is some kind of glue between Model and View.

But for me it seems that it would be better to put connection code to the Controller since maybe in the futre there will be new version of the device which will use USB or something else.

What do you think what fits better in my case?

Best Regards, Marcin


depends on the environment, but we follow the "thin controllers, thick models" principle. Having said that, a library of connection functions would fit in neither.

In the environment I work in, device connection functions would be best suited to a library (then you can have multiple libraries covering all connection types you may want to use in the future, as they are developed) which is then utilized in the appropriate place.

This would also allow re-use at a later stage in different projects/software if required.

A library would ideally have generic functions ( e.g. connectToDevice() and getFile() rather than getApplicationSpecificPhotosFromDevice ), with the app specific stuff happening in the controller and underlying model that uses the library to retrieve or submit data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号