persistence ignorance with ASP.NET identity framework and EF code first

616 views Asked by At

I developed my data layer with POCO classes and persistence ignorance principles in my mind. My code was like this

POCO class

 public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }
   ...
    }
}

repository interface

 public interface IReportRepository
{
    IEnumerable<Report> GetAllReports();
    Report GetReport(int reportId);
 ...
}

and interface implementation with Entity framework code first, like

 public class EFReportRepository : BaseRepository, IReportRepository
{
    public EFReportRepository(string connectionString)
        : base(connectionString)
    {
    }

    public IEnumerable<Report> GetAllReports()
    {
        return DbContext.Reports.Include(r => r.AggregationParameters).Include(r => r.ErrorAttachment).Include(r => r.ExcelAttachment).Include(r => r.XmlAttachment).ToList();
    }

    public Report GetReport(int reportId)
    {
        return DbContext.Reports.Include(r => r.AggregationParameters).Include(r => r.ErrorAttachment).
            Include(r => r.ExcelAttachment).Include(r => r.XmlAttachment).SingleOrDefault(r => r.Id == reportId);
    }

everything work well until authorization(asp.net identity) was added.

using Microsoft.AspNet.Identity.EntityFramework;

namespace DataRepository.Models
{
    public class MyUser : IdentityUser
    {
    }
}

Now every Report has User that created this report

public class Report
{
    public int Id { get; set; }
    public string Name { get; set; }
   ...
    public MyUser CreatedByUser { get; set; }
    public string CreatedByUserId { get; set; }
   ...
   }
}

Now i have tight dependency with entity framework which i wanted to omit, because of inheritance from IdentityUser class.

How can i exclude this dependency ? I will be thankful for any help )

Update Now the problem is with creating UserManager object.
UserManager required IUserStore as input parameter. This interface has implementation in Microsoft.AspNet.Identity.EntityFramework namespace and is called UserStore, so my code was like this

manager = new UserManager<MyUser>(new UserStore<MyUser>(MyDbContext)).

But now MyUser class is not inherited from IdentityUser , so this code is broken(UserStore requires this type of class)

1

There are 1 answers

5
Dion V. On

I used this approach which works fine as is;

namespace DataRepository.Models
{
    public class MyUser
    {
        public int Id { get; }
        //define other user properties
    }       
}

namespace SomeOtherNamespace
{
    public class MyIdentityUser : IdentityUser
    {
        private MyUser _User;

        public MyIdentityUser(MyUser user)
        {
            _User = user;
        }

        public int Id { get { return _User.Id; } }
        //define other IdentityUser properties by using the MyUser object
    }
}