Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 实体框架+POCO_Entity Framework_Fluent Nhibernate_Structuremap_Poco - Fatal编程技术网

Entity framework 实体框架+POCO

Entity framework 实体框架+POCO,entity-framework,fluent-nhibernate,structuremap,poco,Entity Framework,Fluent Nhibernate,Structuremap,Poco,我正在使用MVVM模式构建一个WPF应用程序。我们的堆栈如下所示: SQL Server 2008->实体框架 我们使用StructureMap进行依赖注入来注入我们的DataFactory,它基本上为我们的POCO业务对象执行CRUD ViewModels对CRUD使用数据工厂,xaml是绑定到POCO中属性的数据 这一切都很好,但我发现唯一有点令人烦恼的是数据工厂。我们在选择时将每个属性从EF对象复制到POCO对象,在更新/插入时复制到POCO对象 是否有任何方法可以像Fluent对NHib

我正在使用MVVM模式构建一个WPF应用程序。我们的堆栈如下所示:

SQL Server 2008->实体框架

我们使用StructureMap进行依赖注入来注入我们的DataFactory,它基本上为我们的POCO业务对象执行CRUD

ViewModels对CRUD使用数据工厂,xaml是绑定到POCO中属性的数据

这一切都很好,但我发现唯一有点令人烦恼的是数据工厂。我们在选择时将每个属性从EF对象复制到POCO对象,在更新/插入时复制到POCO对象

是否有任何方法可以像Fluent对NHibernate那样,通过实体框架自动化这个过程

以下是数据工厂中的插入方法示例:

public void InsertCustomer(ref Manager.Model.Customer businessObject)
{
    var mgr = new Manager.Data.PersonData.PersonContext();

    var person = new Manager.Data.PersonData.Person();
    var customer = new Manager.Data.PersonData.Customer();

    customer.Comments = businessObject.Comments;
    customer.Company = businessObject.Company;
    customer.IsBusiness = businessObject.IsBusiness;
    customer.IsCompleted = businessObject.IsCompleted;
    customer.ModifiedBy = "someone";
    customer.ModifiedOn = DateTime.Now;
    customer.CreatedBy = "someone";
    customer.CreatedOn = DateTime.Now;

    person.Customer.Add(customer);
    person.FirstName = businessObject.FirstName;
    person.LastName = businessObject.LastName;
    person.Birthday = businessObject.Birthday;
    person.CreatedBy = "someone";
    person.CreatedOn = DateTime.Now;
    person.Gender = businessObject.Gender;
    person.MiddleInitial = businessObject.MiddleInitial;
    person.ModifiedBy = "someone";
    person.ModifiedOn = DateTime.Now;
    person.Nickname = businessObject.Nickname;
    person.Picture = "";
    person.Suffix = businessObject.Suffix;
    person.Title = businessObject.Title;

    mgr.AddToPeople(person);
    mgr.SaveChanges();
}
最好声明一些类,比如Fluent:

public class CatMap : ClassMap<Cat>  
{  
  public CatMap()  
  {  
    Id(x => x.Id);  
    Map(x => x.Name)  
      .WithLengthOf(16)  
      .Not.Nullable();  
    Map(x => x.Sex);  
    References(x => x.Mate);  
    HasMany(x => x.Kittens);  
  }  
}
从本质上讲,我将消除将数据从业务对象移动到实体框架对象的代码,它将在映射类中执行一次,并且不必为每个方法重复


谢谢

虽然我不知道有什么数据映射器可以满足您对EF的需求,但编写一个并不困难。另外,因为定义映射是大部分工作,所以它实际上并不比使用您列出的fluent接口映射更难。您只需创建一个具有多个映射函数的映射器类,每个映射函数都包含您的映射逻辑

一个可能很有趣的想法是让map函数扩展方法。您仍然可以创建一个映射器类,但是每个映射方法看起来都像

    public static Person MapToPerson(this Manager.Model.Customer bizObject)
    {
        Person person = new Person();
        // mapping logic
        return person;
    }
由于MapToPerson方法是一个扩展方法,而不是bizObject类上的方法,因此您没有破坏POCO。但是,由于扩展方法的语法优势,您的InsertCustomer方法可能有如下代码:

public void InsertCustomer(ref Manager.Model.Customer businessObject)
{
    var mgr = new Manager.Data.PersonData.PersonContext();

    var person = new Manager.Data.PersonData.Person();
    var customer = new Manager.Data.PersonData.Customer();

    Something.Map(person, businessObject);
    Something.Map(customer, businessObject);  

    person.Customer.Add(customer);

    mgr.AddToPeople(newCustomer);
    mgr.SaveChanges();
}
    Customer customer = bizObject.MapToCustomer();
    Person person = bizObject.MapToPerson();

你可以试试Automapper,对我有用