DFL框架分析(一)

1.框架的意义,一个简单windows程序的要素:
封装了windows窗口,控件,简化windows开发.

声明winProc回调函数;定义窗口属性,绑定回调函数,注册窗口类;创建windows窗口;进入消息处理循环,直到结束.

一个简单的win32程序,D的例子:winsamp
[code]// Compile with: dmd winsamp gdi32.lib winsamp.def
import std.c.windows.windows;
import std.c.stdio;

const int IDC_BTNCLICK = 101;
const int IDC_BTNDONTCLICK = 102;

extern(Windows)
int WindowProc(HWND hWnd, uint uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
switch (LOWORD(wParam))
{
case IDC_BTNCLICK:
if (HIWORD(wParam) == BN_CLICKED)
MessageBoxA(hWnd, "Hello, world!", "Greeting",
MB_OK | MB_ICONINFORMATION);
break;
case IDC_BTNDONTCLICK:
if (HIWORD(wParam) == BN_CLICKED)
{
MessageBoxA(hWnd, "You've been warned...", "Prepare to GP fault",
MB_OK | MB_ICONEXCLAMATION);
*(cast(int*) null) = 666;
}
break;
}
break;
}
case WM_PAINT:
{
static char[] text = "D Does Windows";
PAINTSTRUCT ps;
HDC dc = BeginPaint(hWnd, &ps);
RECT r;
GetClientRect(hWnd, &r);
HFONT font = CreateFontA(80, 0, 0, 0, FW_EXTRABOLD, FALSE, FALSE,
FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
HGDIOBJ old = SelectObject(dc, cast(HGDIOBJ) font);
SetTextAlign(dc, TA_CENTER | TA_BASELINE);
TextOutA(dc, r.right / 2, r.bottom / 2, text, text.length);
SelectObject(dc, old);
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProcA(hWnd, uMsg, wParam, lParam); // allow windows process other message
}

int doit()
{
HINSTANCE hInst = GetModuleHandleA(null);

WNDCLASS wc;
wc.lpszClassName = "DWndClass";
wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = &WindowProc; //set winProc
wc.hInstance = hInst;
wc.hIcon = LoadIconA(cast(HINSTANCE) null, IDI_APPLICATION);
wc.hCursor = LoadCursorA(cast(HINSTANCE) null, IDC_CROSS);
wc.hbrBackground = cast(HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = null;
wc.cbClsExtra = wc.cbWndExtra = 0;
RegisterClassA(&wc);

HWND hWnd, btnClick, btnDontClick;
hWnd = CreateWindowA("DWndClass", "Just a window", WS_THICKFRAME |
WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, HWND_DESKTOP,
cast(HMENU) null, hInst, null);
assert(hWnd);

btnClick = CreateWindowA("BUTTON", "Click Me", WS_CHILD | WS_VISIBLE,
0, 0, 100, 25, hWnd, cast(HMENU) IDC_BTNCLICK, hInst, null);

btnDontClick = CreateWindowA("BUTTON", "DON'T CLICK!", WS_CHILD | WS_VISIBLE,
110, 0, 100, 25, hWnd, cast(HMENU) IDC_BTNDONTCLICK, hInst, null);

MSG msg;
while (GetMessageA(&msg, cast(HWND) null, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}

return 1;
}

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();

extern (Windows)
int WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
int result;

gc_init(); // initialize garbage collector
_minit(); // initialize module constructor table

try
{
_moduleCtor(); // call module constructors
_moduleUnitTests(); // run unit tests (optional)
result = doit(); // insert user code here
}
catch (Object o) // catch any uncaught exceptions
{
MessageBoxA(null, cast(char *)o.toString(), "Error", MB_OK | MB_ICONEXCLAMATION);
result = 0; // failed
}
gc_term(); // run finalizers; terminate garbage collector
return result;
}
[/code]

2.DFL的做法

使用面向对象语言的方法,使用继承抽象了窗口组件的层次,封装了消息循环,简化了处理过程.待续...
HibernateD 是 D 语言的 ORM 框架,类似 Java 的 Hibernate,示例代码:import hibernated.core; // Annotations of entity classes class User {     long id;     string name;     Customer customer;     @ManyToMany // cannot be inferred, requires annotation     LazyCollection!Role roles; } class Customer {     int id;     string name;     // Embedded is inferred from type of Address     Address address;     Lazy!AccountType accountType; // ManyToOne inferred     User[] users; // OneToMany inferred     this() {         address = new Address();     } } @Embeddable class Address {     string zip;     string city;     string streetAddress; } class AccountType {     int id;     string name; } class Role {     int id;     string name;     @ManyToMany // w/o this annotation will be OneToMany by convention     LazyCollection!User users; } // create metadata from annotations EntityMetaData schema = new SchemaInfoImpl!(User, Customer, AccountType,                                   T1, TypeTest, Address, Role, GeneratorTest); // setup DB connection factory MySQLDriver driver = new MySQLDriver(); string url = MySQLDriver.generateUrl("localhost", 3306, "test_db"); string[string] params = MySQLDriver.setUserAndPassword("testuser", "testpasswd"); DataSource ds = ConnectionPoolDataSourceImpl(driver, url, params); // create session factory Dialect dialect = new MySQLDialect(); SessionFactory factory = new SessionFactoryImpl(schema, dialect, ds); scope(exit) factory.close(); // Create schema if necessary { // get connection Connection conn = ds.getConnection(); scope(exit) conn.close(); // create tables if not exist factory.getDBMetaData().updateDBSchema(conn, false, true); } // Now you can use HibernateD // create session Session sess = factory.openSession(); scope(exit) sess.close(); // use session to access DB // read all users using query Query q = sess.createQuery("FROM User ORDER BY name"); User[] list = q.list!User(); // create sample data Role r10 = new Role(); r10.name = "role10"; Role r11 = new Role(); r11.name = "role11"; Customer c10 = new Customer(); c10.name = "Customer 10"; User u10 = new User(); u10.name = "Alex"; u10.customer = c10; u10.roles = [r10, r11]; sess.save(r10); sess.save(r11); sess.save(c10); sess.save(u10); // load and check data User u11 = sess.createQuery("FROM User WHERE name=:Name").                            setParameter("Name", "Alex").uniqueResult!User(); assert(u11.roles.length == 2); assert(u11.roles[0].name == "role10" || u11.roles.get()[0].name == "role11"); assert(u11.roles[1].name == "role10" || u11.roles.get()[1].name == "role11"); assert(u11.customer.name == "Customer 10"); assert(u11.customer.users.length == 1); assert(u11.customer.users[0] == u10); assert(u11.roles[0].users.length == 1); assert(u11.roles[0].users[0] == u10); // remove reference u11.roles.get().remove(0); sess.update(u11); // remove entity sess.remove(u11); 标签:HibernateD
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值