Skip to content

Instantly share code, notes, and snippets.

View amoerie's full-sized avatar

Alexander Moerman amoerie

  • DOBCO Medical Systems
  • Belgium
View GitHub Profile
@amoerie
amoerie / gist:4359150
Last active December 10, 2015 01:28
A few simple models
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public int AddressId { get; set; }
public virtual Address Address { get; set; }
}
public class Address
public class PersonFormDto: IDto<Person>
{
public int Id { get; set; }
[Required(ErrorMessage = "Hey why don't you tell me who you are?!")]
public string Name { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Come on now, don't be shy.")]
public DateTime Birthday { get; set; }
public interface IDto<TModel> where TModel : class, new()
{
}
/// <summary>
/// Custom injection to support nested DTO properties
/// </summary>
public class DtoInjection: LoopValueInjection
{
/// <summary>
/// If one of the types implements IDto, check if its generic type parameter
/// matches the other type
/// Otherwise, just use the default typematcher.
/// </summary>
public class PeopleController: Controller
{
private IRepository<Person> _people;
public PeopleController(IRepository<Person> people)
{
_people = people;
}
public ActionResult Index()
public static class DtoExtensions
{
/// <summary>
/// Make a new model and inject dto values
/// </summary>
/// <typeparam name="TModel">The type of the model</typeparam>
/// <param name="dto">The dto object that contains the property values</param>
/// <returns>A new instance of TModel with the same property values of this dto</returns>
public static TModel ToModel<TModel>(this IDto<TModel> dto)
where TModel : class, new()
/// <summary>
/// Represents a string that can be translated into several languages.
/// </summary>
public class MultilingualString
{
private const string TranslationsNode = "translations";
private const string TranslationNode = "translation";
private const string LanguageNode = "language";
private const string ValueNode = "value";
var product1 = new Product();
product1.Name["en"] = "Product 1";
product1.Description["fr"] = "Description 1";
product1.Name["fr"] = "Produit 1";
product1.Description["fr"] = "Déscription 1";
product1.Name["nl"] = "Produkt 1";
product1.Description["nl"] = "Omschrijving 1";
public class Product
{
public int Id { get; set; }
public MultilingualString Name { get; set; }
public MultilingualString Description { get; set; }
}
<translations>
<translation>
<language>en</language>
<value>Description 1</value>
</translation>
<translation>
<language>fr</language>
<value>Déscription 1</value>