Skip to content

这个项目实现了微信读书技术博客介绍的基于AOP的M层抽象的方案

License

Notifications You must be signed in to change notification settings

luoxuwei/ServiceProxy

Repository files navigation

ServiceProxy M层抽象框架


这个项目实现了微信读书技术博客介绍的基于AOP的M层抽象的方案,文章地址:基于 AOP 的 M 层抽象

接入ServiceProxy

implementation 'com.github.luoxuwei.ServiceProxy:library:1.0.1'
implementation 'com.github.luoxuwei.ServiceProxy:annotation:1.0.1'
annotationProcessor 'com.github.luoxuwei.ServiceProxy:annotation-compiler:1.0.1'

使用指南

1.定义业务模块用到的后端api的Retrofit接口

public interface DemoApi {
    @GET("https://easy-mock.com/mock/5c511379d858826be92e5b8d/example/service_test")
    Observable<UserInfo> getUserInfo();

    @GET("https://easy-mock.com/mock/5c511379d858826be92e5b8d/example/service_test1")
    Observable<UserInfo> getUserInfo1();

    @GET("https://easy-mock.com/mock/5c511379d858826be92e5b8d/example/service_test2")
    Observable<UserInfo> getUserInfo2();
}

2.定义封装业务逻辑的模块类,继承定义好的Retrofit接口,并添加ProxyModule注解

@ProxyModule
public abstract class DemoService implements DemoApi {

    public Observable<String> getUserName(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo().blockingFirst().nick;
            }
        });
        return res;
    }

    public Observable<String> getUserName1(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo1().blockingFirst().nick;
            }
        });
        return res;
    }

    public Observable<String> getUserName2(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo2().blockingFirst().nick;
            }
        });
        return res;
    }
}

因为继承了Retrofit接口,又不能实现接口中的方法,类必须声明为abstract。

3.实现IRetrofitFactory接口,给代理提供retrofit对象,通常retrofit的创建是和业务绑定的,通过这个接口可以实现自己的创建逻辑指定业务相关的参数,可以通过给业务模块加@BaseUrl 注解来指定baseurl 方便创建多个不同配置的retrofit对象.

public interface IRetrofitFactory {
    Retrofit create(String baseUrl);
}

ServiceProxy.init(baseUrl -> {
    if (TextUtils.isEmpty(baseUrl)) {
        baseUrl = "https://easy-mock.com";
    }
    return new Retrofit.Builder()
        .baseUrl(baseUrl)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build();
});

@BaseUrl("https://easy-mock.com")
@ProxyModule
public abstract class DemoService1 implements DemoApi1 {

    public Observable<String> getUserName(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo().blockingFirst().nick;
            }
        });
        return res;
    }

    public Observable<String> getUserName1(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo1().blockingFirst().nick;
            }
        });
        return res;
    }

    public Observable<String> getUserName2(String id) {
        Observable<String> res = Observable.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return getUserInfo2().blockingFirst().nick;
            }
        });
        return res;
    }
}

4.在使用了ServiceProxy的项目的build.gradle文件里加上注解处理器的配置.

annotationProcessor 'com.github.luoxuwei.ServiceProxy:annotation-compiler:1.0.1'

About

这个项目实现了微信读书技术博客介绍的基于AOP的M层抽象的方案

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published