Sunday, May 27, 2007

Marking BLL Dataobjects

-CLASS

[System.ComponentModel.DataObject]
public class ProductsBusinessLogic

-METHOD

[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, true)]
public ProductsDataTable GetProducts()

"The DataObject attribute marks the class as being an object suitable for binding to an ObjectDataSource control, whereas the DataObjectMethodAttribute indicates the purpose of the method. As we'll see in future tutorials, ASP.NET 2.0's ObjectDataSource makes it easy to declaratively access data from a class. To help filter the list of possible classes to bind to in the ObjectDataSource's wizard, by default only those classes marked as DataObjects are shown in the wizard's drop-down list. The ProductsBLL class will work just as well without these attributes, but adding them makes it easier to work with in the ObjectDataSource's wizard."

source: http://asp.net/learn/dataaccess/tutorial02cs.aspx?tabid=63
**************
nullable types

int?
decimal?

Friday, May 25, 2007

Use Class Library for DAL

Creating the Data Access Layer
from http://asp.net

When working with data one option is to embed the data-specific logic directly into the presentation layer (in a web application, the ASP.NET pages make up the presentation layer). This may take the form of writing ADO.NET code in the ASP.NET page's code portion or using the SqlDataSource control from the markup portion. In either case, this approach tightly couples the data access logic with the presentation layer. The recommended approach, however, is to separate the data access logic from the presentation layer. This separate layer is referred to as the Data Access Layer, DAL for short, and is typically implemented as a separate Class Library project.

******************************************

W3 Schools Javascript tutorial

Thursday, May 24, 2007

PInvoke (Platform Invoke)

using System;
using System.Runtime.InteropServices;

namespace TestPInvoke
{
///
/// Summary description for Class1.
///

class WrapK32
{
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);

///
/// The main entry point for the application.
///

[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Random random = new Random();

for (int i = 0; i < 10000; i++)
{
Beep(random.Next(10000), 100);
}
}
}
}

Tuesday, May 22, 2007

ASP.NET authentication

ASP.NET authentication modes:

Windows
Passport
Forms

Monday, May 14, 2007

Design Patterns

Source: Design Pattern in C#, 2002 by James W Cooper

"Design Patterns—Elements of Reusable Software, by Gamma,
Helm, Johnson, and Vlissides (1995). This book, commonly referred to as
the Gang of Four, or “GoF,” book, has had a powerful impact on those
seeking to understand how to use design patterns and has become an all time bestseller."

"Design patterns are just convenient ways of reusing object oriented
code between projects and between programmers."

"Model-View-Controller framework for Smalltalk (Krasner and Pope 1988), which divided the user interface problem into three parts, as shown in Figure 1-1. The parts were referred to as a data model, which contains the computational parts of the program; the view, which presented the user interface; and the controller, which interacted between the user and the view."



"In other words, design patterns describe how objects communicate without
become entangled in each other’s data models and methods."

"Design patterns focus more on reuse of recurring architectural design
themes, while frameworks focus on detailed design and
implementation.” (Coplien and Schmidt 1995)"

· Creational patterns create objects for you rather than having you
instantiate objects directly. This gives your program more flexibility in
deciding which objects need to be created for a given case.
· Structural patterns help you compose groups of objects into larger
structures, such as complex user interfaces or accounting data.
· Behavioral patterns help you define the communication between
objects in your system and how the flow is controlled in a complex
program.

"Program to an interface and not to an implementation.
Putting this more succinctly, you should define the top of any class
hierarchy with an abstract class or an interface, which implements no
methods but simply defines the methods that class will support. Then in all
of your derived classes you have more freedom to implement these
methods as most suits your purposes."

"Object composition. This is simply the construction of objects that contain others:
encapsulation of several objects inside another one."


*For example, the Listbox, DataGrid, and TreeView are introduced in the Adapter and Bridge patterns.
*We show how to paint graphics objects in the Abstract Factory,
*We introduce the Enumeration interface in the Iterator and in the Composite, where we also take up formatting.
*We use exceptions in the Singleton pattern and,
*discuss ADO.NET database connections in the Façade pattern.
*And we show how to use C# timers in the Proxy pattern.


--------------------------
Managed languages are garbage collected.
Garbage collected languages take care of releasing unused
memory: you never have to be concerned with this. As soon as the garbage
collection system detects that there are no more active references to a
variable, array or object, the memory is released back to the system. So
you no longer need to worry as much about running out of memory
because you allocated memory and never released it. Of course, it is still
possible to write memory-eating code, but for the most part you do not
have to worry about memory allocation and release problems.
--------------------------

The most significant problem is that the user interface and the data handling are combined in a single program module, rather than being handled separately. It is usually a good idea to keep the data manipulation and the interface manipulation separate so that changing interface logic doesn’t impact the computation logic and vice-versa.

"We will begin class names with capital letters and instances of classes with lowercase letters. We will also spell instances and classes with a mixture of lowercase and capital letters to make their purpose clearer: swimmerTime"

"In C# as well as other object oriented languages, you can have several
class methods with the same name as long as they have different calling
arguments or signatures."

Replacing Methods Using New
Another way to replace a method in a base class when you cannot declare
the base class method as virtual is to use the new keyword in declaring the
method in the derived class. If you do this, it effectively hides any
methods of that name (regardless of signature) in the base class. In that
case, you cannot make calls to the base method of that name from the
derived class, and must put all the code in the replacement method.
public new void draw(Graphics g) {
g.DrawRectangle (rpen, x, y, w, h);
g.DrawRectangle (rdPen, x +5, y+5, w, h);


UML

The symbols in front of the names indicate that member’s visibility,
where “+” means public,
“- ” means private, and
“#” means protected.
Static methods are shown underlined.
Abstract methods may be shown in italics or with an “{abstract}” label.


Hashtables
A Hashtable is a variable length array where every entry can be referred to
by a key value.

Typically, keys are strings of some sort, but they can be any sort of object. Each element must have a unique key, although the elements themselves need not be unique. Hashtables are used to allow rapid access to one of a large and unsorted set of entries, and can also be used by reversing the key and the entry values to create a list where each entry is guaranteed to be unique.

Hashtable hash = new Hashtable ();
float freddy = 12.3f;
hash.Add ("fred", freddy); //add to table
//get this one back out
float temp = (float)hash["fred"];


File Handling
The file handling objects in C# provide you with some fairly flexible
methods of handling files.
The File Object
The File object represents a file, and has useful methods for testing for a
file’s existence as well as renaming and deleting a file. All of its methods
are static, which means that you do not ( and cannot) create an instance of
File using the new operator. Instead, you use its methods directly.
if (File.Exists ("Foo.txt"))
File.Delete ("foo.txt");
You can also use the File object to obtain a FileStream for reading and
writing file data:
//open text file for reading
StreamReader ts = File.OpenText ("foo1.txt");
//open any type of file for reading
FileStream fs = File.OpenRead ("foo2.any");
Some of the more useful File methods are shown in the table below:
Static method Meaning
File.FileExists(filename) true if file exists
File.Delete(filename) Delete the file
File.AppendText(String) Append text
File.Copy(fromFile, toFile) Copy a file
File.Move(fromTile, toFile) Move a file, deleting old
copy
File.GetExtension(filename) Return file extension
File.HasExtension(filename) true if file has an
extension.

Reading Text File
To read a text file, use the File object to obtain a StreamReader object.
Then use the text stream’s read methods:
StreamReader ts = File.OpenText ("foo1.txt");
String s =ts.ReadLine ();
Writing a Text File
To create and write a text file, use the CreateText method to get a
StreamWriter object.
//open for writing
StreamWriter sw = File.CreateText ("foo3.txt");
sw.WriteLine ("Hello file");
If you want to append to an existing file, you can create a StreamWriter
object directly with the Boolean argument for append set to true:
//append to text file
StreamWriter asw = new StreamWriter ("foo1.txt", true);
Exceptions in File Handling
A large number of the most commonly occurring exceptions occur in
handling file input and output. You can get exceptions for illegal
filenames, files that do not exist, directories that do not exist, illegal
filename arguments and file protection errors. Thus, the best way to


handle file input and output is to enclose file manipulation code in Try
blocks to assure yourself that all possible error conditions are caught, and
thus prevent embarrassing fatal errors. All of the methods of the various
file classes show in their documentation which methods they throw. You
can assure yourself that you catch all of them by just catching the general
Exception object, but if you need to take different actions for different
exceptions, you can test for them separately.
For example, you might open text files in the following manner:
try {
//open text file for reading
StreamReader ts = File.OpenText ("foo1.txt");
String s =ts.ReadLine ();
}
catch(Exception e ) {
Console.WriteLine (e.Message );
}

Friday, May 11, 2007

SQL Queries

Specifying Identity column:

ALTER TABLE Menus
ADD MenuID numeric(18, 0) IDENTITY NOT NULL

Adding a Primary Key:

ALTER TABLE Orders
ADD CONSTRAINT PK_Orders PRIMARY KEY (OrderID)

Adding a Foreign Key: (Primary Key is in 'Orders' Table)

ALTER TABLE Roles_Menus
ADD FOREIGN KEY (RoleID) REFERENCES Roles(RoleID);