Base solution for your next web application
Open Closed

Custom Repository (custom methods) #387


User avatar
0
ddnils created

I have created a custom repository (i had to, my entity has a combined primary key (long,long)).

public class UserRelation
    {
        public virtual long UserId { get; set; }
        public virtual long FriendId { get; set; }
        [ForeignKey("UserId")]
        public virtual Users.User User { get; set; }
        [ForeignKey("FriendId")]
        public virtual Users.User Friend { get; set; }
        public DateTimeOffset RequestDate { get; set; }
        public DateTimeOffset? AcknowledgeDate { get; set; }
        public FriendRequestStatus FriendStatus { get; set; }
    }

OnModelCreating contains this code to instruct EntityFramework:

modelBuilder.Entity<Entities.UserRelation>().HasKey(f => new { f.UserId, f.FriendId });
            modelBuilder.Entity<Entities.UserRelation>()
              .HasRequired(f => f.User)
              .WithMany()
              .HasForeignKey(f => f.UserId);
            modelBuilder.Entity<Entities.UserRelation>()
                .HasRequired(f => f.Friend)
                .WithMany()
                .HasForeignKey(f => f.FriendId)
                .WillCascadeOnDelete(false);

So I created my custom repository in [].EntityFramework.Repositories Structure:

public class FriendsRepository: EntityFramework.Repositories.IFriendsRepository {}
interface IFriendsRepository : IRepository<Entities.UserRelation>
    {
        System.Threading.Tasks.Task<bool> ApproveFriendshipAsync(long UserId, long FriendId);
        System.Threading.Tasks.Task<Entities.UserRelation> CreateFriendshipAsync(long UserId, long FriendId);
        System.Linq.IQueryable<Entities.UserRelation> GetAllFriendsOfUserId(long UserId);
        System.Linq.IQueryable<Entities.UserRelation> GetAllRelationsOfUserId(long UserId);
        System.Threading.Tasks.Task<Entities.UserRelation> SetFriendshipStatusAsync(long UserId, long FriendId, Entities.FriendRequestStatus status);
    }

But now I wonder how I can inject it into my service structure, since the entityframework project is not referenced in [].Application?

Have I done something wrong? Or is something still missing? Any help is welcome :)


1 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi, You should create your repository interface in .Core project, implement it in .EntityFramework project. Thus, you can use it from everywhere, including .Application project.