Skip to content

Instantly share code, notes, and snippets.

@glennblock
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glennblock/8888901 to your computer and use it in GitHub Desktop.
Save glennblock/8888901 to your computer and use it in GitHub Desktop.
Script packs as script
public class WebApi : IScriptPackContext {
public WebApi(ILog logger, IControllerTypeManager typeManager) {
...
}
}
public class WebApiScriptPack : IScriptPack
{
private readonly ILog _logger;
private readonly IControllerTypeManager _typeManager;
[ImportingConstructor]
public ScriptPack(ILog logger, IControllerTypeManager typeManager)
{
_logger = logger;
_typeManager = typeManager;
}
IScriptPackContext IScriptPack.GetContext()
{
return new WebApi(_logger, _typeManager);
}
void IScriptPack.Initialize(IScriptPackSession session)
{
session.AddReference("System.Net.Http");
var namespaces = new[]
{
"System.Web.Http",
"System.Web.Http.SelfHost",
"System.Web.Http.Dispatcher",
"Owin"
}.ToList();
namespaces.ForEach(session.ImportNamespace);
}
void IScriptPack.Terminate()
{
}
}
ScriptPack<WebApiScriptPack>();
//Code that the author writes, no need to implement an IScriptPack!
ScriptPack<WebApi>()
.References("System.Net.Http")
.Namespaces("System.Web.Http", "System.Web.Http.SelfHost", "System.Web.Http.Dispatcher");
public class WebApi {
public WebApi(ILog logger, IControllerTypeManager typeManager, IRequirer requirer) {
...
}
}
@glennblock
Copy link
Author

Interesting idea here would be if you just use "using" and "#r" instead of the "AddXXX" methods. Need to think about.

@adamralph
Copy link

Does WebApi still have to implement IScriptPackContext?

@filipw
Copy link

filipw commented Feb 12, 2014

yes

@adamralph
Copy link

Even with the scripty way? Why would it need to?

@adamralph
Copy link

BTW - the scripty way looks awesome.

@VirtueMe
Copy link

I would like it to work like this, so I don't need to write the complete path each time.

ScriptPack<WebApi>()
  .References("System.Net.Http")
  .From("System.Web.Http").Use(".", "SelfHost", "Dispatcher");

// or

ScriptPack<WebApi>()
  .References("System.Net.Http")
  .From("System.Web.Http").Using(".", "SelfHost", "Dispatcher");

The "." seems like overkill, but could give a better undestanding of what we try to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment