• Nie Znaleziono Wyników

Warstwy aplikacji sieciowych

N/A
N/A
Protected

Academic year: 2021

Share "Warstwy aplikacji sieciowych"

Copied!
22
0
0

Pełen tekst

(1)

Aplikacje sieciowe

Warstwy aplikacji sieciowych

(2)

Architektura warstwowa

Warstwa prezentacji

Warstwa logiki (aplikacji)

Warstwa danych

(3)

Warstwa prezentacji

• Strona aspx

• Arkusze stylów css

• Kontrolki wizualne

(4)

Warstwa logiki (aplikacji)

• Własne obiekty

• Obiekty z bibliotek .NET

• Obiekty komunikacji ze źródłem danych

public partial class Kontakt : System.Web.UI.Page {

protected void Page_Load(object sender, EventArgs e) { } protected void SendMailBtn_Click(object sender, EventArgs e) { MailMessage mail = new

MailMessage( MailFromInp.Text,ConfigurationManager.AppSettings["contactPersonMail Adress"] , MailSubjectInp.Text, MailBodyInp.Text );

SmtpClient smpt = new SmtpClient();

smpt.Send(mail);

}

}

(5)

Warstwa danych

(6)

Źródła danych

• Bazy danych

Access, MS Sql, inne

• Pliki XML

- formaty typu SiteMap - inne formaty XML

• Obiekty (tablice, kolekcje itp..)

• Pliki tekstowe i inne

(7)

Podstawowe obiekty Bazodanowe

• SqlCommand

• SqlConnection

• ConnectionString

(8)

Warstwowy schemat pracy ze źródłem danych

<TeacherList>

<teacher name="Paweł Fałat" certificates="MCP, MCAD"/>

<teacher name="Kornel Warwas" certificates="MCP, MCAD"/>

<teacher name="Szymon Tegler" certificates="MCP, MCAD"/>

<teacher name="Andrzej Polański" certificates="MCP, MCAD"/>

<teacher name="Tomasz Gancarczyk" certificates="MCP, MCSE"/>

</TeacherList>

(9)

Obiekty typu DataSource

(10)

Tworzenie Menu: Źródło dokument typu SiteMap

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

<siteMapNode url="Default.aspx" title="Home" description="">

<siteMapNode url="~/ConferenceCommity.aspx" title="Conference Commity" description="" />

<siteMapNode url="~/ScientificCommity.aspx" title="Scientific Commity" description="" />

<siteMapNode url="~/Null3.aspx" title="Comunicates" description="" >

<siteMapNode url="~/Null3a.aspx" title="Comunicate nr 1" description="" />

<siteMapNode url="~/Null3b.aspx" title="Comunicate nr 2" description="" />

<siteMapNode url="~/Null3c.aspx" title="Comunicate nr 2" description="" />

</siteMapNode>

...

</siteMap>

(11)

Źródło danych: dokument XML

<TeacherList>

<teacher name="Paweł Fałat" certificates="MCP, MCAD"/>

<teacher name="Kornel Warwas" certificates="MCP, MCAD"/>

<teacher name="Szymon Tegler" certificates="MCP, MCAD"/>

<teacher name="Andrzej Polański" certificates="MCP, MCAD"/>

<teacher name="Tomasz Gancarczyk" certificates="MCP, MCSE"/>

</TeacherList>

(12)

SqlDataSource

(13)

SqlDataSource: funkcje operujące na danych

• Select()

• Update()

• Insert()

• Delete()

Wykorzystują przygotowane obiekty typu

SqlCommand połączone z kolekcjami parametrów zapytania:

• SelectQuery

• InsertQuery

• UpdateQuery

• DeleteQuery

(14)

SqlCommand

(15)

SqlCommand przykład kodu

using System.Data;

using System.Data.SqlClient;

public static String GetUserNameForReview(String ReviewID) { SqlConnection connection = new

SqlConnection(ConfigurationManager.ConnectionStrings["ConfernceDBConnectionString"].ConnectionString);

SqlCommand getUserNameCommand = new SqlCommand();

getUserNameCommand.CommandType = CommandType.Text;//CommandType.StoredProcedure// TableDirect

getUserNameCommand.Parameters.Add("@ReviewID", SqlDbType.NVarChar);

getUserNameCommand.Connection = connection;

getUserNameCommand.Parameters["@ReviewID"].Value = ReviewID;

getUserNameCommand.CommandText = "SELECT aspnet_Users.UserName"

+ " FROM Review INNER JOIN aspnet_Users"

+ " ON Review.ReviewerID = aspnet_Users.UserId"

+ " WHERE (Review.ReviewID = @ReviewID)";

connection.Open();

//getUserNameCommand.ExecuteXmlReader(); ExecuteReader(); ExecuteNonQuery() ; string UserName = getUserNameCommand.ExecuteScalar().ToString();

connection.Close();

if (UserName == null) return "NullUser";

return UserName;

}

(16)

DataSet

• Może odzwierciedlić strukturę bazy danych z tabelami i związkami między nimi. Umożliwia wykorzystanie i modyfikację tej struktury

• Typed DataSet

Na podstawie definicji tabel tworzy się plik xsd

zawierający strukturę obiektów. Na podstawie tego pliku generowana jest klasa(y) wykorzystywane w trakcie tworzenia programu

- Wsparcie dla Intelisense

- Sprawdzanie składni w trakcie kompilacji

• Untyped Dataset

Do dynamicznego ładowania nie zdefiniowanej

wstępnie struktury tabel

(17)

DataSet Designer

(18)

DataSet Plik xsd

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="UserListDataSet" targetNamespace="http://tempuri.org/UserListDataSet.xsd" xmlns:mstns="http://tempuri.org/UserListDataSet.xsd" xmlns="http://tempuri.org/UserListDataSet.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified"

elementFormDefault="qualified">

<xs:annotation>

<xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">

<DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema"

xmlns="urn:schemas-microsoft-com:xml-msdatasource">

<Connections>

<Connection AppSettingsObjectName="Web.config" AppSettingsPropertyName="ConfernceDBConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly"

Name="ConfernceDBConnectionString (Web.config)" ParameterPrefix="@"

PropertyReference="AppConfig.System.Configuration.ConfigurationManager.0.ConnectionStrings.ConfernceDBConnectionString.ConnectionString" Provider="System.Data.SqlClient" />

</Connections>

<Tables>

<TableAdapter BaseClass="System.ComponentModel.Component" DataAccessorModifier="AutoLayout, AnsiClass, Class, Public" DataAccessorName="aspnet_UsersTableAdapter"

GeneratorDataComponentClassName="aspnet_UsersTableAdapter" Name="aspnet_Users" UserDataComponentName="aspnet_UsersTableAdapter">

<MainSource>

<DbSource ConnectionRef="ConfernceDBConnectionString (Web.config)" DbObjectName="dbo.aspnet_Users" DbObjectType="Table" FillMethodModifier="Public" FillMethodName="Fill"

GenerateMethods="Both" GenerateShortCommands="true" GeneratorGetMethodName="GetData" GeneratorSourceName="Fill" GetMethodModifier="Public" GetMethodName="GetData"

QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="false"

UserGetMethodName="GetData" UserSourceName="Fill">

<DeleteCommand>

<DbCommand CommandType="Text" ModifiedByUser="false">

<CommandText>DELETE FROM [dbo].[aspnet_Users] WHERE (([ApplicationId] = @Original_ApplicationId) AND ([LoweredUserName] =

@Original_LoweredUserName))</CommandText>

<Parameters>

<Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Guid" Direction="Input" ParameterName="@Original_ApplicationId" Precision="0"

ProviderType="UniqueIdentifier" Scale="0" Size="0" SourceColumn="ApplicationId" SourceColumnNullMapping="false" SourceVersion="Original" />

<Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@Original_LoweredUserName" Precision="0"

ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="LoweredUserName" SourceColumnNullMapping="false" SourceVersion="Original" />

</Parameters>

</DbCommand>

</DeleteCommand>

...

(19)

DataSet Przykład kodu (źródło: msdn)

// Assumes a valid SqlConnection object named connection.

SqlDataAdapter customerAdapter = new SqlDataAdapter( "SELECT CustomerID, CompanyName, Phone FROM Customers", connection);

SqlDataAdapter orderAdapter = new SqlDataAdapter( "SELECT OrderID, CustomerID, EmployeeID, OrderAdapter FROM Orders", connection);

// Populate a strongly typed DataSet.

connection.Open();

CustomerDataSet customers = new CustomerDataSet();

customerAdapter.Fill(customers, "Customers");

orderAdapter.Fill(customers, "Orders");

connection.Close();

// Add a strongly typed event.

customers.Customers.CustomerChanged += new

CustomerDataSet.CustomerChangeEventHandler(OnCustomerChanged);

// Add a strongly typed DataRow.

CustomerDataSet.Customer newCustomer = customers.Customers.NewCustomeromer();

newCustomer.CustomerID = "NEW01";

newCustomer.CompanyName = "My New Company";

customers.Customers.AddCustomer(newCustomer);

(20)

LINQ Language-Integrated Query

static void Main() {

// The Three Parts of a LINQ Query:

// 1. Data source.

int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

// 2. Query creation.

// numQuery is an

IEnumerable<int> var numQuery = from num in numbers

where (num % 2) == 0 select num;

// 3. Query execution.

foreach (int num in numQuery) { Console.Write("{0,1} ", num); } }

Źródło MSDN

(21)

LINQ to SQL

// Northwnd inherits from

System.Data.Linq.DataContext.

Northwnd nw = new

Northwnd(@"northwnd.mdf");

var companyNameQuery = from cust in nw.Customers where cust.City == "London"

select cust.CompanyName;

foreach (var customer in companyNameQuery) {

Console.WriteLine(customer);

}

Źródło MSDN

(22)

LINQ to XML

Źródło MSDN

XElement srcTree = new XElement("Root", new XElement("Element1", 1), new XElement("Element2", 2),

new XElement("Element3", 3), new XElement("Element4", 4), new XElement("Element5", 5) );

XElement xmlTree = new XElement("Root", new XElement("Child1", 1), new XElement("Child2", 2),

new XElement("Child3", 3), new XElement("Child4", 4), new XElement("Child5", 5) );

xmlTree.Add(new XElement("NewChild", "new content"));

xmlTree.Add(

from el in srcTree.Elements() where (int)el > 3 select el );

<Root>

<Child1>1</Child1>

<Child2>2</Child2>

<Child3>3</Child3>

<Child4>4</Child4>

<Child5>5</Child5>

<NewChild>new content</NewChild>

<Element4>4</Element4>

<Element5>5</Element5>

</Root>

Cytaty

Powiązane dokumenty

Definicja metody w klasie ApplicationBean1 związanej z odczytem (przygotujksiazki) danych typu kolekcja obiektów TEgzemplarz i TEgzemplarz_termin w warstwie biznesowej –

Zad1.1 - zadanie dotyczące aplikacji Javy na podstawie klasy JPanel lub pochodnej klasy JPanel (wspólnej również dla apletu).. Zad1.2 – zadanie dotyczące tworzenia aplikacji Javy

Głównym celem ćwiczenia jest przeprowadzenie pełnej charakterystyki struktury krystalicznej przykładowego związku w oparciu o plik CIF przy wykorzystaniu programu

Wartość wyrażenia arytmetycznego musi być możliwa do obliczenia podczas kompilacji.. Komentarze są ciągami znaków ignorowanymi podczas

The interface will consist of a JFrame (the calculators window), a JTextArea (for entering data) and a custom component called the CalculatorButtonPanel that builds up the number

W obu plikach stwórz również metody main przyjmujące jako argument dwie liczby i następnie wywołujące drugą metodę z danej klasy zwracającą wynik operacji odpowiadającej nazwie

(1 punkt) Stwórz interfejs znajdujący się we własnym pakiecie i zawierający przynajmniej jedną metodę3. Pokaż że jest ona

(2 punkty) Zaimplementuj w języku Java klasę, której obiekty będą reprezentować pojedyncze karty z talii (nazwa klasy: Karta).. Podstawowe własności każdej karty to: kolor