Aplikacje sieciowe
description
Transcript of Aplikacje sieciowe
Aplikacje sieciowe
Warstwy aplikacji sieciowych
Architektura warstwowa
Warstwa prezentacji
Warstwa logiki (aplikacji)
Warstwa danych
Warstwa prezentacji
• Strona aspx
• Arkusze stylów css
• Kontrolki wizualne
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["contactPersonMailAdress"] , MailSubjectInp.Text, MailBodyInp.Text );
SmtpClient smpt = new SmtpClient();
smpt.Send(mail);
}
}
Warstwa danych
Źródła danych
• Bazy danychAccess, MS Sql, inne
• Pliki XML- formaty typu SiteMap- inne formaty XML
• Obiekty (tablice, kolekcje itp..)
• Pliki tekstowe i inne
Podstawowe obiekty Bazodanowe
• SqlCommand
• SqlConnection
• ConnectionString
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>
Obiekty typu DataSource
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>
Ź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>
SqlDataSource
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
SqlCommand
SqlCommand przykład koduusing 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;
}
DataSet
• Może odzwierciedlić strukturę bazy danych z tabelami i związkami między nimi. Umożliwia wykorzystanie i modyfikację tej struktury
• Typed DataSetNa 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 DatasetDo dynamicznego ładowania nie zdefiniowanej wstępnie struktury tabel
DataSet Designer
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>
.....
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);
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
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
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>