C# course 4

DDD – domain driven design https://en.wikipedia.org/wiki/Domain-driven_design

  1. Interface
    a) definition
  • it is like a contract:
  • contains declarations of methods and fields, which must be implemented by class which uses this interface
  • interface has public decalrations of methods
    -methods don’t have a body
public interface IEquatable
{
bools Equals(T obj);
}

b)example:

//definiujemy interfejs w osobnym pliku
public interface IDatabase
{
void Connect();
void InsertData();
string GetName(); //bedzie zwracac nazwe bazy danych
}

//definiujemy klase SQLServer w osobnym pliku
public class SqlServer: IDatabase
{
public string Name => "SQL Server"; //wartosc domyslna
//trzeba teraz pokolei implementowac metody interfejsu
public void Connect()
{
Console.WriteLine("(SQLServer)Connection to SQL Server");
}

public void InsertData()
{
Console.WriteLine("(SQLServer)Inserting data into tables");
}

public string GetName()
{
return name;
}
}
//definiujemy klase MongoDB w osobnym pliku
public class MongoDB: IDatabase
{
public string Name => "MongoDB"; //wartosc domyslna
//trzeba teraz pokolei implementowac metody interfejsu
public void Connect()
{
Console.WriteLine("(MongoDB)Connection to MongoDB");
}

public void InsertData()
{
Console.WriteLine("(MongoDB)Inserting data into JSON");
}

public string GetName()
{
return name;
}
}
//no i teraz mozna uzyc takiej magicznej klasy, ktora laczy wszystko w jedno
public class DatabaseManager
{
//to jest genialne!!!
public void ExecuteOperations(IDatabase database)
{
Console.WriteLine($"Database name: {database.GetName()}");
database.Connect();
database.InsertData();
}
}
//i teraz mozna w glownym programie zrobic tak:
var manager=new DatabaseManager();
MongoDB mongo=new MongoDB();
SQLServer sqlServer=new SQLServer();
manager.ExecuteOperations(mongoDB);
manager.ExecuteOperations(SQLServer);

c) And here is another power of interface:
-inheritance from several interfaces at the same time!

//czyli robimy tak:
public class MongoDB:IDatabase,IDisposable
{
public string Name=>"MongoDB";
public void Connect()
{
//connecting
}
public EndConnection(IDatabase database)
{
database.Dispose();
//end connection
}
}
//a teraz inna magia
//dziedziczenie interfejsu po interfejsie
public interface IDatabase:Disposable
{
//….
}
//mozna sobie tworzyc zmienne
IDatabase database=sqlServer;
//mozna tworzyc properties w klasach oparte na interfejsach
public IEnumerate in {get;set;}

d) How to differntiate interface from „normal” inhertiance?

//zawsze jest tak:
public class MongoDB:Klasa_nadrzedna,interfejs
  1. Enum
    -enumerable types (accesible as static classes in all project)
    //na przyklad
    public enum DatabaseType
    {
    NoSQL=0, //mozna przypisac konkretne wart. liczbowe
    SQL=1
    }

//i potem mozna to uzyc tak:
class public aa
{
//to jest typu readonly
public Databasetype Type { get; set; }

public void Connect()
{
type =DatabaseType.NoSQL;
///
}

}

//a co jesli chcemy miec enumy typu string -to musimy to zrobic przez klase statyczna
public static class DatabaseStringType
{
public static string NoSQL=>”NoSQL”;
public static string NoSQL=>”SQL”;

}

3. Interface and Abstract Class
Interfaces:
-only declarations without implementation
-class can implement many interfaces

-all of the methods are public
-doesn’t have fields or constants

Abstract class:
-has only declarations with keyword „abstract”
-can have methods with body ,which can be overrriden by child class
-doesn’t allow for muliple inheritance from many abstract classes

  1. Readonly vs Constant
    a) //readonly robimy tak:
    -it is only used for read
    -it is used only in constructor
    -initialized while runtime
    -can be static
    private readonly string _readonlyField=”Readonly field”;

//konstruktor klasy Class
public Class()
{
_readonlyField=”aa”;
}

b) constant
-it always has to be declared and initialized; cannot be set in constructor
-readonly
-initialised while building program (compilation)


private const string _constField=”constant Field”;

Opublikowany w C#Tagged

2 myśli na “C# course 4

Skomentuj here Anuluj pisanie odpowiedzi

Twój adres e-mail nie zostanie opublikowany.