A friend of mine was asking me about interfaces. I know how they work so I thought I'd throw together an example. There is going to be three blog postings in total. This first one does nothing more than set the stage. (The stuff to do with interfaces will be in the next two postings.) I have built a solution that has two projects: InterfaceExample & InterfaceExample.Core
The app is very simply, it just brings up a web page that gives the names of some people. There are all of four files to care about so far. Let's go over them and then that will be it for this first blog posting.
The first one is an object named Person in InterfaceExample.Core:
namespace InterfaceExample.Core
{
public class Person
{
public string Name { get; set; }
}
}
Also in InterfaceExample.Core is PersonRepository which exposes two public methods which return collections of the Person object. The collections are crafted right here within this class in lieu of pulling this content in from some external source. There is, thus, no way to update this content outside of the code. If one wants to update this list it has to be done at this file and if this is all of a public-facing web site then one will have to update a deployed .dll upon EVERY update. Such may not be a big deal. A list of the names of U.S. States for example is rarely, rarely going to change. However, let's say, for our example, that this list will need updating and that it is thus a pain point to have it tucked away in code. Well, what are we going to do about that? I'll get to that in the next two postings. We will refactor this code.
using System.Collections.Generic;
using System.Linq;
namespace InterfaceExample.Core
{
public class PersonRepository
{
public List<Person> GetAllPeople()
{
return People();
}
public List<Person> GetPeopleNotNamedJohn()
{
return People().Where(p => !p.Name.Contains("John")).ToList();
}
private static List<Person> People()
{
return new List<Person>()
{
new Person() {Name = "Thomas J. Watson Jr."},
new Person() {Name = "T. Vincent Learson"},
new Person() {Name = "Frank Cary"},
new Person() {Name = "John Opel"},
new Person() {Name = "John Akers"},
new Person() {Name = "Louis V. Gerstner Jr."},
new Person() {Name = "Samuel J. Palmisano"}
};
}
}
}
InterfaceExample inherits InterfaceExample.Core. While InterfaceExample.Core is a class library, InterfaceExample is a web forms project. I have all of one web form in the project. It looks like this:
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="InterfaceExample.Default" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>People</title>
</head>
<body>
<form id="form1" runat="server">
<h1>People:</h1>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br />
<%# DataBinder.Eval(Container.DataItem,"Name") %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>
Here is the code behind for the web form shown above. It populates the repeater with what comes up from the GetPeopleNotNamedJohn() method at PersonRepository.
using System;
using InterfaceExample.Core;
namespace InterfaceExample
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PersonRepository personRepository = new PersonRepository();
Repeater1.DataSource = personRepository.GetPeopleNotNamedJohn();
Repeater1.DataBind();
}
}
}
No comments:
Post a Comment