开发者

Sharing domain logic between similar classes in my architecture

My MVC ASP.NET web application is split up like this:

Web Application Layer
  Views

Domain Layer
  Repository
    [Repository and Interfaces here]
  Domain
    [Domain Classes here]

It's quite new and evolving, and I'm starting to see some problems with it. My domain classes are POCO objects - I'm using Linq to SQL "Code First" so I'm adding my own classes and decorating them to work with them in the Repository. At first this went well, I could add business/domain logic to the classes and it seemed quite clean.

More recently, I have created a number of database views that are based around the same "core" table - an "Equipment" table. I create a POCO domain class for each view so that I can read from it in my Repoisitory. The trouble I am having is that the domain logic added to the core Equipment domain class is not avaialble to my new views. I can't use a common base class since Linq to SQL doesn't work with this if you're using attributes to decorate your classes. Not using the views isn't an option because they are useful outside of the web app too.

Is there a good solution to this, I'm wondering if I might need to think about seperating the classes that the repository works with from a seperate set of "domain classes" to solve this?

[Edit] More info as requested. For 开发者_高级运维example, my "Equipment" class, (which is a domain class that is mapped to the database) contains this code:

public string EquipmentDescription
        {
            get
            {
                return String.Format("U{0}{1}{2}",
                                         String.Format("{0}", UPos.ToString("00")),
                                         String.IsNullOrEmpty(EquipmentName) ? "" : ": " + EquipmentName,
                                         String.IsNullOrEmpty(Label) ? "" : " - " + Label
                                         );
            }
        }

Now, I have a view in the database called "EquipmentWithCableDetails", which contains additional useful information with the equipment details - so I create a class to model that view (in order to be able to read from it). In the code I have that works with that new class, I want to display the EquipmentDescription - but that property only exists on the Equipment class.

Thanks!


What I did was put my partial classes/ Entity classes/ Domain Classes in a seperate project, this way all the projects including test project can reference this project. I found out that is worked really well for me, you could leave all your data access logic and business logic within your domain layer as you have done.


One pattern you can use when sharing logic between classes is the helper pattern.

All your classes can call the helper we they need access to the domain logic.

Sometimes the helper pattern is considered an anti in pattern, but in your case it is not possible to put it in a base class.

http://blogs.msdn.com/b/nickmalik/archive/2005/09/06/461404.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜