Actualy I want to update many tables as user clicked on SAVE UPDATE. So all changes user made (add/delete/update) will be handled in web api. I dont want to use view model because my superior said it kinda repeating model of what I've done in Angular. Any idea?
Well, let's look at this from a conceptual level.
Since you have a Save button, this means you already have a form and you have Angular models which of course are JavaScript ones. There must be a submit button on that form and you have access to all the data in your angular app. Up to this point you don't need anything from the back-end.
Next, you need to submit the data to WebAPI. We're talking about a transfer from a front end app to a back end app. WebAPI has endpoints and they take models to capture the data. These models will be very close to your front end ones if not exactly the same. This doesn't mean that you shouldn't have them though.
When you build a system where you mix different stacks, then stuff like this where models exist in multiple places, in different formats, does happen.
There are ways of dealing with it, you could have a process where when you compile your MVC application, you add some steps to automatically generate the JavaScripts models for you, so it's not all manual, but at the end of the day you still need models on both sides if you want to make any reasonable sense of your code.
At the end of the day, you will need models on Angular side and you will need models on WebApi side. You'll need to keep them in sync somehow as well.
I would have a chat with the other person and explain these things.
If you don't want to end up in a position like this in the future, you could stick to the classic MVC way of doing things.
You didn't need to use Angular, for example. I am not saying this decision was wrong ( it entirely depends on how much it helps the project and there are valid reasons to throw it in the mix ), but the situation you are in now is a direct result of that. MVC has it's own way of building forms which uses back end models so it doesn't need JavaScript specific ones.
You could, of course, completely eliminate the back-end models and simply get the data from the submitted form ( WebApi can do that ) and then you could simply transfer that data to your Entity Framework DTOs, but it's too much hassle and it's easy to make mistakes so I wouldn't use it.
Related
[Pre]
I have to say that I'm dummy newbie who is trying to get together important puzzles with such crucial details as DDD, TDD, MVVM, and EFCore. I have an about 10 years of windows form develop experience in complete wrong manner, and after I'm joined to Plurasight I'm understood that I'm just lost my last 10 years, and this is really sad :).
[Problem description]
I have an App that i want to re-write from scratch by using latest and greatest technics that've learned for the last 6 month on Pluralsight, but the problem is that these new knowledge’s is stopping me, because simply I'm afraid that I'll do it wrong again...(that is stupid I know, but it is what it is).
So back to my questions, I have a big problem domain, and pretty well documented business logic, which i have to turn in to the code. I'm understand that my start point is design data layer, for these purposes I want to use Entity framework core (I saw Julie Lerman's course on Pluralsight and I think's she is amazing and inspires me to use EFCore as ORM for my app). But at the same time leakage of experience produces more questions than what I’ve learned with Pluralsight, and I will try to write them all(please don’t judge me too hard)
It is looks like that I will need 2 or even more data model projects in my solution, and here is why I have multiple document set types, each of the type contain more than one reference books used to generate unique file names and data sheets. But it looks weird to me have 3 Data model projects such as MyApp.PackType1.DataModel, MyApp.PackType2.DataModel, and each of them will be preinstalled with the EFCore, and each of them will generates its own database based on Data Context defined by EF. Isn’t it very redundant or this is correct way?
I don’t understand how to join these multiple Data Models projects, including Shared Kernel into the one nice model
I don’t understand what is the best way to design my data classes? Should they be just POCO’s or I can design them as nice looking classes with the private var’s and public properties? What are the best practices in here?
Also I don’t understand what is the best practice to use a MVVM pattern on top of that, and is it applicable at all to use MVVM in this case?
Should I keep my Tests in separate projects like MyApp.PackType1.DataModel.Tests, or keep them in same project?
Best regards,
Maks!
P.S.
Apologize for unclear definitions and questions, English isn't my native language.
It's very complicated to answer your question because you have asked for a lot of details, but I going to provide a brief answer and I hope it will be helpful.
You can have only one model for your entities (DDD) and create sub model from this model in your end level projects (Web API or UI)
Read point #1
You have to create an Entity Layer project that represents your database and then you can create DTO's for specific scenarios
From my point of view, use Angular but you can use another UI framework such as React or VueJs, but I prefer to use Angular to build UI interfaces and consume .NET Core Web API from client
Create unit tests and integration tests for you Web API projects and as additional feature you can use Db in memory provider for tests
May be this guide is useful: https://www.codeproject.com/Articles/1160586/Entity-Framework-Core-for-Enterprise
Regards
Hm, multiple DbContexts (models) usually come about when you have distinct databases you are using. General rule is one Context = one Database. Exceptions can occur when there are a lot of tables that can be grouped functionally, but there are downsides to that approach.
A DbContext is a repository pattern but for individual tables. Using a Unit of Work pattern and layering with a custom repository provider would allow you to make it "appear" as a single database, hiding the complexity from the front-end.
Your entity descriptions are usually created as straight POCO. You can get creative with different DTOs
In a nutshell, an MVVM pattern goes like this:
Request from UI to a controller
Controller possibly issues multiple calls to Data Layer to gather data
Assemble data in a single ViewModel (everything the page needs)
Return to UI
The beauty of the approach is single roundtrip (request/response) to the UI
Separate Project in my opinion. There are techniques to spoof the database connection using EF so you are not using "live" data.
That CodeProject article will come in handy.
I am writing a RIA service. I need to decide where to put the business logic.
I see two possibilities
Use the CRUD methods being called by submitchanges and put the business logic there. The main problem is that in some cases I will need to do more effort to detect what has changed in my object as I don't really know which field changed in my object. The methods themself are expected to become big as needing to deal with multiple entity changes.
Give the client specific invoke/named update methods for some of the update ops. These will be called by the UI when doing specific data model change and therefore the effort on the server side will be smaller (will know better the operation being done) and maybe the complication of the server methods will be reduced..
Amit
The second approach makes more sence to me. Client is used only as a terminal to store and show data but there is no contraindications to invoke different server functions as long as you can keep your data consistent. Huge perk is simplicity, ive been tought the easier the better.
I have an architectural question about EF and WCF.
We are developing a three-tier application using Entity Framework (with an Oracle database), and a GUI based on WPF. The GUI communicates with the server through WCF.
Our data model is quite complex (more than a hundred tables), with lots of relations. We are currently using the default EF code generation template, and we are having a lot of trouble with tracking the state of our entities.
The user interfaces on the client are also fairly complex, sometimes an object graph with more than 50 objects are sent down to a single user interface, with several layers of aggregation between the entities. It is an important goal to be able to easily decide in the BLL layer, which of the objects have been modified on the client, and which objects have been newly created.
What would be the clearest approach to manage entities and entity states between the two layers? Self tracking entities? What are the most common pitfalls in this scenario?
Could those who have used STEs in a real production environment tell their experiences?
STEs are supposed to solve this scenario but they are not silver bullet. I have never used them in real project (I don't like them) but I spent some time playing with them. The main pitfalls I found are:
Coupling your data layer with your client application - you must share entity assembly between projects (it also means it is .NET only solution but it should not be a problem in your case)
Large data transfers - you pass 50 entities to clients, client change single entity and you will pass 50 entities back. It will require some fighting with STEs to avoid passing unnecessary data
Unnecessary updates to database - normally when EF works with attached entities it track changes on property level but with STEs it track changes on entity level. So if user modify single property in entity with 100 properties it will generate update with setting all of them. It will require modifying template and adding property level change tracking to avoid this.
Client application should use STEs directly (binding STEs to UI) to get most of its self tracking ability. Otherwise you will have to implement code which will move data from UI back to self tracking entity and modify its state.
They are not proxied = they don't support lazy loading (in case of WCF service it is good behavior)
I described today the way to solve this without STEs. There is also related question about tracking over web services (check #Richard's answer and provided links).
We have developed a layered application with STE's. A user interface layer with ASP.NET and ModelViewPresenter, a business layer, a WCF service layer and the data layer with Entity Framework.
When I first read about STE's the documentation said that they are easier then using custom DTO's. They should be the 'quick and easy way' and that only on really big projects you should use hand written DTO's.
But we've run in a lot of problems using STE's. One of the main problems is that if your entities come from multiple service calls (for example in a master detail view) and so from different contexts you will run into problems when composing the graphs on the server and trying to save them. So our server function still have to check manually which data has changed and then recompose the object graph on the server. A lot has been written about this topic but it's still not easy to fix.
Another problem we ran into was that the STE's wouldn't work without WCF. The change tracking is activated when the entities are serialized. We've originally designed an architecture where WCF could be disabled and the service calls would just be in process (this was a requirement for our unit tests, which would run a lot faster without wcf and be easier to setup). It turned out that STE's are not the right choice for this.
I've also noticed that developers sometimes included a lot of data in their query and just send it to the client instead of really thinking about which data they needed.
After this project we've decided to use custom DTO's with automapper from server to client and use the POCO template in our data layer in a new project.
So since you already state that your project is big I would opt for custom DTO's and service functions that are a specifically created for one goal instead of 'Update(Person person)' functions that send a lot of data
Hope this helps :)
Hi
I've been building the backend of my application (using nHibernate for data access).
So far i've had some simple web-services for manipulating my data, but now I'm required to develop a web UI on for my application (using web forms).
I've been looking at some different web frameworks (webformsMVP, spring.net web), and also at some client-side JS frameworks (knockout, angular), and I can't really decide what would be the best for me, and how to integrate it all.
I was hoping to get some insight from you guys.
I think I would like for my general workflow to be something like this:
View is created, calls its presenter.
Presenter contacts business layer to retrieve information (which in turn contacts DAO etc.)
Presenter returns a view-model object, which the view displays.
User manipulates data (maybe using some AJAX to retrieve further needed information)
View sends a view-model of the manipulated data back to presenter
Presenter translates the view-model into a model entity and sends it to business layer
now here are the parts that I find tricky:
A. How to map between view-model and model entities
I think I should use the entitie's ID to retrieve the unchanged entity from the DAO (which actually stores it in nHibernate's 2nd level cache), and perform changes on it.
(another option is to store the entity i'm currently editing in the user's session. but i'm afraid that this kind of caching would create duplicity with nHibernate's cache.)
B. How to translate changes on the view-model into changes on model entities
I'd like to have some logic when changing model entity's properties.
For example, for moving an employee from one department to another, I don't want to allow this-
department1.Employees.Remove(employee);
department2.Employees.Add(employee);
but rather this:
employee.MoveToDepartment(department2);
I'm afraid this could get complicated when translating from a view-model into a model.
Any thoughts on the above two questions, and also about any client-side / server side frameworks would be appriciated.
P.S. some quick background on my app:
-one page of the web app displays the company's structure (departments, divisions etc.) as a tree, and allows the user to click and edit the different nodes, as well as drag-and-drop nodes to change their location.
-another page displays current stock status (for each warehouse- how many products it has, how many machines are currently operative in that warehouse etc.)
- (some more pages which basically display data and allow editing...)
thanks
Jhonny
You can check out Automapper for your entity mapping. Alternatively you can write your own entity mappers and entity updaters. You can then encapsulate the business logic like Move To Department in your entity classes.
Your approach sounds just fine, and I can recommend Webforms.MVP as that is a good framework for writing testable WebForms apps without rolling your own implementation.
I'm in the middle of converting an existing app built on top of zend framework to work as a plugin within wordpress as opposed to the standalone application it currently is.
I've never really used zend so I've had to learn about it in order to know where to begin. I must say that at first I didn't think much of zend, but it's funny because the more I understand how it works the more I keep questioning why I'd want to remove dependency when it's a clearly well thought out framework. Then I'm reminded that it's because of wordpress.
Now I already know there are WP plugins to make zend play nice with WP. In fact I'm aleady using a zend framework plugin just to get the app functional within the WP admin area which is allowing me to review code, modify code, refresh the browser, review changes, debug code, again and again.
Anyway, I really don't have a specific question but instead I'm looking for advice from any zend masters out there to offer advice on how to best go about a task like this one.... so any comments, advice, examples or suggestions would be super.
One area I'm a little stuck on is converting parts of zend->db calls to work as wpdb calls instead... specifically the zend->db->select.... not sure what to do with that one.
Also on how to handle all the URL routing with automatic calls to "whatverAction" within thier respective controllers files.
Any help would be great! Thanks
You're probably facing an uphill battle trying to get some of the more major components of ZF to work in harmony with Wordpress. It sounds like you've got a full MVC app that you're trying to integrate into a second app that has very different architecture.
You probably want to think about which components handle which responsibilities. Wordpress has it's own routing and controller system that revolves around posts, pages and 'The Loop'. This is entirely different from Zend's Action Controllers and routing system.
It's possible you could write a WP hook to evaluate every incoming request and decide if it should be handled by WP or a ZF controller. However, it is doubtful you would be able to replace WP's routing system outright with ZF's or vice versa.
Same idea, where Zend_Db is concerned. There's nothing stopping you from using Zend_Db to access Wordpress's database, but trying to somehow convert or adapt Zend_db calls into wpdb calls sounds painful. If you have a large model layer, you probably want to hang on to it, and find a way to translate data from those models into the posts/pages conventions that Wordpress uses.
Personally, I would use ZF to build a robust business layer that can be queried through an object model via a Wordpress plugin, and then rely on Wordpress to do the routing and handle the views.
Zend_DB_Select is simple SQL query (but created using objects) that can be used like any other query. Just turn it into string. Ex.:
mysql_query((string)$zendDbSelectObject);