My main application contains a ClassA. The main application then loads a module and in that module I would like would to do:
var classA:InterfaceClassA = new ClassA();
It compiles fine but I get this warning:
Warning: YourApplication is a module or application that is directly referenced. This will cause YourApplication and all of its dependencies to be linked in with module.YourModule:Module. Using an interface is the recommended practice to avoid this.
I can't use an interface to generate the new instance, so what is the correct way to do this?
I found an answer in Accessing the parent application from the modules . I created a helper class in the main Application that contains instances of the classes I want to access. In the module I then use:
parentApplication.myModuleHelper.**myClassInstance**.myMethod();
for instance methods and for static class level methods I use:
parentApplication.myModuleHelper.**MyClassInstance**.myMethod()
To get an instance of my class in a module I use this in MyModuleHelper
public function getFullScreenUtil(iFullScreenUtil:IFullScreenUtil , iSystemManager:ISystemManager):FullScreenUtil {
return new FullScreenUtil(iFullScreenUtil , iSystemManager);
}
and this in MyModule:
var _fullScreenUtil:* = parentApplication.moduleHelper.getFullScreenUtil(this , systemManager);
which is all I need for now. I am not sure how I could cast the result of getFullScreenUtil(..) to an actual instance of FullScreenUtil but I expect that it can not be done in modules. Maybe using an interface would provide the solution to that.
Related
I'm contributing to a package that will provide some blade components. So, the users of this package may use the components on a blade template as:
<x-mypackage-component-a/>
The components are located under the src/Components folder of my package. These components are loaded in the package service provider using the loadViewComponentsAs() method as explained here:
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
...
]);
Now, I need to make some tests for phpunit that should check that the components are loaded by the package service provider, something like next:
public function testComponentsAreLoaded()
{
$this->assertTrue(/*code that check 'x-mypackage-component-a' exists*/);
}
Is there any way (using the Laravel framework) to check a blade component name exists and/or is loaded?
I have manage to do something similar for a set of blade views provided by the package with next code:
// Views are loaded on the package service provider as:
$this->loadViewsFrom($viewsPath, 'mypackage');
// The phpunit test method is:
public function testViewsAreLoaded()
{
$this->assertTrue(View::exists('mypackage::view-a'));
$this->assertTrue(View::exists('mypackage::view-b'));
...
}
Thanks in advance!
Finally managed to find a way to solve this, I'm going to explain this because it may be useful for other readers. First, you need to load the set of views that are used by the component classes (the ones you usually use on the render() method). In my particular case the component views are located on the resources/components folder, so I had to insert next code on the boot() method of my package's service provider:
// Load the blade views used by the components.
$viewsPath = $this->packagePath('resources/components');
$this->loadViewsFrom($viewsPath, 'mypackage');
Where packagePath() is a method that returns the fully qualified path (from the package root folder) to the received argument.
Next, again in the boot() method, I had to load the components as explained on the question:
$this->loadViewComponentsAs('mypackage', [
Components\ComponentA::class,
Components\ComponentB::class,
...
]);
Finally, in order to make a test that asserts the views and the components are loaded correctly by the service provider, I have created the next method to be used with phpunit:
public function testComponentsAreLoaded()
{
// Check that the blade component views are loaded.
$this->assertTrue(View::exists('mypackage::component-a'));
$this->assertTrue(View::exists('mypackage::component-b'));
...
// Now, check that the class components aliases are registered.
$aliases = Blade::getClassComponentAliases();
$this->assertTrue(isset($aliases['mypackage-component-a']));
$this->assertTrue(isset($aliases['mypackage-component-b']));
...
}
As an additional information, I must say that my phpunit test classes inherits from Orchestral/testbench TestCase class, and you may need to include the View and Blade facades on your test file. I'm also using the next method to ensure the boot() method of my package's service provider executes on my test environment before running the tests:
protected function getPackageProviders($app)
{
return ['Namespace\To\MyPackageServiceProvider'];
}
there is no method or helper for checking that a component exists or not but from then blade components are class in laravel, so you can check that your specific component class exists or not:
// application namespaces
namespace App\View\Components;
use Illuminate\View\Component;
// define component
class mypackage extends Component { ... }
// check component
public function testViewsAreLoaded(){
$this->assertTrue(class_exists('\Illuminate\View\Component\mypackage'));
...
}
Is there a way to create objects in HTTP module and pass those objects to applications.
I can use HTTPContext.Items. But that means I will reference System.Web in any DLL in the application that will use those data. Utility.dll is reading data generated by the http module, and I don't want to reference System.Web in that DLL because it is used by both web and desktop applications.
of course Desktop applications will not find the object, this is not a problem.
You can use the new and extensible cache API in System.Runtime.Caching, namely, the MemoryCache class. Just use a shared constant as the name and you're done.
I think your web module should communicate with Utility.dll and not the other way around. It should call a function inside Utility.dll with the data it needs. Since we don't know much about the nature of your application and that you didn't provide much information about how you want your different modules to communicate, it hard to give a definite answer.
The Utility.dll module could have a method accepting data like
public void QueueProcessingData(YourDataType[] data) {/* ... */}
If you really want your Utility.dll module to pull data from the web server, then you could use the builtin cache like #Ricardo Peres said. Example of using the builtin MemoryCache:
var data = "object your web module created and you want to pass to utility.dll";
ObjectCache cache = MemoryCache.Default;
cache.Add("The name you want", data, new CacheItemPolicy { AbsoluteExpiration = DateTime.MaxValue });
That way your other modules don't have to reference System.Web
HttpContext.Items is an IDictionary, so you can avoid a System.Web dependency in your common code by depending on that abstraction:
Utility.dll
class Util
{
static void DoStuff(IDictionary environment) { ... }
}
App.dll
class App
{
void DoStuff()
{
Util.DoStuff(new Hashtable { { "Foo", "Bar" } });
}
}
Web.dll
class MyModule : IHttpModule
{
void Init(HttpApplication context)
{
Util.DoStuff(context.Context.Items);
}
}
Thanks to #user1429080. it is kind of simple DI pattern.
Add an interface to Util.dll.
Add a reference to it in class Util add a property to access it.
The interface is implemented in WebUtil.dll. WebUtil is referencing System.Web.
In HttpModule.Init() I assign the implementation to the interface.
Util class is using the interface to read data generated from HttpModule instead of accessing System.Web.
Now I can have another source of data by creating another implementation for the interface.
I am loading a type from an external assembly and want to create an instance of the type. However, this type/class is setup for constructor injection by objects currently being managed/bound by Ninject. How can I use Ninject to create an instance of this type and inject any constructor dependencies?
Below is how I get this type.
Assembly myAssembly = Assembly.LoadFrom("MyAssembly.dll");
Type type = myAssembly.GetType("IMyType");
Assuming you've created a Kernel, you should be able to create and have it resolved via:
kernel.Get(type)
.... then I read the question.... Assuming MyAssembly.dll has an implementation of IMyType, you need (in your main assembly) :-
kernel.Load( "MyAssembly.dll")
And in your dynamically loaded assembly:-
public class Module : StandardModule
{
public override void Load()
{
Bind<IMyType>().To<MyType>();
}
}
And dont forget to see if MEF is the answer here, as you dont want to go writing reams of explicit plugin management and/or detection logic if you can help it (but if you're just doing straightforward stuff and are only doing the Assembly.LoadFrom() for the purposes of asking the question, you're probably still in Ninject's sweet spot.
Ditto, if you actually need to resolve an interface via Assembly.GetType(), you probably should be using something like dynamic to do the late binding you'll probably have to do (and before you know it you should be using a dynamic language or hosting a scriopting language)
i'm trying to access an mxml component from my external as file. e.g
main.mxml:<br>
<code>[mx:text id="myText" />]</code>
file.as:<br>
<code>var mainM:main = new main();
mainM.text.visible = true;</code>
I get the following error:
[TypeError: Error #1009: Cannot access a property or method of a null object reference]
Any suggestions on how to approach it better.
The ID of your component instance becomes a member of your application and can easy be accessed like so
import mx.core.Application;
mx.core.Application.application.myText.visible = true;
An additional answer is that when you create a new Flex component (new myFlexComponent()), the child UI components are not created until a CREATION_COMPLETE call is invoked, indicating the component is fully created. In the case of application, there is only one, and its automatically created by the framework, and referenced by (Application.application) as stated above.
For example, if your variable was a simple class variable (e.g. myDate:Date), you could access it via the above syntax
I am new to the structure map but i want to use it in my asp.net site for dependency injection
can any one suggest me simple example to use structure map for the dependency injection
you will need to do something like this:-
StructureMapConfiguration
.ForRequestedType<IResourceA>()
.TheDefaultIsConcreteType<ResourceB>()
.CacheBy(InstanceScope.Singleton);
This tells StructureMap to inject ResourceB when there is a request for ResourceA.
Structure Map
You can configure programatically or via configuration file.
Programatical example (there are other ways):
StructureMap.StructureMapConfiguration.ForRequestedType<ISomething>().TheDefaultIsConcreteType<ConcreteSomething>();
then you can get an instance of the configured type using this sort of code:
//The concrete type will be ConcreteSomething
ISomething instance = ObjectFactory.GetInstance<ISomething>();
You can do it in a config file:
<StructureMap MementoStyle="Attribute">
<DefaultInstance PluginType="Blah.ISomething, Blah.SomethingDLL" PluggedType="Blah.Concrete.ConcreteSomething,Blah.ConcreteDLL"/>
</StructureMap>
and in the main method or Global.asax you can set this config by saying:
StructureMap.ObjectFactory.Initialize(x => { x.PullConfigurationFromAppConfig = true; });
and use it the same way as above:
ISomething instance = ObjectFactory.GetInstance<ISomething>();
If the concrete class has a constructor that needs instances injected in it, and you have those configured, the concrete types will get injected by the framework.
There are ways of passing parameters to constructors, dealing with Gereric types, creating named instances that are configured with specific constructor/property values. I use this framework and like it very much.