简体   繁体   中英

Inherit Interface for async methods, best practice

I have a class that works as a layer above the database layer for easy database access and it works very fine. But now I would like all methods that read from the database being able to be accessed asynchronously without any risk of doing something dangerous. So I thought that if I implement an interface with only the "read" methods I could refer to that interface instead of the normal class when working from an async thread.

Is there any better (but hopefully similar) way to make this happen?

Here's an example of how far I've tried, but without success:

// My two interfaces.
// The UserCoreManager class is the layer that works with the database
// layer for the users table.
// but the IUserCoreManagerAsync only reference the async friendly methods.

public interface IUserCoreManagerAsync
{
    User GetData();
}

public interface ICoreAsync
{
    IUserCoreManagerAsync User { get; }
}

So far, so good. By refering to the User in the ICoreAsync interface I would theoretically only be able to Get data and not Save any?

Here's what my actual classes implementing the interfaces looks like:

public class UserCoreManager : IUserCoreManagerAsync
{
    public User GetData()
    {
        ...
    }

    public User SetData()
    {
        ...
    }
}

public partial class Core : ICoreAsync
{
    private UserCoreManager user;
    public UserCoreManager User // <- This is the tricky part
    {
        get
        {
            return user;
        }
        protected set
        {
            user = (UserCoreManager)value;
        }
    }
}

As you can see, The UserCoreManager has a SetData() method, so when working with it not refering too the interface it will not be thread safe for me. But as you also can see, I don't implement the interface property returning a IUserCoreManagerAsync, but I was sort of hoping that the UserCoreManager property would do the trick.

How would you do this? My goal is to achieve something like this when working with these classes:

public void DoSomeAsyncWork(ICoreAsync core)
{
    var myData = core.User.GetData();
}

public void DoSomeWork(Core core)
{
    core.User.SetData("some data");
}

In this way I have the actual same object, but depending on which methods I give it to and how i cast it, they can be forced to work with them thread safe or not.

Check this out

public interface IUserCoreMagagerWrite
{ 
    public void SetData(User user);
}

public interface IUserCoreManagerRead
{
    public User GetData();
}

public interface IUserCoreManagerReadWrite : IUserCoreManagerRead, IUserCoreMagagerWrite
{ 

}

public class UserCoreManager : IUserCoreManagerReadWrite
{
    public User GetData()
    {
        return new User("abbo");
    }

    public void SetData(User user)
    {
        //...
    }
}


public class Core
{
    public IUserCoreManagerReadWrite UserCoreManager { get; set; }
}

public class AsyncCore
{
    public IUserCoreManagerRead UserCoreManager { get; set; }
}

public class TheProgram
{
    public void SynchronousWork()
    {
        Core core = new Core();
        User user = core.UserCoreManager.GetData();
        core.UserCoreManager.SetData(user);
    }

    public void AsyncWork()
    {
        AsyncCore core = new AsyncCore();
        User user = core.UserCoreManager.GetData();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM