<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Hjnntaao's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 18:59:01 GMT</pubDate>
    <description>DZone Snippets: Hjnntaao's Code Snippets</description>
    <item>
      <title>WFC Service Library Code</title>
      <link>http://snippets.dzone.com/posts/show/2579</link>
      <description>Code generated when creating a project of type "WCF Service Library" using Microsoft .NET Framework 3.0 RC and Orcas CTP&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.ServiceModel;&lt;br /&gt;using System.Runtime.Serialization;&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;&lt;br /&gt;    HOW TO HOST THE WCF SERVICE IN THIS LIBRARY IN ANOTHER PROJECT&lt;br /&gt;    You will need to do the following things: &lt;br /&gt;    1)    Add a Host project to your solution&lt;br /&gt;        a.    Right click on your solution&lt;br /&gt;        b.    Select Add&lt;br /&gt;        c.    Select New Project&lt;br /&gt;        d.    Choose an appropriate Host project type (e.g. Console Application)&lt;br /&gt;    2)    Add a new source file to your Host project&lt;br /&gt;        a.    Right click on your Host project&lt;br /&gt;        b.    Select Add&lt;br /&gt;        c.    Select New Item&lt;br /&gt;        d.    Select "Code File"&lt;br /&gt;    3)    Paste the contents of the "MyServiceHost" class below into the new Code File&lt;br /&gt;    4)    Add an "Application Configuration File" to your Host project&lt;br /&gt;        a.    Right click on your Host project&lt;br /&gt;        b.    Select Add&lt;br /&gt;        c.    Select New Item&lt;br /&gt;        d.    Select "Application Configuration File"&lt;br /&gt;    5)    Paste the contents of the App.Config below that defines your service endoints into the new Config File&lt;br /&gt;    6)    Add the code that will host, start and stop the service&lt;br /&gt;        a.    Call MyServiceHost.StartService() to start the service and MyServiceHost.EndService() to end the service&lt;br /&gt;    7)    Add a Reference to System.ServiceModel.dll&lt;br /&gt;        a.    Right click on your Host Project&lt;br /&gt;        b.    Select "Add Reference"&lt;br /&gt;        c.    Select "System.ServiceModel.dll"&lt;br /&gt;    8)    Add a Reference from your Host project to your Service Library project&lt;br /&gt;        a.    Right click on your Host Project&lt;br /&gt;        b.    Select "Add Reference"&lt;br /&gt;        c.    Select the "Projects" tab&lt;br /&gt;    9)    Set the Host project as the "StartUp" project for the solution&lt;br /&gt;        a.    Right click on your Host Project&lt;br /&gt;        b.    Select "Set as StartUp Project"&lt;br /&gt;&lt;br /&gt;    ################# START MyServiceHost.cs #################&lt;br /&gt;&lt;br /&gt;    using System;&lt;br /&gt;    using System.ServiceModel;&lt;br /&gt;&lt;br /&gt;    // A WCF service consists of a contract (defined below), &lt;br /&gt;    // a class which implements that interface, and configuration &lt;br /&gt;    // entries that specify behaviors and endpoints associated with &lt;br /&gt;    // that implementation (see &lt;system.serviceModel&gt; in your application&lt;br /&gt;    // configuration file).&lt;br /&gt;&lt;br /&gt;    internal class MyServiceHost&lt;br /&gt;    {&lt;br /&gt;        internal static ServiceHost myServiceHost = null;&lt;br /&gt;&lt;br /&gt;        internal static void StartService()&lt;br /&gt;        {&lt;br /&gt;            //Consider putting the baseAddress in the configuration system&lt;br /&gt;            //and getting it here with AppSettings&lt;br /&gt;            Uri baseAddress = new Uri("http://localhost:8080/service1");&lt;br /&gt;&lt;br /&gt;            //Instantiate new ServiceHost &lt;br /&gt;            myServiceHost = new ServiceHost(typeof(HelloWorldWCFService.service1), baseAddress);&lt;br /&gt;&lt;br /&gt;            //Open myServiceHost&lt;br /&gt;            myServiceHost.Open();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        internal static void StopService()&lt;br /&gt;        {&lt;br /&gt;            //Call StopService from your shutdown logic (i.e. dispose method)&lt;br /&gt;            if (myServiceHost.State != CommunicationState.Closed)&lt;br /&gt;                myServiceHost.Close();&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ################# END MyServiceHost.cs #################&lt;br /&gt;    ################# START App.config or Web.config #################&lt;br /&gt;&lt;br /&gt;    &lt;system.serviceModel&gt;&lt;br /&gt;    &lt;services&gt;&lt;br /&gt;         &lt;service name="HelloWorldWCFService.service1"&gt;&lt;br /&gt;           &lt;endpoint contract="HelloWorldWCFService.IService1" binding="wsHttpBinding"/&gt;&lt;br /&gt;         &lt;/service&gt;&lt;br /&gt;       &lt;/services&gt;&lt;br /&gt;    &lt;/system.serviceModel&gt;&lt;br /&gt;&lt;br /&gt;    ################# END App.config or Web.config #################&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;namespace WCFService&lt;br /&gt;{&lt;br /&gt;    // You have created a class library to define and implement your WCF service.&lt;br /&gt;    // You will need to add a reference to this library from another project and add &lt;br /&gt;    // the code to that project to host the service as described below.  Another way&lt;br /&gt;    // to create and host a WCF service is by using the Add New Item, WCF Service &lt;br /&gt;    // template within an existing project such as a Console Application or a Windows &lt;br /&gt;    // Application.&lt;br /&gt;&lt;br /&gt;    [ServiceContract()]&lt;br /&gt;    public interface IService1&lt;br /&gt;    {&lt;br /&gt;        [OperationContract]&lt;br /&gt;        string MyOperation1(string myValue);&lt;br /&gt;        [OperationContract]&lt;br /&gt;        string MyOperation2(DataContract1 dataContractValue);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class service1 : IService1&lt;br /&gt;    {&lt;br /&gt;        public string MyOperation1(string myValue)&lt;br /&gt;        {&lt;br /&gt;            return "Hello: " + myValue;&lt;br /&gt;        }&lt;br /&gt;        public string MyOperation2(DataContract1 dataContractValue)&lt;br /&gt;        {&lt;br /&gt;            return "Hello: " + dataContractValue.FirstName;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    [DataContract]&lt;br /&gt;    public class DataContract1&lt;br /&gt;    {&lt;br /&gt;        string firstName;&lt;br /&gt;        string lastName;&lt;br /&gt;&lt;br /&gt;        [DataMember]&lt;br /&gt;        public string FirstName&lt;br /&gt;        {&lt;br /&gt;            get { return firstName; }&lt;br /&gt;            set { firstName = value; }&lt;br /&gt;        }&lt;br /&gt;        [DataMember]&lt;br /&gt;        public string LastName&lt;br /&gt;        {&lt;br /&gt;            get { return lastName; }&lt;br /&gt;            set { lastName = value; }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 21:00:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2579</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>Windows Powershell Cmdlet - Creation and Registration</title>
      <link>http://snippets.dzone.com/posts/show/2578</link>
      <description>Adapted from "Windows PowerShell Programmer's Guide", at http://windowssdk.msdn.microsoft.com/en-us/library/ms714674.aspx&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// Reference added to "System.Management.Automation.dll" in "C:\Program Files\Windows PowerShell\v1.0":&lt;br /&gt;using System.Management.Automation;&lt;br /&gt;&lt;br /&gt;namespace CmdletTest&lt;br /&gt;{&lt;br /&gt;    [Cmdlet(VerbsCommon.Get, "Hjnntaao")]&lt;br /&gt;    public class GetHjnntaaoCommand : Cmdlet&lt;br /&gt;    {&lt;br /&gt;        // BeginProcessing - pre-processing/set-up&lt;br /&gt;&lt;br /&gt;        // If accepts pipeline input, must override ProcessRecord and, optionally, EndProcessing&lt;br /&gt;&lt;br /&gt;        // If does not take pipline input, should override EndProcessing&lt;br /&gt;&lt;br /&gt;        // Cmdlets should never call WriteLine, or equivalent.&lt;br /&gt;        protected override void ProcessRecord()&lt;br /&gt;        {&lt;br /&gt;            Process[] processes = Process.GetProcesses();&lt;br /&gt;&lt;br /&gt;            &lt;br /&gt;            WriteObject(processes);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    /* &lt;br /&gt;       This method of registering the Cmdlet with PowerShell extends the shell using snap-ins. This should&lt;br /&gt;       be the default way of registering Cmdlets. The other way, registering with a custom shell,&lt;br /&gt;       should only be used to create executable files.&lt;br /&gt;     &lt;br /&gt;       To install:&lt;br /&gt;       1) installutil CmdletTest.dll&lt;br /&gt;       2) Verify the snap in is in the list of registered snap-ins awaiting to be loaded by&lt;br /&gt;          running "get-pssnapin -registered"&lt;br /&gt;       3) Add the snap in to the shell by using "add-pssnapin "GetHjnntaaoPSSSnapIn01"&lt;br /&gt;     */&lt;br /&gt;    [RunInstaller(true)]&lt;br /&gt;    public class GetHjnntaaoPSSnapIn01 : PSSnapIn&lt;br /&gt;    {&lt;br /&gt;        public override string Name&lt;br /&gt;        {&lt;br /&gt;            get { return "GetHjnntaaoPSSnapIn01"; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public override string Vendor&lt;br /&gt;        {&lt;br /&gt;            get { return "HjnntaaoCorp"; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public override string Description&lt;br /&gt;        {&lt;br /&gt;            get { return "Hjnntaao's Cmdlet"; }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 10 Sep 2006 18:58:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2578</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>C# - XML serialization from string</title>
      <link>http://snippets.dzone.com/posts/show/2552</link>
      <description>From http://weblogs.asp.net/whaggard/archive/2006/09/04/String-Streams-in-.Net.aspx&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;xmlSerializer.Deserialize(new StringReader(xmlString));&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 06 Sep 2006 00:18:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2552</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>c# IsNumeric method</title>
      <link>http://snippets.dzone.com/posts/show/2542</link>
      <description>From Subtext source code - http://subtextproject.com/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public static bool IsNumeric(string text)&lt;br /&gt;{&lt;br /&gt;    return Regex.IsMatch(text,"^\\d+$");&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Sep 2006 01:30:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2542</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>MVP - Composition overview</title>
      <link>http://snippets.dzone.com/posts/show/2541</link>
      <description>Adapted from code supporting article at http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;public partial class ViewCustomers : System.Web.UI.Page, IViewCustomerView&lt;br /&gt;{&lt;br /&gt;	private ViewCustomerPresenter presenter;&lt;br /&gt;&lt;br /&gt;	protected override void OnInit(EventArgs e)&lt;br /&gt;    	{&lt;br /&gt;        	base.OnInit(e);&lt;br /&gt;        	presenter = new ViewCustomerPresenter(this);&lt;br /&gt;        	this.customerDropDownList.SelectedIndexChanged += delegate&lt;br /&gt;                                                                  {&lt;br /&gt;                                                                  	presenter.DisplayCustomerDetails();&lt;br /&gt;                                                              	  };&lt;br /&gt;    	}&lt;br /&gt;&lt;br /&gt;	// ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class ViewCustomerPresenter&lt;br /&gt;{&lt;br /&gt;	private readonly IViewCustomerView view;&lt;br /&gt;	private readonly ICustomerTask task;&lt;br /&gt;&lt;br /&gt;	public ViewCustomerPresenter(IViewCustomerView view) : this(view, new CustomerTask()) {}&lt;br /&gt;&lt;br /&gt;        public ViewCustomerPresenter(IViewCustomerView view, ICustomerTask task)&lt;br /&gt;        {&lt;br /&gt;            this.view = view;&lt;br /&gt;            this.task = task;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void DisplayCustomerDetails()&lt;br /&gt;        {&lt;br /&gt;            int? customerId = SelectedCustomerId;&lt;br /&gt;&lt;br /&gt;            if (customerId.HasValue)&lt;br /&gt;            {&lt;br /&gt;                CustomerDTO customer = task.GetDetailsForCustomer(customerId.Value);&lt;br /&gt;                UpdateViewFrom(customer);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private void UpdateViewFrom(CustomerDTO customer)&lt;br /&gt;        {&lt;br /&gt;            view.CompanyName = customer.CompanyName;&lt;br /&gt;            view.ContactName = customer.ContactName;&lt;br /&gt;            view.ContactTitle = customer.ContactTitle;&lt;br /&gt;            view.Address = customer.Address;&lt;br /&gt;            view.City = customer.City;&lt;br /&gt;            view.Region = customer.Region;&lt;br /&gt;            view.Country = customer.CountryOfResidence.Name;&lt;br /&gt;            view.Phone = customer.Phone;&lt;br /&gt;            view.Fax = customer.Fax;&lt;br /&gt;            view.PostalCode = customer.PostalCode;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	// ...&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class CustomerTask : ICustomerTask&lt;br /&gt;{&lt;br /&gt;	...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Sep 2006 01:22:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2541</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>ADO.NET - Succinctly populating a DataTable</title>
      <link>http://snippets.dzone.com/posts/show/2540</link>
      <description>From code supporting article at http://msdn.microsoft.com/msdnmag/issues/06/08/DesignPatterns/default.aspx &lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;    public class DatabaseGateway&lt;br /&gt;    {&lt;br /&gt;        public DataTable QueryForDataTable(string expression)&lt;br /&gt;        {&lt;br /&gt;            using (IDatabaseConnection connection = new DatabaseConnection())&lt;br /&gt;            {&lt;br /&gt;                DataTable table = new DataTable();&lt;br /&gt;&lt;br /&gt;                using (IDataReader reader = connection.CreateCommandFor(expression).ExecuteReader())&lt;br /&gt;                {&lt;br /&gt;                    table.Load(reader);&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                return table;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public class DatabaseConnection : IDatabaseConnection&lt;br /&gt;    {&lt;br /&gt;        public IDbCommand CreateCommandFor(string dynamicSqlExpression)&lt;br /&gt;        {&lt;br /&gt;            IDbCommand command = underlyingConnection.CreateCommand();&lt;br /&gt;            command.CommandType = CommandType.Text;&lt;br /&gt;            command.CommandText = dynamicSqlExpression;&lt;br /&gt;            return command;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void Dispose()&lt;br /&gt;        {&lt;br /&gt;            underlyingConnection.Close();&lt;br /&gt;            underlyingConnection.Dispose();&lt;br /&gt;            underlyingConnection = null;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        // ...&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Sep 2006 01:12:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2540</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
    <item>
      <title>Domain object interface</title>
      <link>http://snippets.dzone.com/posts/show/2534</link>
      <description>&lt;code&gt;&lt;br /&gt;public interface IDomainObject&lt;br /&gt;{             &lt;br /&gt;    int Id{get;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 05 Sep 2006 00:51:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2534</guid>
      <author>hjnntaao (hjnntaao)</author>
    </item>
  </channel>
</rss>
