简体   繁体   中英

c# given instance of base class, update base class properties of derived class

I have a view model (using MVC4) that inherits a base class

public class TimeTaskViewModel : TimeDetailTask
{
    public string TaskTypeDescription { get; set; }

}

I have a method to convert view model to class, given an instance of that class. I'd like to be able to something like this so as to not have to explicitly set every property of the base class, then simply update the additional properties of the view model:

public TimeTaskViewModel ConvertClassToViewModel(TimeDetailTask entity)
{
    TimeTaskViewModel viewModel = new TimeTaskViewModel();

    viewModel.base = entity; 
    viewModel.TaskTypeDescription = entity.TaskTypes.TaskTypeDescription;
    return viewModel;
}

Anyway to do that? Or am I way off base here?

Note: I need a flat class as a result as the view Model will also be used in an IEnumerable format to pass to a Kendo UI Grid on the front end and it can't handle complex classes.

No, it can't be done exactly as you are asking. Here are some options:

1: Change your TimeTaskViewModel class to contain a TimeDetailTask instead of extend it.

public class TimeTaskViewModel
{
    public TimeDetailTask TimeDetailTask { get; set; }
    public string TaskTypeDescription { get; set; }
}

2: Create a TimeTaskViewModel(TimeDetailTask) constructor and copy the properties manually there.

public class TimeTaskViewModel : TimeDetailTask
{
    public string TaskTypeDescription { get; set; }
    public TimeTaskViewModel(TimeDetailTask baseTask)
    {
        this.SomeProperty = baseTask.SomeProperty;
        // and so on
    }
}

3: Use AutoMapper , reflection, or something similar to copy properties from one to the other.

Mapper.CreateMap<TimeDetailTask, TimeTaskViewModel>();

// Perform mapping

TimeTaskViewModel viewModel = 
                       Mapper.Map<TimeDetailTask, TimeTaskViewModel>(baseTask);

It sounds like you're looking for easy way for mapping information from one class to an entirely different class that happens to have some of the same properties. I don't think inheritance is the answer you're looking for. It sounds like you want something like AutoMapper , which will allow you to set up rules for mapping properties from one object to another.

For instance, if you're trying to map from your view model to a database entity before writing to the database, you'd do something like:

var entity = Mapper.Map(viewModel);

For straight up matches in property names, AutoMapper would already take care of it, such as for instance copying TimeTaskViewModel.TaskTypeDescription to YourEntity.TaskTypeDescription. In the case where the name isn't a perfect match or you need to do some manipulation of the data (such as casting a string to an int), though, you can set up rules for it in a map file.

In my opinion, AutoMapper is a great tool when you're using it to copy things from a class with one property name to another class with the exact same property name. It's still an okay tool when you need to copy the same datatype between differently named properties, or perform really simple conversions (such as int to string). It starts to feel like more trouble when it's worth if you are doing complex conversions, though (mainly because I find it difficult to debug and unit test the mapping files), at which point it often feels like you should just write your own mapping function. Of course, nothing says you can't use it for the simple cases and roll your own mapping function for the more complex ones.

Try this

//Create a property for TimeDetailTask in TimeTaskViewModel class

public class TimeTaskViewModel : TimeDetailTask
{
    public string TaskTypeDescription { get; set; }

   public TimeDetailTask TimeDetailTaskProperty { get; set; }
}

//Then you assign the entity values to modelclass  TimeDetailTaskProperty 

public TimeTaskViewModel ConvertClassToViewModel(TimeDetailTask entity)
{
    TimeTaskViewModel viewModel = new TimeTaskViewModel ();    
    viewModel.TimeDetailTaskProperty =entity;
    return viewModel;
}

//finally you can get values from this TimeDetailTaskProperty using TimeTaskViewModel object .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM