Skip to content

Generic Repository with string as Primary Key #218

Answered by iammukeshm
RHaughton asked this question in Q&A
Discussion options

You must be logged in to vote

However, thinking about it. I guess it should be pretty straightforward, I tried this. This should work. Can you check?

public abstract class BaseEntity<T>
{
    public virtual T Id { get; private set; }
    public List<DomainEvent> DomainEvents = new();

    protected BaseEntity()
    {
        if(Id.GetType() == typeof(Guid))
        {
            Id = (T)Convert.ChangeType(NewId.Next().ToGuid(), typeof(T));
        }
    }
}

This would allow BaseEntity to sport multiple types. Do let me know @RHaughton

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by iammukeshm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants