Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I write multi tier applications. Each tier has it's own POCO version of each db object. As I pass the objects up and down the layers I have to convert it from one layers version to the next.

I could create an extension that did this for me by parsing the property info, but I'm sure there must be a better way. Am I missing something obvious?

Also, what would I do if the data types don't exactly match (i.e. Linq.Binary => byte[])?

Thanks
Andy ^_^



As an example, here is what I am currently doing between my UIL (user interface layer?) and BLL (business logic layer):

C#
using System;
using System.Collections.Generic;
using System.IO;
using F = DataFactory.Factory; //Generic data factory
using W = DataFactory.Workers; //specific data workers
using M = DataFactory.Models;  //specific BLL models

namespace CustomControls.Projections
{
    // easy to access in UIL
    public class Upload
    {
        public int Id { get; set; }
        public string FileName { get; set; }
        public byte[] File { get; set; }
        public int? SupplierId { get; set; }

        public MemoryStream ToStream()
        {
            // just coz it's easier to read when I use it.  Some files (not these) will need to be read in from a file system etc.
            return new MemoryStream(File);
        }

        public static List<Upload> SelectAll()
        {
            return F.DataFactory<W.Upload>.SelectAll<M.Upload>().ConvertAll(Converter);
        }

        public static Upload SelectById(int id)
        {
            return Converter(F.DataFactory<W.Upload>.SelectById<M.Upload>(id));
        }

        public static int Save(Upload item, bool updateRelated = false)
        {
            return F.DataFactory<W.Upload>.Save<int>(Converter(item), updateRelated);
        }

        private static M.Upload Converter(Upload arg)
        {
            if (arg == null)
                return null;
            return new M.Upload
            {
                Id = arg.Id,
                FileName = arg.FileName,
                File = arg.File,
                SupplierId = arg.SupplierId
            };
        }

        private static Upload Converter(M.Upload arg)
        {
            if (arg == null)
                return null;
            return new Upload
            {
                Id = arg.Id,
                FileName = arg.FileName,
                File = arg.File,
                SupplierId = arg.SupplierId
            };
        }
    }
}
Posted
Updated 18-Jan-16 5:57am
v2
Comments
Sascha Lefèvre 18-Jan-16 12:27pm    
Some mapping solution like AutoMapper?
Andy Lanng 18-Jan-16 12:29pm    
I heard that AutoMapper was not a good thing to use. No idea if that's still true. Thanks for reminding me of this, though. I had forgotten the name ^_^
Sascha Lefèvre 18-Jan-16 12:38pm    
Don't really know either :-) But there are other mapping solutions as well. Or you could spin your own. If you don't want to do the mapping manually and individually for each POCO-"transition" you're pretty much left with reflection. If you then don't do the POCO-initialization through PropertyInfos but with compiled Expressions it'll be as fast as what you have now once "reflected".
Andy Lanng 19-Jan-16 4:05am    
hmm - this is what I was thinking. Maybe I'll introduce Automapper in my next project and compare it against what I have now.
Kevin Marois 18-Jan-16 12:33pm    
AutoMapper

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900