Adding a User field to Meal entity - Mapping Problem

Jan 9, 2014 at 9:22 PM
Hello,

First of all: Congratulations for this solution! I am using for study and leaning a lot. Great coding!

I have added a User field to the Meal entity, because i want to be able to query/filter Meals and Dinners by the users that have created them. And in the future I think I will place some restrictions, so that only the owner and the administrator can edit them.

So I have added this two properties bellow to the Entity and added the field in the database too:

... Meal : DelEntity
...
public int UserId { get; set; }
public virtual User User { get; set; }
...

But my problem now is: How can i add the current user id automactally? For this I am looking to the MealController and Cruder, but it is too generic, and i really don't know here is the best point to maybe break the inherance and add my own implementation.

I think I need to add something like this in some place, but Im really confused:

SomeMealInstance.UserId = HttpContext.Current.User.Identity.Id

Can someone help me?

Thank you very much!!
C.A.
Coordinator
Jan 10, 2014 at 9:12 AM
you could write it without the cruder to make it simpler

or you could create a mapper for the meal just like there is one for the dinner (DinnerMapper), where you would set the UserId in the MapToEntity method
Jan 11, 2014 at 1:32 AM
Tks O.

I have created a MealMapper just like bellow, but now im having other problems related to how search data in the repo and how to save it correctly:

...
public class MealMapper : Mapper<Meal, MealInput>
{
    public override Meal MapToEntity(MealInput input, Meal e)
    {
        var entity = base.MapToEntity(input, e);

        var list = IoC.Resolve<IDelRepo<User>>().Where(o => o.Login.Equals(HttpContext.Current.User.Identity.Name)).GetEnumerator();
        list.MoveNext();
        var user = list.Current;

        //entity.User = user;
        entity.UserId = user.Id;

        return entity;
    }
}
...

I had added the following to the WindsorConfigurator:

...
WindsorRegistrar.Register(typeof(IMealService), typeof(MealService)); // this line was already there ;D
WindsorRegistrar.Register(typeof(IMapper<Meal, MealInput>), typeof(MealMapper));
...


Got this error message: "New transaction is not allowed because there are other threads running in the session."


After a lot of hours trying to understand Im came here to ask: how is the best way to search data in the repo, and how can i save the Meal with my new Complex property?

Tks again!
Coordinator
Jan 11, 2014 at 8:38 AM
Jan 13, 2014 at 1:22 PM
Perfect!

I was not able to use the .ToList, and I didn't know why. But with your help I figure it out.

Now I had placed the using System.Linq, and now I can use it!

Tnks O!