renathy
renathy

Reputation: 5355

How and when is AbpSession set?

The story behind:

Now I have to create my own repository (it is not EF repository, but custom repository). And this repository should take "MyNewConnectionString" from Tenant. I know that Tenant information is available from AbpSession. But I do not understand how and when AbpSession should be sent to MyRepository.

Probably, I have to add IAbpSession in MyRepository constructor, but how to set it? Who sets AbpSession and when AbpSession actually gets value? If I understand how this works in ASP.NET Boilerplate's DbContext, I could recreate the same logic in my own repository.

Upvotes: 1

Views: 2624

Answers (1)

aaron
aaron

Reputation: 43098

Probably, I have to add IAbpSession in MyRepository Constructor, but how to set it?

You can property-inject IAbpSession:

public class MyRepository : ITransientDependency
{
    public IAbpSession AbpSession { get; set; }

    public MyRepository()
    {
        AbpSession = NullAbpSession.Instance;
    }
}

Who sets AbpSession and when AbpSession actually gets value?

IAbpSession is implemented as a singleton by ClaimsAbpSession.

Its properties are getters. For example:

public override long? UserId
{
    get
    {
        // ...

        var userIdClaim = PrincipalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == AbpClaimTypes.UserId);
        // ...

        long userId;
        if (!long.TryParse(userIdClaim.Value, out userId))
        {
            return null;
        }

        return userId;
    }
}

Upvotes: 2

Related Questions