How to retrieve Domain Object from Repositories

后端 未结 3 1610
星月不相逢
星月不相逢 2020-12-19 11:48

I have a little problem understanding repository-domain object relation. Here is some information I know about domain design(they may also be wrong or not accurate). And wit

相关标签:
3条回答
  • 2020-12-19 12:08

    There are options you have:

    1. ORMs can work with private fields.

    As I know, ORMs (e.g. Entity Framework, NHibernate) can set properties via non-public setters.

    There is an example that proves it for Entity Framework - Entity Framework, Private Constructors and Private Setters.

    If you use NHibernate your setters should be public/protected virtual/protected internal virtual or private backing field can be used. You can find more information in the Property Access strategies in NHibernate SO question.

    2. Reflection can be used.

    It can be used to get access to private fields/properties also. It is possible to set private property via reflection.

    3. It is not a bad practice to have public constructor to construct your entity.

    Declaring public constructors containing all of the fields doesn't seems right. I might have several models to fill in, this means I have to define several constructors with different sets of parameters.

    Your Domain Entities need only one public constructor with full list of properties they have. It is enough to have only one constructor in spite of having several models to fill in. It is a responsibility of repository to invoke constructor and map model into its parameters correctly.

    Edit:

    4. Automapper can be used.

    The following test shows that AutoMapper can map properties via private setters.

    [TestClass]
    public class AutomapperTest
    {
        [TestMethod]
        public void Test()
        {
            // arrange
            Mapper.CreateMap<AModel, A>();
            var model = new AModel { Value = 100 };
    
            //act
            var entity = Mapper.Map<A>(model);
    
            // assert
            entity.Value.Should().Be(100);
            entity.Value.Should().Be(model.Value);
        }
    }
    
    public class AModel
    {
        public int Value { get; set; }
    }
    
    public class A
    {
        public int Value { get; private set; }
    } 
    
    0 讨论(0)
  • 2020-12-19 12:15

    It's not true you can't create domain objects with ORM not having public setters. If you're using Entity Framework, it definitely can map private properties in model first approach and you only need public getters in code first approach. I don't know how about other ORM-s.

    0 讨论(0)
  • 2020-12-19 12:24

    I am trying to understand your query here. Some tips on how you can proceed. First of all the Domain should know the repository contracts and not the actual repository infrastructure. in other words, you may choose to have 3 class libs as follows

    1. XYZDomain (will know XYZRepository and make call on the appropriate methods of this interface)
    2. XYZRepository (contains Interface IXYZService interface)
    3. XYZSQLRepository(actual implementation of XYZRepository interfaces).

    Now it's up to you to choose where to inject XYZSQLRepository to the XYZDomain using Dependency injection.

    You can also try using eventing model to register these repositories if you want.

    Use a custom Service Locator to get the concrete objects

    0 讨论(0)
提交回复
热议问题