Home     About PeterBlum.com     Policies     Download a Licensed Product     Newsletter
Peter's Business Logic Driven UI
Adding business logic to POCO ("Plain old CLR objects")
Back to Product Overview  |  Next topic | Language preference:

You can create objects with business logic even when the class does not represent a table in your database. These "Plain old CLR objects" are setup just like Entity classes, by adding DataAnnotation attributes, whether from System.ComponentModel.DataAnnotations or PeterBlum.DES.DataAnnotations. The business layer side of BLD doesn't see them as different, except to know they don't have relationships to other Entity classes like in a database.

BLD's user interface side works the same way as it does with Entity classes too. You only need to switch your DataSource control to the POCODataSource, which is part of BLD.

Here is a class that uses business attributes and the web controls which convert it into an interface. It takes advantage of Automatic Scaffolding to define the list of fields and Pattern Templates to format them as a two column table.

using DESDA = PeterBlum.DES.DataAnnotations;
using System.ComponentModel.DataAnnotations;
…
public class EmailGenerator
{
   public EmailGenerator() { }
   
   [DESDA.Required()]
   [DESDA.EmailAddressDataType(DataType.EmailAddress)]
   [DESDA.DisplayName("From")]
   public string FromEmailAddress { get; set; }
   
   [DESDA.Required()]
   [DESDA.EmailAddressDataType(DataType.EmailAddress)]
   [DESDA.DisplayName("To")]
   public string ToEmailAddress { get; set; }
   
   [DESDA.Required()]
   [DESDA.StringLength(80)]
   public string Subject { get; set; }
   
   [DESDA.Required()]
   [DESDA.StringLength(5000)]
   [DataType(DataType.MultilineText)]
   [DESDA.DisplayName("Message")]
   public string Body { get; set; }
   
   [DataType(DataType.Date)]
   [DESDA.ScaffoldColumn(false)]
   public DateTime SendDate { get; set; }
   
   public void Send()
   {
      MailMessage mailMessage = new MailMessage(FromEmailAddress, ToEmailAddress);
      mailMessage.Subject = Subject;
      mailMessage.Body = Body.ToString();
      SmtpClient client = new SmtpClient("mail.mycompany.com");
      client.UseDefaultCredentials = true;
      client.Send(vMailMessage); 
   }
}
      
<script runat="server">
protected void POCODataSource1_DataChanged(object sender, DataChangedEventArgs args) { if (!args.HasErrors()) { EmailGenerator vGen = args.GetEntity<EmailGenerator>(); vGen.Send(); } }
</script> <des:BLDPageManager ID="BLDPageManager1" runat=server > <Adapters> <des:BLDFormViewAdapter DataBoundControlID="BLDFormView1" SupportsEditActions="true" /> </Adapters> </des:BLDPageManager> <des:BLDFormView ID="BLDFormView1" runat="server" DataSourceID="POCODataSource1" DefaultMode="Edit" /> <des:POCODataSource ID="POCODataSource1" runat="server" POCOTypeName="EmailGenerator" OnDataChanged="POCODataSource1_DataChanged" />

Back to Product Overview  |  Next topic