I've got an issue with extending the partial class from a TDS generated Template for glass mapper. Basically, I want to add a method to the underlying class to say have a custom string based on a property value.

public partial class _Contributor {
   public virtual string ContributorUrlQueryString => $"?name={HttpUtility.UrlEncode(ContributorName)}&id={Id}".ToLower();

var contributor = _contentRepository.GetContentItem(id);

Glass Mapper - 1 never hits the function and 2 returns null. Am I missing something?

Update: Changing calling code to use concrete class instead of Interface allows the code to be hit and the expected results: var contributor = _contentRepository.GetContentItem<_Contributor>(id);

However, I would still like to use the Interface...


Soren's answer is correct.

Another way to solve this would be to used inferred types and infer from your interface to the concrete type, this assumes there is a one-to-one mapping for templates and classes.

public interface MyI{

   string DoWork{get;}

public class MyC : MyI {

  public string DoWork => "Hello world";

var result = service.GetItem<MyI>("/mypath", InferType:true)
  • Good alternative. I thought something like that would work as well, but didn't have time to research it first. Dec 6 '17 at 9:39
  • @Michael - This is actually still not working for me unfortunately when the classes are split with a partial
    – Nona Drake
    May 18 '18 at 20:58

If you are using Glass Mapper to map an item to an interface it will create a proxy class implementing that interface, but it will have no connection to the concrete class (or any other implementation of that interface).

If you use it to map to a concrete class it will still create a proxy but it will pass the call on to the actual implementation (for virtual methods/properties, non-virtual will always call the actual class).

However, I would still like to use the Interface...

Well, one option would then be to make an extension method, like this:

public static class Extensions
    public static string ContributorUrlQueryString(this IContributor contributor)
        return $"?name={HttpUtility.UrlEncode(contributor.ContributorName)}&id={contributor.Id}".ToLower();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.