Creating First Component.
Our first component will be named NewsPublisher and it will show news; as it will
have visual presentation we'll create second class named NewsPublisherView.
NewsPublisher will be derived from VisualComponent and NewsPublisherView will implement
IContract interface because in this case LiveUI binds Compnent and View automatically.
- public
classNewsPublisher :
VisualComponent
- {
- }
-
- public class
NewsPublisherView :
WebComponent, IView,
IContract<NewsPublisher,
NewsPublisherView>
- {
- }
That's how project looks

To make NewPublisherView registered MyFirstApplication should be registered as a
module, so open Configuration/LiveUI.config and add line
- <?xml
version="1.0"?>
- <LiveUI>
- <Modules>
- ...
- < add
moduleName
="
myApplication"
moduleAssembly
="
MyFirstApplication "
location
= ""
/>
- </Modules>
- </LiveUI>
Now let's make it show something. Component will be responsible for finding
data and View would be responsible for showing.
- public class
NewsPublisher :
VisualComponent
- {
- public
override void OnInitializing()
- {
- base.OnInitializing();
- var view =
this.GetContracts().Get<NewsPublisherView>();
- view.Title =
- "EU leaders brace for tough summit";
- view.FullText =
- "Tough talks on climate change, the Lisbon "
+
- "treaty and the EU presidency are set to "
+
- "dominate a leaders' summit in Brussels.";
- }
- }
-
- public class
NewsPublisherView :
WebComponent,
IView, IContract<NewsPublisher,
NewsPublisherView>
- {
- public string
Title { get; set;
}
- public string
FullText { get;
set; }
-
- public
override void OnInitializing()
- {
- base.OnInitializing();
- Workspace.Controls.Add(new
LiteralControl("<h1>"
+ Title + "</h1>"));
- Workspace.Controls.Add(new
LiteralControl(FullText));
- }
- }
It's it. We have to fix WebFormsPage.aspx.cs to look at our Component in action.
- public partial
class
WebFormsPage : Page
- {
- protected
override void OnInit(EventArgs e)
- {
- var rootComponent =
new
WebFormsRootComponent {Page = this,
Workspace = Page.Form};
- rootComponent.Children.Add(new
NewsPublisher());
- rootComponent.Initialize();
- }
- }
And that's what we see
Unfortunately, our component is not testable because it depends of NewsPublisherView.
It would be better if it knew only INewsPublisherView interface.
- public
interface INewsPublisherView :
IView, IContract<NewsPublisher,
INewsPublisherView>
- {
- string Title {
get; set;
}
- string FullText {
get; set;
}
- }
-
- public class
NewsPublisher :
VisualComponent
- {
- public
override void OnInitializing()
- {
- base.OnInitializing();
- var view =
this.GetContracts().Get<INewsPublisherView>();
- view.Title = "EU leaders brace for tough
summit";
- view.FullText = "Tough talks on climate
change, the Lisbon " +
- "treaty and the EU presidency
are set to " +
- "dominate a leaders' summit in
Brussels.";
- }
- }
-
- public class
NewsPublisherView :
WebApplicationComponent,
INewsPublisherView,
IContract<NewsPublisher,
NewsPublisherView>
- {
- public string
Title { get; set;
}
- public string
FullText { get;
set; }
-
- public
override void OnInitializing()
- {
- base.OnInitializing();
- Workspace.Controls.Add(new
LiteralControl("<h1>"
+ Title + "</h1>"));
- Workspace.Controls.Add(new
LiteralControl(FullText));
- }
- }
- public
interface INewsPublisherView :
IView, IContract<NewsPublisher,
INewsPublisherView>
- {
- string Title {
get; set;
}
- string FullText {
get; set;
}
- }
-
- public class
NewsPublisher :
VisualComponent
- {
- public
override void OnInitializing()
- {
- base.OnInitializing();
- var view =
this.GetContracts().Get<INewsPublisherView>();
- view.Title = "EU leaders brace for tough
summit";
- view.FullText =
- "Tough talks on climate change, the Lisbon "
+
- "treaty and the EU presidency are set to "
+
- "dominate a leaders' summit in Brussels.";
- }
- }
Our component still works, it's testable and reusable but View is not nice yet.
There is no good in creating LiteralControls manually. Let's use News.ascx
UserControl for html markup.
There is News.ascx
-
<h1><%=Header %></h1>
-
<% =Text %>
And there is News.ascx.cs
-
public partial
class News
: System.Web.UI.UserControl
- {
-
public
string Header { get;
set;}
-
public
string Text { get;
set;}
- }
And let view use it
- public
class NewsPublisherView
- : WebApplicationComponent,
INewsPublisherView,
IContract<NewsPublisher,
NewsPublisherView>
- {
- public
string Title { get;
set;}
- public
string FullText { get;
set;}
-
- public
override void
OnInitializing()
- {
- base.OnInitializing();
- var markup = LoadControl<News>("News.ascx");
- markup.Header = Title;
- markup.Text = FullText;
- Workspace.Controls.Add(markup);
- }
- }
Of course, result is still the same

|