Im starting a new project using .NET Core and Entityframework core. My project have the following layers : Data Repository Service API
For you to better understand my problem, I will show an example with my user implementation. The data layer contain the following user class:
public class User : BaseEntity
{
public string Email { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
public DateTime JoinDate { get; set; }
public DateTimeOffset? LastLoggedIn { get; set; }
public UserProfile UserProfile { get; set; }
public ICollection<User> ParentUser { get; set; }
public ICollection<UserRoles> UserRoles { get; set; }
public ICollection<UserClaims> UserClaims { get; set; }
public UserToken UserToken { get; set; }
}
(I use no virtual because Entity Framework doesn't implement lazy loading)
My repository layer include a generic repository pattern with other repositories for more specific operations, here is how it look like :
public interface IRepository<T> where T : BaseEntity
{
IQueryable<T> GetAll();
T Get(int id, params Expression<Func<T, object>>[] includeProperties);
T GetById(int id);
T Add(T entity);
void Update(T entity);
void Delete(int id);
void Delete(T entity);
}
(I choosed to return a IQueryable but im not sure about the advantage over an IEnumerable. Im afraid if the service layer will use too much of linq query instead of calling the repository)
My service layer contains my business logic. It should query data from the repository and do the magic. Here is a sample of my user service :
public interface IUserService
{
IEnumerable<User> GetUsers();
User Get(int id);
void InsertUser(User user);
void UpdateUser(User user);
void DeleteUser(int id);
void DeleteUser(User user);
bool UserExist(string email);
User validateUserCredentials(string email, string password);
}
So the problem is, my API need to access the user profil. It will make a call to my service layer using the "Get" method. However, the returned user object will have his UserProfile property set to null.
What is the best approach ? Should I :
- create a method "GetUserProfile" in my User repository which will get exposed in my service ?
- create a method "GetUserProfile" in my User service which call "Get(1, x => x.UserProfile)" ?
- have a UserProfile service and query it from the API to get the profile for a given user id ?
- Let the API pass a lambda expression to the service, which will pass it to the repository to include UserProfile ?
Im quite new to this and getting a bit lost on something I feel is very important. Could anyone please guide my on the right path ? Thank's
Aucun commentaire:
Enregistrer un commentaire