Convert entities to plain DTOs

Apr 11, 2012 at 1:58 PM

Hello,

I've just downloaded the latest version of Nhydrate.
In earlier versions extension methods where generated to convert entities to plain DTOs.
Is this something that's planned for in comming versions or is there another way of doing this when EF is used?

 

/Patrik

 

Apr 18, 2012 at 3:35 PM
Edited Apr 18, 2012 at 3:35 PM

Hi,

I'm also looking for a simple solution to convert entities to DTOs and back when using the visual modeler.
Any hint is appreciated.

Jan.

Coordinator
Apr 19, 2012 at 1:36 AM

I am looking into a solution for this. It should be out shortly.

Apr 20, 2012 at 2:00 PM

Great, thx.

Btw, I love nHydrate: it helps me write better applications, it speeds up my development process and it makes my life easier :-)
So where is that donate button?

Coordinator
Apr 22, 2012 at 8:43 PM
Edited Apr 22, 2012 at 8:44 PM

Here is the DTO mapper. It actually will work for just about any two objects not just DTOs.

 

public static class DTOMapper
{
	/// <summary>
	/// Converts any object into a strongly-typed data transfer object
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="source"></param>
	/// <returns></returns>
	public static T DTOMap<S, T>(S source)
					where T : new()
	{
		var count = 0;
		if (source == null)
			throw new Exception("The source object cannot be null!");
		var target = new T();
		var propertyList1 = source.GetType().GetProperties();
		var propertyList2 = target.GetType().GetProperties();
		foreach (var prop1 in propertyList1)
		{
			var prop2 = propertyList2.FirstOrDefault(x => x.Name == prop1.Name);
			if (prop2 != null && prop2.CanWrite && prop2.PropertyType.GetGenericArguments().Length == 0 && prop2.PropertyType == prop1.PropertyType)
			{
				prop2.SetValue(target, prop1.GetValue(source, null), null);
				count++;
			}
		}
		return target;
	}
 
	/// <summary>
	/// Uses a strongly-typed data transfer object to populate a target object with matching properties
	/// </summary>
	/// <typeparam name="T"></typeparam>
	/// <param name="dto"></param>
	/// <returns></returns>
	public static void DTOMap<S, T>(S dto, T target)
	{
		if (dto == null) throw new Exception("The DTO cannot be null!");
		if (target == null) throw new Exception("The target object cannot be null!");
 
		var count = 0;
		var propertyList1 = dto.GetType().GetProperties();
		var propertyList2 = target.GetType().GetProperties();
		foreach (var prop1 in propertyList1)
		{
			var prop2 = propertyList2.FirstOrDefault(x => x.Name == prop1.Name);
			if (prop2 != null && prop2.CanWrite && prop2.PropertyType.GetGenericArguments().Length == 0 && prop2.PropertyType == prop1.PropertyType)
			{
				var isReadOnly = false;
				var roAtt = prop2.GetCustomAttributes(typeof(System.ComponentModel.ReadOnlyAttribute), true).FirstOrDefault() as System.ComponentModel.ReadOnlyAttribute;
				if (roAtt != null) isReadOnly = roAtt.IsReadOnly;
 
				if (!isReadOnly)
				{
					prop2.SetValue(target, prop1.GetValue(dto, null), null);
					count++;
				}
			}
		}
	}
 
}

Coordinator
Apr 22, 2012 at 8:44 PM

For anyone interested the donate link is on the nHydrate.org main site under the PayPal icon.

https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9008263

Apr 24, 2012 at 10:26 AM

Your DTO mapper is a good starting point for my use case, thx.

I must say your support (response time and resolution time) is better than most paid product I use. You might want to enlarge your PayPal button on the main site, I really had to search for it.

Coordinator
Apr 24, 2012 at 12:56 PM
Thanks for your support. I am working on a new site design that will make everything better looking and hopefully easier. We are also exploring monetizing the product at least somewhat. Everyone who gave a donation will not be affected as we will send license keys to all if and when that day comes.

--Chris Davis

On Tue, Apr 24, 2012 at 5:26 AM, jeankedotcom <notifications@codeplex.com> wrote:

From: jeankedotcom

Your DTO mapper is a good starting point for my use case, thx.

I must say your support (response time and resolution time) is better than most paid product I use. You might want to enlarge your PayPal button on the main site, I really had to search for it.

Read the full discussion online.

To add a post to this discussion, reply to this email (nhydrate@discussions.codeplex.com)

To start a new discussion for this project, email nhydrate@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com




--
If you have any questions please let me know.

Thank you for your interest,
Christopher Davis, MCAD
(+1) 866.GRAVBOX
(+1) 866.472.8269

Code Generation: http://www.nhydrate.org
Consulting Site: http://www.widgetsphere.com
Cloud Repositories: http://www.celeriq.com