none
Use of Action or Something else ... RRS feed

  • Question

  • Hello,

    I have the following code:

    Model model = _ser.GetModel();
    _rep.Add(new Entity { Id = model.Id, Name = model.Name });

    Is is possible to use an Action, or something else, to do something like:

    _rep.Add(
      new Action<Model>(_set.GetModel()), x => x {
        new Entity { Id = x.Id, Name = x.Name }   
      }
    )
    

    Note: my syntax is pure invention ... I am just looking for a way to do this.

    Thank You,

    Miguel

    Friday, February 1, 2013 12:18 PM

Answers

  • The Func<TEntity> can be created something you have in mind, then of course you need method with than kind of an signature in repository.

    Something like

    public class Repository<TEntity>
    {
        List<TEntity> models;
    
        public void Add(Func<TEntity> func)
        {
            this.models.Add(func());    
        }
    }
    
    public class Controller
    {
        Repository<Entity> _rep;
    
        public void Action()
        {
            this._rep.Add(() => {
                var model = Ser.GetModel();
                return new Entity() { 
                    Identity = model.ID
                };
            });
        }
    }

    • Marked as answer by Jason Dot Wang Monday, February 11, 2013 8:58 AM
    Friday, February 1, 2013 12:37 PM