1

I'm building a web api and I have for example this controller,

[RoutePrefix("api/v{version}/profile")]
public class ProfileController : BaseController
{
    private readonly IPersonService _personService;
    private readonly IPersonDtoBuilder _builder;

    public ProfileController(IPersonService personService
        IPersonDtoBuilder builder)
    {
        _personService = personService;
        _builder = builder;
    }

    [HttpGet]
    [Route("")]
    public IHttpActionResult Get()
    {
        var person = _personService.GetPerson(UserId);

        if (person == null)
            return NotFound();

        var model = _builder.Build(person);

        return Ok(model);
    }

Being the IPersonDtoBuilder and PersonDtoBuilder

public interface IPersonDtoBuilder
{
    PersonDto Build(UrlHelper urlHelper, Person person);
}

public class PersonDtoBuilder : IPersonDtoBuilder
{
    public PersonDto Build(UrlHelper urlHelper, Person person)
    {
        var model = new PersonDto
        {
            Links = new List<Link>
            {
                new Link
                {
                    Rel = "update",
                    Href = urlHelper.Link("UpdateProfile", null), 
                    Title = "Update"
                }
            },
            FirstName = person.FirstName,
            MiddleName = person.MiddleName,
            LastName = person.Surname
        };

        return model;
    }
}

}

The personDto is a model that derives from Resources just for the sake of having a property Links in all of the derived classes.

[DataContract]
public class PersonDto : Resource
{
    [DataMember(Name = "firstName")]
    public string FirstName { get; set; }

    [DataMember(Name = "middleName")]
    public string MiddleName { get; set; }

    [DataMember(Name = "lastName")]
    public string LastName { get; set; }
}

[DataContract]
public abstract class Resource
{
    [DataMember(Name = "_links")]
    public IEnumerable<Link> Links { get; set; }
}

My problem is when I'm trying to construct this links in the builder, as I don't have access to the Url.Link method.

I was trying to pass it as a parameter

PersonDto Build(UrlHelper urlHelper, Person person)

and somehow initialize it in the basecontroller

But I'm still not able to initialize it properly. How should I do it?

Thanks

Edit: After reading Silvermind comment I review my basecontroller and figured out how it works

public abstract class BaseController : ApiController
{
    UrlHelper _urlHelper;

    protected UrlHelper TheUrlHelper
    {
        get
        {
            if (_urlHelper == null)
            {
                _urlHelper = new UrlHelper(this.Request);
            }
            return _urlHelper;
        }
    }

    public long UserId { get; set; }
}

Not sure if it's the cleanest way passing the helper to the build methods of all the classes but it seems to fix this issue

mitomed
  • 2,006
  • 2
  • 29
  • 58
  • 1
    Just asking, maybe I am wrong but isn't `Url` available in the base controller which should be of type `UrlHelper`? – Silvermind Dec 02 '13 at 11:12

0 Answers0