📄 librarian.cs
字号:
/*
Copyright 2006 Christian Gross http://www.devspace.com
From the book Ajax Patterns and Best Practices
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
namespace Devspace.Ajax.CacheController.Books {
public class LibrarianSaveToFile : Librarian {
private static String _rootPath;
private Librarian _next;
public LibrarianSaveToFile(Librarian next) {
if (_next == null) {
throw new InvalidOperationException("Next element cannot be null");
}
_next = next;
}
public static void SetRootPath(String path) {
_rootPath = path;
}
public Book CheckOutBook(String isbn) {
// Ignore nothing to do and continue to next element
return _next.CheckOutBook(isbn);
}
public void CheckInBook(Book book) {
String path = _rootPath + "/books/" + book.ISBN + ".xml";
// Save the data to a file ...
_next.CheckInBook(book);
}
}
public class LibrarianSaveToStorage : Librarian {
public LibrarianSaveToStorage() {
}
public Book CheckOutBook(String isbn) {
// TODO
return null;
}
public void CheckInBook(Book book) {
// TODO
}
}
public class LibrarianHTTPValidation : Librarian {
private Librarian _next;
private String _etag;
public LibrarianHTTPValidation(String etag, Librarian next) {
if (_next == null) {
throw new InvalidOperationException("Next element cannot be null");
}
_next = next;
_etag = etag;
}
private bool IsSameState(String etag, String isbn) {
return false;
}
public Book CheckOutBook(String isbn) {
if (IsSameState(_etag, isbn)) {
Book book = new Book();
book.AssignHashCode(int.Parse( _etag));
book.ISBN = isbn;
return book;
}
else {
return _next.CheckOutBook(isbn);
}
}
private void SaveHashCode(Book book) {
}
public void CheckInBook(Book book) {
SaveHashCode(book);
_next.CheckInBook(book);
}
}
public class LibrarianBuilder {
public static Librarian create(String rootPath) {
LibrarianSaveToFile.SetRootPath(rootPath);
return new LibrarianSaveToFile(new LibrarianSaveToStorage());
}
public static Librarian createDynamicValidation(String etag) {
if (etag != null && etag.Length > 0) {
return new LibrarianHTTPValidation(etag, new LibrarianSaveToStorage());
}
else {
return new LibrarianSaveToStorage();
}
}
}
/*
public Book checkOutBook( String isbn) throws Exception {
String path = _rootPath + "/books/" + isbn + ".xml";
File file = new File( path);
if( file.exists()) {
// This error will never occur!!!
FileReader reader = null;
try {
reader = new FileReader(file);
}
catch (FileNotFoundException e) {}
BeanReader beanreader = new BeanReader();
try {
beanreader.registerBeanClass(Book.class);
}
catch (IntrospectionException e) {}
return (Book)beanreader.parse( reader);
}
// We don't have the book, ask Amazon
Book book = new AmazonSearch().searchForBookISBN( isbn);
if( book == null) {
throw new Exception( "could not find book with the ISBN (" + isbn + ")");
}
return book;
}
public void checkInBook( Book book) throws Exception{
String path = _rootPath + "/books/" + book.getISBN() + ".xml";
File file = new File( path);
Writer writer = new FileWriter( file);
BeanWriter beanwriter = new BeanWriter( writer);
beanwriter.enablePrettyPrint();
beanwriter.write( book);
writer.close();
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -