Skip to content

Commit

Permalink
Implement simple usage with Autofac to sample project
Browse files Browse the repository at this point in the history
  • Loading branch information
bugthesystem committed Dec 23, 2014
1 parent f61e2f8 commit fa74598
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 25 deletions.
41 changes: 41 additions & 0 deletions src/FireSharp.WebApp/App_Start/Bootstrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Web.Mvc;
using System.Web.Routing;
using Autofac;
using Autofac.Integration.Mvc;
using FireSharp.Config;
using FireSharp.Interfaces;
using FireSharp.WebApp.App_Start;

namespace FireSharp.WebApp
{
public static class Bootstrapper
{
const string BASE_PATH = "https://firesharp.firebaseio.com/";
const string FIREBASE_SECRET = "fubr9j2Kany9KU3SHCIHBLm142anWCzvlBs1D977";
public static void Start()
{
AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

var builder = new ContainerBuilder();

// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

builder.Register(context => new FirebaseConfig
{
BasePath = BASE_PATH,
AuthSecret = FIREBASE_SECRET
}).As<IFirebaseConfig>().SingleInstance();

builder.RegisterType<FirebaseClient>().As<IFirebaseClient>().SingleInstance();


var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

}
}
}
22 changes: 19 additions & 3 deletions src/FireSharp.WebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FireSharp.Interfaces;

namespace FireSharp.WebApp.Controllers
{
public class HomeController : Controller
{
private readonly IFirebaseClient _firebaseClient;

public HomeController(IFirebaseClient firebaseClient)
{
_firebaseClient = firebaseClient;
}

public ActionResult Index()
{
return View();
}

public ActionResult CallFirebase()
{
_firebaseClient.Push("chat/", new
{
name = "someone",
text = "Hello from backend :" + DateTime.Now.ToString("f")
});

return RedirectToAction("Index");
}
}
}
13 changes: 13 additions & 0 deletions src/FireSharp.WebApp/FireSharp.WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Autofac">
<HintPath>..\packages\Autofac.3.1.0\lib\net40\Autofac.dll</HintPath>
</Reference>
<Reference Include="Autofac.Integration.Mvc">
<HintPath>..\packages\Autofac.Mvc4.3.1.0\lib\net40\Autofac.Integration.Mvc.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down Expand Up @@ -98,6 +104,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\Bootstrapper.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
Expand Down Expand Up @@ -130,6 +137,12 @@
<ItemGroup>
<Content Include="Views\Home\Index.cshtml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FireSharp\FireSharp.csproj">
<Project>{7613B723-E81B-4C02-A3EC-54F8F02C60F6}</Project>
<Name>FireSharp</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
18 changes: 3 additions & 15 deletions src/FireSharp.WebApp/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using FireSharp.WebApp.App_Start;
using System.Web;

namespace FireSharp.WebApp
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Bootstrapper.Start();
}
}
}
8 changes: 6 additions & 2 deletions src/FireSharp.WebApp/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<link rel="stylesheet" type="text/css" href="https://www.firebase.com/css/example.css">
}

<a href="@Url.Action("CallFirebase")">Send Message From Backed</a>
<br />
<br />

<div id='messagesDiv'></div>
<input type='text' id='nameInput' placeholder='Name'>
<input type='text' id='messageInput' placeholder='Message...'>
Expand All @@ -20,7 +24,7 @@
//dataRef.set("I am now writing data into Firebase!");
// When the user presses enter on the message input, write the message to firebase.
$('#messageInput').keypress(function(e) {
$('#messageInput').keypress(function (e) {
if (e.keyCode == 13) {
var name = $('#nameInput').val();
var text = $('#messageInput').val();
Expand All @@ -30,7 +34,7 @@
});
// Add a callback that is triggered for each chat message.
dataRef.limit(10).on('child_added', function(snapshot) {
dataRef.limit(10).on('child_added', function (snapshot) {
var message = snapshot.val();
$('<div/>').text(message.text).prepend($('<em/>')
.text(message.name + ': ')).appendTo($('#messagesDiv'));
Expand Down
12 changes: 7 additions & 5 deletions src/FireSharp.WebApp/packages.config
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Autofac" version="3.1.0" targetFramework="net45" />
<package id="Autofac.Mvc4" version="3.1.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="4.0.20710.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="1.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="2.0.20715.0" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="2.0.20710.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
</packages>

0 comments on commit fa74598

Please sign in to comment.