Riddick
Riddick

Reputation: 1318

asp.net c# Automap a class from within that class

To best describe what I want to happen, i'll show what i'm doing, as to me it makes sense that this would work ...

public class foo()
{
    public foo()
    {
        MyContext db = new MyContext();
        foobar = db.foobar.first();
        this = Mapper.Map<bar, foo>(foobar);
    }
}

Basically, I want to use automapper within the destination class to map from the source class within the destination classes constructor.

Is there a way to do this?

Upvotes: 0

Views: 768

Answers (1)

Kevin Junghans
Kevin Junghans

Reputation: 17540

You cannot do this because this is read only in C#. You cannot assign this a value in the constructor. Not cool to try to change the reference of an object in its constructor. You will have to do the mapping manually and assign each individual property. I would also question if it as a good practice to assign an object values from a database or service in a default constructor. It is not very transparent to the user of the object what is going on and you can get an exception in your constructor.

Upvotes: 1

Related Questions