2010-05-25

Merge SQLite to executable file

 Xem bài trước http://gponster.blogspot.com/2010/05/building-sqlitenet-from-source-and.html

Sau một hồi cố merge mix-mode dll vào executable file vẫn không làm được mình đã thử dùng csharp-sqlite http://code.google.com/p/csharp-sqlite/. Tuy nhiên performace của nó khá chậm (khoảng chừng 3-5 lần khi dùng System.Data.SQLite.dll đó là theo cảm giác của mình) với lại mình gặp mới trường hợp báo assert failed có vẻ là không ổn lắm.

Benmark cho small database http://code.google.com/p/csharp-sqlite/wiki/Benchmarks

Jan 20, 2010 Updated to 3.6.22

Small Databases

# RecordsInsertingSearching 2XIteration 2XDeleting
SQLite100,0002.4s3.9s0.4s2.7s
C#-SQLite100,0005.9s4.9s0.5s5.1s
C#/SQLite2.5x1.3x1.0x1.9x

Sau một hồi search trên Google không có cách giải quyết nào khá hơn mình đành về giải pháp đầu tiên mà mình đã nghĩ. Embedded dll vào assembly resource rồi thực hiện load assembly dynamic. Data của SQLite thì cũng build dạng resource rồi thực hiện extract bình thường. Hai cái này khác nhau chút ít trong Build Action là Resource và Embedded Resource.
Để truy cập Embedded Resource dùng GetManifestResourceStream tuy nhiên phải biết chính xác tên của resource. Cách tốt nhất là xem trước tên của resource rồi thực hiện lấy resource.

-->
Assembly assembly = Assembly.GetExecutingAssembly();
string[] names = assembly.GetManifestResourceNames();
foreach (string name in names)
{
    System.Diagnostics.Debug.WriteLine(name);
}


Thêm nữa search trên stackoverflow được method sau, nhìn ok hơn là write trực tiếp ra byte
http://stackoverflow.com/questions/96732/embedding-one-dll-inside-another-as-an-embedded-resource-and-then-calling-it-from/97290#97290
-->
static byte[] StreamToBytes(Stream input)
{
    var capacity = input.CanSeek ? (int)input.Length : 0;

    using (var output = new MemoryStream(capacity))
    {
        int readLength;
        var buffer = new byte[4096];

        do
        {
            readLength = input.Read(buffer, 0, buffer.Length);
            output.Write(buffer, 0, readLength);
        } while (readLength != 0);

        return output.ToArray();
    }
}

Tiếp theo là thực hiện xử lý
-->
AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveEventHandler;
 
-->
static Assembly AssemblyResolveEventHandler(object sender, ResolveEventArgs args)
{
    Assembly result = null;

    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
    {
        if (asm.FullName == args.Name)   //already have
        {
            result = asm;
            break;
        }
    }

    if (result == null && args.Name.Contains("System.Data.SQLite"))
    {
        var prefix = "xxx.yyy."// get full pattern or embebbed reseource
        var name = string.Format("{0}.dll", args.Name.Split(',')[0]);
        var resource = prefix + name;     // DLL have embedded
        var assembly = Assembly.GetExecutingAssembly(); // get assembly

        using (Stream input = assembly.GetManifestResourceStream(resource))
        {
            result = input != null ? LoadLibrary(name, StreamToBytes(input)) : null;
        }
    };

    return result;
}


Prefix xxx.yyy. là phần tên của resource trong phần trước assembly.GetManifestResourceNames() đã biết thông thường là MyAssemblyName.ResourceFolder.

Mới đầu thực hiện load memory sẽ bị lỗi FileLoadException Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402) do SQLite là mix-mode không được build với /clr:safe nên chỉ có các load từ file. Tiếp theo đó khi write ra file với file name lấy từ Path.GetTempFileName() thay vì "System.Data.SQLite.dll" cũng bị báo lỗi tại lúc connect database
Unable to load DLL 'SQLite.Interop.DLL': The specified module could not be found

. (Exception from HRESULT: 0x8007007E)
Don't know nên chuyền thành 
-->
string tempFile = Path.GetTempFileName();
tempFile = Path.Combine(Path.GetDirectoryName(tempFile), "System.Data.SQLite.dll");

Method load library
-->
[DllImport("kernel32.dll")]
private static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004;

static void MarkFileToDeleteOnReboot(string fileName)
{
    MoveFileEx(fileName, null, MOVEFILE_DELAY_UNTIL_REBOOT);
}

static Assembly LoadLibrary(string name, byte[] data)
{
    Assembly asm = null;

    try
    {
        asm = Assembly.Load(data);
    }
    catch (FileLoadException e)
    {

        if (e.Message.Contains("HRESULT: 0x80131402"))
        {

            // This is C++/CLI Mixed assembly which can only be loaded from disk, not in-memory
            string tempFile = Path.GetTempFileName();
            tempFile = Path.Combine(Path.GetDirectoryName(tempFile), name);
            File.WriteAllBytes(tempFile, data);
            asm = Assembly.LoadFile(tempFile);
            MarkFileToDeleteOnReboot(tempFile);
        }
        else
        {
            throw; // don't ignore other load errors
        }
    }

    return asm;
}

Phần data của SQLite thực hiện build với Action là Resource sẽ extract trước khi connect như sau:
-->
static string ExtractDataFile(string resource)
{
    string tempPath = Path.GetTempPath();
    string tempFile = Path.Combine(tempPath, Path.Combine("Application define here ... ", resource));
    Directory.CreateDirectory(Path.GetDirectoryName(tempFile));

    StreamResourceInfo sri = Application.GetResourceStream(
        new Uri(string.Format("ResourcesFolder/{0}", resource),
        UriKind.Relative));

    byte[] data = StreamToBytes(sri.Stream);
    sri.Stream.Close();
    sri.Stream.Dispose();

    using (FileStream fs = File.Open(tempFile, FileMode.Create))
    {
        fs.Write(data, 0, data.Length);
        fs.Close();
    }
    MarkFileToDeleteOnReboot(tempFile);
    return tempFile;
}





Các resource khác như file hay ảnh cũng thực hiện tương tự, load helper method như sau
-->
public static T LoadResource<T>(string path)
{
    T c = default(T);
    StreamResourceInfo sri = Application.GetResourceStream(new Uri(path, UriKind.Relative));

    if (sri.ContentType == "application/xaml+xml")
    {
        c = (T)XamlReader.Load(sri.Stream);
    }
    else if (sri.ContentType.IndexOf("image") >= 0)
    {
        BitmapImage bi = new BitmapImage();
        bi.BeginInit();
        bi.StreamSource = sri.Stream;
        bi.EndInit();

        if (typeof(T) == typeof(ImageSource))
        {
            c = (T)((object)bi);
        }
        else if (typeof(T) == typeof(Image))
        {

            Image img = new Image { Source = bi };
            c = (T)((object)img);
        }
    }

    sri.Stream.Close();
    sri.Stream.Dispose();

    return c;
}

2010-05-20

Building SQLite.Net from source (and netmodules)

Original post from http://www.csharphacker.com/technicalblog/index.php/2009/07/05/sqlite-for-c-part-7%E2%80%93building-sqlite-net-from-source/

Thực hiện xong một vài thứ và nhận được yêu cầu merge lại thành 1 file. Mình thử dùng ILMerge như lâu giờ vẫn dùng nhưng bị báo lỗi vì System.Data.SQLite không thực hiện merge được bằng ILMerge.

Mệt quá không đóng tiền net, không làm được gì cả. Giờ mới có net lên tính tìm source của SQLite, tính chơi cách giang hồ, build 1 solution one project, copy files của mỗi project cũ thành một folder rồi build. Ai dè vẫn là đụng cái thằng này, source vừa C# vừa C++ mới nhớ netmodule, thực hiện build ra netmodule rồi link với thành 1 assembly. Sẵn tiện hỏi anh Google ra được cái link hay trên nên quote lên đây 1 bài. Để xem thử cái có cái SQLite provider nào code 100% bằng C# có lành hơn không.

2010-03-06

Zend Studio 5.5 on Windows 7


Original post http://melikedev.com/2009/11/28/zend-studio-5-5-on-windows-7/ . It is very helpful post, thank the author.





 


If you are reading this, then you have Windows 7 installed and gave Zend Studio 7.x the old college try, and found it to be quite cumbersome and annoying. After trying to adapt to Zend with Eclipse you opted to go back to Zend Studio 5.5 but found that it doesn’t work ‘out of the box’ on Windows 7. If you googled ‘Zend 5.5 Windows 7′ you probably found a response on Zend’s Forums where a savior post is located. At first read it is a bit confusing so I am going to attempt to make it a bit more clear here. Make no mistake, save for a few minor changes, the original author djvirgen (appended to by phliplip), are the true geniuses behind the work. You can find the original post @ http://www.zend.com/forums/index.php?t=msg&th=7855#21175

1. Download and install the latest JRE for Windows 7: http://java.com/en/download/manual.jsp

2. Download and install WinRAR (ensure you install with Explorer shell extension checked): http://rarlabs.com

3. Download Zend Studio 5.5.1 (you will need Zend account): http://www.zend.com/products/studio/downloads-prev

4. Extract Zend Studio 5.5.1

5. Click into ZendStudio-5_5_1 directory after extraction

6. Right mouse click on ZendStudio-5_5_1 executable, you will then see an option to ‘extract to (thanks to winrar shell extension), click it.

7. After Zend Studio 5.5.1 has been extracted click into ZendStudio-5_5_1/Windows/r
esource
8. You should see a dir named ‘jre’, rename this to ‘jre_came_with_zend’
9. Copy ‘jre6′ from your Java install directory (typically C:\Program Files (x86)\Java) into the Zend Studio extracted directory (ZendStudio-5_5_1\Windows\resource)
10. Rename ‘jre6′ to ‘jre’
11. Click up one dir so you are in ZendStudio-5_5_1/ and click on ZendStudio-5_5_1.exe
12. You will instantly know if you did not do the above steps correctly was windows 7 will inform you that you are attempting to run an app incompatible with Aero (this is because the jre packaged with zend is old, if so re-read above steps and try again)
13. If everything went fine, you should now be faced with a message “Java Runtime Environment Not Found.” Don’t worry about it, install zend as you normally would.
14. Zend Studio install should be finished, do not launch zend yet, we aren’t quite done.
15. Copy the ‘jre_came_with_zend’ directory from the original extraction folder and paste it into your Zend install directory (the one you selected during install, default is: c:\Program Files (x86)\Zend\ZendStudio-5.5.1
16. Rename the current ‘jre’ directory found in the Zend install directory to ‘jre_installed_with_zend’
17. Rename the ‘jre_came_with_zend’ to ‘jre’
18. Click into bin directory
19. Right mouse click ZDE.exe
20. Click properties
21. Click the compatibility tab
22. Click checkmark for ‘Compatibility Mode’ and select ‘Windows Vista’ from the dropdown menu
23. Click apply
24. Click ok
25. Now, with any luck you can double click on the ZDE.exe file and Zend should start normally.
Took a few hours to get all this down, hope this helps anyone else out there wanting to use Zend Studio 5.5.x vs the new Zend /w Eclipse crap. Good luck!

2010-02-26

Inversion of Control (IoC), Dependency Inversion Principle (DIP) và Dependency Injection (DI)


Inversion of Control (IoC), Dependency Inversion và Dependency Injection

Mục tiêu giới thiệu Inversion of Control (IoC), Dependency Inversion và mối liên quan giữa 2 khái niệm này với Dependency Injection.

Trên Wikipedia và trong series của Rich cũng không define chính xác IoC là gì. Inversion of Control ý muốn đề cập tới các software architecture designs có flow of control ngược với kiến trúc cũ dạng software libraries (traditional architecture of software libraries). Không có gì ghê gớm cả ... :D, cứ cố hiểu đơn giản vậy là được. Trên blog của Martin Flower cũng có một bài viết khá hay 'Inversion of Control Containers and the Dependency Injection pattern', nếu có thời gian thì bạn có thể tham khảo để biết quan điểm của một người chuyên nghiên cứu về software design.

Inversion of Control in Relation to Frameworks

IoC có quan hệ với framework và code re-used. Thông thường khi sử dụng lại code chúng ta thường call một library của ai đó. Ví dụ trong .NET khi gọi method Math.Tan tức là make a call và tức là có control method đó (đơn là hãy nghĩ rằng sử dụng được nó có nghĩ là control được nó).
Nhưng khi thực hiện implement một IComparable hay IEnumerable thì .NET framework sẽ call ngược lại code implement. Trong những trường hợp này thì direction of control bị đảo ngược: something else calling you. Ví dụ implement IComparable thì phải implement method CompareTo. Khi chúng ta gọi hàm Sort thì .NET framework gọi ngược lại method CompareTo mà chúng ta implement.

Inversion of Control và Dependency Injection

Hai khái niệm này thường được coi là đồng nghĩa tuy nhiên DI chỉ là một dạng đặc biệt mô tả cách thực hiện IoC tức IoC có một phạm vi lớn hơn DI.

Inversion of Control và CAB

CAB là một framework IoC và cho phép chúng ta thực hiện DI. Tức CAB có thể thực hiện call us chứ không hoàn toàn là một framework để chúng ta call method và sử dụng. Ví dụ như trong lab về module loader hàm Load() của ModuleInit đã được CAB thực hiện.

Dependency Inversion

Một term có liên quan và dễ gây bối rối là Dependency Inversion. Dependency Inversion còn gọi là Dependency Inversion Principle- DIP, là wider concept của Dependency Injection. DI chỉ là một phần trong DIP, DIP quy định cách thức mà một high-module cần thực hiện khi sử dụng một low-module. Inversion (đảo ngược) ở đây nằm ở chỗ high level thông thường dựa trên low level thì sẽ chuyển lại giở đây cả high level và low level depend upon một shared abstraction. Software consultant Robert C. Martin (tham khảo Agile Software Development, Principles, Patterns, and Practices published by Prentice Hall 10/15/2002 ISBN-10: 0135974445) phát biểu như sau:

High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.

Đại khái là module cao hơn không nên phụ thuộc vào module bên dưới. Cả hai nên dựa vào một 'trừu tượng'. 'Trừu tượng' không được phụ thuộc vào 'chi tiết' mà 'chi tiết' sẽ phụ thuộc 'trừu tượng'.

DIP và IoC

Trong bài lab về DI chúng ta chỉ thực hiện invert dependent giữa các classes. Chúng ta chưa thấy IoC xuất hiện rõ ràng trong ví dụ đó. Nói chung chúng ta chưa cần đi quá sâu thay vì chỉ cần hiểu có thể làm được gì với CAB.

CAB & SCSF - Part 03: Dependency Injection


Dependency Injection

Tìm hiểu Dependency Injection ở phạm vi chung không phải Dependency Injection cụ thể cài đặt trong CAB. Mục tiêu hiểu khái quát về khái niệm Dependency Injection.

Giả sử class Car có đang có nhu cầu sử dụng class EngineA. Tuy nhiên có khả năng class Engine sẽ thay đổi trong tương lai hoặc có nhu cầu sử dụng nhiều kiểu Engine khác nhau. Nếu sử dụng thông qua một interface IEngine, khi chuyển qua sử dụng một loại Engine khác là EngineB sẽ không cần thực hiện modify lại code mà chỉ cần thực hiện config lại theo 1 cách nào đó.

Scenario thứ 2 là việc sử dụng engine theo kiểu service yêu cầu nhiều dependency services khác tạo thành một dependency graph. Do đó mỗi khi thực hiện tạo một instance của class Car rất mất công và bạn phải biết constructor hay builder của Engine.

var generator = new Generator();
var engine = new Engine(generator);
var car = new Car(engine);

Có thể thấy một chút giống Strategy Pattern chỉ khác Dependency Injection thiên về việc cấu hình để có thể chọn lựa được class sử dụng tại thời điểm run-time. Việc này có thể thực hiện bằng các ngôn ngữ hỗ trợ reflection. Có 3 loại Dependency Injection là thực hiện qua setter injection, constructor injectioninterface injection. Việc thực hiện DI còn liên quan đến 1 việc gọi là service locator. Nhiệm vụ của service locator là xác định đúng engine cần dùng thông qua GetCorrectEngine method. Thông thường một class giả sử tên là EngineLocator sẽ thực hiện nhiệm vụ này.

Constructor method

public interface IEngine
{
    void Start();
}

public class Engine : IEngine
{
    public void Start();
}

public class Car
{
    protected IEngine engine;

    public Car(IEngine engine)
    {
        this.engine = engine;
    }
}

Setter method

public class Car
{
    protected IEngine engine;

    public IEngine Engine
    {
        set
        {
            engine = value;
        }
    }
}

Interface method

public interface IInjectEngine
{
    void InjectEngine(IEngine engine);
}

public class Car: IInjectEngine
{
    protected IEngine engine;

    void IInjectEngine.InjectEngine(IEngine engine)
    {
        this.engine = engine;
    }
}

Thông thường việc thực hiện sẽ dùng hướng interface injection. Đến đây nếu chưa hiểu rõ DI chắc chắn bạn vẫn chưa hình dung được việc tách biệt interfaceimplement là có ý nghĩa như thế nào và tại sao Car không cần thiết phải chỉnh sửa mà chỉ cần chỉnh sửa thông qua file config.

Implementation

Lập một solution tên là Part03A với các project như sau:
  1. Car: console application, có một file là Car.cs, assembly name là ConsoleApp, output ConsoleApp.exe.
  2. EngineA: library project, chứa EngineA.cs, assembly EngineA.
  3. EngineB: library project, chứa EngineB.cs, assembly EngineB.
  4. IEngine: library project, chứa định nghĩa interface.
- Để đơn giản các project đang sử dụng cùng một namespace là DI. Cấu hình Assembly name và default namespace trong Properties của project.

- Bước tiếp theo cấu hình Output của các project giả sử là '..\bin\Debug' mục đích các file IEngine.dll, EngineA.dll và EngineB.dll sẽ cùng thư mục với ConsoleApp.exe (vì các projects này không reference đến nhau)

- Thêm vào Car project một application config file App.config. Add reference tới IEngine cho 3 project còn lại. Việc này có nghĩa Car không có reference tới bất kỳ cài đặt nào của Engine: EngineA hoặc EngineB. Việc này cho phép luôn luôn build OK với Car cho dù có hay không có EngineA và EngineB, do đó việc develop Car là hoàn toàn độc lập với Engine.

Thực hiện code của các file như sau:

IEngine.cs

namespace DI
{
    public interface IEngine
    {
        void Start();
    }
}

IInjectEngine.cs

namespace DI
{
    public interface IInjectEngine
    {
        void InjectEngine(IEngine engine);
    }
}

EngineA.csEngineB.cs

public class EngineA : IEngine
{

    public void Start()
    {
        Console.WriteLine("Engine A start...")
    }
}

public class EngineB : IEngine
{

    public void Start()
    {
        Console.WriteLine("Engine B start...")
    }
}

Car.cs

namespace DI
{
    public class Car : IInjectEngine
    {
        IEngine engine;

        public void Start()
        {
            engine.Start();
        }

        public void InjectEngine(IEngine engine)
        {
            this.engine = engine;
        }
    }
}

Program.cs

namespace DI
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create our car and inject the dependency
            Car car = new Car();

            // Service locator, get the correct address based on configuration file
            IEngine engine = GetCorrectEngine();
            ((IInjectCar)car).InjectEngine(engine);

            // Use the car, the method references the car
            // so behavior depends on the configuration file
            car.Start();
            Console.ReadLine();
        }

        // Instantiate and return a class conforming to the IEngine interface:
        // which class gets instantiated depends on the ClassName setting in
        // the configuration file
        static IEngine GetCorrectEngine()
        {
            string className = System.Configuration.ConfigurationSettings.AppSettings["ClassName"];
            Type type = System.Type.GetType(className);
            return (IEngine)Activator.CreateInstance(type);
        }
    }
}

- Method GetCorrectEngine đọc file config và tạo một instance của Engine đúng với implement cần chỉ định thông qua reflection.

- Nếu nội dung file config App.config không đúng (không có key ClassName) việc build vẫn OK tuy nhiên sẽ gặp run-time error vì không tạo được object. Cần phải chỉ định assembly name.

xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ClassName" value="DI.EngineA" />
  </appSettings>
</configuration>

- Chỉnh lại file config tốt nhất là dùng AssemblyQualifiedName hoặc đơn giản chỉ cần dùng "DI.EngineA, EngineA"

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="ClassName" value="DI.EngineA, EngineA, Version=1.0.0.0, Culture=neutral, PublicKey Token=null" />
  </appSettings>
</configuration>

- Thực hiện thay đổi file config dùng EngineB và run lại application, nhận xét và rút ra kết luận.

2010-01-17

CAB & SCSF - Part 02: WorkItems

-->
WorkItems

Part 2: Overview khái niệm WorkItem.

WorkItems – Basic Concepts

Theo tài liệu của Microsoft mô tả là một run-time container chứa các component hợp tác với nhau thực hiện chức năng của một use case (a run-time container of components that are collaborating to fulfill a use case). Có thể xem WorkItems là các class trong đó có chứa các collection của các class khác. WorkItem trích từ metadata như sau:

using Microsoft.Practices.CompositeUI.Collections;
using Microsoft.Practices.CompositeUI.Commands;
using Microsoft.Practices.CompositeUI.EventBroker;
using Microsoft.Practices.CompositeUI.SmartParts;
using Microsoft.Practices.CompositeUI.Utility;
using Microsoft.Practices.ObjectBuilder;
using System;
using System.ComponentModel;
using System.Diagnostics;

namespace Microsoft.Practices.CompositeUI
{
    public class WorkItem : IBuilderAware, IDisposable
    {
        public WorkItem();

        public ManagedObjectCollection<Command> Commands { get; }
        public ManagedObjectCollection<EventTopic> EventTopics { get; }
        public string ID { get; set; }
        protected Builder InnerBuilder { get; }
        protected IReadWriteLocator InnerLocator { get; }
        public ManagedObjectCollection<object> Items { get; }
        [Dependency(NotPresentBehavior = NotPresentBehavior.ReturnNull)]
        [Browsable(false)]
        public WorkItem Parent { get; set; }
        [Browsable(false)]
        public WorkItem RootWorkItem { get; }
        public ServiceCollection Services { get; }
        public ManagedObjectCollection<object> SmartParts { get; }
        public State State { get; }
        public WorkItemStatus Status { get; }
        public TraceSource TraceSource { set; }
        public UIExtensionSiteCollection UIExtensionSites { get; }
        public ManagedObjectCollection<WorkItem> WorkItems { get; }
        public ManagedObjectCollection<IWorkspace> Workspaces { get; }

        public event EventHandler Activated;
        public event CancelEventHandler Activating;
        public event EventHandler Deactivated;
        public event CancelEventHandler Deactivating;
        public event EventHandler Disposed;
        public event EventHandler<DataEventArgs<string>> IdChanged;
        public event EventHandler Initialized;
        public event EventHandler RunStarted;
        public event EventHandler Terminated;
        public event EventHandler Terminating;

        public void Activate();
        protected internal void BuildUp();
        protected virtual Command CreateCommand(Type t, string name);
        protected virtual EventTopic CreateEventTopic(Type t, string topicName);
        public void Deactivate();
        public void DeleteState();
        public void Dispose();
        protected virtual void Dispose(bool disposing);
        protected internal void FinishInitialization();
        public TSmartPartInfo GetSmartPartInfo<TSmartPartInfo>(object smartPart) where TSmartPartInfo : ISmartPartInfo;
        protected internal void InitializeRootWorkItem(Builder builder);
        protected virtual void InitializeServices();
        [InjectionMethod]
        public void InitializeWorkItem();
        public void Load();
        protected virtual void OnActivated();
        protected virtual void OnActivating(CancelEventArgs args);
        public virtual void OnBuiltUp(string id);
        protected virtual void OnDeactivated();
        protected virtual void OnDeactivating(CancelEventArgs args);
        protected virtual void OnDisposed();
        protected virtual void OnIdChanged();
        protected virtual void OnInitialized();
        protected virtual void OnObjectAdded(object item);
        protected virtual void OnObjectRemoved(object item);
        protected virtual void OnRunStarted();
        public virtual void OnTearingDown();
        protected virtual void OnTerminated();
        protected virtual void OnTerminating();
        public void RegisterSmartPartInfo(object smartPart, ISmartPartInfo info);
        public void Run();
        public void Save();
        public void Terminate();
    }
}

Dễ dàng nhận thấy WorkItem có các 3 collection như sau:
1.      Items là một collection loại object nên có thể contains tất cả mọi thứ.
2.      Services collection chứa các CAB services (sẽ đề cập sau)
3.      WorkItems collection là một collection base trên WorkItem. Đây là các child WorkItems. Collection này thể hiện Composite pattern mà chúng ta đã tìm hiểu.
4.      Các collection khác SmartParts, UIExtensionSites, Workspaces.

WorkItem còn có State để theo dõi sự thay đổi trạng thái implement ISerializable và Status chỉ định là active hay inactive.

Theo mô tả ở trên sẽ thấy có rất nhiều thuật ngữ chưa đề cập và các thuật ngữ này rất mới và khó hiểu.

Container Hierarchy và Root WorkItem

Các WorkItems có thể biểu diễn dưới dạng phân cấp. Theo như lab 1 thì program có một RootWorkItem và Shell là một instance của ShellForm. Blue module và Red module sẽ được load vào RootWorkItem. WorkItem child có thể được truy cập qua code dạng như sau:

this.WorkItem.WorkItems["SpecificName"]

Đến đây chúng ta có thể mơ hồ nhận ra mối quan hệ giữa CAB và Composite pattern mà chúng ta đã tìm hiểu.

WorkItems và FormShellApplication
Ví dụ như trong lab về module loader của Part 1, Program chứa top-root WorkItem và được truy cập thông qua code:
this.RootWorkItem

Program cũng chứa một Shell có thể truy cập qua code:
this.Shell

Notes: hiện tại lab module loader Part 1 vẫn chưa thực hiện dùng hay truy cập thông qua các properties này. Các bài labs tiếp theo sẽ thực hiện trên các property này.

Trong phần tiếp theo chúng ta sẽ tìm hiểu cách thực hiện việc load modules cụ thể như thế nào. Đối tượng để tìm hiểu là Dependency Injection, một khái niệm quan trọng.