Skip to content

Instantly share code, notes, and snippets.

@Kikimora
Created April 21, 2020 18:28
Show Gist options
  • Save Kikimora/ebc35806a85028a4908965e74b546861 to your computer and use it in GitHub Desktop.
Save Kikimora/ebc35806a85028a4908965e74b546861 to your computer and use it in GitHub Desktop.
public interface IMyBriefDto
{
public string Name { get; set; }
public string Address { get; set; }
}
public interface IMyDto : IMyBriefDto
{
public int Age { get; set; }
public string Country { get; set; }
}
public interface IListItem
{
string Id { get; }
}
public interface IMyListItem : IMyBriefDto, IListItem
{
}
[DataContract]
class MyDto : IMyDto, IMyListItem
{
[DataMember]
public string Id => Name;
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public string Country { get; set; }
}
[AllowAnonymous]
[Route("admin/sample")]
public class SampleController : ControllerBase
{
[HttpGet("list.json")]
public IMyBriefDto[] GetListItems()
{
return new IMyBriefDto[]
{
new MyDto() {Name = "Andrey", Address = "Andrey Address"},
new MyDto() {Name = "Maxim", Address = "Maxim Address"},
new MyDto() {Name = "Sergey", Address = "Sergey Address"},
};
}
[HttpGet("{id}")]
public IMyDto GetMyDto([FromRoute] string id)
{
return id switch
{
"Andrey" => new MyDto() {Name = "Andrey", Address = "Andrey Address", Age = 35, Country = "Russia"},
"Maxim" => new MyDto() {Name = "Andrey", Address = "Andrey Address", Age = 35, Country = "Russia"},
"Sergey" => new MyDto() {Name = "Andrey", Address = "Andrey Address", Age = 35, Country = "Russia"},
_ => throw new Exception("Unknown dto")
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment