Different value of property for contracts

I have two interfaces implemented by one main class. How can I reorganize my code so that when implementing each contract, the methods of each contract have a different value for a parameter, such as DatabaseName.

Example:

  • Class1 implements interface1, interface2
  • Interface1.GetData () has a DatabaseName for database 1
  • Interface2.GetData () has a DatabaseName for database 2

I can configure these values ​​in the GetData () methods, but I need a cleaner way to do this.

Any recommendation on a template is that DI, Domain driven, even an example of basic inheritance that does the above, is what I'm looking for.

+4
source share
2 answers

, :

public class Class1 : Interface1, Interface2
{
    // Note the lack of access modifier here. That important!
    Data Interface1.GetData()
    {
        // Implementation for Interface1
    }

    Data Interface2.GetData()
    {
        // Implementation for Interface2
    }
}

, , .

+6

, , 2 , , . , , , , , . 2 , factory, , , . , .

public interface ISQLReader
    {
        string GetData();
    }

    public interface IOracleReader
    {
        string GetData();
    }

    public abstract class Reader
    {
        protected void CommonFunctionaility()
        {

        }
    }
    public class MSSQLReader : Reader, ISQLReader
    {
        public string GetData()
        {
            return "MSSQL";
        }
    }

    public class OracleReader : Reader, IOracleReader
    {
        public string GetData()
        {
            return "Oracle";
        }
    }

    public interface IReaderFactory
    {
        OracleReader CreateOracleReader();
        MSSQLReader CreateMSSQLReader();
    }

    public class ReaderFactory : IReaderFactory
    {
        public MSSQLReader CreateMSSQLReader() => new MSSQLReader();

        public OracleReader CreateOracleReader() => new OracleReader();
    }

    public class ReaderClient
    {
        private IReaderFactory _factory;
        public ReaderClient(IReaderFactory factory)
        {
            this._factory = factory;
        }
    }

- , , , .

0

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


All Articles