I have the following BaseClass:
public class BaseRepositorio : IDisposable
{
protected IDbConnection postgresql;
public BaseRepositorio(IDbConnection postgresql)
{
this.postgresql = postgresql;
}
public void Dispose()
{
}
}
What is the proper way to inject the dependency into the BaseClass?
public class RepositorioEmpresa : BaseRepositorio?!?!, IRepositorio<Empresa>
{
}
Inject it in the derived and use base constructor
public class RepositorioEmpresa : BaseRepositorio, IRepositorio<Empresa>
{
public RepositorioEmpresa(IDbConnection connection): base(connection)
{
}
}
Related
I'm trying to create a screen manager for a game where I should be able to open the screens through said manager using a controller classes
I have this:
public interface IScreenController { }
public class UIScreen<T> where T : IScreenController
{
public void Open(T controller) { }
}
public class ScreenManager
{
public T GetScreen<T>()
{
//some screen getter method here
}
public void Open<T, J>(J controller) where T : UIScreen<J> where J : IScreenController
{
var screen = GetScreen<T>();
screen.Open(controller);
}
}
public class MyScreenController : IScreenController { }
public class OtherScreenController: IScreenController { };
public class MyScreen : UIScreen<MyScreenController> { }
and currently, i can make it work with this:
public class SomeClass
{
public ScreenManager Manager;
public void SomeMethod()
{
Manager.Open<MyScreen, MyScreenController>(new MyScreenController());
}
}
is it possible to do only Manager.Open<MyScreen>(Controller) while keeping enforcing the parameter?
I know i can do Open<T>(IScreenController controller) where T: UIScreen, but that would allow me to put any screen controller as a parameter.
I think you're setting yourself up for a world of heartache here trying to use generics like this. Once you're trying to constrain the types you should begin to ask yourself if it's better to just provide function overloads.
I know you've given us abbreviated code, but instead of:
public interface IScreenController { }
public class UIScreen<T> where T : IScreenController
{
public void Open(T controller) { }
}
Why not just put the Open function in the interface? Then you can ditch UIScreen altogether, or if you really need it to implement some functionality that would make inheritance worthwhile then you could make UIScreen an abstract class, like:
public interface IScreenController
{
public void Open(IScreenController controller) { }
}
public abstract class UIScreen : IScreenController
{
public void Open(IScreenController controller) { // functionality goes here }
}
Another thing you could do is to specify the type as a constructor in your inheritance chain, like instead of what you wrote here:
public class UIScreen<T> where T : IScreenController
{
public void Open(T controller) { }
}
public class MyScreenController : IScreenController { }
public class MyScreen : UIScreen<MyScreenController> { }
You could make UIScreen take an IScreenController as part of the constructor argument, and you could pass that when a subclass is constructed, like the following:
public class UIScreen
{
protected IScreenController screenController;
public UIScreen(IScreenController screenController)
{
this.screenController = screenController;
}
}
public class MyScreenController : IScreenController { }
public class MyScreen : UIScreen
{
public MyScreen() : base(new MyScreenController()) { }
}
then you can do whatever needs doing from your protected IScreenController.
Why
public class UIScreen<T> where T : IScreenController
{
public void Open(T controller) { }
}
and not simply
public class UIScreen
{
public void Open(IScreenController controller) { }
}
I have a structure like below. I need ideas on how to clearly record all instances. Do you think Microsoft.Extensions.DependencyInjection library is suitable for these?
public interface IRules
{
void Do();
}
Implementation :
public class MyRules:IRules
{
public void Do()
{
//Something;
}
}
Businnes Interface:
public interface IRulesOperation
{
void Do();
}
Businnes class:
public class RulesOperation:IRulesOperation
{
private readonly IRules rules;
private readonly IConfiguration conf;
public RulesOperation(IRules rules,IConfiguration conf)
{
this.conf=conf;
this.rules = rules;
}
public void Do()
{
rules.Do();
}
}
Main Class:
public class App
{
IRulesOperation rules;
public App(IRulesOperation rules)
{
this.rules=rules;
}
}
I have this class where I'm trying to inject a list of qualifying objects:
public class BlingDispatcher : IBlingDispatcher
{
readonly IEnumerable<IDomainEventHandler> _domainEventHandlers;
#region IBlingDispatcher Members
public BlingDispatcher(IEnumerable<IDomainEventHandler> domainEventHandlers)
{
_domainEventHandlers = domainEventHandlers;
}
}
Classes like this get injected here and work great:
public class NotifyFrontEndSomethingHappened : IDomainEventHandler<SomethingHappened>
{
private readonly IFrontEndNotifier _frontEndNotifier;
public NotifyFrontEndAfterSomethingHappened(IFrontEndNotifier frontEndNotifier)
{
_frontEndNotifier = frontEndNotifier;
}
}
Classes like this do not:
public class NotifyFrontEndAfterEvent : IDomainEventHandler<SomethingHappened>,
IDomainEventHandler<SomethingElseHappened>,
IDomainEventHandler<MoreThingsHappened>,
IDomainEventHandler<AndYetMoreThings>
{
readonly IFrontEndNotifier _frontEndNotifier;
public void Handle(SomethingHappened #event)
{
_frontEndNotifier.Notify(#event, #event.CommanderId);
}
...
}
How can I get classes with multiple interfaces to be injected by autofac as well?
EDIT
More information:
public interface IDomainEventHandler
{
}
public interface IDomainEventHandler<in T> : IBlingHandler<T>, IDomainEventHandler
{
}
public interface IBlingHandler<in T>
{
void Handle(T #event);
}
Registering like this in bootstapper:
container.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(x => x.GetInterfaces().Any(i => i.Name.StartsWith("IBlingHandler")))
.AsImplementedInterfaces();
I have a wcf service application and I have just implemented ninject library. In ninject web site there is following example:
public class Samurai {
public IWeapon Weapon { get; private set; }
public Samurai(IWeapon weapon)
{
this.Weapon = weapon;
}
}
public class WarriorModule : NinjectModule
{
public override void Load()
{
this.Bind<IWeapon>().To<Sword>();
}
}
I have classes like this. But problem is how can I create instance of Samurai class? It's constructor have parameter (IWeapon), but the parameter is binded Sword or different class. When I want to create instance of Samurai class, compiler expects IWeapon type parameter. In my module class (like WarriorModule) I have already defined it's binding. How can I pass parameter (or some different way) to Samurai class's constuctor?
Try this
class Program
{
static void Main(string[] args)
{
IKernel kernel = new StandardKernel(new WarriorModule());
var samurai = kernel.Get<ISamurai>();
}
}
public interface ISamurai
{
}
public class Samurai : ISamurai
{
public IWeapon Weapon { get; private set; }
public Samurai(IWeapon weapon)
{
this.Weapon = weapon;
}
}
public interface IWeapon
{
}
public class Sword : IWeapon
{
}
public class WarriorModule : NinjectModule
{
public override void Load()
{
this.Bind<ISamurai>().To<Samurai>();
this.Bind<IWeapon>().To<Sword>();
}
}
I just figured out how to do is. There is no need to create ISamurai interface. In Warriormodule:
public class WarriorModule : NinjectModule
{
public override void Load()
{
Bind<IWeapon>().To<Sword>();
Bind<Samurai>().ToSelf();
}
}
To create Samurai instance:
var samurai = kernel.Get<Samurai>();
I'm using CQRS pattern in my recent project, so I defined some Commands that I call them CommandParameter and CommandHandlers.
For CommandParameters I have these Classes and Interfaces:
public interface ICommandParameter
{
}
public abstract class BaseEntityCommandParameter<T> : IAggregateRoot,ICommandParameter
where T : ModelEntitySuperType, new()
{
public T Entity { get; set; }
protected BaseEntityCommandParameter()
{
Entity = new T();
}
}
public class InsertCommandParameter<T> : BaseEntityCommandParameter<T>
where T : class, new()
{
}
And for CommandHandlers I defined these Classes and Interfaces:
public interface ICommandHandler<TCommandParameter>
where TCommandParameter :ICommandParameter
{
void Handle(TCommandParameter parameter);
string CommandCode { get; }
}
public class InsertCommandHandler<TCommandParameter, TEntity>
: ICommandHandler<TCommandParameter>
where TCommandParameter : BaseEntityCommandParameter<TEntity>, new()
where TEntity : ModelEntitySuperType, IAggregateRoot, new()
and I used them to make appropriate CommandParameters and CommandHandlers for each Entity for example for Order I have:
public class OrderInsertCommandParameter:InsertCommandParameter<Order>
{
}
public class OrderInsertCommandHandler
: InsertCommandHandler<OrderInsertCommandParameter, Order>
{
private readonly IUnitOfWorkFactory _factory;
public OrderInsertCommandHandler(IUnitOfWorkFactory factory,
IRepository<Order> repository)
: base(repository)
{
_factory = factory;
}
public override void Handle(OrderInsertCommandParameter parameter)
{
var uow = _factory.Create();
parameter.Entity.OrderCreationTime = DateTime.Now;
base.Handle(parameter);
uow.Commit();
}
}
I want to register these CommandParameters and appropriate CommandHandlers using structuremap automatically, How could I define a custom Conversion to do this?
The following should do the trick:
container.Configure(r =>
{
r.Scan(s =>
{
s.Assembly(typeof(ICommandHandler<>).Assembly);
s.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
});