0

Can I set the newly created object equal to the supplied object in the constructor of the class?

I want to do something like this:

public MyClass
{

public MyClass(MyClassDto myClassDto)
{
MyClass convertedMyClassObj = AutoMapper.LoadEntityFromDto<MyProject.DTO.MyClassDto, MyClass>(myClassDto);

//Assign the new object being created here equal to convertedMyClassObj:
this = convertedMyClassObj;    //I want to reference the current object in place of 'this'.


} 

public int MyProperty1 { get; set;}
public int MyProperty2 { get; set;}
public int MyProperty3 { get; set;}

}

I don't want to do it property by property. Do I have to use a a singleton like procedure to return the copied instance in a GetInstance() method. Because constructor has no return parameter. I just want to know if this is possible or not.

zeppelin
  • 451
  • 1
  • 4
  • 24
  • 1
    If, by "singleton like procedure", you mean a `static` method, that would be the way to do it. You can also make an extension method on the dto (`public static MyClass ToMyClass(this MyClassDto dto)`) that would call the auto-mapper. [There is basically a dupe for this](http://stackoverflow.com/questions/18285087/why-cant-i-set-this-to-a-value-in-c) – Zdeněk Jelínek Oct 28 '15 at 13:35

4 Answers4

1

You won't be able to do that because this can't be set. But you are already using AutoMapper which creates your object from your DTO. You could just use a static method if you want a shorthand.

public MyClass
{
    public static MyClass FromDto(MyClassDto myClassDto)
    {
        return AutoMapper.LoadEntityFromDto<MyProject.DTO.MyClassDto, MyClass>(myClassDto);
    }

    //Properties
}

Then you would just use it with

var myClass = MyClass.FromDto(myClassDto);
Kevin
  • 749
  • 5
  • 10
1

There is an overload of AutoMapper.Map that takes a reference to an existing destination object.

public static TDestination Map<TSource, TDestination>(TSource source, TDestination destination);

You can use it with this to set the properties based on the DTO:

class MyClass
{
    public MyClass(MyClassDTO dto)
    {
        AutoMapper.Mapper.Map(dto, this);
    }
}    

The problem with this approach is that MyClass gets coupled to Automapper and MyClassDTO. It might be better to move the responsibility of converting MyClassDTO to MyClass to a separate class.

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
1

You're essentially talking about creating a factory, and if you look at the factory pattern, the key is a static class that does the work of instantiating the new "thing". So, no, you can't really do this like you're trying to, but if you want to encapsulate the AutoMapper logic, then you can simply do something like:

public static class MyClassFactory
{
    public static MyClass FromMyClassDto(MyClassDto myClassDto)
    {
        return AutoMapper.LoadEntityFromDto<MyProject.DTO.MyClassDto, MyClass>(myClassDto);
    }
}

Then:

var myClassInstance = MyClassFactory.FromMyClassDto(myClassDto);
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Got it. Thanks. So basically this work should be handled in a method and not in the constructor itself, Also creating a new object is unnecessary I can just call the static method of the class to return the object generated by Automapper rather than instantiating a new object and setting its reference to the one returned by Automapper. I can't accept multiple answers, so up-voted yours. – zeppelin Oct 29 '15 at 13:27
0

You cannot assign a value to this in a class. You'll need to copy the values over.

Servy
  • 202,030
  • 26
  • 332
  • 449