Internal and external interfaces and collections

What would be the best way to implement the following?

I have a set of objects that implement an interface, internally I want to be able to expose the set and get properties and get only external ones.

Here is an example of what I want ... It does not compile.

public interface ITable
{
   string Name { get; }
}

internal interface IInternalTable 
{
   string Name { get; set; }
}

internal class Table : ITable, IInternalTable
{
   public string Name { get; set; }
   public string ITable.Name { get { return Name; } }
}

public class Database
{
    private List<IInternalTable> tables;

    public List<ITable>
    {
       get { return this.tables; }
    }
}
+3
source share
5 answers

Use this:

public interface ITable
{
    string Name { get; }
}

public class Table : ITable
{
    public string Name { get; internal set; }
}

public class Database
{
    public List<ITable> Tables { get; private set; }
}

Note . The accessibility modifier used in get or set accessor can limit visibility without increasing it.

+3
source

IInternalTable , IInternalTable , ( IInternalTable:

public interface ITable
{
   string Name { get; }
}

internal interface IInternalTable 
{
   string Name { get; set; }
}

public class Table : ITable, IInternalTable
{
   public string Name { get; set; }
   string ITable.Name { get { return Name; } }
}

public class Database
{
    private List<Table> tables;

    public List<Table> Tables
    {
       get { return this.tables; }
    }
}

( , ... . .)

+1

, IInternalTable ITable. Koistya Navin:

public class Table {
    public string Name {get; internal set; }
}

public class Database {
    public IList<Table> Tables { get; private set;}

    public Database(){
        this.Tables = new List<Table>();
    }
}
0

, , , , . , , :

public interface ITable
{
    string Name { get; }
}

internal interface ITableInternal
{
   void SetName(string value);
}

public class Table : ITable, ITableInternal
{
    public string Name { get; }

    public void SetName(string value)
    {
       // Input validation

       this.Name = value;
    }
}

public class Database
{
    public Table CreateTable()
    {
        Table instance = new Table();
        ((ITableInternal)instance).SetName("tableName");

        return table;
    }    
}
0

, setter :

public interface ITable
{
   string Name { get; }
}

public class Database
{

    private interface IInternalTable 
    {
       string Name { get; set; }
    }

    private class Table : ITable, IInternalTable
    {
        public string Name { get; set; }
        string ITable.Name { get { return Name; } }
    }

    private List<IInternalTable> tables;

    public List<ITable> Tables
    {
       get { return this.tables; }
    }
}

This way, no one Databasecan change items in Database.Tables.

0
source

Source: https://habr.com/ru/post/1705957/


All Articles