Is this a typical use case for IOC?

前端 未结 2 1586
灰色年华
灰色年华 2021-02-19 03:44

My current application allows users to define custom web forms through a set of admin screens. it\'s essentially an EAV type application. As such, I can\'t hard code HTML or AS

2条回答
  •  醉话见心
    2021-02-19 04:01

    As I understand the question, you need to modify the Form before sending it off to the UI layer. That sounds to me like a Decorator would be in place. Keep the old IFormService interface without the IFormViewCreator.

    You can now create one or more Decorating FormService(s) that implement the desired filtering or modification.

    public class MyDecoratingFormService : IFormService
    {
        private readonly IFormService formService;
    
        public MyDecoratingFormService(IFormService formService)
        {
            if(formService == null)
            {
                throw new ArgumentNullException("formService");
            }
    
            this.formService = formService;
        }
    
        public Form OpenFormForEditing(int formId)
        {
            var form = this.formService.OpenFormForEditing(formId);
            return this.TransformForm(form);
        }   
    
        public Form OpenFormForViewing(int formId)
        {
            var form = this.formService.OpenFormForViewing(formId);
            return this.TransformForm(form);
        }
    
        public Form TransformForm(Form form)
        {
            // Implement transformation/filtering/modification here
        }
    }
    

    You can now decorate your original IFormService implementation with one or more of such Decorators.

    IFormService formService = new MyDecoratingFormService(new MyFormService());
    

    You can wrap as many Decorators (each with their own responsibility) around each other as you'd like.

    There's no explicit need for a DI Container to do this, but it fits nicely with other DI patterns. I use Decorator all the time :)

提交回复
热议问题