Hi all,
This question follows on very closely to one I asked earlier but I do not think they are the same thing so thought it was sensible to split them up (which is a little ironic when you hear the question).
I have a web application that was split into two projects:
1) The ASP.NET site 2) DLL that contained the busines objects, logic layer methods and DAL methods. They where split into different namespaces but all within the same DLL.
The more I looked at it and extended the development the more I realized I was very much going down the wrong road. I wanted to split them up into separate DLL's so that I could have a clear separation of duties.
Once I had the code in different DLL's I realized that a lot of classes called each other. For example when a List of Client objects were created in the DAL it would also call the companyDAL.cs and get a list of Companies that the Clients belonged to. So I now had two classes that directly referenced each other. This seems like a rather bad thing! It smelt bad but I carried on and tried to separate things out where I could.
To that end I took all the objects out to interfaces and changed all references to them to the interface.
I was hoping to use some sort of DI (just learning about that at the moment) so for each object I created a constructor that took in a Factory for that DLL, (this factory would create a new instance of each object that the class might require). I also put a default constructor in that would call the DI constructor with a default Factory to ensure that there was always one factory used.
It all compiled so I gave it a spin and got a stackoverflow! This is were I fully realized that if I have two classes that require each other you can not instantiate without the other.
Factory
{
IClient CreateNewIClient()
{
return new Client();
}
ICompany CreateNeIwCompany()
{
return new Company();
}
}
Client
{
private ICompany _company;
public Client() : this (new Factory()) {}
public Client(Factory factory)
{
_company = factory.CreateNewICompany();
}
public Client GetClientbyID(int id)
{
.... do stuff to get client from db
... got client object and companyid from db.
client.Company = _company.GetCompanybyID(companyid);
return client;
}
}
Company
{
private IClient _client;
public Company() : this (new Factory()) {}
public Company(Factory factory)
{
_client = factory.CreateNewIClient();
}
public Company GetCompanyWithAllClients(int companyid)
{
.... do stuff to get a company out and client ids
.... for each client id found
company.Clients.add(_client.GetClientByID(clientid));
return company;
}
}
Am I going about the DI thing all wrong, is it that factory idea ok?
How should I avoid the classes needed each other? The reason they are like that at the moment is to avoid repeated code. I am sure I could write cleaner SQL to get it all out in one go, or at least in a couple of queries in the same method, but I was trying to avoid repeating code in different places.
Thanks for any and all suggestions.