Add Base Entity Type for IdentityServer4.EntityFramework.Entities
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.
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... :)
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
At the end of the day, I can always fork if I need to...:)
Yea, in mentioning it I was just being a jackass in an attempt to avoid doing any work :)
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 .
I still want to think about it and I've not yet had time. Give me another week or so.
@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?
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/
@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.
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?
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.
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.
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 .
Ok, thanks for the discussion. We'll close it then.
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.
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.