inversion-of-control

Should I use Unity Config file or Code to register types and instances?

不问归期 提交于 2019-12-13 19:02:02
问题 Finally started to configure an IoC Container! I'm using Unity and have configured it to have my objects registered using the config file: e.g. <container> <register type="ILogger" mapTo="Logger"> <lifetime type="singleton"/> </register> <register type="IPdfWriter" mapTo="PdfWriter"> <lifetime type="perthread" /> <constructor /> </register> </container> I've reached a point where I doubt this is a good approach to register types. For example a class of mine is dependent on the ICacheManager

DI Singleton instance vs Transient instance

泄露秘密 提交于 2019-12-13 18:35:35
问题 Several years ago IoC performance guidelines stated IoC containers should be used to resolve only long-lived instances (singletons basically), whereas transient type objects should be created using a singleton factory (held by the container). I am reading now about ASP.NET Core, and several examples I have seen use Transient lifetime for their injected objects. Has something changed where transient is now the preferred method for services that provide static methods (and are stateless)? 回答1:

Laravel 4: Pass validation messages obtained from repository to controller

蹲街弑〆低调 提交于 2019-12-13 16:42:27
问题 Learning about Ioc and Repositories and stuck at last hurdle! Assuming I am validating input, how do I pass back messages from the Validator within the repository to the controller? UserRepository interface UserRepository { public function all(); public function create($input); public function findById($id); } Sentry2UserRepository class Sentry2UserRepository implements UserRepository { ... public function create($input) { $validation = Validator::make($input, User::$rules); if ($validation-

Chaining containers with StructureMap

自闭症网瘾萝莉.ら 提交于 2019-12-13 14:18:10
问题 Is it possible to link containers together in StructureMap like it is in WindsorContainer.AddChildContainer()? I want to achieve having 3 container levels; - 1 page request level - 1 session level - 1 application level These would then be chained together so only one instance request would be made to the "base level" container. The levels of container are unimportant really, just whether there is the ability to link them together. 回答1: This seems to do the trick, not sure if there is a better

Autofac property injection with ValidationAttribute

主宰稳场 提交于 2019-12-13 14:02:43
问题 I've got a ValidationAttribute that looks like this: public class RegistrationUniqueNameAttribute : ValidationAttribute { public IRepository<User> UserRepository { get; set; } public override bool IsValid(object value) { //use UserRepository here.... } } In my container setup (in app start) I have this: builder.Register(c => new RegistrationUniqueEmailAttribute { UserRepository = c.Resolve<IRepository<User>>() }); However, when debugging, the value of UserRepository is always null, so the

Ninject - Kernel in static class?

点点圈 提交于 2019-12-13 13:49:11
问题 Is it right at all to "wrap" a StandardKernel with the required NinjectModule s in a static class in a separate, shared library, and use that same library whenever injection is needed (instead of instantiating a new kernel everytime)? Edit: I am trying to use Ninject from within the WCF service I am developing at the moment. (Please bear with me if what I am saying is completely rubish since I just started learning about DI and IoC containers) 回答1: See https://github.com/ninject/ninject

how to write libraries without forcing users to use the library's IOC container

帅比萌擦擦* 提交于 2019-12-13 12:26:21
问题 The short question is: Given a library warrants using a particular IOC container for its internals, when an application consumes that library, given the app warrants using an IOC container for wiring its dependencies, given if the the two containers are different, how can they play well together? The scenario is, the application has classes defined that depend on types from the library. So when the application container attempts to build such a class, it needs to know how to resolve the type

How to use Simple injector, Repository and Context - code first

我们两清 提交于 2019-12-13 11:33:17
问题 I'm trying to use Simple Injector to create my repository and use it in the Business logic layer ( also i want to use PerWebRequest method ) . In the DAL layer i have : public interface IRepository<T> where T : class { void Add(T entity); void Delete(T entity); void Delete(int id); void Update(T entity); T GetById(int Id); IQueryable<T> All(); IEnumerable<T> Find(Func<T, bool> predicate); } and : public class EFRepository<T> : IRepository<T>, IDisposable where T : class { #region Members

Syfmony - load service on boot

折月煮酒 提交于 2019-12-13 07:16:31
问题 I posted another question trying to find a way to statically access a repository class outside of a controller in a custom "helper" class. So far the only way I have figured out how to achieve this is using the code below. If anyone wants to chime into the other question about "best practice" or "design patterns" please do. I opened this question to seek the best method on having a singleton service (?) loaded when symfony boots so other classes can access it statically without any dependency

Reference vs Dependency

。_饼干妹妹 提交于 2019-12-13 06:50:14
问题 Following on from this question, I now have the following structure: Wolfie.Core - Contains business logic & entities, also contains repository interfaces (eg IUserRepository) Classes that need to access the repository are using constructor injection. Wolfie.Data - References Wolfie.Core and has a UserRepository implementing IUserRepository So I'm happy with this so far. Core doesn't know anything about data implementation and therefore isn't dependent on anything. The stumbling block I get