Thanks Michael,
That makes sense.
I was thinking I would need to do some sort of cast
Eg
If (repositoryB is ILowLevelDataRepositoryB)
{
ILowLevelDataRepositoryB lowLevelRepositryB =
(ILowLevelDataRepositoryB) repositryB
.
}
But I really don't need to. They can both just be separate dependencies.
Jeff
From: [email protected] [mailto:[email protected]]
On Behalf Of Michael Minutillo
Sent: Wednesday, 28 July 2010 8:22 PM
To: ozDotNet
Subject: Re: Dependency injection
Hi Jeff,
Most IoC containers (Unity included) allows you to have two type-mappings
pointing to the same destination type. That means you could create a second
interface that is only about those low-level calls.
So you might have
interface IDataRepositoryA { Customer Load(int id); }
interface IDataRepositoryB { Order Load(int id); }
interface ILowLevelDataRepositoryB { AddOrdersToQuery(Query q); }
class DataRepositoryA : IDataRepositoryA {
public DataRepositoryA(ILowLevelDataRepositoryB ordersRepo) {...};
}
class DataRepositoryB : IDataRepositoryB, ILowLevelDataRepositoryB {
}
class BusinessClass {
public BusinessClass(IDataRepositoryB ordersRepo) { ... };
}
var container = new UnityContainer();
container.RegisterType<IDataRepositoryB, DataRepositoryB>();
container.RegisterType<ILowLevelDataRepository, DataRepositoryB>();
// Register Everything else as per normal
now this should hold
var high = container.Resolve<IDataRepositoryB>();
var low = container.Resolve<ILowLevelDataRepositoryB>();
Assert.IsTrue(ReferenceEquals(high, low);
As the RepoA and the Business class rely on different abstractions they
never need to know they're talking to the same object and in time it may
even come to pass that they don't. This is the Interface Segregation
Principle in action: "An consumer class should not be aware of more methods
than it needs to do it's job"
http://www.objectmentor.com/resources/articles/isp.pdf
Regards,
--
Michael M. Minutillo
Indiscriminate Information Sponge
Blog: http://wolfbyte-net.blogspot.com
On Wed, Jul 28, 2010 at 7:44 PM, Jeff Sinclair
<[email protected]> wrote:
Hi,
I am using Microsoft unity to do dependency inject but I have a small
problem.
Let's say I have a business layer assembly and a data layer assembly. The
data layer exposes 2 interfaces,
IDataRepositortyA and IDataRepositoryB. These are both constructed by unity.
Now let's say IDataRepositoryA needs to do some low level calls to
repository B, say for some type of mash-up.
How do I expose a low level function on repository B that really should not
be visible to the business layer.
Do I just add the low level functions to the public IDataRepositoryB
interface, which makes them visible to the business layer?
Or do I create a new interface for the extra low level functions? If I make
the new interface internal, how does it work with injection ?
Jeff