Showing posts with label ninject. Show all posts
Showing posts with label ninject. Show all posts

Monday, February 11, 2013

What did you do this weekend?


Well it's been quite a full weekend!

During the work week:

  • Learned about WebAPI's IQueryable possibilities
  • Found out they changed the ruling on Entity Framework
    • We can now select straight into POCOs instead of only anonymous types or EF types
    • Still can't find any articles, blog posts, or announcements about this change.
  • Forced EF to allow me to do queries that span linked databases.
  • Used Knockout to consume the OData (or WebApi IQueryable)
    • complete with a user friendly search form
  • Explored PhantomJs for headless browser automation and testing
  • Updated my win forms tool for scraping data from our Stash and Crucible
  • Explored some SpecFlow

What did you do?

Monday, December 31, 2012

ClickOnce Dynamically Loaded assemblies + ninject

ClickOnce deployment of my winforms dev tool was failing to the dba team because Microsoft.Web.Administration wasn't installed. Is this feature required for the tool to be useful? Not at all. Did MEF, and it wasn't helpful at all. How do you get MEF assemblies deployed for the clients to be able to pick them up? Read about Dynamically loaded assemblies via ClickOnce. Ah the magic is happening now.
[SecurityPermission(SecurityAction.Demand, ControlAppDomain = true)]
  class DynamicDownload
  {
    // Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample, 
    // but will be important in real-world applications where a feature is spread across multiple DLLs, 
    // and you want to download all DLLs for that feature in one shot. 
    Dictionary<String, String> DllMapping = new Dictionary<String, String>();

    public DynamicDownload()
    {
      DllMapping["Domain.WebAdministration"] = "Domain.WebAdministration";
      AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
    }

    System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
      Assembly newAssembly = null;

      if(!ApplicationDeployment.IsNetworkDeployed)
      {
        return null; //this was required to debug locally, otherwise reactive was failing to load
        throw new FileLoadException("Cannot load assemblies dynamically - application is not deployed using ClickOnce");
      }
      var deploy = ApplicationDeployment.CurrentDeployment;

      var nameParts = args.Name.Split(',');
      var dllName = nameParts[0];
      string downloadGroupName = DllMapping[dllName];
      try
      {
        deploy.DownloadFileGroup(downloadGroupName);
      }
      catch (DeploymentException de)
      {
        MessageBox.Show("Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
        throw;
      }

        newAssembly = Assembly.LoadFile(Application.StartupPath + @"\" + dllName + ".dll");
      
      return newAssembly;
    }
  }
static void RegisterDynamicAssemblies()
    {
      Kernel.Bind<DynamicDownload>().ToSelf().InSingletonScope(); //this should never run twice
      var dd=Kernel.Get<DynamicDownload>(); 

      Kernel.Bind<Func<string, IAdministerIIS>>().ToMethod(context => s => new IISAdministration(s));
    }
And now that feature is dynamically loaded the first time the feature is attempted to be used.

Friday, January 13, 2012

Stand up Ninject on MVC against EF4

Here's the bindings I used to get it done


    /// <summary>
    
    /// Load your modules or register your 
services here
    
    /// </summary>
    
    /// <param name="kernel">The kernel.</param>
    
private static void RegisterServices(IKernel kernel)
    {
      
  kernel.Bind<string>()
        .ToMethod(f =>
#pragma warning disable 0618 //disable obsolete warning as this is the ONLY place this method should be used in the application
          
  ProjectDb.Adapter.EfExtensions.CreateConnectionString(
#pragma warning restore 0618
          "data source=databaseSource;initial 
catalog=YourDbName;integrated 
security=True;multipleactiveresultsets=True;App=EntityFramework"))
      .Named("efString");

      
kernel.Bind(typeof(Project.Shared.Behaviors.IRepository<>))
  .To<ProjectDb.Adapter.DEVEntities>()
  .InThreadScope()
        
  .WithConstructorArgument("connectionString",kernel.Get<string>("efString"));


    }