This project is read-only.

A few questions about Glue

Sep 18, 2009 at 2:49 PM

Hello,

I have been working with Asp.Net MVC and AutoMapper.

I have some problems since I need to use Bidirectional mapping and AutoMapper was not planned for that.

I am trying Glue today and I have a few questions so I can clarify if I can use it the way I am trying:

1. Is it possible to use Glue as AutoMapper is used in the following example:

   http://www.jimzimmerman.com/blog/2009/05/18/Entity+Framework+And+Creating+A+Reusable+Abstraction.aspx

   Basically it creates an Automatic bidirectional mapping between EF Entities and ViewModels using an automatic mapper or a custom mapper I define.

2. Can Glue map to an existing object or it creates always a new instance of the destination object?

3. Can I use conditional mapping?

   For example, on a EF Entity a File is saved in a byte[] which corresponds to varbinary in the database.

   However in the ViewModel the file is represented by HttpPostedFileBase which cannot be created.

   So the map would be:

   EF -> ViewModel: Do not map

   ViewModel -> EF: Map only if HttpPostedFileBase ContentLenght is not 0. If it is 0 don't map ... don't even return byte[0]:

    if (source.ContentLength == 0) {
      // Do not map
    } else {
      byte[] buffer = new byte[source.ContentLength];
      source.InputStream.Read(buffer, 0, source.ContentLength);
      return buffer;
    } 

4. Can I ignore mapping for a property?

5. Can I create custom mapping using more then one property. For example:

   destination.Content = source.Title+ source.Description IF source.Published IS TRUE

I hope you can help me out with these questions since I need to solve my mapping problem and decide for a implementation and I am not sure if I go for AutoMapper or Glue.

Thank You,

Miguel

 

 

Sep 18, 2009 at 8:00 PM
Edited Sep 18, 2009 at 8:03 PM

 

Hi Miguel,
Thank you for your interrest in Glue.
To answer your questions:
1) Sure, this seems possible with Glue.
2) Glue will by default map to existing objects
3) This can be done by using a custom converter: http://glue.codeplex.com/Wiki/View.aspx?title=Mapping%20between%20different%20types . I can make a code example for this.
4) Yes you can ignore properties. Here's how you do it (when it comes to verification): http://glue.codeplex.com/Wiki/View.aspx?title=Verification%20of%20relations
5) This is not supported yet, but I get regularely requests for it, so I think it will be implemented.
If you have any other questions or issues feel free to tell me. I'll gladly provide both support and discuss new features that you might want.

 

Hi Miguel,

 

Thank you for your interest in Glue. To answer your questions:

1) Sure, this seems possible with Glue.

2) Glue will by default map to existing objects.

3) This can be done by using a custom converter: check it here . I can make a code example for this.

4) Yes you can ignore properties. Here's how you do it (when it comes to verification)

5) This is not supported yet, but I get regularly requests for it, so I think it will be implemented.

 

If you have any other questions or issues feel free to tell me. I'll gladly provide both support and discuss new features that you might need.

- Tore Vestues

 

 

Sep 18, 2009 at 8:19 PM

Hello,

2. This can be done by using a custom converter: check it here . I can make a code example for this.

   But the custom converter has the ability to take the decision of "not map" instead of returning a default value? That is the problem with AutoMapper.

5) This is not supported yet, but I get regularly requests for it, so I think it will be implemented.

   Do you know when?

1) Sure, this seems possible with Glue.

    I just finish this implementation with AutoMapper but I am not sure if I will use it because I still have a few problems with it.

    But to replace AutoMapper with Glue or vice versa is quite simple.

    Basically this code creates a UnitOfWork over EF entities with a BaseRepository and BaseService.

    BaseService uses the mapper to convert the Entities to ViewModels.

    This is wah I have so far:

  public interface ISession {
    void Commit();
    void Rollback();
  } // ISession

  public partial class AppEntities : ISession {

    public void Commit() {
      SaveChanges();
    } // Commit
    public void Rollback() {
      Dispose();
    } // Rollback

  } // AppEntities

  // IRepository  
  public interface IRepository<T> {

    void Create(T o);
    void Delete(T o);
    //void Find(System.Linq.Expressions.Expression<Func<T, bool>> predicate);
    IQueryable<T> GetAll();
    T GetById(int id);
    int Update(T o); 

  } // IRepository

  // Repository
  public class Repository<T> : IRepository<T> {

    private AppEntities context;

    public Repository(ISession session) {
      if (session == null)
        throw new ArgumentNullException("session");
      context = session as AppEntities;
    }

    public void Create(T o) {
      var tableName = typeof(T).Name;
      context.AddObject(tableName, o);
    }

    public void Delete(T o) {
      var tableName = typeof(T).Name;
      context.DeleteObject(o);
    }

    public IQueryable<T> GetAll() {
      var tableName = typeof(T).Name;
      return context.CreateQuery<T>(tableName).AsQueryable();
    }

    public T GetById(int id) {
      var tableName = typeof(T).Name;
      var key = new EntityKey("AppEntities." + tableName, "Id", id);
      var r = (T)context.GetObjectByKey(key);
      return r;
    }

    public int Update(T o) {
      var tableName = typeof(T).Name;
      var Id = GetPrimaryKeyValue(o);
      T currentObject = GetById(Id);
      currentObject = o;
      context.ApplyPropertyChanges(tableName, currentObject);
      return context.SaveChanges();
    }

    private int GetPrimaryKeyValue(T o) {
      int Id = 0;
      var properties = typeof(T).GetProperties();
      foreach (PropertyInfo p in properties) {
        if (string.Compare(p.Name, "Id", true) == 0) {
          var r = p.GetValue(o, null).ToString();
          Id = int.Parse(r);
          break;
        }
      }
      return Id;
    }

  } // Repository

  // Service
  public abstract class Service<TModel, T> where TModel : new() where T : new() {
    
    public Repository<T> _repo;

    protected Service(ISession session) {
      _repo = new Repository<T>(session);
      
      var tm = Mapper.FindTypeMapFor<T, TModel>();
      if (tm == null) {
        Mapper.CreateMap<T, TModel>();
      }

      tm = Mapper.FindTypeMapFor<TModel, T>();
      if (tm == null) {
        Mapper.CreateMap<TModel, T>();
      }
    }

    public virtual TModel GetById(Int32 id) {
      var item = _repo.GetById(id);
      var model = Map(item);
      return model;
    }

    public virtual TModel Map(T d) {
      return Mapper.Map<T, TModel>(d);
    }

    public virtual IList<TModel> Map(IList<T> d) {
      return Mapper.Map<IList<T>, List<TModel>>(d);
    }

  } // Service

  public class ArticleUI {

    public int Id { get; set; }
    public String Title { get; set; }
    public String Content { get; set; }

  }

  // ArticleService
  public class ArticleService : Service<ArticleUI, Article> {
    
    public ArticleService(ISession session) : base(session) {
    }

    public IList<ArticleUI> GetPublished() {
      var query = _repo.GetAll().Where(a => a.Published == true);
      var articles = query.ToList();
      return Map(articles);
    }

  }

That is where I want to integrate Glue.

Thank You,

Miguel

 

 

 

 

Sep 19, 2009 at 7:45 AM

 

2. You are right. Glue does not support "not mapping" at the moment. I haven't really thought about that as an option. If you give me an example to when you might need this, I will turn my head around and see if I can find a nice way to integrate it.
5.I can't make any promises, but it might be in a month or two.
1. I would love to see Glue in there :)

Miguel,

2. You are right. Glue does not support "not mapping" at the moment. I haven't really thought about that as an option. If you give me an example to when you might need this, I will turn my head around and see if I can find a nice way to integrate it.

5. I can't make any promises, but it might be in a month or two.

1. I would love to see Glue in there :)

- Tore Vestues

 

Apr 22, 2011 at 9:18 AM

5. ... does anybody know if such "property aggregating" is already supported by Glue?

Apr 22, 2011 at 10:02 AM

Sorry, "property aggregating" is not yet supported. Will look into it and hopefully add it to the next major release.

Apr 22, 2011 at 10:10 AM

Thanks for info.

I think it could be designed in a way of converters. But now it is not possible to relate a single property to the entire right object: mapping.Relate(a => a.AggregatingProperty, b => b, customConverter);