-
Notifications
You must be signed in to change notification settings - Fork 2
Examples
DerAlbertCom edited this page Sep 13, 2010
·
1 revision
Simple Example, an entity model
public class News
{
public int Id { get; private set; }
public DateTime Created { get; private set; }
public DateTime Updated { get; private set; }
public string Title { get; private set; }
public WebUser Author { get; private set; }
public DateTime DisplayStart { get; private set; }
public DateTime DisplayEnd { get; private set; }
public bool Hidden { get; private set; }
}
public class WebUser
{
public int Id { get; private set; }
public DateTime Created { get; private set; }
public DateTime Updated { get; private set; }
public string UserName { get; private set; }
public string EMail { get; private set; }
public string PasswordHash { get; private set; }
public bool Confirmed { get; private set; }
public bool Active { get; private set; }
}
A ViewModel which is mapped via AutoMapper `Mapper.CreateMap<News, PartialNewsModel>();`.
public class PartialNewsModel
{
public int Id { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public string AuthorUserName { get; set; }
public DateTime DisplayStart { get; set; }
public DateTime DisplayEnd { get; set; }
}
Here the FluentMetadata. This MetaData can be used from ASP.NET MVC 2 and FluentNHibernate.
public class NewsMetadata : ClassMetadata<News>
{
public NewsMetadata()
{
Property(x => x.Created).Is.Required();
Property(x => x.Updated).Is.Required();
Property(c => c.Title).Is.Required().Length(255);
Property(c => c.Author).Is.Required();
Property(c => c.DisplayStart).Is.Required();
Property(c => c.DisplayEnd).Is.Not.Required();
}
}
And here the additional Metadate for a the ViewModel, which copies the MetaData from News
public class PartialNewsModelMetadata : ClassMetadata<PartialNewsModel>
{
public PartialNewsModelMetadata()
{
CopyMetadataFrom<News>();
Property(x => x.Id).Should.HiddenInput();
Property(x => x.AuthorId).Should.HiddenInput();
Property(x => x.Title).Display.Name("Überschrift:");
Property(x => x.AuthorUserName).Display.Name("Benutzername:").Is.ReadOnly();
Property(x => x.DisplayStart).Display.Name("Anzeigen ab:");
Property(x => x.DisplayEnd).Display.Name("Anzeigen bis:");
}
}
Validation is implemented for ASP.MVC 2, feel free to add Validators or Dataproviders for other Project-Types.
To use this from ASP.NET MVC 2 you simple the FluentMetadataProvider from the MVC Project.
Set the Metadata-Provider on Startup
FluentMetadataBuilder.ForAssemblyOfType<News>();
FluentMetadataBuilder.ForAssemblyOfType<PartialNewsModelMetadata>();
ModelMetadataProviders.Current = new FluentMetadataProvider();