forum

Home / DeveloperSection / Forums / What is Dynamic factory in mvc?

What is Dynamic factory in mvc?

Anonymous User174611-Oct-2014

I have a situation where I have 8 objects that all inherit from one object. For certain purposes, these objectsmust be their own classes, but at the initial creation there is nothing different about them.

They cannot be created as the base object, they must be added to the database as their own object. This is not negotiable.

It seems stupid to create 16 controller actions and 8 views for this. I only have to know which type is being added, never anything different. So basically I need to do the following 

abstractclassBase {
          Guid Id { get; set; }
          string Name { get; set; }
          string Description { get; set; }
        }
 
        classAlpha : Base { // }
        classBeta : Base { // }
        classSigma : Base { // }
        classDelta : Base { // }
 
        classObjectViewModel {
          string Name { get; set; }
          string Description { get; set; }
        }
 
 
        ActionResult Create(){
         return View();
        }
 
        [HttpPost]
        ActionResult Create(ObjectViewModel model) {
           // determine which type needs to be created
           Factory.Create(model); // the factory will create the right object based on the type
           repository.Add(factoryCreatedObject);
           // ...
        }

It seems simple enough, but it just is not working. I've tried putting a System.Type property on the ViewModel - it just doesn't work. The only thing I have been able to get to work is to use a gigantic switch statement, but that seems like a poor approach.

Is there any way I can get this done without excessive redundancy?


Updated on 11-Oct-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By