WHAT IS DYNAMIC FACTORY IN MVC?

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
 842  View(s)
Ratings:
Rate this:

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 

abstract class Base {

          Guid Id { get; set; }

          string Name { get; set; }

          string Description { get; set; }

        }

 

        class Alpha : Base { // }

        class Beta : Base { // }

        class Sigma : Base { // }

        class Delta : Base { // }

 

        class ObjectViewModel {

          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?

  1. Kamlakar Singh

    Post:194

    Points:1396
    Re: What is Dynamic factory in mvc?

    How about creating a custom model binder:

    public class BaseModelBinder : DefaultModelBinder

            {

                private Type _type;

     

                protected override ICustomTypeDescriptor GetTypeDescriptor(ControllerContext controllerContext, ModelBindingContext bindingContext)

                {

                    return TypeDescriptor.GetProvider(_type).GetTypeDescriptor(_type);

                }

     

                protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)

                {

                    var result = bindingContext.ValueProvider.GetValue("type");

                    if (result == null)

                    {

                        throw new Exception("please provide a valid type parameter");

                    }

     

                    _type = Type.GetType(result.AttemptedValue);

                    if (_type == null || !typeof(Base).IsAssignableFrom(_type))

                    {

                        throw new Exception("please provide a valid type parameter");

                    }

                    return Activator.CreateInstance(_type);

                }

            }

    which you would register in Application_Start :

    ModelBinders.Binders.Add(typeof(Base), new BaseModelBinder());

    and now you could have the following controller action:

    public ActionResult Foo(Base model)

            {

                ...

            }

    Now when you are invoking this action simply pass an additional type parameter indicating the concrete instance you would like to create.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!