Skip to content

Web framework idea

irxground edited this page Sep 20, 2012 · 11 revisions

Railsの場合

FooBar::Application.routes.draw do
  root to: 'groups#index'

  resources :groups, only: [:index, :show, :create] do
    collection do
      get 'ranking'
    end
    member do
      get 'info'
    end
    resources :users, only: [:show]
  end
end
class GroupsController
  def index;                     end
  def show;    id = params[:id]; end
  def create;                    end
  def ranking;                   end
  def info;    id = params[:id]; end
end
class UsersController
  def show
    group_id = params[:group_id]
    user_id  = params[:user_id]
  end
end
<%= link_to 'Taro', group_user_path(3, 5) %>

C#でやりたいこと

public class FooBarEngine : ApplicationEngine {
    [Action(HttpMethod.Get, Path="")]
    public Response GetRoot() { return GetGroup().Index(); }

    [Route("groups")]
    public GroupShard GetGroup() { return new GroupShard(); }
}
public class GroupShard : Shard {
    [Action(HttpMethod.Get)]
    public Response Index() { throw new NotImplementedException(); }
    [Action(HttpMethod.Get, Path=":id")]
    public Response Show(int id) { throw new NotImplementedException(); }
    [Action(HttpMethod.Post)]
    public Response Create() { throw new NotImplementedException(); }

    [Action(HttpMethod.Get, Path="ranking")]
    public Response Ranking() { throw new NotImplementedException(); }
    [Action(HttpMethod.Get, Path=":id/info")]
    public Response GetInfo(int id) { throw new NotImplementedException(); }

    [Route(":id/users")]
    public UserShard GetUser(int id) { return new UserShard(id); }
}
public class UserShard : Shard {
    private readonly int _GroupId;
    public UserSard(int groupId) { _GroupId = groupId; }
    [Action(HttpMethod.Get, Path=":id")]
    public Response Show(int id) { throw new NotImplementedException(); }
}
<!--         <a href="/groups/3/users/5">Taro</a>         -->
<%= Html.LinkTo("Taro", e => e.GetGroup.GetUser(3).Show(5) %>

微妙にインデックスの位置がおかしい気がするので、こちらのほうがより良い

<%= Html.LinkTo("Taro", e => e.Group[3].User[5].Show %>

その場合、以下のようにShardを書く必要がある

public class FooBarEngine : ApplicationEngine {
    [Action(HttpMethod.Get, Path="")]
    public Response GetRoot() { return GetGroup().Index(); }

    [Route("groups")]
    public GroupShard Group { get { return new GroupShard(); } }
}
public class GroupShard : Shard {
    [Action(HttpMethod.Get)]
    public Response Index() { throw new NotImplementedException(); }
    [Action(HttpMethod.Post)]
    public Response Create() { throw new NotImplementedException(); }
    [Action(HttpMethod.Get, Path="ranking")]
    public Response Ranking() { throw new NotImplementedException(); }

    [Route(":id")]
    public Member this[int id] { get { return new Member(id); } }

    public class Member : Shard {
        private readonly int _Id;
        public Member(int id) { _Id = id; }

        [Action(HttpMethod.Get)]
        public Response Show() { throw new NotImplementedException(); }
        [Action(HttpMethod.Get, Path="info")]
        public Response GetInfo() { throw new NotImplementedException(); }

        [Route("users")]
        public UserShard User { get { return new UserShard(id); } }
    }
}
public class UserShard : Shard {
    private readonly int _GroupId;
    public UserSard(int groupId) { _GroupId = groupId; }

    [Route(":id")]
    public Member this[int id] { get { return new Member(_GroupId, id); } }

    public class Member : Shard {
        private readonly int _Id;
        private readonly int _GroupId;

        public Member(int id, int groupId) { _Id = id; _GroupId = groupId; }

        [Action(HttpMethod.Get)]
        public Response Show() { throw new NotImplementedException(); }
    }
}
Clone this wiki locally