以下是对你的“框架”提出的两项建议。
<>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.