0

above is code which I use to manipulate with data from my domain to dto model, which I use for wcf serialization. My question is how to pass object mother with collection of childrens into MotherDTO. With current code situation I pass only data without collection children. Do I need to use session in line and to add session MotherDTO dto = new MotherDTO(data, session); and to use that session to retreive collection of childrens in dto. If so, how ? Please help.

Regards,

public MotherDTO GetMotherData()
    {
        using (ISession session = instance.OpenSession())
        {
            using (ITransaction tx = session.BeginTransaction())
            {
                Mother data = session.Query<Mother>()
                    .Fetch(x => x.Childrens)
                    .FirstOrDefault();
                tx.Commit();

                MotherDTO dto = new MotherDTO(data);
                return dto;
            }
        }
    }

MotherDTO.cs

        public MotherDTO(Mother x)
        {
            Name = x.Name;
            List<Children>Childrens= new List<Children>();
            foreach (Children obj in x.Childrens)
            {
                States.Add(obj);
            }
        }

Mother.cs

 public virtual string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
            }
        }


        public virtual Iesi.Collections.Generic.ISet<Children> Childrens
        {
            get
            {
                return _Childrens;
            }
            set
            {
                if (_Childrens == value)
                    return;
                _Childrens = value;
            }
        }
0

1 Answer 1

0

Since you're already (eager) loading your Children collection you can use Automapper to populate your DTOs.
If you want to know how to configure Automapper to work with nested collection you can read here:

Mapper.CreateMap<Order, OrderDto>()
    .ForMember(dest => dest.OrderLineDtos, opt => opt.MapFrom(src => src.OrderLines));
Mapper.CreateMap<OrderLine, OrderLineDto>()
    .ForMember(dest => dest.ParentOrderDto, opt => opt.MapFrom(src => src.ParentOrder));
Mapper.AssertConfigurationIsValid();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.