English 中文(简体)
带有仓库模式的域域模型
原标题:Domain Model with Repository Pattern

我有一个目标:图书馆

一个图书馆可拥有多个书籍、光盘和文件。 每一类都有Id、 名称和价格属性, 除了有地址的图书馆类 。

我要创建存储器模式, 抽象数据存储于上层。 现在, 数据存储在 xml 文件中 。

<library>
<books></books>
<cds></cds>
<papers></papers
</library>

稍后我们将考虑将它储存在数据库中,然后我们将有一个图书馆表、一个书桌、一张CD表和一张纸桌。

用户在屏幕上创建一个库对象,然后在屏幕上创建书籍、 cds 和文件。

然后他点击“ 保存 ” 。 在这一点上, 我想将新建的图书馆对象移到 XML 中保存, 这样我就可以使用仓库, 并使用依赖性注射, 我会在上层输入 xml 仓库, 这样数据就可以存储到正确的位置 。 我可以将图书馆对象序列化, 并获得 xml 并调用简单的 保存 方法来保存 库对象 。

但想象一下,我用数据库存储器取代 XML 存储器。

In that case, I want the rest of the layers above to remain unchanged. I am hoping that LibraryRepository.Save() will take care whether it needs to go in XML or CSV or DB. I am confused how I will create a Repository pattern to solve LibraryRepository.Save() method.

Everyone everywhere says that Repository class should have a single responsibility. It should only save one object type and shouldn t take an object, take its related classes and save them too. Each class should have its own Repository class.

另外,每个图书馆对象都含有多个图书对象。 我不想使用环绕来浏览图书馆仓库.保存方法中的每本书。

我不知道该如何创建我的域名模型,

请引导。

问题回答

由于存储库是您 BL 使用存储库接口的持久性的抽象化 。

每个存储库的实施应注意对数据存储的天体结构进行测绘,或使用特定的绘图仪。 BL/Services类将通过接口与存储库合作,而不知道某一存储库的内部实施情况。

所以,你有一样的东西

class LibraryService {
    public LibraryService (ILibraryRepository repo) {} 
    public DoSomeWork(somedata) {
        Library lib = repo.Load(somedata.libid);
        ....
        repo.Save(lib);
    }
}

和若干项执行

class DBLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //hibernate session 
        //and you ll have mapping defined for all entities
        //and relations between library and stuff, so books are saved automatically
        _session.Save(lib)
    }
}

class XMLLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //serialized does all the work, so no additional loops here
        var xml = Serialize(lib);
        SaveToFile(xml);
    }
}

class CSVLibraryRepository : ILibraryRepository {
    public Save(Library lib) {
        //for example, save library to lib.csv, books - to books.csv.
        //add mappers to do real work, but we d take care of separation 
        //of books and library manually.
        // (in case of ORM - it has own mappers, 
        //  for XML - serializator is mapper itself)
        var data = LibraryCSVDataMapper.Map(lib);
        data.Save();
        foreach(var book in lib.Books){
            data = BookCSVDataMapper.Map(book);
            data.Save();
        }
    }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签