Skip to content

Nested Interface Support

Cy Scott edited this page Aug 23, 2021 · 3 revisions

If an interface with the [Generate] attribute contains a member that has a return type that is also an interface, then that interface will get a class generated for it. Here is a simple example of how it works:

public interface IHaveAnIdAndName
{
    Guid Id { get; set; }
    string Name { get; set; }
}

[Generate]
public interface IHaveUsers
{
    IHaveAnIdAndName[] Users { get; set; }
}

The generated code will look like:

public class HaveUsersModel : IHaveUsers
{
    public MGen.Tests.InterfaceSupport.IHaveAnIdAndName[] Users
    {
        get
        {
            return _Users;
        }
        set
        {
            _Users = value;
        }
    }
    private MGen.Tests.InterfaceSupport.IHaveAnIdAndName[] _Users;
        
    public HaveUsersModel()
    {
    }
        
}
    
public class HaveAnIdAndNameModel : IHaveAnIdAndName
{
    public System.Guid Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }
    private System.Guid _Id;
        
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
    private string _Name;
        
    public HaveAnIdAndNameModel()
    {
    }
        
}

Collections

Interfaces referenced by the generic arguments for the collection interfaces will get generated classes. Here is a simple example of how it works:

public interface IHaveAnIdAndName
{
    Guid Id { get; set; }
    string Name { get; set; }
}

[Generate]
public interface IHaveUsers
{
    IDictionary<Guid, IHaveAnIdAndName> Users { get; set; }
}

The generated code will look like:

public class HaveUsersModel : IHaveUsers
{
    public System.Collections.Generic.IDictionary<System.Guid, MGen.Tests.InterfaceSupport.IHaveAnIdAndName> Users
    {
        get
        {
            return _Users;
        }
        set
        {
            _Users = value;
        }
    }
    private System.Collections.Generic.IDictionary<System.Guid, MGen.Tests.InterfaceSupport.IHaveAnIdAndName> _Users;
        
    public HaveUsersModel()
    {
    }
        
}
    
public class HaveAnIdAndNameModel : IHaveAnIdAndName
{
    public System.Guid Id
    {
        get
        {
            return _Id;
        }
        set
        {
            _Id = value;
        }
    }
    private System.Guid _Id;
        
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }
    private string _Name;
        
    public HaveAnIdAndNameModel()
    {
    }
        
}
Clone this wiki locally