Friday, October 29, 2010
INSTEAD OF TRIGGER
create table test (id
intime int identity (1,1) , datetime,outtime datetime)
create trigger testtrigger on testinstead of update as begindeclare @outtime datetime
select @outtime = intime from insertedupdate test set outtime = @outtimeend
insert test (intime) values (GETDATE())
update test set intime = getdate()where id = 1
select * from test
Thursday, October 28, 2010
UPDATE B
UPDATE ASET A.Class = B.Class ,A.[Division] = B.[Division],A.[RouteNo] = B.[RouteNo],A.[PickUpPoint] = B.[PickUpPoint],A.[DropRouteNo] = B.[DropRouteNo],A
.[DropDownPoint] = B.[DropDownPoint]FROM DailyAttendanceTable A JOIN SCHULE_STUDENTDETAILS BON A.StudentId = B.StudentId
Update A (without using join)
UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = B.Class ,DailyAttendanceTable.[Division] = B.[Division],DailyAttendanceTable.[RouteNo] = B.[RouteNo],DailyAttendanceTable.[PickUpPoint] = B.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = B.[DropRouteNo],DailyAttendanceTable
.[DropDownPoint] = B.[DropDownPoint]FROM SCHULE_STUDENTDETAILS BWHERE DailyAttendanceTable.StudentId = B.StudentId
SQL SERVER : UPDATING MULTIPLE FIELDS FROM ANOTHER TABLE IN A SINGLE QUERY
Two syntaxes are possible :
1.
===================================================================
UPDATE
DailyAttendanceTable SET DailyAttendanceTable.Class = B.Class ,DailyAttendanceTable.[Division] = B.[Division],DailyAttendanceTable.[RouteNo] = B.[RouteNo],DailyAttendanceTable.[PickUpPoint] = B.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = B.[DropRouteNo],DailyAttendanceTable
UPDATE ASET A.Class = B.Class ,A.[Division] = B.[Division],A.[RouteNo] = B.[RouteNo],A.[PickUpPoint] = B.[PickUpPoint],A.[DropRouteNo] = B.[DropRouteNo],A
.[DropDownPoint] = B.[DropDownPoint]FROM DailyAttendanceTable A JOIN SCHULE_STUDENTDETAILS BON A.StudentId = B.StudentId
.[DropDownPoint] = B.[DropDownPoint]FROM SCHULE_STUDENTDETAILS BWHERE DailyAttendanceTable.StudentId = B.StudentId UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = SCHULE_STUDENTDETAILS.Class ,DailyAttendanceTable.[Division] = SCHULE_STUDENTDETAILS.[Division],DailyAttendanceTable.[RouteNo] = SCHULE_STUDENTDETAILS.[RouteNo],DailyAttendanceTable.[PickUpPoint] = SCHULE_STUDENTDETAILS.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = SCHULE_STUDENTDETAILS.[DropRouteNo],DailyAttendanceTable
2.UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = SCHULE_STUDENTDETAILS.Class ,DailyAttendanceTable.[Division] = SCHULE_STUDENTDETAILS.[Division],DailyAttendanceTable.[RouteNo] = SCHULE_STUDENTDETAILS.[RouteNo],DailyAttendanceTable.[PickUpPoint] = SCHULE_STUDENTDETAILS.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = SCHULE_STUDENTDETAILS.[DropRouteNo],DailyAttendanceTable
Aliasing can be done like this :
1 A.
UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = B.Class ,DailyAttendanceTable.[Division] = B.[Division],DailyAttendanceTable.[RouteNo] = B.[RouteNo],DailyAttendanceTable.[PickUpPoint] = B.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = B.[DropRouteNo],DailyAttendanceTable
2 A.
UPDATE ASET A.Class = B.Class ,A.[Division] = B.[Division],A.[RouteNo] = B.[RouteNo],A.[PickUpPoint] = B.[PickUpPoint],A.[DropRouteNo] = B.[DropRouteNo],A
.[DropDownPoint] = B.[DropDownPoint]FROM DailyAttendanceTable A JOIN SCHULE_STUDENTDETAILS BON A.StudentId = B.StudentId .[DropDownPoint] = B.[DropDownPoint]FROM SCHULE_STUDENTDETAILS BWHERE DailyAttendanceTable.StudentId = B.StudentId .[DropDownPoint] = SCHULE_STUDENTDETAILS.[DropDownPoint]FROM DailyAttendanceTable JOIN SCHULE_STUDENTDETAILS ON DailyAttendanceTable.StudentId = SCHULE_STUDENTDETAILS.StudentId
.[DropDownPoint] = SCHULE_STUDENTDETAILS.[DropDownPoint]FROM SCHULE_STUDENTDETAILS WHERE DailyAttendanceTable.StudentId = SCHULE_STUDENTDETAILS.StudentId
1.
===================================================================
UPDATE
DailyAttendanceTable SET DailyAttendanceTable.Class = B.Class ,DailyAttendanceTable.[Division] = B.[Division],DailyAttendanceTable.[RouteNo] = B.[RouteNo],DailyAttendanceTable.[PickUpPoint] = B.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = B.[DropRouteNo],DailyAttendanceTable
UPDATE ASET A.Class = B.Class ,A.[Division] = B.[Division],A.[RouteNo] = B.[RouteNo],A.[PickUpPoint] = B.[PickUpPoint],A.[DropRouteNo] = B.[DropRouteNo],A
.[DropDownPoint] = B.[DropDownPoint]FROM DailyAttendanceTable A JOIN SCHULE_STUDENTDETAILS BON A.StudentId = B.StudentId
.[DropDownPoint] = B.[DropDownPoint]FROM SCHULE_STUDENTDETAILS BWHERE DailyAttendanceTable.StudentId = B.StudentId UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = SCHULE_STUDENTDETAILS.Class ,DailyAttendanceTable.[Division] = SCHULE_STUDENTDETAILS.[Division],DailyAttendanceTable.[RouteNo] = SCHULE_STUDENTDETAILS.[RouteNo],DailyAttendanceTable.[PickUpPoint] = SCHULE_STUDENTDETAILS.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = SCHULE_STUDENTDETAILS.[DropRouteNo],DailyAttendanceTable
2.UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = SCHULE_STUDENTDETAILS.Class ,DailyAttendanceTable.[Division] = SCHULE_STUDENTDETAILS.[Division],DailyAttendanceTable.[RouteNo] = SCHULE_STUDENTDETAILS.[RouteNo],DailyAttendanceTable.[PickUpPoint] = SCHULE_STUDENTDETAILS.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = SCHULE_STUDENTDETAILS.[DropRouteNo],DailyAttendanceTable
Aliasing can be done like this :
1 A.
UPDATE DailyAttendanceTable SET DailyAttendanceTable.Class = B.Class ,DailyAttendanceTable.[Division] = B.[Division],DailyAttendanceTable.[RouteNo] = B.[RouteNo],DailyAttendanceTable.[PickUpPoint] = B.[PickUpPoint],DailyAttendanceTable.[DropRouteNo] = B.[DropRouteNo],DailyAttendanceTable
2 A.
UPDATE ASET A.Class = B.Class ,A.[Division] = B.[Division],A.[RouteNo] = B.[RouteNo],A.[PickUpPoint] = B.[PickUpPoint],A.[DropRouteNo] = B.[DropRouteNo],A
.[DropDownPoint] = B.[DropDownPoint]FROM DailyAttendanceTable A JOIN SCHULE_STUDENTDETAILS BON A.StudentId = B.StudentId .[DropDownPoint] = B.[DropDownPoint]FROM SCHULE_STUDENTDETAILS BWHERE DailyAttendanceTable.StudentId = B.StudentId .[DropDownPoint] = SCHULE_STUDENTDETAILS.[DropDownPoint]FROM DailyAttendanceTable JOIN SCHULE_STUDENTDETAILS ON DailyAttendanceTable.StudentId = SCHULE_STUDENTDETAILS.StudentId
.[DropDownPoint] = SCHULE_STUDENTDETAILS.[DropDownPoint]FROM SCHULE_STUDENTDETAILS WHERE DailyAttendanceTable.StudentId = SCHULE_STUDENTDETAILS.StudentId
Wednesday, October 27, 2010
cast and convert for date - effect of sql server settings
1. This works :
select
2010-10-12
2. This doesnt work, because the string format is not compliant with the date format currently used by sql serverselect
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
3. The following will work : select
2010-10-12 CONVERT(date , cast('10-12-2010' as datetime ) , 102) CONVERT(date , '10-12-2010', 102) CONVERT(date , '2010-10-12' , 102)
select
2010-10-12
2. This doesnt work, because the string format is not compliant with the date format currently used by sql serverselect
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
3. The following will work : select
2010-10-12 CONVERT(date , cast('10-12-2010' as datetime ) , 102) CONVERT(date , '10-12-2010', 102) CONVERT(date , '2010-10-12' , 102)
App Pattern: Four-Tier Web Application Scenario (Table Module) CodePlex
http://apparch.codeplex.com/wikipage?title=App%20Pattern%20-%20Four-Tier%20Web%20Application%20Scenario%20(Table%20Module&referringTitle=Application%20Patterns&ProjectName=apparch
The following is a summary of the patterns used by this scenario:
In this solution you will:
The following is a summary of the technologies, interfaces or objects shown above:
In this solution you will:
- For more information, see Enterprise Solution Patterns Using Microsoft .NET at http://msdn.microsoft.com/en-us/library/ms998469.aspx.
- For more information, see Integration Patterns at http://msdn.microsoft.com/en-us/library/ms978729.aspx.
- For more information, see Designing Data Tier Components and Passing Data Through Tiers at http://msdn.microsoft.com/en-us/library/ms978496.aspx.
- For more information on exception management, see Exception Management Architecture Guide at http://msdn.microsoft.com/en-us/library/ms954599.aspx.
Applies To
- ASP.NET 2.0, 3.5
- Windows Server 2003, 2008
Scenario
In this scenario, you have an existing database with tables or views that provide data used by your applications. In addition, security requirements dictate that the Web server should be deployed to a perimeter network that is isolated from the organization’s internal network. As a result, the solution is divided across four separate tiers, or physical machines. The client workstation represents a tier that uses a browser application to interact with the Web server. The Web server represents a second tier that contains the presentation layer, which is responsible for handling requests from the user and interacting with the business layer to implement the request. The application server represents the third tier with service, business, and data access layers. The fourth tier in this design is the database server.Key Characteristics
- Stand-alone ASP.NET Web application that supports basic CRUD operations.
- Presentation and Business logic are distributed across physical boundaries.
- Browser interaction with the Web Server uses standard HTTP GET and POST requests.
- The presentation layer uses a message-based protocol to interact with the business layer.
- The application uses data from an existing database schema.
- Tables and views in the database define data structures used by the application.
Pattern Solution
The following diagram displays the major patterns used by this scenario and the layers where those patterns are implemented.The following is a summary of the patterns used by this scenario:
- User interface processing is handled by a Model-View-Controller pattern.
- The user interface is composed of multiple controls, with some that can be bound to data elements.
- A proxy is used to communicate between the presentation layer and service layer
- The Data Transfer Object (DTO) pattern is used to package multiple data structures into one.
- The service layer provides translation between internal and external data structures.
- The business layer uses a façade pattern to support coarse-grained message-based operations.
- Transactions are managed by objects in the business layer.
- Business entities are defined using the Table Module pattern.
- The Table or Row Data Gateway pattern is used to provide a data access interface.
- The Query Object pattern is used to support the generation of SQL queries.
Pattern | Description |
---|---|
Bound Data Control | Control that can be bound to a data structure, which is used to provide data displayed by the control. |
Composite View | Combine individual views into a composite representation. |
Data Transfer Object | Used to combine multiple data structures into a unified view that can be passed across physical and logical boundaries. |
Entity Translator | Implement an object that transforms message data types to business types for requests and reverses the transformation for responses. |
Façade | Implement a unified interface to a set of operations to reduce coupling between systems. |
Front Controller | Implementation of MVC where the controller is composed of a handler and abstract command objects. The handler accepts requests for multiple views and instantiates an appropriate concrete command object to process the request. |
Page Controller | Implementation of MVC with a one-to-one relationship between the view and controller. |
Proxy | Provides a local implementation of a remote interface. The proxy is responsible for interaction with the remote interface and the serialization of data sent to and from the remote interface. |
Query Object | An object that represents a database query. |
Row Data Gateway | Create an object that acts as a gateway to a single record in a data source. |
Service Interface | A programmatic interface that systems use to interact with other systems. |
Table Data Gateway | Create an object that acts as a gateway to a table in a data source. |
Table Module | Business entity that represents a table or view within a database and provides operations to interact with the table or view. |
Transaction Script | Encapsulate operations that occur as part of a transaction into a single procedure, and execute the individual operations one at a time. |
Pattern Solution Details
The Pattern Solution section above identified the primary patterns used in this scenario. The Pattern Solution Details section will provide additional information about each pattern, and how the patterns are used within the design. Each table below represents a logical layer in the design and contains the patterns associated with that layer.In this solution you will:
- Use a Model-View-Controller (MVC) pattern to handle user interaction.
- Use ASP.NET page, user, and server controls to create a composite view.
- Use a message-based protocol for communication between the presentation and business layers.
- Implement a Table Module pattern to represent business entities.
- Interact with the database using a Query Object pattern to generate queries.
Web Server
The web server is deployed to a perimeter zone as required by security policies. The presentation layer is responsible for accepting user input and rendering the user interface that is returned from the web server.Pattern Info | Example |
---|---|
Front Controller | |
A controller handles HTTP requests from the browser. | Common implementations use an HTTP handler or ISAPI filter to intercept inbound requests to IIS. |
The controller interacts with a proxy to retrieve model data. | Based on request information the controller creates an instance of the proxy to interact with the application tier, which returns data to the presentation layer. |
The controller chooses an appropriate view. | Once the model has been initialized request information is used to identify the appropriate page to display, which is initialized with data from the model. |
Composite View | |
Page, server and user controls represent a Composite View. | Most development environments use controls and designers to create the user interface. |
The controls are used to render HTML that is returned to the browser. | Controls used to define the user interface are responsible for generating HTML. A common technique is to loop through all controls on a page when generating the page HTML. |
Bound Data Control | |
Data Transfer Objects are either bound to or used to initialize Composite View objects. | In this design, data structures returned from service represent Data Transfer Objects (DTO) that contain data. In most cases these DTOs can be bound to controls in the view. |
Proxy | |
Provides a local interface used to interact with services on the application tier. | Adding a service reference will generate classes used to interact with the service. From a coding perspective the proxy provides a local interface that hides details related to interaction with the service. |
Application Server – Service Layer
Service layer components provide access to business logic in the application. The presentation layer interacts with the service layer by passing messages to and from it over a communication channel.Pattern Info | Example |
---|---|
Service Interface | |
Provides a contract that defines operations and data structures supported by the service. | SOAP service and data contracts can be used to define a service interface. |
Data Transfer Object (DTO) | |
Used to combine multiple data structures into a single unit that can be transmitted across physical and logical boundaries. | Demographic information contains data from multiple tables or views. Rather than passing the data from each table or view across service boundaries one at a time all of the data related to demographic information is combined into a single structure. |
The service message structure represents a DTO that contains one or more data structures. | In this scenario business entities are translated into WCF data contracts and the data contracts are included in WCF message contracts, which represent data transfer objects. |
Entity Translator | |
Used to translate between business entities and data structures exposed by the service. | Data structures exposed by the service represent an external contract while business entities are internal to the service. As a result, translators are required to move data from one format to another. |
Application Server – Business Layer
Business layer components implement the core functionality of the system, and encapsulate the relevant business logic. In this scenario a façade pattern is used to support a message-based, or coarse-grained, interface into the business layer. Request messages from the service are translated into message calls into the business layer through a common interface, which is the façade.Pattern Info | Example |
---|---|
Façade | |
Provides a coarse grained interface into the business layer. | Rather than defining chatty object based operations a façade combines multiple operations into a single interface. |
The service implementation interacts with the business tier through the façade. | The service implementation is responsible for translating between external contracts and internal entities and then passing the request on to the business layer façade. |
An interface type is used to define the façade interface. | In order to provide a common interface, a typical approach is to define the façade using an Interface type with a single operation used to handle all requests. |
Transaction Script | |
The façade uses transaction script objects to execute business operations. | Within the façade an appropriate transaction script object is initialized based on the operation being executed, and control is passed on to a transaction script operation. |
Used for business operations those need to be executed as a single unit. | Within the transaction script operation a transaction is started, one or more business operations are performed, and the transaction is committed or rolled-back based on the outcome of the business operations. |
Table Module | |
Business entities are defined that represent tables or views in the database. | Each table or view from a database that contains information required by your application is defined as a class that represents a business entity. |
Application Server – Data Access Layer
Data layer components provide access to data that is hosted within the boundaries of the system, and data exposed by other back-end systems.Pattern Info | Example |
---|---|
Table/Row Data Gateway | |
Data access interface component that provides a gateway to tables or rows in the database. | Each business entity in the design has an associated gateway object that provides operations used to perform Create, Read, Update, and Delete (CRUD) operations against the business entity. |
Returns result sets that are used to initialize business entities in the business layer. | A common approach with .NET is to return a DataReader from the Gateway and then use the DataReader to initialize one or more business entities. |
Query Object | |
Data helper object that builds queries used to interact with the database. | The query object allows you to encapsulate the generation of SQL code within operations that generate specific types of commands using criteria information. |
Table or Row data gateway objects use query objects to build queries. | While it is possible to generate SQL directly within the gateway object the use of a query object provides a centralized and common approach that can be used by multiple gateway objects. |
Technical Solution
The following diagram represents the technical solution by replacing patterns shown in the Pattern Solution with technologies, interfaces, or objects that are used to implement the pattern.The following is a summary of the technologies, interfaces or objects shown above:
- Internet explorer is the target browser for this application scenario.
- The ASP.NET MVC framework is used to implement the Model-View-Control pattern.
- ASP.NET Page, Server, and User controls are used to define the user interface.
- A WCF service reference is used to generate a proxy to the service layer.
- WCF service contracts are used to define the service interface.
- WCF message contracts are used to define Data Transfer Objects (DTO).
- Objects are used to provide translation between external and internal data structures.
- A common interface is defined for the façade into the business layer.
- Business process objects are used to implement the transaction script pattern.
- Business entities have a one-to-one relationship with tables or views in the database.
- Gateway objects are used to provide an interface into the data access layer.
- The ADO.NET object model is used to implement query objects.
- The database used by this application scenario is SQL Server.
Technical Solution Details
The technical solution provides information about technologies used to implement patterns that were identified in the Pattern Solution. Each table below represents a logical layer within the design and provides information and examples related to implementing patterns in the solution. In addition to the technical solution for patterns, these tables also provide technical considerations that should be checked for this scenario.In this solution you will:
- Use the ASP.NET MVC framework to implement a Front Controller pattern.
- Use ASP.NET page, user, and server controls to create a composite view.
- Host the web application using Microsoft Internet Information Services (IIS).
- Use WCF services to provide an interface between the presentation and business layers.
- Use ADO.NET to generate queries against the database.
- Access tables or views from a SQL Server database.
Client Workstation
Check & More Info | Example |
---|---|
Target browser is Internet Explorer 6.x and higher. | Check the Browser agent for appropriate identification information, such as “MSIE 6.0”. |
Web Server
Check & More Info | Example |
---|---|
ASP.NET MVC | |
ASP.NET MVC framework is used to implement a Front Controller. | The controller intercepts all inbound requests, interacts with the business layer to initialize a model (data), and chooses the appropriate view (page) to display. |
Your design does not depend on ASP.NET viewstate. | When using ASP.NET MVC, as the standard ASP.NET postback processing is not executed, viewstate is not available when the controller intercepts calls. |
Standard ASP.NET page processing is used to handle control events. | You can mix standard ASP.NET postback processing with ASP.NET MVC to handle control events. |
ASP.NET Page Control | |
ASP.NET Page controls are used to define each page of the web application. | The ASP.NET page control is used to render an HTTP page that will be sent back to clients. |
ASP.NET User and Server Controls | |
ASP.NET User controls are used to provide common menus | Common links used to represent a menu are grouped into an ASP.NET user control so they can be reused on multiple pages. |
ASP.NET User and Server controls are used to provide the interface | ASP.NET user and server controls are used to generate HTML interface elements, such as INPUT, that are used to provide a user interface into the application. |
WCF Service Reference | |
A WCF service reference is added to the presentation layer for interaction with the business layer | Within Visual Studio use “Add Service Reference”, which will generate a proxy in the presentation layer that is used to access the service in the business layer. You can also use svcutil.exe to generate a proxy from the command line. |
Application Server – Service Layer (WCF)
Check & More Info | Example |
---|---|
WCF Service Contract | |
You are using WCF to define business operations called by the presentation layer. | The service layer is defined using WCF service, data, message, and fault contracts. |
The service contracts are coarse grained. | Operations exposed by the service are application scoped. For example, instead of providing multiple operations to return demographic information you would provide one operation that returns all data related to demographic information. |
A configuration file with WCF binding information is defined for the assembly that implements the service. | This makes the WCF service discoverable within a Visual Studio solution. |
WCF Message Contract | |
Message contracts are used to define Data Transfer Objects. | |
You have defined explicit message contracts instead of relying on implicit message contracts. | |
Translator Objects | |
Classes are defined that provide operations to translate between business entities and WCF data contracts. |
Application Server – Business Layer
Check & More Info | Example |
---|---|
Façade Interface | |
The business layer implements a façade with coarse grained operations. | Define a generic operation that takes the command and array of data objects as parameters and returns an Errors collection, which would only contain errors if they occurred during processing. |
An Interface type is used to define the façade interface. | |
The Façade acts as a boundary that catches all unhandled exceptions that may occur when processing business operations. | |
Business Process Objects | |
You are using business process objects to handle requests made to the business layer. | Business process objects allow you to implement business rules and provide transaction support if required. |
Business operations that need to be included in a transaction are combined in a single operation exposed by a business process object that implements the transaction script pattern. | |
Business Entities | |
Classes are defined that represent tables or views from the database. | |
Business rules are not implemented within the business entities. | With this design the business entities are just data containers. Business rules are handled by business processing components. |
Application Server – Data Access Layer
Check & More Info | Example |
---|---|
Gateway Objects | |
A gateway is defined for each business entity in the design. | |
The gateway encapsulates all logic related to interacting with the associated table or view. | Code related to interacting with a database and generating database commands can only be executed as part of a gateway operation. |
ADO.NET | |
Use ADO.NET object model to build queries instead of string concatenation. | |
Separate classes are used to define query objects. | Instead of implementing the generation of queries within a gateway object you can use objects that are dedicated to building queries and can be reused by multiple gateway objects. |
Database Server
Check & More Info | Example |
---|---|
Tables and views are accessible to the data access layer. | Security in the database is configured to allow access to tables and views from the application tier. |
Trusted sub-system is used to access the database. | Define a common business identity and then use that identity when accessing tables and views in the database. |
Table Data Gateway - Table Module - Data Mapper patterns - David Hayden
http://davidhayden.com/blog/dave/archive/2005/07/27/2406.aspx
Database First Design vs. Class First Design
No matter what the project, some developers are database first kinds of developers. They immediately start building tables, stored procedures and views before writing a single line of code. Once this is accomplished, they then start to pick apart the other layers of the application and determine how the application will pass data between the layers. This database first notion more times than not results in more Table Data Gateway and Table Module Patterns and the use of the DataSet as a container to pass data between the layers of the application. Certainly there is no rule that says you can't use custom objects when starting with a Database, it has just been my experience that this tends to be the norm.
On the otherhand, class first types of developers tend to focus on the problem domain first, and the result are custom objects. Once all the custom objects are built, you usually have some type of object repository (aka Domain-Driven Design) that hides / handles the persistence of the objects. Here you get into more Data Mapper Patterns that bridge the object world to the relational database world, etc.
Full Article :
Both Ambrose's session and the Cutting Edge article were informative, but they only talk about Objects vs. DataSets from a purely technical aspect, which I think is only half the picture. Also, much of the technical cons discussed about Objects were about the time and energy needed to create them, which is a fairly weak argument given the use of 1) Code Generation Tools, and 2) O/R Mappers to help you with much of the plumbing and bare bone classes.
In this post I want to change the focus from technical aspects to more project, project methodology, and developer preference aspects. In general, I want to focus on the following:
Disclaimer: I am sure there are other things we could talk about, but these are the topics that initially come to mind. The items being mentioned are certainly just my opinion and aren't written in stone. I also don't claim to be an expert on these ideas. I just want to look at these choices from a different view.
Domain-Driven Design vs. Database-Driven Design
Projects come in all shapes and sizes. Some are a mere shell around a database with few business rules (Database-Driven Design) while others are more complex in nature with a sophisticated problem domain (Domain-Driven Design).
If your new project centers around a database and is mainly about pulling data to and from a database and reporting on that data, a DataSet is a very attractive solution. The DataSet has so much built-in functionality for these types of projects that you would be silly to not consider them. New feature in ASP.NET 2.0, such as the DataSource Controls, also give you a lot more functionality when using DataSets for these types of applications. Using custom objects for database-driven projects doesn't really require much more effort if using a good Code Generation Tool, but if you are hand-coding this functionality, you are probably not getting much bang for your buck as you will be handcoding a lot of functionality that you get for free in the DataSet.
If your project has a problem domain rich with business rules, behaviors, a complex class hierarchy, and persistence is very much NOT the focus, custom objects will better give you the flexibility and functionality you desire. Collaboration between objects and the assignment of responsibilities will be foremost in your mind and the actual repository for the object data will be the least of your concerns. DataSets don't fair as well in these types of projects given the limited ability to add business rules and responsibilities as well as express your problem domain to its richest extent.
Database First Design vs. Class First Design
No matter what the project, some developers are database first kinds of developers. They immediately start building tables, stored procedures and views before writing a single line of code. Once this is accomplished, they then start to pick apart the other layers of the application and determine how the application will pass data between the layers. This database first notion more times than not results in more Table Data Gateway and Table Module Patterns and the use of the DataSet as a container to pass data between the layers of the application. Certainly there is no rule that says you can't use custom objects when starting with a Database, it has just been my experience that this tends to be the norm.
On the otherhand, class first types of developers tend to focus on the problem domain first, and the result are custom objects. Once all the custom objects are built, you usually have some type of object repository (aka Domain-Driven Design) that hides / handles the persistence of the objects. Here you get into more Data Mapper Patterns that bridge the object world to the relational database world, etc.
One-Time Applications vs. Evolutionary Applications
Some applications are simple and are built to solve a known problem. There is very little notion of adding functionality or coming out with new versions. The ability to maintain and extend fuctionality is of very little concern. You just need to get the application out and solve the immediate and current need. These applications tend to be more drag-n-drop, Rapid Application Development applications that once finished will “never” be touched again. Developers use every feature built into .NET and the Visual Studio IDE to complete the project in the least expensive and fastest way possible. These applications seem to be a natural fit for the DataSet given that it is built into .NET and a first class citizen of the Visual Studio IDE.
Other applications are evolutionary. Although the idea is to get out a version 1.0 as soon as possible, the intent is that the application itself will grow with the needs of the customers and be very flexible and extensible to handle new, different, and better technologies. The business model is very much based on a subscription model as well as grabbing marketshare from different platforms, operating systems, databases, programming languages, etc. The layers of these applications are based very much on abstraction, loose-coupling, providers, put your overused buzzword here :) The application needs to be highly maintainable and extensible, which often comes from using custom objects.
Novice Developers vs. Experienced Developers
New developers to .NET will embrace the DataSet for the reasons mentioned above - it is built into .NET and a first class citizen in the Visual Studio IDE. They can leverage the drag-n-drop functionality as well as the rich components in the IDE to limit the amount of code they need to write. This is not only smart, but the marketing push done by Microsoft to sell ASP.NET 2.0. Most of their applications will be database-driven and database reporting in nature as well, so the DataSet seems to be a natural fit. The focus will be on less code and more drag-n-drop and leveraging built-in functionality.
More experienced developers will be hired as well as seek those projects that are more challenging - Enterpise Applications if you will. Drag-n-drop will not always meet their needs as the problem at hand is usually not so cut-and-dry. The Enterprise Applications will have a more complex problem domain that realistically only custom objects will satisfy for reasons mentioned above. However, we are talking about experienced developers here seeking a challenge, so they wouldn't be satisfied by using a DataSet or drag-n-drop anyway. They not only want to use those aspects of .NET that are a bit more obscure and challenging, they won't be satisfied if they don't use it. This is hardcore development at its best, at least as hardcore as you can get from a managed language :)
Conclusion
I find that that the most interesting reasons for choosing Objects vs. DataSets are less technical, but more about the project, the developer's methodology, and the developer's preference. The technical reasons for choosing one over the other are pretty weak and can be resolved pretty easily. Many of the reasons for choosing one over the other is based on the project, developer preference, the development methodology, and the experience of the developer. Ideally, one would want to leverage them both to their maximum extent and not look at one as better than the other, but that they both serve a purpose depending on the context of the situation.
Have fun with this stuff and let the project dictate the best possible solutions and tools. If you notice your preferences and experience sway the solutions and tools in a particular way that may not be the most appropriate for the problem, welcome to the human race. Build those skills that make you
Database First Design vs. Class First Design
No matter what the project, some developers are database first kinds of developers. They immediately start building tables, stored procedures and views before writing a single line of code. Once this is accomplished, they then start to pick apart the other layers of the application and determine how the application will pass data between the layers. This database first notion more times than not results in more Table Data Gateway and Table Module Patterns and the use of the DataSet as a container to pass data between the layers of the application. Certainly there is no rule that says you can't use custom objects when starting with a Database, it has just been my experience that this tends to be the norm.
On the otherhand, class first types of developers tend to focus on the problem domain first, and the result are custom objects. Once all the custom objects are built, you usually have some type of object repository (aka Domain-Driven Design) that hides / handles the persistence of the objects. Here you get into more Data Mapper Patterns that bridge the object world to the relational database world, etc.
Full Article :
Objects vs. DataSets - Is it Really About the Technical Aspects?
I have been meaning to talk about Objects vs. DataSets ever since Ambrose Little's session at the Tampa Code Camp as well as my reading of Dino Esposito's Cutting Edge Article, DataSets vs. Collections, in the August 2005 Edition of MSDN Magazine.Both Ambrose's session and the Cutting Edge article were informative, but they only talk about Objects vs. DataSets from a purely technical aspect, which I think is only half the picture. Also, much of the technical cons discussed about Objects were about the time and energy needed to create them, which is a fairly weak argument given the use of 1) Code Generation Tools, and 2) O/R Mappers to help you with much of the plumbing and bare bone classes.
In this post I want to change the focus from technical aspects to more project, project methodology, and developer preference aspects. In general, I want to focus on the following:
- Domain-Driven Design vs. Database-Driven Design
- Database First Design vs. Class First Design
- One-Time Applications vs. Evolutionary Applications
- Novice Developers vs. Experienced Developers
Disclaimer: I am sure there are other things we could talk about, but these are the topics that initially come to mind. The items being mentioned are certainly just my opinion and aren't written in stone. I also don't claim to be an expert on these ideas. I just want to look at these choices from a different view.
Domain-Driven Design vs. Database-Driven Design
Projects come in all shapes and sizes. Some are a mere shell around a database with few business rules (Database-Driven Design) while others are more complex in nature with a sophisticated problem domain (Domain-Driven Design).
If your new project centers around a database and is mainly about pulling data to and from a database and reporting on that data, a DataSet is a very attractive solution. The DataSet has so much built-in functionality for these types of projects that you would be silly to not consider them. New feature in ASP.NET 2.0, such as the DataSource Controls, also give you a lot more functionality when using DataSets for these types of applications. Using custom objects for database-driven projects doesn't really require much more effort if using a good Code Generation Tool, but if you are hand-coding this functionality, you are probably not getting much bang for your buck as you will be handcoding a lot of functionality that you get for free in the DataSet.
If your project has a problem domain rich with business rules, behaviors, a complex class hierarchy, and persistence is very much NOT the focus, custom objects will better give you the flexibility and functionality you desire. Collaboration between objects and the assignment of responsibilities will be foremost in your mind and the actual repository for the object data will be the least of your concerns. DataSets don't fair as well in these types of projects given the limited ability to add business rules and responsibilities as well as express your problem domain to its richest extent.
Database First Design vs. Class First Design
No matter what the project, some developers are database first kinds of developers. They immediately start building tables, stored procedures and views before writing a single line of code. Once this is accomplished, they then start to pick apart the other layers of the application and determine how the application will pass data between the layers. This database first notion more times than not results in more Table Data Gateway and Table Module Patterns and the use of the DataSet as a container to pass data between the layers of the application. Certainly there is no rule that says you can't use custom objects when starting with a Database, it has just been my experience that this tends to be the norm.
On the otherhand, class first types of developers tend to focus on the problem domain first, and the result are custom objects. Once all the custom objects are built, you usually have some type of object repository (aka Domain-Driven Design) that hides / handles the persistence of the objects. Here you get into more Data Mapper Patterns that bridge the object world to the relational database world, etc.
One-Time Applications vs. Evolutionary Applications
Some applications are simple and are built to solve a known problem. There is very little notion of adding functionality or coming out with new versions. The ability to maintain and extend fuctionality is of very little concern. You just need to get the application out and solve the immediate and current need. These applications tend to be more drag-n-drop, Rapid Application Development applications that once finished will “never” be touched again. Developers use every feature built into .NET and the Visual Studio IDE to complete the project in the least expensive and fastest way possible. These applications seem to be a natural fit for the DataSet given that it is built into .NET and a first class citizen of the Visual Studio IDE.
Other applications are evolutionary. Although the idea is to get out a version 1.0 as soon as possible, the intent is that the application itself will grow with the needs of the customers and be very flexible and extensible to handle new, different, and better technologies. The business model is very much based on a subscription model as well as grabbing marketshare from different platforms, operating systems, databases, programming languages, etc. The layers of these applications are based very much on abstraction, loose-coupling, providers, put your overused buzzword here :) The application needs to be highly maintainable and extensible, which often comes from using custom objects.
Novice Developers vs. Experienced Developers
New developers to .NET will embrace the DataSet for the reasons mentioned above - it is built into .NET and a first class citizen in the Visual Studio IDE. They can leverage the drag-n-drop functionality as well as the rich components in the IDE to limit the amount of code they need to write. This is not only smart, but the marketing push done by Microsoft to sell ASP.NET 2.0. Most of their applications will be database-driven and database reporting in nature as well, so the DataSet seems to be a natural fit. The focus will be on less code and more drag-n-drop and leveraging built-in functionality.
More experienced developers will be hired as well as seek those projects that are more challenging - Enterpise Applications if you will. Drag-n-drop will not always meet their needs as the problem at hand is usually not so cut-and-dry. The Enterprise Applications will have a more complex problem domain that realistically only custom objects will satisfy for reasons mentioned above. However, we are talking about experienced developers here seeking a challenge, so they wouldn't be satisfied by using a DataSet or drag-n-drop anyway. They not only want to use those aspects of .NET that are a bit more obscure and challenging, they won't be satisfied if they don't use it. This is hardcore development at its best, at least as hardcore as you can get from a managed language :)
Conclusion
I find that that the most interesting reasons for choosing Objects vs. DataSets are less technical, but more about the project, the developer's methodology, and the developer's preference. The technical reasons for choosing one over the other are pretty weak and can be resolved pretty easily. Many of the reasons for choosing one over the other is based on the project, developer preference, the development methodology, and the experience of the developer. Ideally, one would want to leverage them both to their maximum extent and not look at one as better than the other, but that they both serve a purpose depending on the context of the situation.
Have fun with this stuff and let the project dictate the best possible solutions and tools. If you notice your preferences and experience sway the solutions and tools in a particular way that may not be the most appropriate for the problem, welcome to the human race. Build those skills that make you
Factory Method Very Important by David Hayden
The fact that a method creates a new object does not in itself mean that it is an example of the Factory Method pattern. The Factory Method pattern requires that an operation that creates an object also isolates the client from knowing which class to instantiate. When a client requests a new object, the precise class of the object that is created depends on the behavior of the object receiving the creation request.
.net interview questions allinterview.com
Where is ViewState information stored? HCL 13
Types of optimization and name a few ? Accenture 3
What is the T-SQL equivalent of IIF (immediate if/ternary operator) function of other programming languages? Syntax-Softtech 1
How do you install windows service? Tech-Mahindra 3
What is the roll of JIT in asp.net? 5
what is Disco?what it will do? Microsoft 2
what do machine key element in configuration file specify? TCS 1
Compare and contrast between inline and code behind - which one is best? 2
Hey I am using asp.net mvc architecture. I creating one dropdownlist using this is dynamic list.Its displaying properly. But in time of Edit.If i load a page dropdownlist is not displaying the item which is stored in table. 1
how to retrieve data using web services in asp.net pls give me the code and explain me briefly 3
Which namespace in used to connction web cam 3
Differences between session state and Application state? 2
please give a brief knowledge about these events ? page_render() page_prerender() page_unload page_loadcomplete page_preinit Netsweeper 1
How many web.config files in each solution General-Mills 10
Difference between datagrid and datareader? 1
What is the use of @Register directives? 1
Rate yourself in .net and sql database? Satyam 1
There is a login page that has two text boxes with required field validators on it. The page has a login and cancel button. How can we ensure that the click on the cancel button doesnt fire a validation event. Proteans 2
How do you handle unhandled exceptions in ASP.NET?. 3
Should validation (did the user enter a real date) occur server-side or client-side? Why? 2
whats the best method to cal id in javascript 1 286
What is serialization? Piramal-Healthcare 3 748
what are types of viewstate 1 994
How do you access individual items of an Master Page TCS 3 1296
what is session,cokkies in asp.net?? 4 1091
how to integrate Language conversion, without asking the enduser to download font, it should auto integrate 1 305
How do you insert multiple rows from a grid view to database table under a single transaction? 1 460
what is difference between java and .net Karur-Vysya-Bank-KVB 7 1528
how to send mail in asp.net 2 1353
How many types is controls is there in Asp.Net?If it means what are types of custom controls?Explain about user controls,Web server controls,Hrml server controls? Cap-Gemini 1 1572
what are debugging types in .net? NIIT 2 1803
You have multiline textbox and submit button. if you paste xml content into multiline textbox and hit submit button. what action will perform? D&B 5 1067
What are the asp.net objects? is it application obects, cache objests etc. or is it state managements, web services etc? Syntel 3 1348
how many character send in one sms forget 160limit and think about gateway allowed Wipro 2 604
is gateway for sms continue connected for sending sms.how?
What is RCW (Run time Callable Wrappers)? 308
Can you give an example of when it would be appropriate to use a web service as opposed to a non-serviced .NET component 200
what are login controls? 105 Six-Sigma
What is the need of tabindex property is label control? 419
write a program to create login form 213
what is the difference between Exportdll and Importdll ? 54
7. Oop-Diff B/w Interfacse and abstract class. 47 PCS
The maximum report processing jobs limit configured by your system administrator has been reached.How I can Solve this problem when i using crystal report to load from my application. 7062
About Iunknown interface Queue ,its methods Query Interface Addref,Release and Explain each ? 344 DELL
what is the method while we are using adapter and dataset ? 51
Interop Services? 126
Difference between user groups and code groups 93
what is an assembly ? 35
how print PGL by XML 344 Wipro
5. Wcf- Session Managment 47 PCS
How to implement CAS in .Net? 283 247Customer
How are the activation URLs different in case of SAO and CAO in .NET remoting? 337
Which file is taken by compiler when we have both file Application and Server Configuration file? 36
What are the consideration in deciding to use .NET Remoting or ASP.NET Web Services? 226
What is Event - Delegate? clear syntax for writing a event delegate
which is the best dot net coaching center in visakhapatnam?? 0 25
what is the difference between Exportdll and Importdll ? 0 54
Writing a Stored procedure to insert the values into a table 0 39
Sql Queries: A Table will be given Omiting Duplicate rows and adding a new column 0 25
Write Code for DataSet,Datareader,and by deleting the button gridview should be empty? 0 32
How to Exchange data between Webservices 2 177
How to Create and Consume a Web Service? 0 15
what are abstract classes? what is overriding? 0 107
Which is the institute which also caters to your personal development other than subject. TCS 1 174
In which testing using in .net framwork? 0 49
what is a mvc in asp.net? HCL 1 441
what is more complex to implement property, methods or event? how can I define criteria to compare the difficulty of implementation between them? for example the number of methods wanted to implements property is 2 methods. how many methods I need it to implements events? 0 41
Dynamic Fonts 0 34
How to set background for total website, on that another another layer, in that we keep website data,,for example see www.msn.com. On one faded background we ll have layer like other background..Do explain how its possible 0 53
what is namespace?
Types of optimization and name a few ? Accenture 3
What is the T-SQL equivalent of IIF (immediate if/ternary operator) function of other programming languages? Syntax-Softtech 1
How do you install windows service? Tech-Mahindra 3
What is the roll of JIT in asp.net? 5
what is Disco?what it will do? Microsoft 2
what do machine key element in configuration file specify? TCS 1
Compare and contrast between inline and code behind - which one is best? 2
Hey I am using asp.net mvc architecture. I creating one dropdownlist using this is dynamic list.Its displaying properly. But in time of Edit.If i load a page dropdownlist is not displaying the item which is stored in table. 1
how to retrieve data using web services in asp.net pls give me the code and explain me briefly 3
Which namespace in used to connction web cam 3
Differences between session state and Application state? 2
please give a brief knowledge about these events ? page_render() page_prerender() page_unload page_loadcomplete page_preinit Netsweeper 1
How many web.config files in each solution General-Mills 10
Difference between datagrid and datareader? 1
What is the use of @Register directives? 1
Rate yourself in .net and sql database? Satyam 1
There is a login page that has two text boxes with required field validators on it. The page has a login and cancel button. How can we ensure that the click on the cancel button doesnt fire a validation event. Proteans 2
How do you handle unhandled exceptions in ASP.NET?. 3
Should validation (did the user enter a real date) occur server-side or client-side? Why? 2
whats the best method to cal id in javascript 1 286
What is serialization? Piramal-Healthcare 3 748
what are types of viewstate 1 994
How do you access individual items of an Master Page TCS 3 1296
what is session,cokkies in asp.net?? 4 1091
how to integrate Language conversion, without asking the enduser to download font, it should auto integrate 1 305
How do you insert multiple rows from a grid view to database table under a single transaction? 1 460
what is difference between java and .net Karur-Vysya-Bank-KVB 7 1528
how to send mail in asp.net 2 1353
How many types is controls is there in Asp.Net?If it means what are types of custom controls?Explain about user controls,Web server controls,Hrml server controls? Cap-Gemini 1 1572
what are debugging types in .net? NIIT 2 1803
You have multiline textbox and submit button. if you paste xml content into multiline textbox and hit submit button. what action will perform? D&B 5 1067
What are the asp.net objects? is it application obects, cache objests etc. or is it state managements, web services etc? Syntel 3 1348
how many character send in one sms forget 160limit and think about gateway allowed Wipro 2 604
is gateway for sms continue connected for sending sms.how?
What is RCW (Run time Callable Wrappers)? 308
Can you give an example of when it would be appropriate to use a web service as opposed to a non-serviced .NET component 200
what are login controls? 105 Six-Sigma
What is the need of tabindex property is label control? 419
write a program to create login form 213
what is the difference between Exportdll and Importdll ? 54
7. Oop-Diff B/w Interfacse and abstract class. 47 PCS
The maximum report processing jobs limit configured by your system administrator has been reached.How I can Solve this problem when i using crystal report to load from my application. 7062
About Iunknown interface Queue ,its methods Query Interface Addref,Release and Explain each ? 344 DELL
what is the method while we are using adapter and dataset ? 51
Interop Services? 126
Difference between user groups and code groups 93
what is an assembly ? 35
how print PGL by XML 344 Wipro
5. Wcf- Session Managment 47 PCS
How to implement CAS in .Net? 283 247Customer
How are the activation URLs different in case of SAO and CAO in .NET remoting? 337
Which file is taken by compiler when we have both file Application and Server Configuration file? 36
What are the consideration in deciding to use .NET Remoting or ASP.NET Web Services? 226
What is Event - Delegate? clear syntax for writing a event delegate
which is the best dot net coaching center in visakhapatnam?? 0 25
what is the difference between Exportdll and Importdll ? 0 54
Writing a Stored procedure to insert the values into a table 0 39
Sql Queries: A Table will be given Omiting Duplicate rows and adding a new column 0 25
Write Code for DataSet,Datareader,and by deleting the button gridview should be empty? 0 32
How to Exchange data between Webservices 2 177
How to Create and Consume a Web Service? 0 15
what are abstract classes? what is overriding? 0 107
Which is the institute which also caters to your personal development other than subject. TCS 1 174
In which testing using in .net framwork? 0 49
what is a mvc in asp.net? HCL 1 441
what is more complex to implement property, methods or event? how can I define criteria to compare the difficulty of implementation between them? for example the number of methods wanted to implements property is 2 methods. how many methods I need it to implements events? 0 41
Dynamic Fonts 0 34
How to set background for total website, on that another another layer, in that we keep website data,,for example see www.msn.com. On one faded background we ll have layer like other background..Do explain how its possible 0 53
what is namespace?
Interface cannot contain data fields - they can contain only abstract properties.
Interfaces consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers.
http://msdn.microsoft.com/en-us/library/ms173156.aspx
http://msdn.microsoft.com/en-us/library/ms173156.aspx
difference between abstract classess and interfaces - II
Summary
An Interface cannot implement methods.
An abstract class can implement methods.
An Interface can only inherit from another Interface.
An abstract class can inherit from a class and one or more interfaces.
An Interface cannot contain fields.
An abstract class can contain fields.
An Interface can contain property definitions.
An abstract class can implement a property.
An Interface cannot contain constructors or destructors.
An abstract class can contain constructors or destructors.
An Interface can be inherited from by structures.
An abstract class cannot be inherited from by structures.
http://en.csharp-online.net/Interfaces_and_Abstract_Classes
[see also
http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F
]
An Interface can support multiple inheritance.
An abstract class cannot support multiple inheritance.
[edit]MSDN references
abstract (C# Reference)
Abstract and Sealed Classes and Class Members (C# Programming Guide)
Interfaces (C# Programming Guide)
Should I use an abstract class or an interface?
An Interface cannot implement methods.
An abstract class can implement methods.
An Interface can only inherit from another Interface.
An abstract class can inherit from a class and one or more interfaces.
An Interface cannot contain fields.
An abstract class can contain fields.
An Interface can contain property definitions.
An abstract class can implement a property.
An Interface cannot contain constructors or destructors.
An abstract class can contain constructors or destructors.
An Interface can be inherited from by structures.
An abstract class cannot be inherited from by structures.
http://en.csharp-online.net/Interfaces_and_Abstract_Classes
[see also
http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F
]
An Interface can support multiple inheritance.
An abstract class cannot support multiple inheritance.
[edit]MSDN references
abstract (C# Reference)
Abstract and Sealed Classes and Class Members (C# Programming Guide)
Interfaces (C# Programming Guide)
Should I use an abstract class or an interface?
Subscribe to:
Posts (Atom)
How to check local and global angular versions
Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...
-
Most of the google tutorials on keras do not show how to display a confusion matrix for the solution. A confusion matrix can throw a clear l...
-
This error means you have created the DbContext but not configured/added it to the project using either DI in startup.cs or by using DbCon...
-
CONCLUSION : 1. Normally, use following two when you do not want query compilation also to come into picture. CHECKPOINT DBCC DROPCLEA...