English 中文(简体)
ASP.NET MVC 3 - Model Validation
原标题:ASP.NET MVC 3 - Model Validation

I m just learning MVC and I have a few questions about the design/how things are supposed to work.

I m使用MVC 3和Razor,设有实体框架课程(例如,地点),Im则将创建具有审定说明的混合班。 在我看来,我有一部分观点,它使“DevExpress”树木(使用一个位置清单)和“在树中建立/编辑位置”的形式。 我有1个地点管理员、1个地点管理意见、1个地点管理部分 观点(含有树眼法)和定位模型。 “Model”组织将举办假肢班,并找到孩子的学习方法(孩子们在节日扩大后只有假装)。 我有一个服务包(供我服务客户使用),将使用结构分配。

Should I inject the service wrapper into the Controller s constructor or into the Model s constructor?

Also, my Model has get methods that use the service wrapper to fetch data from the database (do those methods belong here in the Model?): for example, GetChildren for the tree view.

此外,在《示范法》中储存布置舱物类别是否正确?

我希望确保我能很好地设计这一申请,因为这是更大项目的一部分。 任何设计点都受到高度赞赏。 我正在读到斯科特·格为MVC的 st。

Ref:

最佳回答

以下是对你的“框架”提出的两项建议。

<>Entity Framework

每种模型都从电子格式中回来,摘录了电子格式的接口,并将接口作为数据来源,而不是使用电子格式。 原因是,如果你决定为一个或多个模型(或整个实体框架)使用另一个数据来源,那么你就可以确保你的新数据。 等级为相同的接口,无需修改整个网络代码。 倒数正确保您的接口在进行模型改动时达到最新水平。

这还使你的看法模式得以实施欧洲复兴开发银行模式的接口(加上你选择的其他逻辑)。 如果你们要求插入/更新数据层的所有要求都接受同样的接口模式。 这使你能够形成多种模式,要求不同,所有模式都适合数据层需要插入/更新的内容。 倒数是,在你身上。 数据 你们必须[制定新的欧洲一体化体系模式]/[更新模式],并从接口到模型绘制这些领域的地图。

View Models

我高度建议,每种观点模式不是需要展示的实际模式,而是包含模型的类别。 实例:

public class Car  //Not what I recommend passing to a view
{
    public string Make { get; set; }
    public string Model { get; set; }
}

//Pass this to the view, i ll explain why...
public class CarViewModel : IPartialViewCar      {
    public Car Car { get; set; }
    public ColorCollection { get; set; }
}

By passing the example "CarViewModel", you can decouple partial views from views. Here s how (using the models above):

public interface IPartialViewCar  
{
    public Car { get; }
}

[BuildCar.cshtml]
@Model MyNameSpace.Models.Car

@Html.EditorFor(model)

[PartialViewCar.cshtml]
@Model MyNameSpace.Models.IPartialViewCar  

@Html.EditorFor(model)  //something like that..

现在,你想要使用<代码>部分Car,你只是需要制定执行<代码>IPartialViewCar的模型。 相互之间,基本上从观点上贬低了部分观点。

Validation

我建议建立接口(如果你真的希望,但确实没有必要的话,等级),这些接口具有你的所有验证逻辑。

Lets say we want to require anonymous users to type both a make and a model, but registered users only have to type a Make. How can this be done easily, here s how: (extending more on the previous code)

public interface IAnonymouseCarValidation
{
    [required]
    public string Make { get; set; }
    [required]
    public string Model { get; set; }
}

public interface IRegisteredCarValidation
{
    [required]
    public string Make { get; set; }
}

public interface ICar
{
    public string Make { get; set;}
    public string Model { get; set; }
}

[updating the Car model to abstract and use an interface now]
public abstract class Car : ICar
{
    //maybe some constructor logic for all car stuff

    public string Make { get; set;}
    public string Model { get; set; }

    //maybe some methods for all car stuff
}

//MetadataType tells MVC to use the dataannotations on the
//typeof class/interface for validation!
[MetadataType(typeof(AnonymouseCarValidation))]
public class AnonymousCar : Car
{
}


[MetadataType(typeof(AnonymouseCarValidation))]
public class RegisteredCar : Car
{
}

[Now update the ViewModel]
public class CarViewModel : IPartialViewCar     
{
    public ICar Car { get; set; }  //this is now ICar 
    public ColorCollection { get; set; }
}

现在你可以创建<条码>。 AnonymouseCar或RegisteredCar,将其并入CarViewModel,并允许MVC注意验证。 当你需要更新鉴定时,你更新了单一接口。 与此相对应的是,它感到相当复杂。

Injection & Data Requests

My preference is to try to keep the Controller Actions as simple as possible and not to include code there to retrieve data. The reason I choose not to do this is that I don t like to repeat code. For example:

public class AccountControllers
{
    DataServer _Service;

    public AccountControllers(DataServer Service)
    {
        this._Service = Service;
    }

    public ActionResult ShowProfiles()
    {
        ProfileViewModel model = new ProfileViewModel();
        model.Profiles = this._Service.Profiles();
        return View(model);
    }

    public ActionResult UpdateProfile(ProfileViewModel updatedModel)
    {
        service.UpdateProfile(updatedModel);

        ProfileViewModel model = new ProfileViewModel();
        model.Profiles = this._Service.Profiles();
        return View(model);
    }
}

相反,我要做的是:

    public ActionResult ShowProfile(Guid ID)
    {
        ProfileViewModel model = new ProfileViewModel(this._service);
        return View(model);
    }

    public ActionResult UpdateProfile(ProfileViewModel updatedModel)
    {
        // pass in the service, or use a property or have SetService method
        updatedModel.Update(this._service)

        ProfileViewModel model = new ProfileViewModel(this._service);
        return View(model);
    }

public class ProfileViewModel()
{
    DataServer _Service;
    Profile _Profile
    public ProfileViewModel()
    {
    }

    public ProfileViewModel(DataServer Service)
    {
        this._Service = Service;
    }


    public Profile Profiles()
    {
        get
        {
            if (this._service == null)
            {
                throw new InvalidOperationException("Service was not set.");
            }
            return = Service.Profiles(ID);
    }

This means that profiles is Enumerated ONLY when it s requested, I don t have to populate it myself. Tends to be less bugs if I use the code to my advantage, instead of requiring me or other programmers to manually populate models.

问题回答

暂无回答




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签