I am Using Entity Framework 4.0 calling the below mentioned code from asp.net. I have a address table in which I need to insert some data. my code is :
IRepository<Address> addressRepository;
int addressHOCODE = 0;
try
{
**addressRepository = ObjectFactory.GetInstance<IRepository<Address>>();**
addressRepository.Add(address);
addressRepository.SaveChanges();
addressHOCODE = address.HOCODE;
}
catch ...
At the addressRepository = ObjectFactory.GetInstance<IRepository<Address>>(); line, we're getting the following error.
StructureMap Exception Code: 202 No
Default Instance defined for
PluginFamily
Domain.IRepository`1[[Data.Address,
DataAccessLayerNew, Version=1.0.0.0,
Culture=neutral,
PublicKeyToken=null]],
DataAccessLayerNew, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
Looks like you worked this out for yourself, but to help others who might come across this page, I'd expect to see something like this in the Global.asax.cs file:
using System;
namespace Host
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start (object sender, EventArgs e) {
ObjectFactory.Configure(config =>
{
config.For<IRepository>().Use<ConcreteRepository>();
});
}
}
}
Related
I am attempting to mix F# with C#.
In my C# Dependency property, I need the type of e.NewValue -- which is a supplied object from F#/Elmish.WPF
e.NewValue.GetType() returns:
{Name = "ViewModel2" FullName = "Elmish.WPF.ViewModel2[[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]"} System.Type {System.RuntimeType}
which tells me nothing.
How do you get the class/module type of an object from F#/Elmish.WPF
Thank you.
TIA
As full disclosure, this comes from:
public static readonly DependencyProperty VisitProperty =
DependencyProperty.Register("Visit", typeof(IInnerRow), typeof(DataGridAnnotationControl), new PropertyMetadata(null, (s, e) =>
{
var sender = s as DataGridAnnotationControl;
var visit = (IInnerRow)e.NewValue;
if (visit != null)
sender.LastName = visit.LastName;
}));
However, I don't see how to cast the NewValue from Elmish, so was hoping the GetType() would shed some light on this.
The type is Elmish.WPF.ViewModel<,>. Its access scope is internal, so you don't have access to any stronger type than Object.
I have some trouble, i need to initialize some RouteGuard:
RouteGuard.php
namespace Acl\Guard;
class RouteGuard {
public function __construct(EntityManager $em)
{
}
}
Module.php
.........
class Module {
public function onBootstrap(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach($sm->get('di')->get('Acl\Guard\RouteGuard'));
}
.....
And i get this Exceptions:
Fatal error: Uncaught exception 'Zend\Di\Exception\RuntimeException' with message 'Invalid instantiator of type "NULL" for "Doctrine\ORM\EntityManager".' in [projectDir]/vendor/zendframework/zendframework/library/Zend/Di/Di.php on line 767
Zend\Di\Exception\RuntimeException: Invalid instantiator of type "NULL" for "Doctrine\ORM\EntityManager". in [projectDir]/vendor/zendframework/zendframework/library/Zend/Di/Di.php on line 298
Zend\Di\Exception\MissingPropertyException: Missing instance/object for parameter entityManager for Acl\Service\AclService::__construct in [projectDir]/vendor/zendframework/zendframework/library/Zend/Di/Di.php on line 767
I understand that i try to get(Doctrine\ORM\EntityManager) and DI tries to create new instance and falls with error because EntityManager has a protected constructor. I understand that EntityManager should be instantiated with static method ::create, but i see that DoctrineORMModule already have an instance of Doctrine\ORM\EntityManager with configuration for Doctrine so how could i get this instance in Bootstrap with DI ?
Thanks, in advance.
I think the best way to get the entity manager in the RouteGuard class would be to implement the ServiceLocatorAwareInterface and then always retrieve the RouteGuard class from the Service locator.
RouteGuard.php
namespace Acl\Guard;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManagerAwareTrait;
class RouteGuard implements ServiceManagerAwareInterface {
use ServiceLocatorAwareTrait;
public function __construct()
{
}
public function getEntityManager()
{
return $this->getServiceManager()->get('Doctrine\ORM\EntityManager');
}
}
Then in RouteGuard you can call $this->getEntityManager() to retrieve the entity manager.
I'm currently working through this code snippet from a design patterns book:
public static class DomainEvents
{
public static IDomainEventHandlerFactory DomainEventHandlerFactory { get; set; }
public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
DomainEventHandlerFactory.GetDomainEventHandlersFor(domainEvent).ForEach(h => h.Handle(domainEvent));
}
}
This deals with wiring up DomainEvents, and this particular code snippet is responsible allowing me to raise an event via the Raise method on DomainEvents.
Here is the code in my Bootstrapper file:
public class ControllerRegistry : Registry
{
public ControllerRegistry()
{
ForRequestedType<IDomainEventHandlerFactory>().TheDefault.Is.OfConcreteType<StructureMapDomainEventHandlerFactory>();
ForRequestedType<IDomainEventHandler<OrderSubmittedEvent>>().AddConcreteType<OrderSubmittedHandler>();
}
}
When I go to access DomainEvents.Raise from my Service layer (in this example, I'm raising the OrderSumittedEvent), DomainEvents is null (when it should be set via StructureMap):
public class OrderService
{
public void Create(Order order)
{
DomainEvents.Raise(new OrderSubmittedEvent() { Order = order });
}
}
Unless I explicitly set the DomainEvents.DomainEventHandlerFactory by hand to StructureMapDomainEventHandlerFactory like this:
public class OrderService
{
public void Create(Order order)
{
// this is the manual assignment I have to make into the DomainEventHandlerFactory of
// the static DomainEvents class. Basically, this is what StructureMap should take care
// of for me, but is not.
DomainEvents.DomainEventHandlerFactory = new StructureMapDomainEventHandlerFactory();
DomainEvents.Raise(new OrderSubmittedEvent() { Order = order });
}
}
Below is the output of StrucutureMap using .WhatDoIHave(), and it appears that StructureMap does have a configured instance of StructureMapDomainEventHandlerFactory for type IDomainEventHandlerFactory. Here is the dump:
================================================================================================================================================================================================================================================================================================================================================================================================
PluginType Name Description
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Func`1<TResult> (Func`1<TResult>)
Scoped as: Transient
311731d7-60c7-46de-9ef4-24608f21df04
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IDomainEventHandlerFactory (DE.Infrastructure.Domain.Events.IDomainEventHandlerFactory) dbcb010c-fa92-4137-85b8-161ab17c2c98 Configured Instance of DE.Infrastructure.Domain.Events.StructureMapDomainEventHandlerFactory, DE.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient
dbcb010c-fa92-4137-85b8-161ab17c2c98 Configured Instance of DE.Infrastructure.Domain.Events.StructureMapDomainEventHandlerFactory, DE.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IDomainEventHandler`1<OrderSubmittedEvent> (IDomainEventHandler`1<OrderSubmittedEvent>) DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Configured Instance of DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Scoped as: Transient
DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Configured Instance of DE.Services.DomainEventHandlers.OrderSubmittedHandler, DE.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
IContainer (StructureMap.IContainer) e24f9e45-caaf-48b6-89f7-d8125310102a Object: StructureMap.Container
Scoped as: Transient
e24f9e45-caaf-48b6-89f7-d8125310102a Object: StructureMap.Container
================================================================================================================================================================================================================================================================================================================================================================================================
I've used StructureMap, but I haven't used Setter Injection, nor have I had to deal with using StructureMap with static classes (if that even makes sense), so I'm a little lost as to why this code won't work.
Is it possible to use Setter Injection with a static implementation of a class like this?
Am I not using StructureMap properly?
Should StructureMap be responsible for creating the DomainEvents class as a Singleton, and I can get rid of the static implementation?
Thanks,
Mike
Is it possible to use Setter Injection with a static implementation of a class like this?
No. It is only possible with createable classes.
Am I not using StructureMap properly?
You are not. Dependency injection deals with creating and providing instances. By definition static classes are non-createable in code and therefore, non-injectable.
Should StructureMap be responsible for creating the DomainEvents class as a Singleton, and I can get rid of the static implementation?
It looks like a core feature of this design is to use a single instance so the event is only fired once. I am not sure what design pattern you are going for, but in many cases you can substitute Action<T> or Func<T> for events, and if you use the singleton lifestyle properly you can probably register a single instance that can be shared between all of your classes rather than resorting to a static class.
If the static event is absolutely required, you can inject an instance of IDomainEventHandlerFactory in your application startup code by resolving the instance and setting it manually after you have completed your registration code (in your composition root).
// Provide the DI container with configuration
var container = DIConfig();
// Set the static event
DomainEvents.DomainEventHandlerFactory = container.Resolve<IDomainEventHandlerFactory>();
To ensure your application doesn't abuse the setter by calling it more than once or using it before it is set, you can put some guard clauses in your static class.
public static class DomainEvents
{
private static IDomainEventHandlerFactory domainEventHandlerFactory;
public static IDomainEventHandlerFactory DomainEventHandlerFactory
{
get
{
return domainEventHandlerFactory;
}
set
{
if (value == null)
throw new ArgumentNullException("value");
if (domainEventHandlerFactory != null)
throw new ArgumentException("DomainEventHandlerFactory is already set and cannot be set again.");
domainEventHandlerFactory = value;
}
}
public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
ThrowIfNotInitialized()
DomainEventHandlerFactory.GetDomainEventHandlersFor(domainEvent).ForEach(h => h.Handle(domainEvent));
}
private static void ThrowIfNotInitialized()
{
if (domainEventHandlerFactory == null)
{
throw new MvcSiteMapException("Not initialized. You must set DomainEventHandlerFactory at application start.");
}
}
}
First off, here's the code:
Binding in the NinjectControllerFactory
class MrBigglesworthServices : NinjectModule
{
public override void Load()
{
Bind<IAuthenticationRepository>()
.To<AuthenticationRepository>()
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["VoiceDB"].ConnectionString
);
Bind<IAppRepository>()
.To<AppRepository>()
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["SessionStore"].ConnectionString
);
}
}
Constructor for the Search Controller:
private IAppRepository appRepository;
public SearchController(IAppRepository appRepository)
{
this.appRepository = appRepository;
}
Based on what I've seen with online examples, this should be enough, but for some reason, it's bringing up the error mentioned above. Any suggestions? Please and thank you.
Because you mention using a NinjectControllerFactory I think you are using an incorrect implementation. Consider to switch to https://github.com/ninject/ninject.web.mvc/wiki/MVC3 instead. This is a widely used integration of Ninject and MVC3.
I have a SysMsgManager class defined in CoreService project as following:
public class SysMsgManager
{
private ISysMsgRepository _SysMsgRepository;
public SysMsgManager()
{
_SysMsgRepository = ObjectFactory.GetInstance<ISysMsgRepository>();
}
....
}
In my DataAccess project I have 'ISysMsgRepository' interface and two concrete implementations defined as following:
namespace DataAccess.Repository
{
[Pluggable("Default")]
public class SysMsgRepository : ISysMsgRepository
{
...
}
}
namespace DataAccess.Repository
{
[Pluggable("Stub")]
public class SysMsgRepository_Test : ISysMsgRepository
{
...
}
}
and this is what I have in my StructureMap.config file
<StructureMap>
<Assembly Name="CoreService" />
<Assembly Name="DataAccess" />
<PluginFamily
Assembly="DataAccess"
Type="DataAccess.Repository.ISysMsgRepository"
DefaultKey="Default" />
</StructureMap>
When I try to run my app, I got the following error:
StructureMap Exception Code: 202\nNo Default Instance defined for PluginFamily DataAccess.Repository.ISysMsgRepository, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Can anyone help me to solve this problem? Thanks!
Unfortunately I have little familiarity with configuring StructureMap via Xml. Let me show you how it is done using C#.
var container = new Container(config=>
{
config.For<ISysMsgRepository>().Use<SysMsgRepository>();
});
It seems you are using the standard naming convention for your interfaces and classes (just tacking an I onto the front of the class name). If you do that for all your types you can just configure your container like this:
var container = new Container(config=>
{
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
});
I hope this helps. It is much easier to configure your container using code rather than Xml. Give it a try. You'll be a convert.