مروری بر کاربردهای Action و Func - قسمت چهارم
نویسنده: وحید نصیری
تاریخ: ۱۳۹۱/۰۶/۰۵ ۲۳:۵۳
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
using System;
using System.Collections.Generic;
namespace Test
{
public class Table
{
public Header Header { set; get; }
public IList<Cell> Cells { set; get; }
public float Width { set; get; }
}
public class Header
{
public string Title { set; get; }
public DateTime Date { set; get; }
public IList<Cell> Cells { set; get; }
}
public class Cell
{
public string Caption { set; get; }
public float Width { set; get; }
}
}
using System;
using System.Collections.Generic;
namespace Test
{
public class TableApi
{
public Table CreateTable(Action<TableCreator> action)
{
var creator = new TableCreator();
action(creator);
return creator.TheTable;
}
}
public class TableCreator
{
readonly Table _theTable = new Table();
internal Table TheTable
{
get { return _theTable; }
}
public void Width(float value)
{
_theTable.Width = value;
}
public void AddHeader(Action<HeaderCreator> action)
{
_theTable.Header = ...
}
public void AddCells(Action<CellsCreator> action)
{
_theTable.Cells = ...
}
}
}
using System;
using System.Collections.Generic;
namespace Test
{
public class CellsCreator
{
readonly IList<Cell> _cells = new List<Cell>();
internal IList<Cell> Cells
{
get { return _cells; }
}
public void AddCell(string caption, float width)
{
_cells.Add(new Cell { Caption = caption, Width = width });
}
}
public class HeaderCreator
{
readonly Header _header = new Header();
internal Header Header
{
get { return _header; }
}
public void Title(string title)
{
_header.Title = title;
}
public void Date(DateTime value)
{
_header.Date = value;
}
public void AddCells(Action<CellsCreator> action)
{
var creator = new CellsCreator();
action(creator);
_header.Cells = creator.Cells;
}
}
}
using System;
using System.Collections.Generic;
namespace Test
{
public class TableCreator
{
readonly Table _theTable = new Table();
internal Table TheTable
{
get { return _theTable; }
}
public void Width(float value)
{
_theTable.Width = value;
}
public void AddHeader(Action<HeaderCreator> action)
{
var creator = new HeaderCreator();
action(creator);
_theTable.Header = creator.Header;
}
public void AddCells(Action<CellsCreator> action)
{
var creator = new CellsCreator();
action(creator);
_theTable.Cells = creator.Cells;
}
}
public class CellsCreator
{
readonly IList<Cell> _cells = new List<Cell>();
internal IList<Cell> Cells
{
get { return _cells; }
}
public void AddCell(string caption, float width)
{
_cells.Add(new Cell { Caption = caption, Width = width });
}
}
public class HeaderCreator
{
readonly Header _header = new Header();
internal Header Header
{
get { return _header; }
}
public void Title(string title)
{
_header.Title = title;
}
public void Date(DateTime value)
{
_header.Date = value;
}
public void AddCells(Action<CellsCreator> action)
{
var creator = new CellsCreator();
action(creator);
_header.Cells = creator.Cells;
}
}
}
var data = new TableApi().CreateTable(table =>
{
table.Width(1);
table.AddHeader(header=>
{
header.Title("new rpt");
header.Date(DateTime.Now);
header.AddCells(cells=>
{
cells.AddCell("cell 1", 1);
cells.AddCell("cell 2", 2);
});
});
table.AddCells(tableCells=>
{
tableCells.AddCell("c 1", 1);
tableCells.AddCell("c 2", 2);
});
});