For example, I have a class which collect data from web-sites and return object collections. This data doesn't store in the database so it is not model. This class is used by many controllers so it is not controller.
Where can I place such files?
Should go into lib folder! As Modules perhaps!
I would put them in app/models. Then you don't have to deal with the autoload path or with require vs. require_dependency.
Just because a class isn't persisted to your database doesn't mean it isn't part of your data model. Putting them in lib suggests they're some sort of utility class that doesn't have much to do with your app.
Rails confuses model and persistence. I think it's fine to have some of your business logic along with your persistent entities, but there's no reason why you can't have model logic outside of your ActiveRecord objects.
I usually place those classes in the lib directory. and than just require them.
Its a value object and should remain same for whole site, and your controller or other components should use its refernce from model, class refernce should be in model and class it self can be put with value object
Related
I have entity framework classes generated using the update from database option and model classes that I have created. My question is if I have a table called 'car' and have a model called 'car' as well what is the best way of using these? Should they be the same class or should I use partial or should I have separate class for the model and separate class to represent the database table can you point me to an example?
I've always kept view models in a separate class library (with a separate namespace) from the entity data models. This way if the database schema changes (which almost always happens) I'm not breaking my views. And to move data between the data objects and view models I'm rather fond of Automapper. It'll even give you unit test failures if there are properties on a destination object that you are missing a source mapping for, helping you catch errors early.
I've found it's normally good practice to have a separate project in your solution specifically for data access.
I normally set this up as a class library project with something similar to the following structure.
/ProjectNameData
/Models/ -- POCO's go here
/Mapping/ -- EntityTypeConfiguration classes go here
/Repositories/ -- Repositories go here
/ProjectContext.cs
Then I would have another class library for services as well as my main application project.
I'm new to ASP.Net MVC and I'm having a question I can't seem to find the answer to on the Googles.
I have a page that needs a model that is significantly different from the way the data is stored in the database. It's trivial for me to write a function that would translate from the database model to the required MVC model (MyModel ConvertToMvcModel(DataFromDatabase d).
My question, where should I put this code? Should it be in the controler. Should it be in the Data access layer (Using the repository pattern).
Another related question is where should I put the repository class? Up until now I've put repository classes allong side the model class in the same .cs file since every model had a corresponding repository. This time the model will be different from what I get from the repository so it does not make much sense to put it in the same file. Maybe I should separate all my DAL (repositories) from the model code.
Any suggestions?
My question, where should I put this code?
The mapping between your domain models and view models should ideally be placed inside a separate and dedicated mapping layer. For example if you use AutoMapper, which I would recommend, you could place your mapping definitions inside separate files called profiles and inside the controller action simply call the Mapper.Map<TSource, TDest> method.
Another related question is where should I put the repository class?
Up until now I've put repository classes allong side the model class
in the same .cs file since every model had a corresponding repository.
The DAL represents the data access layer and is where the repositories should go. You could define a common contract (interface) that the repositories must obey (implement) and then have several implementations against the different data sources that you are working with.
You shouldn't return entities anyway, use ViewModels for that purpose.
Regarding the mapping: what you're looking for already exists, it's called AutoMapper. You can let the service layer return entities to your controller, and the controller will map them to ViewModel objects.
Keep in mind to initialize the mappings only one time, so launch them from your application start.
Is there a specific naming convention for a model that will not be stored in the database? For example, i have a schedule which will be a model, but will not be in the database because it is just a data structure. In other words it will not extend ActiveRecord::Base?
I view this as an internal implementation detail; I wouldn't reflect it in the name, because other models that are interacting with a given model should not know or care whether it is persisted. And your requirements could change later and it would become persisted.
If you want to create a class in the Model that doesn't persist to the database than just don't inherit ActiveRecord::Base
e.g
class SomeClass
end
The class definition is still saved to the file some_class.rb in the model directory
As for naming convention. Well a Model class is a Model class, it doesn't matter if it persists to the database, or some place else, or not at all. I see no need for any special naming convention.
Your Controller and Views should just interact with the your Model objects without being concerned about that object's underlying persistence mechanism. That's one of the main advantages of Model-View-Controller ... The Controller and View need not be concerned with the inner working on the Model objects. So neither should your naming convention.
I tend to make non-active record models to inherit from the NonActiveRecordModel class (or something like that, which you can define yourself). The abstract NonActiveRecordModel class can have common behavior that is used by all of your non-active record models, such as validations via validatable gem, etc.
I like having separate classes, one class represents the entity, and that a separate DAO (database access object).
Is this possible with rails and active record?
Most of what you would put into a DAO is already hidden inside of ActiveRecord anyway, so there's not much of a need to split these up. But, if you insist you can split out whatever methods you want into a separate Module and then include it in your model.
I don't know if this is bad form or not, but I needed to set a file path that's accessible to all objects within actions in my controller. One action in the controller creates a file and stores it in a path. Another action serves the file using send_file. The only place I have been storing variables is along with an object in the model. However it seems really silly to store a URL in arbitrarily the first object, or copy the url over all objects. What's the best way to do this?
I hope this was clear.
If this is a file path that is specific to the user of the site, so each user has a different path, you can store it in the session.
session[:file_path] = generate_file!
…user goes to the next page…
send_file session[:file_path]
You could create a method in your application controller that returns the path. This method will then be available throughout your controllers. Don't know if this is necessarily "best practice" but it works for me.
The answer depends on your context. Here is some generic advice:
If there's one file per model, then you need to store one path on each model that has it.
If there's one file shared by several models, but your objects are relatd on a hierarchy, you need to store it on the "father object" - the one that has_many others. The other objects will have to do self.parent.file_path.
Finally, if there's one file used by several non-related models, then I don't know what to suggest, except that maybe there's a better way to organize your models.
What objects are you trying to store, and what relationships are between them?