1

I can't explain my problem in English. so let me show my situation.

// in Main Project
public class User
{
    public int version
    {
        get;
        set;
    }
}


// in Common Project
public class Service : BaseService
{
    User _user;
    public void SetVersion(int versionID)
    {
        _user.version = versionID;
    }

    public bool HasMessage()
    {
        return GetMessage(user.version);
    }
}

And now I have another sub project. and I need use Service class in there. so I wish make Service class independent from User class.

how do I do that?

I have only below solution. Is there any brilliant way?

public class Service : BaseService
{
    Action<int> _getCallBack;
    Func<int> _setCallBack;

    public Service(Action<int> getCallback, Func<int> setCallBack)
    {
        _getCallback = getCallback;
        _setCallback = setCallback;
    }

    public void SetVersion(int versionID)
    {
        setCallback(versionID);
    }

    public bool HasMessage()
    {
         return GetMessage(getCallback())
    }
}
Søren
  • 6,517
  • 6
  • 43
  • 47
오범선
  • 35
  • 5
  • Why is `User` in your main project anyway? – stuartd Jan 16 '17 at 12:06
  • This looks perfect for inheritence? Your `User` could be derived from the `Service`? Thats because there is a 1:1 link between a user and the service. – Jeroen van Langen Jan 16 '17 at 12:33
  • these library is part of Opensource Project. and i try to not change Original Source code on there... so it is not so easy for me to change architecture..anyway if i have convince some solution.. i conside launch new branch or new repository – 오범선 Feb 20 '17 at 02:01

2 Answers2

1

It depends on your use of 'User' in service. You can add an interface IUser in Common project, and have User implement it.

Then in the other SubProject, write UserSub : IUser that also implements the interface IUser.

That way Service is independent, but you'll still have to implement something in each project that uses Service. (Which you need to do anyway, because Service currently uses it as an inner variable.

Ori Nachum
  • 588
  • 3
  • 19
  • 1
    i like this solution.. i need to change whole architecture... User is already inherit many base calss and Interface. so i can't move it to common project.. i must consider dependency.. and architecture.. thanks for comment. it helped me a lot – 오범선 Feb 20 '17 at 02:08
  • I'm glad I could help :) Kindly mark this as Answer if it solves your issue. – Ori Nachum Feb 20 '17 at 12:35
1

Yes there are several best practices which allow decoupling components, they are called design patterns. I would recommend to take a look at all of them to decide which one fits your context best. All of them have advantages and disadvantages, application scope and impact. There is no one brilliant solution for decoupling.

I think the command pattern can be the right one for your problem.

See: http://www.dofactory.com/net/design-patterns and https://csharpdesignpatterns.codeplex.com

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • I don't know why this was downvoted - it's a good (Though, more demanding) answer, and those links worth checking and getting to know. – Ori Nachum Jan 16 '17 at 12:06