简体   繁体   中英

Assign Base Class Property Values to Derived Class

I am having a base model and a view model derived from it.

Base Model

public class Feed
{
public int Id { get; set; }
public string Name { get; set; }
public string Url { get; set; }
}

Derived Model

public class FeedViewModel :  Feed
{
   public bool EditMode { get; set; }    
}

Data Access Layer

public Feed GetFeed(){
   --db code to retreive all feed
}

Controller

public FeedController : Controller
{
   public ActionResult Index()
   {
        var data = DAL.GetFeed();
        var model = new FeedViewModel{ EditMode = true };

         model.Id = data.Id;
         model.Name = data.Name;
         model.Url = data.Url;   //This is working

       //But i dont want like this, coz i cant reassign all the proerties again. Is there any other easy way like this
        model = (FeedViewModel)data;
    } 
 }

I dont want to reassign all the properties values again to derived model. Instead i am looking for any other easy way? Any ideas?

I would suggest using composition instead of inheritance:

public class FeedViewModel
{
    public bool EditMode { get; set; }    
    public Feed Feed { get; set; }
}

Then:

var data = DAL.GetFeed();
var model = new FeedViewModel { EditMode = true, Feed = data };

Fundamentally I wouldn't expect a view-model to be the same as the model it's a view-model for - I'd want to be able to use that model, but I wouldn't expect to use inheritance for it.

The view model can expose properties of the view via delegation, should you wish - possibly with property change notification.

EDIT: Another alternative (which may not be applicable either - we have no idea how much you can change in this project) is to make your GetFeed method generic in the DAL:

public Feed GetFeed<T>() where T : Feed, new()
{
   --db code to retreive all feed
}

Then you can call it with:

var model = DAL.GetFeed<FeedViewModel>();
model.EditMode = true;

Using AutoMapper .

Create an AutoMapperConfig class like so:

public class AutoMapperConfig
{
    public static void CreateMaps()
    {
        // Feed domain / view model mappings
        Mapper.CreateMap<FeedViewModel, Feed>()
        Mapper.CreateMap<Feed, FeedViewModel>();            
    }
}

Call your CreateMaps() method from your Global.asax to register the mappings at application start.

Now in your Action all you need to do is this:

public FeedController : Controller
{
   public ActionResult Index()
   {
        var data = DAL.GetFeed();
        var model = Mapper.Map<Feed, FeedViewModel>(data);
        model.EditMode = true;

        return View(model)
    } 
 }

And then in some other action you just reverse the process

[HttpPost]
public ActionResult SomeOtherAction()
{
    FeedViewModel model = new FeedViewModel();
    TryUpdateModel(model);

    if (ModelState.IsValid)
    {
        var feed = Mapper.Map<FeedViewModel, Feed>(model);
        DAL.UpdateFeed(feed);
    }

    return View(model)
}

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