AutoMapper 2 way mapping - c#

I am using AutoMapper.org to map my DTO objects to Model objects in MVC4. DTO objects are retrieved from SOAP web services. The operations on the services are mostly CRUD.
This works nicely.
I have 2 questions. Firstly, is it bad practise to map both ways (2 way mapping). So when I update on screen map the Model to a DTO, as well as the orginal mapping of DTO to Model?
Second question, is it possible for AutoMapper to map enums?

I map both ways without issue - I map from the DTOs to the business objects to get the data, and map back the other way to save the data. This is so that the DTOs that are used in my WCF service are reusable (I'm using CSLA framework and the data portal model in CSLA doesn't really let 3rd parties consume the service without having access to my bizobj library).
It does mean that some of the business logic is repeated in the web layer, but since the rules are sparse this isn't a big issue
In my case I don't think it's a bad thing. I have a very simple data model which is mostly reads, there is only the occasional time when data goes back across to be modified.
As far as I know it maps enums natively (assuming it's a direct enum to enum - since enum is just a primitive underneath), but you can always provide your own custom type converters to resolve any enum issues or if you need to do string parsing for enums.

Related

Data and Model objects management in MVC4

I have Data Access Layer objects which represent data retrieved from a webservice.
In other hand, I have MVC Model objects which represent the same data structure but enforced with Data Annotation attributes in order to perform UI validation, etc.
Apparently, I have to swap data between DAL and Model objects for every read/write operation.
The question is whether I can simplify this approach to have just one set of objects and use them for both layers (Data Access and Model). What is the best practice?
I know there are some NuGets to sync data between those two kinds of objects. Could you please suggest which one to use? Thank you.
The best practice is to have a separate set of domain objects and mvc models. That way you can wire up all of your annotations on your viewmodel and people won't be able to post fields that you don't want the user to edit
You can also use AutoMapper to simplify binding between the two

NHibernate DTO with deep object graph

I am writing a smart client WPF application using MVVM that communicates with a WCF service layer containing the business logic and domain objects that uses NHibernate to manage persistence. We are in control of both sides of the wire.
Currently, I am working on creating a screen to Edit Product Details it has a tab control with each tab representing some aspect of the Product such as Main Details, Product Class, Container Type and so on. In the end, there will probably be at least 5 of these tabs.
Up to now I have been working on transforming simple domain objects to DTOs using SetResultTransformer and this has been working quite nicely.
Now that I am getting to a more complicated object I am getting a bit stuck. I would like to return a DTO to be displayed that contains the Main Product details, categories and classes. As far as categories and classes are concerned I would not want to return every single property of the domain object.
Questions:
1) How do people go about creating a DTO where there are several one to
many collections to return as in this example?
2) Is there any concerns about the DTO becoming too large?
3) When sending the DTO back to the back end is it better to send the same type of DTO with the updated values or some other more command oriented DTO?
Thanks for any help
Alex
We are currently using pretty big DTOs and it is working pretty fine. NHibernate is doing a lot of lazy loading, so this helps with big objects.
We are using bags for one to many relations, they are lazy loaded and are working pretty well.
Depending on the type of application lazy loading can be a bit of a problem. We had some problems with our rich client application with big DTOs but with some planning and a sound architecture it works pretty well.
I don't know if large DTOs are really a problem with NHibernate, but so far we don't have got any problems.
We are sending the whole object back and forth and it is doing well. NHibernate updates just the changed fields and this is really nice.
I wouldn't serialize the NHIbernate objects over web services or something like that (I don't know the WCF service layer and how it communicates with your application). If I am transferring data through web services I am generating new data objects and fill them accoringly, transfer them back and forth and update the NHibernate objects with those.
Have you tried Automapper? I do all my DTO mappings with Automapper and it works like a charm.
Have a look at automapper. I'm sure you'll like it.

Best pattern to implement Translators between DB type object to Business type object

Our project had implemented Entity framework and we were floating the entity object type across all layers. Due to the sheer size of those objects we have decided to have business entities and data entities. Now I am confused as in
1) Where to implement the translation of these objects
2) How i.e any patterns or practices which I should follow to get better results.
*Edited *
Thanks for all the replies I am looking into Automapper and found it might fit well. But instead of using a library I wanted to create my own translator which would seem like reinventing the wheel. But my thought is I would have a lot more control on things then.
So going back to the original quesiton
On which layer should I implement my translation i.e we have an N-tire app and there are two schools of thought one being we should have a business layer and treat it as a facade and implement all translation and other business logic there and treat all others layer as a dumb layer which have no knowledge about the other layers and keep minimum reference of them so the data layer would be throwing out the same data entities and can be reused in another project where the Business object are somewhat different then our current object. The second school of thought is that the data layer should be returning and accepting business entities which would restrict non-database developers from unnecessary call on the db layer to create a business entitie.
Any patterns which are there for such kind of translation libraries.
This sounds similar to the problem faced by many people when using the MVVM pattern in association with an ORM. Automapper (http://automapper.codeplex.com/) is a great library that provides clean, convention based translation between types.
Please take a look the following, it has a really nice overview of all of microsoft techs. It might be a bit complicated though but it shows the layers very nicely.
N Layer sample app
You can try DTO's and ValueInjecter if you are moving data over the wire, or the same tool if you use ViewModels to bind to Views ... AutoMapper is great too.

What is the best practice for sending data to the client: POCO or DTO?

I'm starting a project using EF 4 and POCO.
What is the best practice for sending data to the client ? Should I send the POCO or I should have a DTO instead?
Are there any issue I should be aware of when sending the entity (that is disconnected from the context) to the client ?
Is it a recommended practice to send the POCO to the client layer?
I believe that we are mixing 2 definitions here that don't have relation with each other.
DTO or Data Transfer Object is a design pattern, you can use it to transfer data between layers, and also they don't have behavior. Martin Fowler explains this very well at: http://www.martinfowler.com/eaaCatalog/dataTransferObject.html
In the other hand we have POCO or Plain Old CLR Object. But to talk about POCO, we have to know where it started, that is POJO, or Plain Old Java Object. Martin Fowler with two partners coined the term and he explains it here: http://www.martinfowler.com/bliki/POJO.html
So POCOs can have behavior and everything you want. They are the same common classes you write in your daily-basis, they just gave them that name to call them in a short and easy-remember way.
In anwser to your second question, I think the best approach and the one I always go for is sending DTOs from the Busines Layer to everything that uses it (e.g.: your services, web site, desktop app, mobile app, etc.). This is because they don't have behavior and not pretty much than only properties in most of the cases, so they are light-weight and ideally to use in services, and of course, they don't reveal sensitive data from your business.
That being said, if you are planning to use DTO, I can recommend you to download EntitiesToDTOs, an Entity Framework DTO Generator that I just recently published at CodePlex, it is free and open source. Go to http://entitiestodtos.codeplex.com
For me, one of the main reasons to use EF4 with POCO is the fact that you don't need DTO's. I can understand using DTO's with traditional EDMX files where your entities are pretty bloated, but this isn't the case.
Your POCO obviously needs to be serializable, but there really shouldn't be any issues specific to sending POCO entities that don't also occur with DTO's.
I have a bit different opinion from above opinions.
I believe DTO or ViewModel is still needed for out side of the Server Layer.
In real world application, there is a few view layer which only need one Domain Object, that is, almost every views need multiple Domain Objects.
And all those Domain Objects are wrapped in one DTO or ViewModel Class.
This is why I insist DTO or ViewModel is still needed even though they are POCO.
I would consider EF4 entities business models AND viewmodels rolled into one. They already implement PropertyChanged out of the box, for example. Partial classes can provide custom functionality if you need. Mirroring the entities with your own safety layer creates unnecessary work and maintenance, in my opinion.
I'm a believer in separation of business logic and everything else. However in the case of EF4 the work is already done for you. Go nuts.

Update entity through DTO

I have got a common scenario: SL app and WCF service as data source.
I use DTOs for transferring business objects over the wire. I have an issue - Update entity. In the client form, I have a binded DTO and I send this to WCF. I want to know a nice way to update a real database entity by DTO data. In WCF, I use NHibernate for the DAL. My solution is getting the attached entity by id from the database and I copy all properties (with nested objects) from the DTO.
But I think it's not flexible or good. Can you advise me a better way?
Like anything DTOs have pros & cons but when your DTOs are exactly the same as your Entities it can seem like a burden to do the property mapping manually.
Try looking at AutoMapper which should help you speed up the property mapping.

Categories

Resources