Suggestions on Yii project structures? - yii

I am currently developing a web project using Yii framework. I'm wondering where is a good place to put all the business logic, in the Controllers, or Models(models here as in mappings from database tables to actual objects)? Both doesn't seem right. I think I might need an extra "asset" layer in between controller and models, but I have no idea how to start. Any suggestions?

Generally the suggestion is to go about using Fat Models and Thin Controllers. So business logic in your model. It makes it far easier to make your code re-useable.
More info here:
http://www.yiiframework.com/doc/guide/1.1/en/basics.best-practices
If you've got a lot of custom logic, you could potentially have an "asset" layer of additional models that handled your DB models. Depends on your specific system though … I'm finding I do use CFormModel objects this way at times to map from a form with a bunch of different models to the models as needed.

Related

What is the Best Practices for Optimal Performance in Silverlight and MVVM

I have many Normalized tables - might be more than 50... I was wondering what is the best approach for defining ViewModels - individual ViewModel for each form or making Common ViewModel for multiple Forms. Because making individual forms might increase the size of the data that needs to be downloaded and it might increase the redundancy of data on the client. i.e. using Category on each form has different set of data for each of them. and On the otherside making common viewmodel for set of Forms might increase the complexity in managing stuff.
Is there any proper article describing such aspect of Development. What is the best practices for managing overall Application so that it will offer optimal performance. (Fetching minimum Data from Server)
Thanks for your time and help.
The amount of views & models will increase the size of your XAP file, which is downloaded completely on open, this can be compressed. Actual performance during use is different and depends on other factors as well, try using SilverlightSpy to get an idea of actual browser performance. It is possible to download parts of your silverlight app as required, but this is an advanced technique.
If Messaging is your main concern, then check out Binary Messaging.
I recommend using a new ViewModel for every view, or nested Usercontrol, then use an event aggregator for communication between models.
Typically you'll want to create a View Model for each View. If two Views display the same data and allow the user to perform the same actions but differ only in UI implementation then they can share a View Model but the goal is to keep your View Models cohesive. If your View Models contain code to operate multiple views you run the risk of implementing the "God Object" anti-pattern. If you find that your View Models all share a certain amount of common code, consider moving that code to a common base class.
Remember that two completely different View Models can manipulate the same Models. This might be the case if two views display the same data but each allows the user to interact with it in a unique way.
I would highly recommend reading Pro WPF and Silverlight MVVM by Gary Hall. It's a great book to get started with MVVM, particularly for use with WPF and/or Silverlight.

Why models are named "user.rb" and not "user_model.rb"?

Why controllers are named "users_controller.rb" and models are not named "user_model.rb"?
Why there is "application_controller.rb" but inside views the folder "layout" is not named "application"?
Code flows from thought best when the naming supports the developers internal model of the problem. When building an application, I don't think of finding a user model (UserModel.find) I think of finding a user (User.find). On the other hand, the controllers are the translation layer between the web interface and the data store (and business logic), so it makes more sense to call them something different.
There's also the problem of namespacing; if both my model and controller are named User, then which User am I referring to at any given moment? In this case, either you name everything with their type, which runs into the problem I describe above, or one 'wins' and is allowed to be referenced 'bare'. It seems to make the most sense that the model would win, so as to provide a better mental mapping.
Inside app/views/layout is application.html.erb and you can have other layouts which are selected by different controllers.
In the end, however, these were choices made during the development of Rails, and they are entirely stylistic choices based on what the developers thought made the most sense, so there isn't really a 'right' answer to your question, unfortunately. In fact, some similar decisions have been revisited. (application_controller.rb used to just be named application.rb.)
Ruby on Rails follows "Convention over configuration" principle. Particularly naming conventions are extensively used by Rails while mapping your routes to controllers, auto-loading and reloading classes, finding appropriate template for an action and many other features.
That principle leads to some restrictions as you can't easily break some of the conventions without getting into troubles. But on the other hand it makes our lives easier as we get smaller amount of configuration and can easily move from one Rails project to the other because all of them have similar structure and follow same conventions. In addition, I believe, that makes Rails core development much easier as core team have a lot of information about how the project using Rails will be structured and they don't have to worry that much about generalization. They simply assume you play by the rules and follow conventions.
Though, I doubt many of naming conventions have serious reasoning behind them. I think at some point someone just decided that it'd be easier for Rails to handle your controllers and distinguish them from other classes if they all have Controller suffix. And here we are having all our controllers in app/controllers directory with that suffix.

Best way to use ActiveRecord models in multiple applications

I have 2 applications that are going to be built (possibly a 3rd API), all are going to use the same database. What is the best way to use the same models across all applications.
Also, what are some of the caveats you have experienced or foresee with this method. Looking for the best solution to this.
Rails, as opinionated software, prefers a single app, rather than shared models. I've tried it both ways. Are you going to just have copies of the models that can get out of sync? Are you still planning to use Rails' migrations? When you have multiple apps, migrations become difficult. Do you use just one app for migrations? Then you lose the ability to check in migrations along with the code it refers to. Automating builds can become very difficult. You can possibly find a way share migrations too, but that requires some source code management sleight-of-hand which ultimately makes it more difficult to do separate apps and still get everything Rails has to offer. At that point you may want to look into Sinatra.
On the other hand, there's a lot of organizing you can do in a single app that keeps the model domain shared, yet separates the controllers, such as using namespaces or engines. I'd recommend those techniques.

Is it feasable to have DAL and BLL layers in a Mac OS X Application?

I am developing a Mac OS X application using Objective-C and Xcode 4 and want to find out the best way of handling data access and undertaking business logic tasks without having to use CoreData.
I am from a .NET MVC background and would normally have my controller call through to a service layer (using a Repository Pattern) to return data that could be mapped to my View. This would work in a similar way to the traditional Business Logic and Data Access Layer.
However on the Mac most of my reading suggests that my Models and Controllers should share the responsibility of populating the Model with with data and undertake business and validation logic.
This seems to me a little restrictive and goes against the DRY principle as I may need to repeat some data access/business logic operations in other models thus having to write tha same bit of code again.
Therefore is it feasible to have a set of classes or external libraries that undertake business/data access logic (to a SQLite database) that can then be called from any controller? Therefore the Model will only contain data about itself and validation logic? Or does this go against the core MVC principles and ways of building applications on the Mac?
Is there a particular reason not to use Core Data in this scenario? It's highly-optimized for persisting objects to and from the local filesystem. It also performs validation at the model level, results caching, notifications, etc.
What you describe sounds like a good idea to me. Putting your validation and business logic in your model classes is proper use of MVC, and having the data stored in an sqlite database (that the model classes talk to) is a commonly used methodology too.
I'm not sure if we're on the same page with terminology: if you use that design, your classes "that undertake business/data access logic (to a sqlite database) that can then be called from any controller" will in fact be model classes.

Is CoreData typically used as the Model or is it an implementation detail of the Infrastructure?

I would like to use a Sqlite datbase in an iphone application. The example in the book I am reading has the controllers directly calling into CoreData objects.
Coming from MVC/MVP in .NET to me this is akin to opening a SQL Connection in a button event handler. I would typically have a repository that handled the details of retrieving/persisting the model.
1) Is it the norm to use CoreData functionality directly in a Controller?
2) Is extracting my domain model into separate classes that get translated back and forth to the persistence layer not a good idea on the iPhone (in regards to performance, memory, expected project organization, etc)?
3) Will creating a repository layer work well on an iPhone?
Going with the SmallTalk background Objective-C and the MVC approach of iPhone applications I was expecting to leverage Domain Models, Repositories, IoC, etc.
Is that just not realistic? Are the author and I just on different pages? Thanks for any input.
iPhone SDK does follow the MVC pattern, but also many others (delegate). So it may not be as strict as other languages you mentioned. To answer your questions:
You will inevitably have some kind of access to your Models from your Controller. So long as the implementation of these actions is handled within the Model then you are following the MVC pattern. This is provided by the CoreData Framework. So in the end, it is your DAO, ActiveRecord, etc.
You can generate the Model classes (Model.m/h) if you wish to add extra functionality. However, CoreData typically builds them for you on the fly.
I don't quite understand this without a .NET background, but I think my answer to your first question may have answered it indirectly.
I can't speak to the book you are reading, but it may be best for you to see some additional references. Check out the Introduction to Core Data in the ADC's Reference Docs as well as one of the sample projects - CoreDataBooks.
Yes, Core Data is typically used as the model. If I understand what you are saying correctly, you expect a layer between the actual data store and the controller. Core Data is that layer -- it (sometimes) is implemented in sqlite, so that you never actually use sqlite directly. This is I think the source of your confusion -- you're not supposed to use both in the same project for the same data.