1 / 58

C#: Introduction for Developers

Neal Stublen nstublen@jccc.edu. C#: Introduction for Developers. Tonight’s Agenda. Interfaces Generics Code Organization Databases ADO.NET Datasets Q&A. Chapter 15 Interfaces. What’s an interface?. It’s like a class with… No member data All methods are public

arden-burke
Download Presentation

C#: Introduction for Developers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Neal Stublen nstublen@jccc.edu C#: Introduction for Developers

  2. Tonight’s Agenda • Interfaces • Generics • Code Organization • Databases • ADO.NET • Datasets • Q&A

  3. Chapter 15Interfaces

  4. What’s an interface? • It’s like a class with… • No member data • All methods are public • All methods are abstract • An interface implies nothing about the implementation behind it • A class can only inherit from one class • A class can implement multiple interfaces • Interfaces often represent only a portion of a class’ functionality

  5. Why interfaces? • Interfaces are better suited to situations in which your applications require many unrelated object types to provide certain functionality • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces. • Interfaces are better in situations in which you do not want or need to inherit implementation from a base class. • Structures cannot inherit from classes, but they can implement interfaces.

  6. Common .NET Interfaces • IComparable • Used to compare two objects • Sorting algorithms • IDisposable • Frees unmanaged resources when an object goes out of scope • If your classes implement these interfaces, they can be used wherever these interfaces are specified

  7. Example Interfaces public interface IStreamable { bool Read(FileStreaminInput); bool Write(FileStreaminOutput); } public interface IHTMLDisplayable { void Render(OutputStreaminStream); }

  8. Implementing Interfaces public interface IStreamable { bool Read(FileStreaminInput); bool Write(FileStreaminOutput); } public class MyObject : IStreamable { public boolRead(FileStreaminInput) { } public boolWrite(FileStreaminOutput) { } }

  9. Implementing ICloneable // Duplicate any cloneable object public List<object> Duplicator(ICloneablesrc, int count) { List<object> list = new List<object>(); for (int index = 0; index < count; ++index) { list.Add(src.Clone()); } return list; }

  10. Looking at IComparable • Any object that implements IComparable can be sorted with the generic Array.Sort() method

  11. Chapter 15, Part 2Generics

  12. What’s a generic class? • A class definition that doesn’t explicitly declare all of the types it uses internally • Allows creation of new types by specifying those internal types later

  13. Using Generic Types • Generic types are declared using <> after the type name • Instead of the general purpose interface: IComparable • Use the type-specific interface: IComparable<T> class Temperature : IComparable class Temperature : IComparable<Temperature>

  14. Generics Example class Average<T> { public void Include(TinValue); public T Average { get { ... } } } Average<int> integerAverage; Average<double> doubleAverage;

  15. Updating IComparable • Instead of comparing generic objects, we can compare Temperature objects

  16. Common Generics • Collection classes • List<T> • SortedList<K, V> • Enumeration (foreach) • IEnumerable<T> • IEnumerator<T>

  17. Generic Constraints public class MyGeneric<T> where T: class { // T is a class (can be assigned null) } public class MyGeneric<T> where T: class, IComparable<T> { // T implements IComparable interface } public class MyGeneric<T> where T: struct { // T is a struct } public class MyGeneric<T> where T: new() { // T has a default constructor }

  18. Chapter 16Code Organization

  19. Code Organization • Multiple classes in a single file • Closely related classes (e.g. EventArgs) • Nested classes • Objects that only exist within the context of another type of object

  20. Partial Classes • Split a single class over multiple files • Forms split Designer code into separate file • Possible to split interface implementations into separate files • Partial classes must have the same visibility • Partial methods split the declaration and implementation across files

  21. Namespaces • Organize classes into logical groupings • Avoid type name collisions • using <namespace> • <namespace>.<ClassName>

  22. Nested Namespaces using ns1 { using ns2 { } } using ns1.ns2 { }

  23. Chapter 16, Part 2Documenting Classes

  24. XML Documentation • Standardized documentation format for C# code • Type three slashes “///” before a method implementation or class member • <summary> tag is used by IntelliSense • Processed by third party tools • Doxygen • Sandcastle

  25. Chapter 16, Part 3Class Libraries

  26. Class Libraries • Share class across multiple projects • Projects “reference” other projects • Move SportsTeam into a class library

  27. Chapter 17Database Programming

  28. Databases • Client-server architecture • One server, many clients • Server runs Microsoft SQL Server • Clients use ADO.NET 4 • Relational databases • SQL (Structured Query Language)

  29. Tables • Tables store data • One or more records (rows) • A primary key uniquely identifies each row • Indexes provide an efficient way to access data based on values in specific columns

  30. “Relations” Among Tables • Key columns are used to relate tables • Foreign keys in one table correspond to primary keys in another table • One-to-many • One-to-one • Many-to-many

  31. Table Columns • Columns are defined by a name and data type • bit • char, varchar, text • datetime, smalldatetime • decimal, numeric • float, real • bigint, int, smallint, tinyint • money, smallmoney

  32. Column Values • null (maybe, maybe not – depends on column definition) • Default value • Identity column (auto-increment) • Constraints

  33. SELECT • Select data from a database • SELECT column_name,column_nameFROM table_name; • SELECT * FROM table_name; • SELECT title, publisher FROM books;

  34. INNER JOIN • Select all rows from two table where specified columns have matching values • SELECT column_name(s)FROM table1INNER JOIN table2ON table1.column_name=table2.column_name; • SELECT column_name(s)FROM table1JOIN table2ON table1.column_name=table2.column_name; • INNER JOIN is the same as JOIN

  35. ADD, UPDATE, DELETE • Add a row to a table • INSERT INTO table_nameVALUES (value1,value2,value3,...); • INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...); • Update a row in a table • UPDATE table_nameSET column1=value1,column2=value2,...WHERE some_column=some_value; • Delete a row from a table • DELETE FROM table_nameWHERE some_column=some_value;

  36. Online Reference • Tutorials and references • http://w3schools.com/sql

  37. ADO.NET

  38. ADO.NET • Data providers implement a common API to various database servers • SQL Server • OLE DB • ODBC • Oracle • Third party • MySQL • SQLite

  39. Components • Database server • .NET data provider • Connection • Connects to a specific database • Command • SELECT, UPDATE, etc. • DataReader, DataAdapter • Help retrieve data from a query

  40. Datasets • Store data in a disconnected cache • Data source may be a database, XML data, local application data • Mimics the structure and behavior of a relational database

  41. Datasets Mimic Relational DB • Dataset contains a collection of tables • Not necessarily the same table from the database • May be a subset of columns and rows • May be joined to another table • Tables contain a collection of columns • Tables contain a collection of rows • Tables contain a collection of constraints • Dataset contains a collection of relations • Everything is accessed through object properties

  42. Alternatives to Datasets • You can use Command and Connection objects directly • Select, Insert, Update, Delete

  43. Database Concurrency • Multiple clients accessing data • ADO.NET datasets are “disconnected” • Pessimistic concurrency • Lock database records to prevent conflicts • Optimistic concurrency • Check for data changes before writing • Throw an exception if data has changed • “Last in wins” • Data is written by last operation • Data may be lost

  44. Avoid Concurrency Issues • Update and refresh datasets frequently • Avoid updating large tables in datasets • Only reduces risk! • You still must handle the exceptions

  45. Chapter 18Creating Datasets

  46. Database Check

  47. Populating a Database • SQLExpress should be installed with Visual Studio • The book provides a .sql file for populating the MMABooks database in SQLExpress • Double-click the .bat file on the S: drive • We’ll need to repeat this process at the start of each class session

  48. Confirm Database Access • Using Visual Studio to locate the new database as a Data Source • View > Server Explorer • Add Connection... • Server name: .\SQLEXPRESS • Database name: MMABooks • Test Connection

  49. Using the Data Sources Window

  50. Dataset Summary • Instead of a database, we can pull data from: • WCF Data Services • Custom objects • SharePoint • Entity Framework • Object-relational mapping framework (ORM) • Maps database table data to C# objects • Object instance represents a table row • View > Server Explorer

More Related