1

I Have class that have some property injection like this:

public class MyRepository
{
    public IBaseRepository BaseRepository { get; set; } //Injected By IoC
    public IUid Uid { get; set; } // Injected By IoC

    private static AnotherClass _anotherClass;


    public MyRepository()
    {
        _anotherClass = BaseRepository.Db.SingleOrDefault<AnotherClass>();
        //another logic in here....
    }

    public string MethodUsingUid()
    {
        return Uid.SomeMethodHere(_anotherClass);
    }
}

And used by Services like this:

public class TheServices : Service
{
    public MyRepository Repo { get; set; }

    public object Post(some_Dto dto)
    {
        return Repo.MethodUsingUid();
    }
}

And my Apphost.configuration looks like this:

 container.Register<IDbConnectionFactory>(conn);
 container.Register<IBaseRepository>(c => new BaseRepository(){ DbFactory =    c.Resolve<IDbConnectionFactory>()}).ReusedWithin(ReuseScope.Request);

 container.Register(
                c => new MyRepository() { BaseRepository = c.TryResolve<IBaseRepository>(), Uid = c.TryResolve<Uid>() });

container.RegisterAutoWired<Uid>().ReusedWithin(ReuseScope.Request);

I know it will not injected because it will created before funq have chances to inject. and according to this answer: ServiceStack - Dependency seem's to not be Injected?

I need to move the constructor into Apphost.config() My question is, how I move this class constructor out into apphost.config()? and how to manage that if I have many class like that?

Community
  • 1
  • 1
reptildarat
  • 1,036
  • 2
  • 18
  • 35
  • The other answer talks about setting up the dependencies for injection in AppHost.Configure() method. I've never used ServiceStack but it looks like pretty standard dependency initialization. What you want to do is use constructor injection if possible. But using the other class like that in the constructor seems like you may want to reconsider the design. – crad Dec 20 '13 at 04:22
  • Actually i'm talking about the configurations (and thanks btw, i edited my question and adding the configuration). actually, i already considered about the constructor injection (and tried it), but the problem is, the BaseRepository class have some dependency that normally is injected by the IoC, and if the IoC not initialized, then it will not injected too. – reptildarat Dec 20 '13 at 04:45
  • 1
    That shouldn't be your issue. Just make sure you register the repository's dependencies first. AppHost.Configure() should run before your first use of the repository. If you have circular dependencies you need to adjust your design. – crad Dec 20 '13 at 04:53
  • You mention about adjusting the design, can you share with me how to adjust the circular dependencies like that? – reptildarat Dec 20 '13 at 07:56

1 Answers1

2

Ok, so it's been a while when I create my questions, I resolve this with changing from property injection into constructor injection like this:

public class MyRepository
{

    private static AnotherClass _anotherClass;
    private readonly IBaseRepository _baseRepository;
    private readonly IUid _uid;

    public MyRepository(IBaseRepository _baseRepository, IUid uid)
    {
        _baseRepository = baseRepository;
        _uid = uid;

        _anotherClass = BaseRepository.Db.SingleOrDefault<AnotherClass>();
       //another logic in here....
    }

    public string MethodUsingUid()
    {
        return _uid.SomeMethodHere(_anotherClass);
    }
}

And I move the injection to Service:

public class TheServices : Service
{
    public IBaseRepository BaseRepository { get; set; } //Injected By IoC
    public IUid Uid { get; set; } // Injected By IoC

    public object Post(some_Dto dto)
    {
        var Repo= new MyRepository(BaseRepository, Uid);
        return Repo.MethodUsingUid();
    }
}

I Hope there's another way, but it's only solutions that I can think.

reptildarat
  • 1,036
  • 2
  • 18
  • 35