How can I keep my MVC views, models, and model bindings as clean as possible?

I am new to MVC, and as I get more involved in the whole structure, I find that model lines are becoming tough to maintain.

Let me explain ...

I am writing a basic CRUD-over-database application. My domain models will be very rich. Trying to limit my controllers as much as possible, I configured them so that in the Create / Edit commands the parameter for the action was a richly populated instance of my domain model. To do this, I implemented a custom connectivity device.

As a result, however, this custom communication device is very specific to presentation and model. I decided to just override the DefaultModelBinder that comes with MVC 2. In the case where the field attached to my model is just a text field (or something simple), I just delegate the base method. However, when I work with a drop-down list or something more complex (the user interface determines that the date and time are separate data entry fields, but for the model this is one property), I have to perform some checks and some manual data manipulations.

The end result of this is that I have a pretty close relationship between View and Binder. I am from an architectural point of view with this, but from a code maintenance point of view, this is a nightmare.

, , , Log ( , Action). "ServiceStateTime" . "log.ServiceStartDate" "log.ServiceStartTime" (Html.TextBox( "log.ServiceStartTime" ,...))

protected override object GetPropertyValue(ControllerContext controllerContext,
                                               ModelBindingContext bindingContext,
                                               PropertyDescriptor propertyDescriptor,
                                               IModelBinder propertyBinder)
{
        if (propertyDescriptor.Name == "ServiceStartTime")
        {
            string date = bindingContext.ValueProvider.GetValue("log.ServiceStartDate").ConvertTo(typeof (string)) as string;
            string time =
                bindingContext.ValueProvider.GetValue("log.ServiceStartTime").ConvertTo(typeof (string)) as string;
            DateTime dateTime = DateTime.Parse(date + " " + time);
            return dateTime;
        }
        if (propertyDescriptor.Name == "ServiceEndTime")
        {
            string date = bindingContext.ValueProvider.GetValue("log.ServiceEndDate").ConvertTo(typeof(string)) as string;
            string time =
                bindingContext.ValueProvider.GetValue("log.ServiceEndTime").ConvertTo(typeof(string)) as string;
            DateTime dateTime = DateTime.Parse(date + " " + time);
            return dateTime;
        }

Log.ServiceEndTime - .

. -, ServiceStartTime ServiceEndTime , ( , R #, , ). -, "log.ServiceStartDate" "log.ServiceStartTime" , . - .

, , , , :

  • , , , ViewModel, aspx/ascx. ViewModel.
  • . , , .

, Fluent NHibernate. , .

, !

+3
1

ViewModel:

class ViewModelClass 
{
    [DateValidationAttribute]
    public property DateTime ServiceStartTimeDate { get; set; }
    [TimeValidationAttribute]
    public property DateTime ServiceStartTimeTime { get; set; }
}

DefaultModelBinder - . :

class DateUtil
{
     public static DateTime CombineDateAndTime(DateTime Date, DateTime Time);
}

:

var entity = context.GetUpdatedEntity(id);
entity.ServiceStartTime = DateUtil.CombineDateAndTime(viewModel.ServiceStartTimeDate,viewModel.ServiceStartTimeTime);

, :

public class DateAndTimeModelBinder : DefaultModelBinder
{
    protected override object GetPropertyValue(ControllerContext controllerContext,
                                           ModelBindingContext bindingContext,
                                           PropertyDescriptor propertyDescriptor,
                                           IModelBinder propertyBinder)
    {
        if (propertyDescriptor.PropertyType == typeof(DateTime))
        {
            string time = bindingContext.ValueProvider.GetValue(CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name + "Time")).ConvertTo(typeof(string)) as string;
            var date = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
            if ((date != null) && (time != null))
                return CombineDateAndTime(date, time);
        }
        return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
    }
}

DateTime , , "Time" ( "ServiceStartTime", "ServiceStartTimeTime", "ServiceStartTime" "ServiceStartTimeTime" ). , . . , , , , .

+1
source

Source: https://habr.com/ru/post/1735999/


All Articles