Generic DAL and Buildup

Feb 11, 2009 at 1:48 PM
Edited Feb 12, 2009 at 11:49 AM
Hi.

I just wanted to know if there is a more elegant way of doing the following:

I got a generic LinqtoSql Service. This DAL service knows nothing about my other components and about unity being used. The interface looks like this:

public interface IDBService
{
        T SelectByPK<T>(String id) where T : class;
        T SelectByField<T>(String field, object id) where T : class;
        IList<T> SelectAll<T>() where T : class;
        void Insert<T>(T item) where T : class;
        void Update<T>(T item) where T : class;
        void Remove<T>(T item) where T : class;
}

The service gets registered like this:

_container.RegisterInstance<IDBService>(new Services.LinqSqlDBService());

LinqSqlDBService being the concrete implementation of the service.

Then I got another service which is using the IDBService. Let it be a UserManagementService.
The IDBService is being injected into the UserManagement Service. So far no problem.

Then I have a User class. The user class mainly just defines the fieds, but offers also limited functionality. For example one can validate if a password is correct. The passwordfield of the user class is a byte array, because the password is being stored in an encrypted form.

The UserManagement Service offers a function for Encrypting a password. So I decided to inject the UserManagementService into the User class and call that function when needed.

When I create a User class instance by calling container.Resolve<User>() there is no problem. The UserManagementService gets injected into the User class.

BUT: when I load a user from my data store using my generic DAL, which knows nothing about unity, the UserManagementService is not being injected into the user class. As a work around, I call _container.BuildUp<User>(objUser); after getting the User object returned from my DAL.

Is there a better way to do this?

Cheers.

PS: Sorry for my bad english, I hope I was able to explain my "problem"
Feb 13, 2009 at 10:27 PM
These kinds of scenarios are why the BuildUp method exists.

However, I'd suggest you reevaluate your domain model a little. Perhaps there should be an AuthenticationService that takes the user in as input and tells you if the password is correct instead?

Feb 14, 2009 at 5:04 AM
Thanks for your reply.

This is exactely what I did. I changed my domain model and designed an authentication service which implements the needed functions.

Cheers.