0
votes

I have a simple Interface which I would like to implement with different Accessors

public interface IValidatorByState : IValidator
{
    AuthorizedDbContext Context { get; set; }
}

I'm trying to implement this on a class with an Public, Or Protected Get and an Internal Set;

Edit This class is is exposed to the user, they should only be able to retrieve the Context.

public class ValidatorByState<TEntity> : AbstractValidator<TEntity>, IValidatorByState
{
    public AuthorizedDbContext Context { get; internal set;}
}

No matter which way Implement this I've been given an error

Attempts:

public AuthorizedDbContext Context { get; internal set;}

ValidatorByState.Context is not public

protected AuthorizedDbContext Context { get; internal set;}

ValidatorByState.Context.set Accessor must be more restrictive than property,

AuthorizedDbContext _context;
public AuthorizedDbContext IValidatorByState.Context => this._context;
public AuthorizedDbContext IValidatorByState.Context {
    set { this._context = value; }
}

For this I get an error on both stating

Explicit implementation missing Accessor

How can I have a public get method with a internal set?

2

2 Answers

3
votes

I would create a public interface that was readonly and internal interface with the setter methods/properties. The internal interface has to be explicitly implementated:

public interface IValidatorByStateReadOnly 
{
    AuthorizedDbContext Context { get; }
}  

internal interface IValidatorByState : IValidatorByStateReadOnly
{
    new AuthorizedDbContext Context { get; set; }
}

public class ValidatorByState<TEntity> : IValidatorByState
{
    private AuthorizedDbContext _context;

    public AuthorizedDbContext Context => _context;

    AuthorizedDbContext IValidatorByState.Context 
    {
        get => _context;
        set => _context = value; 
    }
}
2
votes

Interface members must be public.

What about of:

public interface IValidatorByStateReader
{
    AuthorizedDbContext Context { get; }
}

internal interface IValidatorByStateWriter
{
    AuthorizedDbContext Context { set; }
}

public class ValidatorByState<TEntity> : IValidatorByStateReader, IValidatorByStateWriter
{
    private AuthorizedDbContext _context;
    AuthorizedDbContext IValidatorByStateReader.Context => this._context;
    AuthorizedDbContext IValidatorByStateWriter.Context { set => this._context = value; }
}