🗊Презентация Dispose Pattern in C#

Нажмите для полного просмотра!
Dispose Pattern in C#, слайд №1Dispose Pattern in C#, слайд №2Dispose Pattern in C#, слайд №3Dispose Pattern in C#, слайд №4Dispose Pattern in C#, слайд №5Dispose Pattern in C#, слайд №6Dispose Pattern in C#, слайд №7Dispose Pattern in C#, слайд №8Dispose Pattern in C#, слайд №9Dispose Pattern in C#, слайд №10Dispose Pattern in C#, слайд №11Dispose Pattern in C#, слайд №12Dispose Pattern in C#, слайд №13Dispose Pattern in C#, слайд №14Dispose Pattern in C#, слайд №15Dispose Pattern in C#, слайд №16Dispose Pattern in C#, слайд №17Dispose Pattern in C#, слайд №18Dispose Pattern in C#, слайд №19Dispose Pattern in C#, слайд №20Dispose Pattern in C#, слайд №21Dispose Pattern in C#, слайд №22Dispose Pattern in C#, слайд №23Dispose Pattern in C#, слайд №24Dispose Pattern in C#, слайд №25Dispose Pattern in C#, слайд №26Dispose Pattern in C#, слайд №27Dispose Pattern in C#, слайд №28Dispose Pattern in C#, слайд №29

Вы можете ознакомиться и скачать презентацию на тему Dispose Pattern in C#. Доклад-сообщение содержит 29 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





Dispose Pattern in C#
Описание слайда:
Dispose Pattern in C#

Слайд 2





Agenda
Destructor and Finalizer in C#
IDisposable and RAII
Dispose Pattern for Managed and Unmanaged Resources
Objects with Critical Finalization
Simplified Dispose Pattern
Recommended Links
Описание слайда:
Agenda Destructor and Finalizer in C# IDisposable and RAII Dispose Pattern for Managed and Unmanaged Resources Objects with Critical Finalization Simplified Dispose Pattern Recommended Links

Слайд 3





Destructor and Finalizer in C#
Описание слайда:
Destructor and Finalizer in C#

Слайд 4





Destructor in C#
Destructor in C# language created with tilde (“~”) is syntax sugar for Finalize method, to which it is converted on compilation stage of the application
That is why it is correct to say that destructor and finalizer is the same in C#
While “destructor” term has special meaning in programming, it is better to say that C# does not have destructor at all, we will use term “finalizer” instead
Описание слайда:
Destructor in C# Destructor in C# language created with tilde (“~”) is syntax sugar for Finalize method, to which it is converted on compilation stage of the application That is why it is correct to say that destructor and finalizer is the same in C# While “destructor” term has special meaning in programming, it is better to say that C# does not have destructor at all, we will use term “finalizer” instead

Слайд 5





Finalizer Problem
Time of finalizer call is not defined in .NET, that is why finalizers do not guarantee:
Time of resource release 
Fact of resource release
Описание слайда:
Finalizer Problem Time of finalizer call is not defined in .NET, that is why finalizers do not guarantee: Time of resource release Fact of resource release

Слайд 6





IDisposable and RAII
Описание слайда:
IDisposable and RAII

Слайд 7





Interface IDisposable
Provides a mechanism for releasing unmanaged resources.
Описание слайда:
Interface IDisposable Provides a mechanism for releasing unmanaged resources.

Слайд 8





RAII Idiom
RAII – Resource Acquisition Is Initialization
RAII means that resource should be allocated in constructor and released in destructor
OO languages with direct resource management completely corresponds to RAII
Описание слайда:
RAII Idiom RAII – Resource Acquisition Is Initialization RAII means that resource should be allocated in constructor and released in destructor OO languages with direct resource management completely corresponds to RAII

Слайд 9





Keyword using
Keyword “using” not completely implements RAII
Описание слайда:
Keyword using Keyword “using” not completely implements RAII

Слайд 10





Method Dispose
Dispose method differs from destructor in that way that it not destroys the object but destroys the resource
Danger Consequences of dispose call: object is not destroyed but resource is not available and any further method call or access to property is potentially dangerous
Описание слайда:
Method Dispose Dispose method differs from destructor in that way that it not destroys the object but destroys the resource Danger Consequences of dispose call: object is not destroyed but resource is not available and any further method call or access to property is potentially dangerous

Слайд 11





Dispose Pattern for Managed and Unmanaged Resources
Описание слайда:
Dispose Pattern for Managed and Unmanaged Resources

Слайд 12





Dispose Pattern
Taking into account all previously mentioned, we have to implement special dispose pattern in .NET to ensure that resources are released in proper way
Описание слайда:
Dispose Pattern Taking into account all previously mentioned, we have to implement special dispose pattern in .NET to ensure that resources are released in proper way

Слайд 13





Managed and Unmanaged Resources
Unmanaged resources – IntPtr, socket descriptors, any OS objects obtained with WinAPI etc.
If unmanaged resource is wrapped into class with RAII it becomes managed resource
Any of two types of resources implies different approaches to work with them
Описание слайда:
Managed and Unmanaged Resources Unmanaged resources – IntPtr, socket descriptors, any OS objects obtained with WinAPI etc. If unmanaged resource is wrapped into class with RAII it becomes managed resource Any of two types of resources implies different approaches to work with them

Слайд 14





Sample Resource Wrapper
Описание слайда:
Sample Resource Wrapper

Слайд 15





Main Idea of Dispose Pattern
The main idea of Dispose Pattern is: 
Place all logic of resource release into separate method;
Call it from Dispose method; 
Also call it from finalizer;
Add special flag that helps to distinguish who exactly (Dispose or Finalizer) called the method.
Описание слайда:
Main Idea of Dispose Pattern The main idea of Dispose Pattern is: Place all logic of resource release into separate method; Call it from Dispose method; Also call it from finalizer; Add special flag that helps to distinguish who exactly (Dispose or Finalizer) called the method.

Слайд 16





1. Interface Implementation
Class that has both managed and unmanaged resources implements IDisposable interface
Описание слайда:
1. Interface Implementation Class that has both managed and unmanaged resources implements IDisposable interface

Слайд 17





2. Method Dispose(bool disposing)
Class contains method Dispose(bool disposing) that does all job to release resources; 
disposing parameter tells if method is called from Dispose method or from Finalize. This method should be protected virtual for non-sealed classes and private for sealed classes
Описание слайда:
2. Method Dispose(bool disposing) Class contains method Dispose(bool disposing) that does all job to release resources; disposing parameter tells if method is called from Dispose method or from Finalize. This method should be protected virtual for non-sealed classes and private for sealed classes

Слайд 18





3. Method Dispose()
Dispose method implementation: first we call Dispose(true), then we may call GC.SuppressFinalize() method that suppresses finalizer call:
Описание слайда:
3. Method Dispose() Dispose method implementation: first we call Dispose(true), then we may call GC.SuppressFinalize() method that suppresses finalizer call:

Слайд 19





Notes to GC.SupressFinalize() Call
GC.SuppressFinalize() should be called after Dispose(true) but not before  because if method Dispose(true) fails with exception the execution of finalizer should not be cancelled and it will give another chance to free resources
GC.SuppressFinalize() should be called for classes that do not have finalizers because finalizers may be created for child classes. The only exception is sealed classes.
Описание слайда:
Notes to GC.SupressFinalize() Call GC.SuppressFinalize() should be called after Dispose(true) but not before because if method Dispose(true) fails with exception the execution of finalizer should not be cancelled and it will give another chance to free resources GC.SuppressFinalize() should be called for classes that do not have finalizers because finalizers may be created for child classes. The only exception is sealed classes.

Слайд 20





4. Parameter “disposing”
Method Dispose(bool disposing) has two parts:
If this method called from Dispose (disposing parameter is true) we should release both managed and unmanaged resources; 
If this method is called from finalizer (that is possible under normal circumstances only during garbage collection process when disposing parameter is false), we release only unmanaged resources.
Описание слайда:
4. Parameter “disposing” Method Dispose(bool disposing) has two parts: If this method called from Dispose (disposing parameter is true) we should release both managed and unmanaged resources; If this method is called from finalizer (that is possible under normal circumstances only during garbage collection process when disposing parameter is false), we release only unmanaged resources.

Слайд 21





5. Finalizer
[OPTIONAL] Class may have finalizer and call Dispose(bool disposing) from it passing false as parameter. 
Also we should take into account that finalizer may be called even for partially constructed classes, if constructor for such class raises an exception. That is why resource releasing code should handle situation when resources are not allocated yet
Описание слайда:
5. Finalizer [OPTIONAL] Class may have finalizer and call Dispose(bool disposing) from it passing false as parameter.  Also we should take into account that finalizer may be called even for partially constructed classes, if constructor for such class raises an exception. That is why resource releasing code should handle situation when resources are not allocated yet

Слайд 22





6. Field “disposed”
The good practice is to create special Boolean field disposed which indicates that object’s resources are released. 
Disposable objects should allow any number of Dispose() method calls and generate an exception when any public member of the object is accessed after first call to the method (when dispose flag is set to true).
Описание слайда:
6. Field “disposed” The good practice is to create special Boolean field disposed which indicates that object’s resources are released. Disposable objects should allow any number of Dispose() method calls and generate an exception when any public member of the object is accessed after first call to the method (when dispose flag is set to true).

Слайд 23





Objects with Critical Finalization
Описание слайда:
Objects with Critical Finalization

Слайд 24





7. Object with Critical Finalization
Class may be inherited from CriticalFinalizerObject:
Finalizer for such classes compiled with JIT-compiler immediately when the instance is constructed (apart to default on demand compilation). This allows finalizer to complete successfully even if the memory is full
CLR does not guarantee order of finalizer calls that makes impossible to access other objects from finalizer that contain unmanaged resources. But CLR guarantees that finalizers for usual objects will be called before childs of CriticalFinalizerObject. This allows from “usual” objects to access field SafeHandle that is guaranteed to be released later    
Finalizers for such classes will be called even in case of abnormal termination of application domain.
Описание слайда:
7. Object with Critical Finalization Class may be inherited from CriticalFinalizerObject: Finalizer for such classes compiled with JIT-compiler immediately when the instance is constructed (apart to default on demand compilation). This allows finalizer to complete successfully even if the memory is full CLR does not guarantee order of finalizer calls that makes impossible to access other objects from finalizer that contain unmanaged resources. But CLR guarantees that finalizers for usual objects will be called before childs of CriticalFinalizerObject. This allows from “usual” objects to access field SafeHandle that is guaranteed to be released later Finalizers for such classes will be called even in case of abnormal termination of application domain.

Слайд 25





Simplified Dispose Pattern
Описание слайда:
Simplified Dispose Pattern

Слайд 26





Simplifying Dispose Pattern
Most difficulties with Dispose pattern implementation based on assumption that same class (or class hierarchy) may contain managed and unmanaged resources at the same time
But Single Responsibility Principle (SRP) suggest us that we do not mix resources of different kinds
RAII idiom suggests a solution: if you have unmanaged resource, do not use it directly, wrap it into managed wrapper and work with it
Описание слайда:
Simplifying Dispose Pattern Most difficulties with Dispose pattern implementation based on assumption that same class (or class hierarchy) may contain managed and unmanaged resources at the same time But Single Responsibility Principle (SRP) suggest us that we do not mix resources of different kinds RAII idiom suggests a solution: if you have unmanaged resource, do not use it directly, wrap it into managed wrapper and work with it

Слайд 27





Simplified Dispose Pattern
Used only for managed resources
Описание слайда:
Simplified Dispose Pattern Used only for managed resources

Слайд 28





Recommended Links
Описание слайда:
Recommended Links

Слайд 29





Recommended Links
Dispose pattern http://habrahabr.ru/post/129283/
IDisposable: What Your Mother Never Told You About Resource Deallocation
http://www.codeproject.com/Articles/29534/IDisposable-What-Your-Mother-Never-Told-You-About 
Implementing Finalize and Dispose for cleaning unmanaged resources http://msdn.microsoft.com/ru-ru/library/b1yfkh5e.aspx 
Does C# have destructor? http://habrahabr.ru/post/122639/
Описание слайда:
Recommended Links Dispose pattern http://habrahabr.ru/post/129283/ IDisposable: What Your Mother Never Told You About Resource Deallocation http://www.codeproject.com/Articles/29534/IDisposable-What-Your-Mother-Never-Told-You-About Implementing Finalize and Dispose for cleaning unmanaged resources http://msdn.microsoft.com/ru-ru/library/b1yfkh5e.aspx Does C# have destructor? http://habrahabr.ru/post/122639/



Теги Dispose Pattern in C#
Похожие презентации
Mypresentation.ru
Загрузить презентацию