Question

I'm working on a desktop application that has generated code for database access and uses static objects for user identification.

Now we need to expose some of the logic by webservice and we are looking for the least intrusive form to push the user information down the pipe to the database access classes.

What we came up with was to pass a delegate to the Insert / Update methods that looks like this:

public delegate string GetLogin();

public class BaseEntity : BaseNotifiableEntity, System.ComponentModel.IDataErrorInfo
{
    public GetLogin Login { get; set; }
    (...)
}

public static class BaseEntityHelper
{
    public static SqlCommand buildUpdateCommand(BaseEntity entity)
    {
        UpdateDefaultValues(entity, false);
        (...)
    }

    public static void UpdateDefaultValues(BaseEntity entity, bool affectCreationFields)
    {
        if (entity.Login == null && AppServer.RunningApplication.CurrentUser == null)
            throw new Exception("Something went wrong");

        (...)
    }
}

So in our logic will would have something like this:

    public class Service
    {
        T_DIST_Service record;
        (...)

        public bool Update(DataAccess.Base.GetLogin login)
        {
            record.Login = login;
            (...)
            record.Update();
        } 
    }

This of course involves changing a lot of methods in the application. So i was wondering if there's a seamless way to accomplish this using dependency injection (for example).

Probably some of you have already go down this road and have some insights to share. Thank you for your time.

EDIT 1: Using .NET

Was it helpful?

Solution

On an architectural level it sounds like me that you are attempting to put logic in the data access layer that doesn't belong there. A data access component should be nothing but an anti-corruption layer, so any logic should ideally be implemented in the calling layer.

However, if you want a more immediate fix here and now, it would be most recommendable to use the built-in Thread.CurrentPrincipal Ambient Context.

If you have special information that your user object must carry around, you can use a custom implementation of IPrincipal to create a custom User Context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top