0

I have a class library that have

namespace Project.Core
{
    [Table("ActorBases")]
    public class ActorBase
    {
        //Properties
    }

    public class Player : ActorBase
    {
        //Properties
    }

    [Table("ActorBases")]
    public class Character: ActorBase
    {
        //Properties
    }

    public class Match
    {
        //Properties
        public Player Organizor { get; set; }
    }
}

Then in my web app, I have this:

namespace Project.Web
{
    [Table("ActorBases")]
    public class Player : Core.Player
    {
        public virtual ApplicationUser User{ get; set; }
    }
}

to connect a player with a user. Now, when I create a DbContext, first I tried this:

using Project.Core

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
    ...
    protected override void OnModelCreating(ModelBuilder builder)
    {
        public DbSet<ActorBase> ActorBases { get; set; }
        public DbSet<Project.Web.Player> Players { get; set; }
        public DbSet<Character> Characters { get; set; }
        public DbSet<Match> Matches { get; set; }
    }
}

but there's a problem because table Matches requires Project.Core.Player in the table ActorBases with the discriminator Player and the class Project.Web.Player also need the same discriminator. So it fails, saying

An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: The discriminator value for 'Player' is 'Player' which is the same for 'Player'. Every concrete entity type in the hierarchy needs to have a unique discriminator value.

So second, I tried to solve this issue by changing the definition of Project.Web.Player to

[Table("ActorBases")]
public class Player_Web : Core.Player
{
    public virtual ApplicationUser User{ get; set; }
}

Then I noticed that even though I could get rid of the problem, I couldn't still write something like: var user = dbContext.Matches.Single(t=>t.Id==1).Organizor.User. Therefore, I've started to think it was essentially a wrong way that I tried to include the connection beween Player and ApplicationUser in the database.

To separate (capsulize?) the core from the web app, it's necessary that the core namespace doesn't know anything about User that is handled by web app. So, the database should looks like:

using Project.Core

protected override void OnModelCreating(ModelBuilder builder)
{
    public DbSet<ActorBase> ActorBases { get; set; }
    public DbSet<Player> Players { get; set; }
    public DbSet<Character> Characters { get; set; }
    public DbSet<Match> Matches { get; set; }
}

and I need to implement a function or api that takes an argument of Player and returns an object of User corresponding to the player.

Is my understanding correct in terms of designing and referring a class library?

kemakino
  • 1,041
  • 13
  • 33
  • Have you tried some explicit casting, e.g., var user = ((Player_Web)dbContext.Matches.Single(t=>t.Id==1).Organizor).User? – hector-j-rivas Feb 08 '18 at 18:01
  • It's with the last DbContext model right? – kemakino Feb 08 '18 at 23:04
  • In that context matches contain core players, not web players. – hector-j-rivas Feb 09 '18 at 15:23
  • Yes, so eventually I cannot contain web players in the context unless I implement Project.Web.Match inheriting the core class Project.Core.Match. Hence I think the last example of the context is enough for the database and I need to write something like an explicit casting as you mentioned. – kemakino Feb 09 '18 at 16:19
  • As to whether this is the right track, Dependency Injection is the more modern approach. It allows you to decouple core code and application code. You're close; implement your core as a service with interfaces that your application can use (the builder figures out how to inject them). – hector-j-rivas Feb 09 '18 at 17:16
  • Oh I've heard about the feature but never tried to implement it by myself... Anyways I'm glad my trial is not too far from the right way. Do you have some good tutorial or example on the web about implementing dependency injection for a core library class? – kemakino Feb 09 '18 at 18:33
  • It is not to hard to implement take a look at https://stackoverflow.com/questions/14301389/why-does-one-use-dependency-injection. That said, you might as well go ahead and use the IMO best open source solution, http://structuremap.github.io/documentation/. – hector-j-rivas Apr 10 '18 at 22:12

0 Answers0