Joe

Mister Doctor Professor
posts - 23, comments - 19, trackbacks - 134

Using Binsor in an Asp.Net Application

This tutorial covers using Binsor2 in an asp.net application. 

Because of different versions and the dearth of information available about getting started with Binsor, I would recommend getting the files listed later in this article directly from the trunk as the syntax between versions 1 and 2 has changed.  There is a readme file covering how to build the trunk, and I would add the following points to that:

1.  Run your "Visual Studio Command Line" so that MSBuild is in your path (Start|Programs|Microsoft Visual Studio 200x|Visual Studio Tools)
2.  I had to add a folder called "lib" to "C:\Program Files\Microsoft SDKs\Windows\v6.0A" to get the build to run.

Getting Started

Create a new library called BinsorDemo.Domain, and add the following domain entities (fields made public for brevity).

    public class Account
    {
        public string FirstName;
        public string LastName;
        public string State;
        public decimal Balance;
        public List<Purchase> PurchaseHistory;
    }

    public class Purchase
    {
        public DateTime Date;
        public decimal Amount;
        public decimal AmountDue;

        public Purchase(DateTime date, decimal amount, decimal amountDue)
        {
            Date = date;
            Amount = amount;
            AmountDue = amountDue;
        }
    }

 

Next, add the following interfaces.  These are the services that we will "wire-up" using Binsor.

    public interface ICreditService
    {
        decimal GetCreditRemaining(Account c);
    }

    public interface IInterestCalculator
    {
        decimal CalculateInterestOn(Account account);
    }

Finally, add the following concrete implementations of the interfaces.

    public class CreditService : ICreditService
    {
        private decimal _CreditLimit;

        public CreditService(decimal creditLimit)
        {
            _CreditLimit = creditLimit;
        }

        public decimal GetCreditRemaining(Account account)
        {
            decimal totalOutstanding = 0M;
            account.PurchaseHistory.ForEach(delegate (Purchase p){totalOutstanding += p.Amount - p.AmountDue;});
            return _CreditLimit = totalOutstanding;
        }
    }

    public class InterestCalculator : IInterestCalculator
    {
        private readonly decimal _InStateAmount;
        private readonly decimal _OutOfStateAmount;
        private readonly string _ThisState;

        public InterestCalculator(decimal inStateAmount, decimal outOfStateAmount, string thisState)
        {
            _InStateAmount = inStateAmount;
            _OutOfStateAmount = outOfStateAmount;
            _ThisState = thisState;
        }

        private decimal DetermineInterestRateFor(Account account)
        {
            if (account.State.Equals(_ThisState, StringComparison.InvariantCultureIgnoreCase))
                return _InStateAmount;
            else
                return _OutOfStateAmount;
        }

        public decimal CalculateInterestOn(Account account)
        {
            decimal interestRate = DetermineInterestRateFor(account);
            decimal interest = 0M;

            account.PurchaseHistory.ForEach(delegate (Purchase purchase){interest += purchase.AmountDue*interestRate;});
            return interest;
        }
    }

Create the Web Application

Next create a web application called BinsorDemo.Web.  Add the following references (as mentioned above, I would pull these directly from the trunk - these will be located in [x:\trunk]\rhino-commons\Rhino.Commons\bin\Debug).

Boo.Lang
Boo.Lang.Compiler
Boo.Lang.Parser
Castle.Core
Castle.Windsor
log4net
Rhino.Commons
BnisorDemo.Domain

 

Add a new file to the root of the application called "windsor.boo", and add the following to it.

import BinsorDemo.Domain

creditLimitAmount as decimal = 500.0

component 'credit_service', ICreditService, CreditService:
                        creditLimit=creditLimitAmount


inState as decimal = .05
outOfState as decimal = .075
myState = 'MD'

component 'interest_service', IInterestCalculator, InterestCalculator: 
                            inStateAmount = inState
                            outOfStateAmount = outOfState
                            thisState = "MD"

 

Next add a global code file (global.asax), if it is not already there.  Add the following to this file.  The Application_Start event is where the magic takes place.

 

 public class Global : System.Web.HttpApplication
    {
        private static IWindsorContainer container;

        public static IWindsorContainer Container
        {
            get { return container; }
        }

        protected void Application_Start(object sender, EventArgs e)
        {
            XmlConfigurator.Configure();
            string binsorConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "windsor.boo");
            container = new WindsorContainer();
            BooReader.Read(container, binsorConfigFilePath);
        }
    }

Finally

Let's run a quick test to see if everything is working OK.  Add the following to a WebForm's Page_Load event, and navigate to that page.

    protected void Page_Load(object sender, EventArgs e)
        {
            Account demoAccount = new Account();
            demoAccount.FirstName = "Foo";
            demoAccount.LastName = "Bar";
            demoAccount.Balance = 250;
            demoAccount.State = "MD";

            demoAccount.PurchaseHistory = new List<Purchase>();
            demoAccount.PurchaseHistory.Add(new Purchase(new DateTime(2007, 10, 16), 100M, 15M));
            demoAccount.PurchaseHistory.Add(new Purchase(new DateTime(2007, 11, 16), 10M, 0));
            demoAccount.PurchaseHistory.Add(new Purchase(new DateTime(2007, 12, 16), 250M, 35M));

            Response.Write(Global.Container.Resolve<ICreditService>().GetCreditRemaining(demoAccount));
            Response.Write("<hr/>");
            Response.Write(Global.Container.Resolve<IInterestCalculator>().CalculateInterestOn(demoAccount));
        }
 

Print | posted on Tuesday, January 08, 2008 12:40 PM |

Feedback

Gravatar

# Ass.

Creampie ass. Big tits round ass. Babes kick ass. Hot ass. Big tits and ass. Teach my ass.
8/12/2008 5:27 PM | Kick ass babes.
Gravatar

# Incest sex stories.

Incest taboo. Free erotic incest stories. Incest thumbs. Real incest. Brother sister incest stories. Mom son incest. Incest sex. Father son incest. Incest story.
8/12/2008 7:38 PM | Incest.
Gravatar

# Incest stories.

Free incest stories. Family incest photos. Incest. Incest sex. Incest stories.
8/13/2008 11:30 PM | Incest storys.
Gravatar

# Celebrities pics.

Celeb blogging nude celebrities. Free nude male celebrities. Nude pictures celebrities. Celebrities. Nude celebrities. Naked male celebrities.
8/26/2008 12:07 AM | Celebrities legs.
Gravatar

# Britney spears paris hilton lindsay lohan.

Paris hilton sex video. Free paris hilton sex tape. Paris hilton.
9/16/2008 8:12 PM | Paris hilton sex video.
Gravatar

# Free big boobs pics.

Lanas big boobs. Big boobs.
10/11/2008 3:33 AM | Lanas big boobs.
Gravatar

# Lesbian personals.

Free lesbian photos. Lesbian porno. Free lesbian porn. Lesbian porn.
10/17/2008 10:11 PM | Lesbian action.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 4 and type the answer here:

Powered by: