I'm developing an ASP.NET Web Api 2 service with .NET Framework 4.5.1 and C#.
I'm following this book to do it: ASP.NET MVC 4 and the Web API Building a REST Service from Start to Finish
I have these two classes.
User:
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Group> Groups { get; set; }
}
Group:
public class Group
{
public int GroupId { get; set; }
public string GroupName { get; set; }
public ICollection<User> Members { get; set; }
}
An User could have Groups, and a Group has Users as Members.
These two classes are my Data.Models and I use a Mapper class to 'translate' them as Api.Models with these two methods:
public Models.User CreateUser(Data.Models.User modelUser)
{
if (modelUser == null)
return null;
Models.User user = new Models.User()
{
UserId = modelUser.UserId,
UserName = modelUser.UserName
};
if (modelUser.Groups != null)
user.Groups = modelUser.Groups
.Select(CreateGroup)
.ToList();
}
public Models.Group CreateGroup(Data.Models.Group modelGroup)
{
if (modelGroup == null)
return null;
Models.Group group = new Models.Group
{
GroupId = modelGroup.GroupId,
GroupName = modelGroup.GroupName
};
if (modelGroup.Members != null)
group.Members = modelGroup.Members
.Select(CreateUser)
.ToList();
return group;
}
As you can see in Mapper class, CreateUser method calls CreateGroup method, and CreateGroup method calls CreateUser.
In my case user1 is member of group1 and group1 has user1 as a member. So, I get this:
- On
CreateUserforuser1it will callCreateGroupforgroup1. - On
CreateGroupforgroup1it will callCreateUserforuser1. - And so on...
Any idea about how to avoid this infinite recursive calls?
A solution could be to remove navigation properties like Groups on User, or Members in Group class from CreateUser and CreateGroup methods.