Chintoo Semi

Total Post:135

Posted by  Chintoo Semi
 1043  View(s)
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();
        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
           // ...

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


    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.

      Modified On Apr-05-2018 11:44:42 PM



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