DAAB 5 Complex Domain Object Map

Topics: Data Access Application Block
Jul 7, 2010 at 9:02 PM
Edited Jul 7, 2010 at 9:05 PM

I am having a little problem mapping a domain object that contains a domain object.


Vendor -- another domain object



When I execute my SQL statement: "SELECT Id, Name, Vendor FROM Product WHERE Id = 1", it won't map the vendor field. The vendor field in the product table is a guid for the vendor ID in the vendor table.

So I am using a mapper like this: IRowMapper<Product> rowMapper = MapBuilder<Product>.MapAllProperties()
                                                                                                           .DoNotMap(a => a.Vendor)
                                                                                                           .Map(a => a.Vendor.Id).ToColumn("Vendor")

This doesn't work. I can get everything back fine except for the vendor id. I assume that the vendor object needs to be instantiated before it can be assigned a value, but how would I do something like that using DAAB?

BTW, I love the new additions to the DAAB.

Thanks, Vincent

Jul 8, 2010 at 7:02 AM

Instead of trying to find ways of instantiating the vendor object, I recommend utilizing the Map(property).WithFunc(function) to achieve this.  For me, this will also give you quite pretty simple approach too.

All you need are as follows, please see sample below;

static void Main(string[] args)
Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>();

IRowMapper<Product> rowMapper = MapBuilder<Product>.MapNoProperties()
                .Map(x => x.ProductId).ToColumn("ProductId")
                .Map(x => x.ProductName).ToColumn("ProductName")
                .Map(x => x.Vendor)
                    .WithFunc(y => VendorMapProperties(y.GetValue(2).ToString(), 

var results = _db.ExecuteSqlStringAccessor<Product>("Select * From Products", rowMapper);


//Function used in mapping Vendor object properties
 static Vendor VendorMapProperties(string name, int id)
      var typeCount = new Vendor { Name = name, Id = id };
      return typeCount;


//Business Objects
public class Product
        public int ProductId{ get; set; }

        public string ProductName { get; set; }

        public Vendor Vendor { get; set; }


    public class Vendor
        public int Id { get; set; }
        public string Name { get; set; }


Gino Terrado
Global Technology and Solutions
Avanade, Inc.



Jul 8, 2010 at 7:39 PM

That worked perfectly!  Thanks.