Say I have a person table in a database. Whenever a new person is added into the database, a pdf will be generated and emailed to someone.
Does this logic go into the model, so all the application does is pass in the data to insert the new person, and the model also handles the pdf and email?
Or does this logic go into the controller?
If this should go into the model, and I am using entity framework, how do I do this? Create a .cs class that wraps the .edmx model?
It is my understanding that you would not want to muck up the controller with that kind of logic. Things that belong in the controller would be defining models that are going to get passed to the view or turning validations on or off. There are several other view related things that the controller should control but that is just a couple. The general point that I am trying to make is that the controller is responsible for populating models by calling services or mocked repositories and then passing those models to the view.
To do what you are explaining I would add a service layer that sits above the data access layer (dal - where your EDMX and repositories sit). Inside that service layer is where I would call the methods or services to generate the pdf and send emails.
The repositories and data access layer should only be concerned with querying the database and retrieving records. That is regardless of what ORM or data access tool you use. the retrieved records are passed to the service layer where you can perform actions against the data (such as validations or sending out pdf's) and then the presentation layer is returned whatever format of the records that it needs to display it in the view. The controller doesn't perform any logical action against the retrieved data but instead just hands it off to the view.
If you would like some more details about the architecture layout that I have described then I would be happy to give you more insight into it.
Related
We want to filter / hide / clear specific api model properties based on the user permission level.
The model itself will not differ. Just no return for those.
I found a lot of different ideas online:
switch(userrole) and call different logic methods
pass user role to logic
reflection to clear properties in the response (I hate this idea)
middleware to redirect the user to different actions
What is the recommended way to filter api model properties for specific roles?
It's hard to give any real specifics with what you've provided, but generally, I'd say this should go into your mapping logic. Utilize view models/DTOs to accept data from client requests. Then map that data from the view model/DTO to your entity instance. During this process, you can make decisions based on the user's role/permission set about what properties should or should not be mapped over. All of this logic can be factored out into a separate class or library of classes. Ultimately it doesn't really matter what the client sends. You can't control that anyway. You just need to ensure that they ultimately can't set any properties they don't have "access" to, and mapping logic is a great place for that.
We are revamping a big enterprise application which has more business workflows. We are using activiti bpm with spring MVC. Currently we are saving the required variables for workflow inside execution individually. My idea is to create object for each workflow and save the object as variable inside activiti execution instead of individual variables.
For example for the job application workflow I will have JobRequest object which have all the requested details like approvers, interviewers, candidate profiles, current status, etc., I will save this object as variable inside activiti. Whenever I need I will just get the object back and show the needed info on the web page. It will be hassle free than maintaining all those information on separate tables and getting the reference alone from activiti.
Is this good approach?
Still I have some concerns on my approach.
If suppose later we add additional fields to the Class. Then how to
handle the history object variables. (In the above example we are
adding additional field under JobRequest.class).
In our current approach if some values really needs to be verified I
can connect to the activiti database and able to see. Because the
variables are saved individually.
Any valuable suggestion?
I am designing a project in asp.net 4.0, i am using MVC 3. I have table in my database (sql server ) . I have a table named "employee". I want to display whole information whose name is SANJAY. So please suggest me how and where i should write query ? Means i should write query in controller class or model class..?
Really you want to hide your actual data access from your MVC application entirely. So just like the MVC pattern means that the view only needs to know about the model and doesn't care how it is constructed logically, your MVC application shouldn't need to know about what queries are performed or what database they are performed against.
There are many design patterns that handle this separation of concerns, but you might want to start with the Repository Pattern. You can read about the pattern here: http://martinfowler.com/eaaCatalog/repository.html - or just search for "Repository Pattern".
Ideally, your repository queries the database and returns a domain object. You would map the domain object to a much simpler Model in your MVC application. This not only protects your model from becoming bloated by changes to the domain object, it also means your model can contain things your domain object doesn't contain and therefore stops the domain object from becoming polluted by things being added to the model.
I have a asp.net web application that uses Entity Framework. The application data layer uses a method outlined by Jordan Van Gogh found here. In a nutshell, this method uses one shared objectcontext instance per http request and is disposed of using a AspNetObjectContextDisposalModule that inherits IHttpModule.
I have added another project, a few additional tables, and setup a data layer that copies (exactly) the example I described above to my application. This additional project and subsequent different data model all work perfectly. I can perform different operations using the 2 data layers with seemingly no consequences. Of course the objectsets are different in the 2 data layers as they represent different tables.
My question is this:
Is this approach a good idea? I get most of what is going on behind the scenes but both of these models use System.Data.Objects.ObjectContext. If user A performs an operation using the first data layer while simultaneously user B performs an operation using the second data layer, are there going to be problems with the "shared" objectcontext?
Thanks. And be gentle.
Edit
Note: I am using different oc keys
You should be OK:
The object context is per http request so the context from different users will not interfere with each other
Each context updates different tables so they will not interfere with each other
One thing that you may have to watch out for is what happens it two users update the same data at the same time.
I am developing an ASP.NET 2.0 website. I have created data access and business logic layers. Now in the presentation layer I am returning data from the business layer as a dataset.
My question is whether to use a dataset or object collection (for example a Category object representing the Category table in my database). I have defined all classes that are mapped to database tables (Common objects). But there are situations where I need all of the records from the category table in the presentation layer. I am just confused. What should I do?
You don't want to return datasets, you want to return objects.
Generally when you have a data access layer and a business logic layer you will also want to have an entity layer. The entity layer will be an in memory repersentation of the database result set. If you return one row from the database you load one entitiy object. If you return more than one row, you will load an entity for each row, and return a collection of entities to be consumed by the presentation layer.
If you're using .net 2.0 and above for example, you can create generic collections of the entity type and easily bind different types of controls to these collections.
Hope this is helpful for you.
I would recomend returning objects or IEnumerable/IList etc of objects.
Depending upon your DB access you can populate a list of category objects manually or using something like LINQ2SQL or ADO.NET Entity framework very quickly, and if required cache them.
I've used both methods depending on the situation. If you only need to display the data from a table in a grid, the dataset(or datatable) method isn't terrible because if fields are added to the table they will automatically appear in the grid...assuming you are auto populating the grid with the columns. I look at this method as more of the quick and dirty method.
I would not return datasets at all. You are tightly coupling you service to you database. In the future when you database evolves you will be forced to change anything that uses the datasets. You want to create an abstraction layer between the database and the service.
I'd go with an object/collection solution. So if you are returning one row from a table you have one object, if you are returning several you'd use a generic collection. Generic collection will avoid a boxing/unboxing hit.
[edit]seems I was beaten to it[/edit]
You should create an entity layer, having classes representing each table in database. And then return lists of those classes.