henkmollema / Dapper-FluentMap

Provides a simple API to fluently map POCO properties to database columns when using Dapper.
MIT License
427 stars 86 forks source link

Map Nested Object #85

Closed markofranjic closed 1 year ago

markofranjic commented 5 years ago

Hi,

I tried map nested object over FluentMap. I call store procedure and I got columns like this. Id Description Manufacture_Id Manufacture_Name Category_Id Category_Name Status_Id Status_Name

Columns which have underscore they are nested type.

public class Asset
{
   public int Id {get;set;}
   public string Description {get;set;}
   public Manufacture {get;set;}
   public Category {get;set;}
   public Status {get;set;}
}

public class Manufacture
{
   public int Id {get;set;}
   public int Name {get;set;}
}

public class Category
{
   public int Id {get;set;}
   public int Name {get;set;}
}

public class Status
{
   public int Id {get;set;}
   public int Name {get;set;}
}

I call store procedure like this var result = connection.Query<Asset>("dbo.GetAsset", CommandType.StoredProcedure);

And map doesn't work

I map like this

public class CategoryMap : EntityMap<Category>
    {
        public CategoryMap()
        {
            Map(p => p.Id)
                .ToColumn("Category_Id");

            Map(p => p.Name)
                .ToColumn("Category_Name");
        }
    }

Also tried map to parent class

public class AssetMap : EntityMap<Asset>
    {
        public AssetMap()
        {
            Map(p => p.Status.Id).ToColumn("Status_Id");
            Map(p => p.Status.Name).ToColumn("Status_Name");
        }
    }
FluentMapper.Initialize(config =>
            {
                config.AddMap(new AssetMap());
                config.AddMap(new StatusMap());
                config.AddMap(new CategoryMap());
                config.AddMap(new ManufactureMap());
            });

and that doesn't work.

Could you please help me with that?

vbaros commented 5 years ago

Did you find the solution for this issue?

HyperboLemuR commented 5 years ago

Also looking for a solution to this. Below is what I am looking at

public class AspectStartEnd

{

        public string Aspect { get; set; }

        public DateTime EffectiveDate { get; set; }

        public DateTime EndDate { get; set; }

}
public class Member

{

    public int ID { get; set; }

    public AspectStartEnd Software { get; set; }

}
public class MemberMap : EntityMap<Member>

{

    public MemberMap()

    {

        Map(m => m.ID).ToColumn("m_id", caseSensitive: false);

        Map(m => m.Software.Aspect).ToColumn("sw_name", caseSensitive: false);

    }

}
var member = conn.QueryFirstOrDefault<Member>("MemberGet", new { Id = id }, commandType: CommandType.StoredProcedure);

the json for this object looks like:

{
    "ID":164522,
    "Software":null,
    "Name":"Macrosoft"
}

where i would expect it to look like

{
    "ID":164522,
    "Software": {
        "Aspect":"Macrosoft"
    }
}
Hessam8008 commented 5 years ago

I have the same issue and cannot afford it. Any solution? `
public class ComputedHistoryMap : EntityMap

{

    public ComputedHistoryMap()

    {

        Map(x => x.Start.Time).ToColumn("StartTime");
        Map(x => x.Start.Type).ToColumn("StartType");
        Map(x => x.End.Time).ToColumn("EndTime");
        Map(x => x.End.Type).ToColumn("EndType");
        Map(x => x.LeaveItemID).ToColumn("LeaveID");
        Map(x => x.Type).ToColumn("ComputedType");

    }

}

` Exception throw: Duplicate mapping detected. Property 'Time' is already mapped to column 'Time'. I think Map() uses the name of properties, not PropertyInfo object. BTW, it cannot set value for the nested object !!

grandsilence commented 5 years ago

Nested objects still not working( That's very strange behavior. Any fix?

Joao98Victor commented 5 years ago

I'm facing the same problem, this older issue was oppened about it, but I guess it still without a solution. @g-adolph gave us a solution that can work in his gist and he mentioned about doing a pull request. He said that fluent mapper must have a more sophisticated reflection algorithms do to what we need.

Link for his gist - > https://gist.github.com/g-adolph/f0eb5b977006b96e12dff27c4e61212f Issue -> https://github.com/henkmollema/Dapper-FluentMap/issues/46

henkmollema commented 1 year ago

I'm archiving this repository as I'm not using this library myself anymore and have no time maintaining it. Thanks for using it.