Beta 2.0


Step by Step example

As an example we will write the program of summation of two numbers.
  1. Begin !
  2. Download the up-to-date release (bin+src+example) of Bluesky framework http://bluesky.codeplex.com/Release/ProjectReleases.aspx
  3. Create new ASP.NET MVC project
  4. Add reference to BluskyCore.dll
  5. Add some interfaces and classes:


ICalcModel.cs
    [Implementor(typeof(CalcModel))]    
    public interface ICalcModel : IModel
    {
        int A { get; set; }
        int B { get; set; }
        int Result { get; }
        string Operation { get; }
    }

CalcModel.cs
    public class CalcModel : ICalcModel
    {
        public int A { get; set; }
        public int B { get; set; }
        public int Result {  get { return A + B; }  }

        public string Operation {  get { return "+"; }  }

        public void Refresh()
        {                        
        }
        public void OnInit(ModelArgumentBase args)
        {
        }        
    }

CalcRequest.cs
    [ModelBinder(typeof(CalcBinder))]
    public class CalcRequest : ModelArgumentBase
    {
        public int? A { get; set; }

        public int? B { get; set; }

        public CalcRequest(int? a, int? b) : base()
        {
            A = a;
            B = b;
        }
    }

CalcBinder.cs
    public class CalcBinder : ModelBinderBase
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            int? a = GetHttpRequestParam<Int32?>(controllerContext, "a", 0);
            int? b = GetHttpRequestParam<Int32?>(controllerContext, "b", 0);
            return new CalcRequest(a, b);
        }
    }

CalcController.cs
    public class CalcController : BlueSkyCore.Controller<CalcFactory>
    {
        public ActionResult Index()
        {
            return View();
        }
      
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Sum(SumRequest sr)
        {

            return ViewByRequest(sr);        
        }
    }

CalcFactory.cs
    public class CalcFactory : ModelFactoryBase
    {
        public CalcFactory()
        {
            Register<CalcRequest>(ProcessCalcRequest);
        }
       
        protected IModel ProcessCalcRequest(CalcRequest arg)
        {
            ICalcModel m = Implementor.Resolve<ICalcModel>();
            
            m.A = arg.A.HasValue ? arg.A.Value : 0;
            m.B = arg.B.HasValue ? arg.B.Value : 0;           
            return m;
        }      
    }

For CalcController:
Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>
    
    <% using (Html.BeginForm("Sum", "Calc", FormMethod.Post ))
       {%>
       A=<%= Html.TextBox("a", "3")%><br />
       B=<%= Html.TextBox("b", "6")%><br />
       
       <input type="submit" value="+" />                            
    <% } %> 
</asp:Content>

Sum.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
Inherits="System.Web.Mvc.ViewPage<ExampleWebSite.Models.ICalcModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	sum
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>sum</h2>
    <p>
        <%= Model.A %><%= Model.Operation %><%= Model.B %>=<%= Model.Result %>
    </p>

</asp:Content>

Run !

Last edited Dec 17, 2009 at 12:45 PM by oakio, version 22

Comments

No comments yet.