2

I want to make my architecture better but i don't know how to resolve dependency trouble.

What i have:

  1. class library CoreService witch have a lot of interfaces IMail, Ilog, ISession
  2. In app start i created IMail, Ilog, ISession based classes and pass it to CoreService class
  3. base controller class with CoreService instance (property CoreService Services {get;})
  4. user plugin use CoreService interfaces

The problem: Every time i need to add new service i must edit app start, CoreService. Ideally I would like just create a class lib for IMail (or any other service) and register it in web.config or in app start.

I would appreciate for any advice or links that will help me.

Thanks for all so users!

Sanja Melnichuk
  • 3,465
  • 3
  • 25
  • 46
  • You might be able to achieve something like this with a dependency injection framework and XML config, but why do you not want to edit the app start code? – Roman Aug 14 '11 at 14:22
  • @R0MANARMY Thanks for response, its because i try to build an enterprise architecture, so we have a lot programmers witch will create some theirs specific services and i just want quickly add it to common project with minimal steps – Sanja Melnichuk Aug 14 '11 at 14:27

2 Answers2

2

Take a look at Ninject and XML configuration Binding.

I think this covers both parts of what you're aiming to achieve. This is dependency injection as well as being able to modify configuration without needing to recompile.

If you add entirely new services that Ninject (or other DI containers - Unity for example) isn't resolving for you, you will need to edit your code. I don't think that's avoidable.

Community
  • 1
  • 1
Yuck
  • 49,664
  • 13
  • 105
  • 135
1

What you need is a Dependency Injection framework to resolve your dependencies. DI frameworks will manage the creation and lifetime and disposing of objects.

builder.RegisterType<MySession>().As<ISession>().InstancePerHttpRequest();
builder.RegisterType<MyMail>().As<IMail>().Singleton();

Then you can use constructor injection to inject these

public class MyController : BaseController
{
    public MyController(ISession session, IMail mail)
    {
         _session = session;
         _mail = mail; 
    }

   //other methods and member variables
}

This way you have to modify only one place when you need to add new dependencies.

Eranga
  • 32,181
  • 5
  • 97
  • 96
  • thanks for response, but i don't want each time to change controller ctor. Can i resolve it throw an other class? – Sanja Melnichuk Aug 14 '11 at 14:31
  • 1
    @Sanja you can supply a factory class. But the above approach is much more cleaner. another member looking at the above code will instantly recognize the dependencies. – Eranga Aug 14 '11 at 14:58