IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Add Base Entity Type for IdentityServer4.EntityFramework.Entities

Open jamescrowley321 opened this issue 8 years ago • 15 comments

As a developer, I want the entities in IdentityServer4 to inherit from a common base class, so that I can write generic repositories or services for performing CRUD functions.

Something along the lines of below would help with creating generic base classes that can be used to reduce repetitive code when writing middle tier code that perform CRUD operations on the IdentityServer4 persistence layer.

Example base class:

public abstract class BaseEntity
{
     public int Id { get; set; }
}

Inherited class:

using System.Collections.Generic;

namespace IdentityServer4.EntityFramework.Entities
{
    public class ApiResource : BaseEntity
    {
        public bool Enabled { get; set; } = true;
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public List<ApiSecret> Secrets { get; set; }
        public List<ApiScope> Scopes { get; set; }
        public List<ApiResourceClaim> UserClaims { get; set; }
    }
}

This would allow for something like below to exist:

 public interface IRepository<T> : IDisposable where T : BaseEntity
    {
        Task<T> GetAsync(int id);
        Task<IEnumerable<T>> GetAllAsync();
        void Add(T entity);
        void Update(T entity);
        void Delete(T entity);
        Task SaveAsync();
        Task<IEnumerable<T>> FindByAsync(Expression<Func<T, bool>> predicate);
    }

I am exposing REST endpoints as part of a project for creating an administration platform for IdentityServer4, and without a way to create a few generics, I'm being forced to write a lot of boilerplate code. I am willing to roll out these changes and submit a PR, if it's something that other users may find useful.

jamescrowley321 avatar Jan 04 '18 00:01 jamescrowley321

Jeremy Miller says "don't do this" https://twitter.com/jeremydmiller/status/955875905355698176 https://gist.github.com/jeremydmiller/41bd108bb2103d686d7e4363bfcdf328

Maybe this means we should not consider this feature request... :)

brockallen avatar Jan 23 '18 22:01 brockallen

The repository was really just an example of the type of patterns that many applications utilize in their middle tier when the models or entities implement some sort of base class or interface. I wasn't suggesting the addition of the IRepository interface, just a base type. I think consumers should be responsible for implementing things like IRepository.

There are other uses for a base type that go beyond wrapping the data layer. Since every entity contains the same Id property, which represents an identity, what is the harm in adding it so that others can interop the library with preexisting architectures? Not all of us have the ability to throw out patterns that are widely used in teams or projects.

I'd be interested in seeing an alternative to the IRepository pattern mentioned, but the primary purpose I use it for is to prevent higher level modules from having any reference to EF, Raven, Mongo, etc. Without some layer of abstraction in between the ORM or data access library and higher level modules, you risk creating a dependency on an implementation instead of an abstraction. Without some sort of base type to leverage, you end up writing the same or similar code over and over again.

At the end of the day, I can always fork if I need to...:)

jamescrowley321 avatar Jan 23 '18 23:01 jamescrowley321

Yea, in mentioning it I was just being a jackass in an attempt to avoid doing any work :)

brockallen avatar Jan 24 '18 13:01 brockallen

Haha well I'll do the work! The only thing I wasn't sure of is how to test these changes.

On Jan 24, 2018 8:30 AM, "Brock Allen" [email protected] wrote:

Yea, in mentioning it I was just being a jackass in an attempt to avoid doing any work :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IdentityServer/IdentityServer4/issues/1940#issuecomment-360130641, or mute the thread https://github.com/notifications/unsubscribe-auth/AMbxoK1yv_znj8NPgWyRyHZRuiBjLJhYks5tNy4sgaJpZM4RSc3G .

jamescrowley321 avatar Jan 24 '18 13:01 jamescrowley321

I still want to think about it and I've not yet had time. Give me another week or so.

brockallen avatar Jan 24 '18 14:01 brockallen

@jamescrowley321 Will your administration platform be open source? It's something I'm also going to need to build soon, as I'm using EF Core 2.0 for my configuration store. I assumed a lot more people would have requested this feature as a quickstart example @brockallen?

andyblack19 avatar Jan 29 '18 23:01 andyblack19

I assumed a lot more people would have requested this feature as a quickstart example @brockallen?

We have a commercial option for administration: https://www.identityserver.com/products/

brockallen avatar Jan 30 '18 01:01 brockallen

@andyblack19 Yes, it will be open source. Message me directly if you want more information or want to collaborate.

I've checked out the commercial version, and it is awesome.

jamescrowley321 avatar Jan 30 '18 02:01 jamescrowley321

Adding to what @jamescrowley321 said above, it would be really useful if we could have the ability to change the key type of the IdentityServer models.

Example base class:

public abstract class BaseEntity<TKey>
{
     public TKey Id { get; set; }
}

The inherited class would look like:

using System.Collections.Generic;

namespace IdentityServer4.EntityFramework.Entities
{
    public class ApiResource<TKey> : BaseEntity<TKey>
    {
        public bool Enabled { get; set; } = true;
        public string Name { get; set; }
        public string DisplayName { get; set; }
        public string Description { get; set; }
        public List<ApiSecret<TKey>> Secrets { get; set; }
        public List<ApiScope<TKey>> Scopes { get; set; }
        public List<ApiResourceClaim<TKey>> UserClaims { get; set; }
    }
}

This would allow for something like below to exist:

public interface IRepository<TModel, TKey> : IDisposable 
        where TModel : BaseEntity<TKey>
   {
       Task<TModel> GetAsync(TKey id);
       Task<IEnumerable<TModel>> GetAllAsync();
       void Add(TModel entity);
       void Update(TModel entity);
       void Delete(TModel  entity);
       Task SaveAsync();
       Task<IEnumerable<TModel>> FindByAsync(Expression<Func<TModel, bool>> predicate);
   }

What do you think?

umar-zahoor avatar Apr 08 '18 15:04 umar-zahoor

Personally, I don't see a huge benefit in this. I'd only consider it if @scottbrady91 thinks there's value in it. Scott? We either do this in 2.3 or never, I think.

brockallen avatar Aug 03 '18 00:08 brockallen

Agreed, I'm not sold. I think the custom stores require minimal effort to implement, allowing you to define your own entities and keys, and whatever patterns your desire.

scottbrady91 avatar Aug 03 '18 12:08 scottbrady91

The custom stores don't solve the problem of creating a CRUD interface for the data store. I used a different pattern to get around this, so feel free to close the issue.

On Fri, Aug 3, 2018, 8:35 AM Scott Brady [email protected] wrote:

Agreed, I'm not sold. I think the custom stores require minimal effort to implement, allowing you to define your own entities and keys, and whatever patterns your desire.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/IdentityServer/IdentityServer4/issues/1940#issuecomment-410240053, or mute the thread https://github.com/notifications/unsubscribe-auth/AMbxoBVoB5N9_iz5luozGRsYBx8GxSf3ks5uNEOigaJpZM4RSc3G .

jamescrowley321 avatar Aug 03 '18 16:08 jamescrowley321

Ok, thanks for the discussion. We'll close it then.

brockallen avatar Aug 03 '18 21:08 brockallen

The custom stores don't solve the problem of creating a CRUD interface for the data store. I used a different pattern to get around this, so feel free to close the issue. … On Fri, Aug 3, 2018, 8:35 AM Scott Brady @.***> wrote: Agreed, I'm not sold. I think the custom stores require minimal effort to implement, allowing you to define your own entities and keys, and whatever patterns your desire. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1940 (comment)>, or mute the thread https://github.com/notifications/unsubscribe-auth/AMbxoBVoB5N9_iz5luozGRsYBx8GxSf3ks5uNEOigaJpZM4RSc3G .

Hi @jamescrowley321, would you mind sharing your solution with us please?

Thanks in advance.

lucascc26 avatar Oct 28 '19 18:10 lucascc26

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 10 '20 15:01 lock[bot]