Kiwi
Kiwi

Reputation: 2773

Entity Framework load relation

I'm trying to load a relation, but due to my setup I can't seem to get it working.

I disabled lazy loading for fixing the looping exception.

But first things first:

Models

public class DatabaseItem 
{
    [Key]
    public int Id { get; set; }
}

public class Value : DatabaseItem
{
    [Required]
    public string Name { get; set; }
    [Required]
    public string State { get; set; }

    [DefaultValue("Hidden")]
    public string HiddenValue { get; set; }

    public virtual List<SecondValue> SecondValues { get; set; } 
}

public class SecondValue : DatabaseItem 
{
    public string Name { get; set; }
    public string State { get; set; }

    public virtual Value Value { get; set; }
}

Controller:

public IHttpActionResult Get(int id) 
{
    Log.Debug(string.Format("Getting value with id {0}", id));
    try {
        return Json(_apiService.Values.Get(id));
    } catch (Exception e) {
        Log.Error(string.Format("Getting value with id {0}", id), e);
        throw new Exception(string.Format("Getting value with id {0}", id), e);
    }
}

[Route("values/{id}/secondvalues")]
public IHttpActionResult GetSecondValues(int id) {
    Log.Debug(string.Format("Getting secondvalues for value with id {0}", id));
    try {
        var test = _apiService.Values.Get(id);
        // here I should get the SecondValues :)    
        return Json(test.SecondValues);
    } catch (Exception e) {
        Log.Error(string.Format("Getting secondvalues for value with id {0}", id), e);
        throw new Exception(string.Format("Getting secondvalues for value with id {0}", id), e);
    }
}

Services

Interfaces

public interface IApiService 
{
    IValueService Values { get; }
    ISecondValueService SecondValues { get; }
}

public interface IValueService : IService<Value>, IObjectService<Value> {}
public interface ISecondValueService : IService<SecondValue>, IObjectService<SecondValue> {}

public interface IService<T>
{
    List<T> GetList();
    T Get(int id);
    bool Exists(int id);
    int Update(int id, T obj);
    T Add(T obj);
    void Delete(T obj);
}

public interface IObjectService<T> 
{
    T Find(T obj);
}

public interface IDalContext : IUnitOfWork 
{
    IValueRepository Values { get; }
    ISecondValueRepository SecondValues { get; }
}

Classes:

internal abstract class Service<TObject> : IService<TObject> where TObject : class {
    protected static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    protected readonly IRepository<TObject> _repository;

    protected Service(IRepository<TObject> repository) {
        _repository = repository;
    }

    public List<TObject> GetList() {
        Log.Debug(string.Format("Getting {0}", typeof (TObject)));
        try {
            return _repository.All().ToList();
        } catch (Exception e) {
            Log.Error(string.Format("Error Getting {0}", typeof (TObject)), e);
            throw new Exception(string.Format("Error Getting  {0}", typeof (TObject)), e);
        }
    }

    public TObject Get(int id) {
        Log.Debug(string.Format("Getting {0}, id: {1}", typeof (TObject), id));
        try {
            return _repository.Find(id);
        } catch (Exception e) {
            Log.Error(string.Format("Error Getting {0}, id: {1}", typeof (TObject), id), e);
            throw new Exception(string.Format("Error Getting  {0}, id: {1}", typeof (TObject), id), e);
        }
    }

    public bool Exists(int id) {
        Log.Debug(string.Format("Checking existance {0}, id: {1}", typeof (TObject), id));
        try {
            return _repository.Contains(o => (o as DatabaseItem).Id == id);
        } catch (Exception e) {
            Log.Error(string.Format("Checking existance {0}, id: {1}", typeof (TObject), id), e);
            throw new Exception(string.Format("Checking existance  {0}, id: {1}", typeof (TObject), id), e);
        }
    }

    public int Update(int id, TObject obj) {
        var json = new JavaScriptSerializer().Serialize(obj);

        Log.Debug(string.Format("Adding {0}, {1}", typeof (TObject), json));
        try {
            return _repository.Update(obj);
        } catch (Exception e) {
            Log.Error(string.Format("Error Adding {0}, {1}}", typeof (TObject), json), e);
            throw new Exception(string.Format("Error Adding {0}, {1}", typeof (TObject), json), e);
        }
    }

    public abstract TObject Find(TObject obj);

    public TObject Add(TObject obj) {
        var json = new JavaScriptSerializer().Serialize(obj);

        Log.Debug(string.Format("Adding {0}, {1}", typeof (TObject), json));
        try {
            var val = Find(obj);
            return val ?? _repository.Create(obj);
        } catch (Exception e) {
            Log.Error(string.Format("Error Adding {0}, {1}}", typeof (TObject), json), e);
            throw new Exception(string.Format("Error Adding {0}, {1}", typeof (TObject), json), e);
        }
    }

    public void Delete(TObject obj) {
        var json = new JavaScriptSerializer().Serialize(obj);

        Log.Debug(string.Format("Adding {0}, {1}", typeof(TObject), json));
        try {
            _repository.Delete(obj);
        } catch (Exception e) {
            Log.Error(string.Format("Error Adding {0}, {1}}", typeof(TObject), json), e);
            throw new Exception(string.Format("Error Adding {0}, {1}", typeof(TObject), json), e);
        }
    }
}

class ValueService : Service<Value>, IValueService {
    public ValueService(IRepository<Value> repository) : base(repository) { }

    public override Value Find(Value obj) {
        if (obj.Name == null || obj.HiddenValue == null || obj.State == null) {
            return null;
        }
        return _repository.Find(obj1 => obj1.Name == obj.Name && obj1.HiddenValue == obj.HiddenValue && obj1.State == obj.State);
    }
}

class SecondValueService : Service<SecondValue>, ISecondValueService {
    public SecondValueService(IRepository<SecondValue> repository) : base(repository) {}

    public override SecondValue Find(SecondValue obj) {
        throw new NotImplementedException();
    }
}

Repositories:

Interfaces:

public interface IRepository<T> : IDisposable where T : class 
{
    IQueryable<T> All();
    IQueryable<T> Filter(Expression<Func<T, bool>> predicate);
    IQueryable<T> Filter<Key>(Expression<Func<T, bool>> filter, out int total, int index = 0, int size = 50);
    bool Contains(Expression<Func<T, bool>> predicate);
    T Find(params object[] keys);
    T Find(Expression<Func<T, bool>> predicate);
    T Create(T t);      
    void Delete(T t);
    int Delete(Expression<Func<T, bool>> predicate);
    int Update(T t);
    int Count { get; }
}

Classes:

public class Repository<TObject> : IRepository<TObject> where TObject : class 
{
    protected static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
    protected Db Context;
    private bool shareContext = false;

    public Repository() {
        Context = new Db();
    }

    public Repository(Db context) {
        Context = context;
    }

    // Here are all the implementations of IRepository<TObject>
 }

internal class ValueRepositroy : Repository<Value>, IValueRepository {
    public ValueRepositroy(Db context) : base(context) { }
}

internal class SecondValueRepository :  Repository<SecondValue>, ISecondValueRepository {
    public SecondValueRepository(Db context) : base(context) {}
}

So how could I best load in the SecondValues when going to /values/{id}/secondvalues?

UPDATE

I managed to get the include working while not having it run in the get list.

Renamed and changed the databaseItem to a new interface:

interface IEntity {
    int Id { get; }
}

Then updated to the following in the Service to:

internal abstract class Service<TObject> : IService<TObject> where TObject : class, IEntity

public IQueryable<TObject> GetList() {
    Log.Debug(string.Format("Getting {0}", typeof (TObject)));
    try {
        return _repository.All();
    } catch (Exception e) {
        Log.Error(string.Format("Error Getting {0}", typeof (TObject)), e);
        throw new Exception(string.Format("Error Getting  {0}", typeof (TObject)), e);
    }
}

public IQueryable<TObject> Get(int id) {
    Log.Debug(string.Format("Getting {0}, id: {1}", typeof (TObject), id));
    try {
        return _repository.All().Where(o => o.Id == id);
    } catch (Exception e) {
        Log.Error(string.Format("Error Getting {0}, id: {1}", typeof (TObject), id), e);
        throw new Exception(string.Format("Error Getting  {0}, id: {1}", typeof (TObject), id), e);
    }
}

So now I can do the following in my controllers:

[Secure("value.detail")]
public IHttpActionResult Get(int id) {
    Log.Debug(string.Format("Getting value with id {0}", id));
    try {
        return Json(_apiService.Values.Get(id).FirstOrDefault());
    } catch (Exception e) {
        Log.Error(string.Format("Getting value with id {0}", id), e);
        throw new Exception(string.Format("Getting value with id {0}", id), e);
    }
}

[Route("values/{id}/secondvalues")]
public IHttpActionResult GetSecondValues(int id) {
    Log.Debug(string.Format("Getting secondvalues for value with id {0}", id));
    try {
        var test = _apiService.Values.Get(id).Include(value => value.SecondValues).FirstOrDefault();

        return Json(test.SecondValues);
    } catch (Exception e) {
        Log.Error(string.Format("Getting secondvalues for value with id {0}", id), e);
        throw new Exception(string.Format("Getting secondvalues for value with id {0}", id), e);
    }
}

But now getting a self-referencing loop when requesting the SecondValues

Upvotes: 2

Views: 80

Answers (1)

Marc
Marc

Reputation: 3959

By default, the JSON and XML formatters write all objects as values. If two properties refer to the same object, or if the same object appears twice in a collection, the formatter will serialize the object twice. This is a particular problem if your object graph contains cycles, because the serializer will throw an exception when it detects a loop in the graph.

To preserve object references in JSON, add the following code to Application_Start method in the Global.asax file:

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = 
Newtonsoft.Json.PreserveReferencesHandling.All;

But because object references are not that standard in JSON, it would be a better approach to not to transfer these objects to the client if you don't need them. Just map your entities in a DTO and send that to the client.

Upvotes: 4

Related Questions