I'm working on decoupling a Xamarin (.NetStandard) solution. It was using DryIOC container but I didn't see the IOC structure from the dependency, as the projects are still tightly coupled.
Three main projects go as below:
MyApp -> MyCommon -> MyAPI
namespace MyAPI
{
public interface IApiHelper //Q1
{
Task<string> CheckSessionAsync();
}
public class ApiHelper : IApiHelper, IDisposable
{
public async Task<string> CheckSessionAsync()
{
//do something
}
}
}
namespace MyCommon
{
public static class AssemblyInfo
{
public static void IocRegister()
{
MyIoc.Current.Register<IApiHelper, ApiHelper>();
MyIoc.Current.Register<IMyService, MyService>(); //Q2
}
}
}
namespace MyCommon
{
public class MyService : IMyService
{
public MyService(IApiHelper helper)
{
Helper = helper;
}
}
public interface IMyService
{
}
}
namespace MyApp
{
public class AppPage
{
public AppPage()
{
MyService = MyIoc.Current.Resolve<IMyService>();
}
public void OnAppearing()
{
var checkResult = MyService.Helper.CheckSessionAsync(); //Q3
}
}
}
My questions are:
- Is it ok to move
IApiHelper
into projectMyCommon
, so the dependency will be an inversion. Do I need an individual project to contain all the interfaces for better structure if there are heaps? - Is it ok to separate the registration into different classes? If I register
MyService
in assembly class ofMyApp
, I can use constructor-injection then, any problem? - Any code smell here? Is it ok to move
IMyService
into projectMyApp
to make another inversion? - To make
MyApp
dependent on an interface assembly (don't need extra build every time changing the implementation), how can I achieve this? Maybe like this?
IService IApi
/ \ / \
MyApp MyCommon MyAPI
Or this (can those interfaces assembly be merged)? Any suggestion would be helpful, cheers.
ICommon
/ | \
MyApp MyCommon MyAPI
Edited:
Sorry, I didn't elaborate Q4 well. My intent of all those questions is to decouple the projects.
When the dependency goes like (currently)
MyApp -> MyCommon (IService) -> MyAPI (IMyAPI)
when I change the implementation in MyCommon
or MyAPI
, I have to rebuild and update the output dll files.
MyApp
- MyCommon.dll
MyCommon
- MyAPI.dll
By applying IOC, (I assumed it is not applied now) the hierarchy can be:
MyApp(IService) <- MyCommon(IMyAPI) <- MyAPI
Then I'm free to update the implementation without affecting MyApp.
(Correct me if anything wrong please)
MyApp
MyCommon
- MyApp.dll
MyAPI
- MyCommon.dll
However, there might be MyWebApp
, MyUwpApp
and MyMobileApp
, so I was thinking about the structure like graph1 in Q4. And dependencies (my intent) can be
MyWebApp
- IMyCommon.dll
MyUwpApp
- IMyCommon.dll
MyCommon
- IMyCommon.dll
- IMyAPI.dll
MyAPI
- IMyAPI.dll
To achieve this, I had my Q1 Q2 Q3, which seems to be necessary changes to me.
Q5 (extended from Q2):
By applying Q4 Graph1, if I leave MyIoc.Current.Register<IApiHelper, ApiHelper>();
in MyCommon, does that still requires a dependency with MyAPI, which would be a circular dependency? Or where shall I do the registration for a container?