2013-06-28 22 views
0

我有非常方法等于相同重构在C#codefirst

我希望有一种方法做

,但我尝试使用通用的方法或反射,但不能写出好的方法或 类下工作三个或三个以上的方法我的方法:

第一种方法

public ActionResult _Create1(MyClassView1 content) 
    {    
     if (ModelState.IsValid)    
     { 
      MyClass1 Home = new MyClass1(); 
      Mapper.Map(content, Home); 
      if (content.Id <= 0)     
       db.MyClasse1s.Add(Home);          
      else    
       db.Entry(Home).State = EntityState.Modified; 
      db.SaveChanges(); 
      return Content("Ok"); 
     } 
     return PartialView(content); 
    } 

秒方法

public ActionResult _Create2(MyClassView2 content) 
    {    
     if (ModelState.IsValid)    
     { 
      MyClass2 Home = new MyClass2(); 
      Mapper.Map(content, Home); 
      if (content.Id <= 0)     
       db.MyClasse1s.Add(Home);          
      else     
       db.Entry(Home).State = EntityState.Modified; 
      db.SaveChanges(); 
      return Content("Ok"); 
     } 
     return PartialView(content); 
    } 

第三种方法

public ActionResult _Create3(MyClassView3 content) 
    {    
     if (ModelState.IsValid)    
     { 
      MyClass3 Home = new MyClass3(); 
      Mapper.Map(content, Home); 
      if (content.Id <= 0)     
       db.MyClasse1s.Add(Home);          
      else     
       db.Entry(Home).State = EntityState.Modified; 
      db.SaveChanges(); 
      return Content("Ok"); 
     } 
     return PartialView(content); 
    } 

这个类怎么能合并成一个班?

感谢答案

回答

0

有一个这样的方法:AddOrUpdate<T>。但问题是,您必须找到一种方法来提供通用参数T。假设您可以更改您的视图模型MyClassViewX,以便它们通过方法GetEntity()将自己映射到它们表示的实体。那么这就是你能做什么:

public ActionResult Create<T>(MyClassView content) 
{    
    if (ModelState.IsValid)    
    { 
     var entity = content.GetEntity(); 
     GetDbSet(db, entity).AddOrUpdate(entity); 
     db.SaveChanges(); 
     return Content("Ok"); 
    } 
    return PartialView(content); 
} 

DbSet<T> GetDbSet<T>(DbContext db, T entity) where T : class 
{ 
    return db.Set<T>(); 
} 

而且方法GetEntity()

Class1 GetEntity() 
{ 
    return Mapper.Map<Class1>(content); 
} 

这样可以减少重复代码的数量GetEntity方法究竟属于他们的地方:在了解类它们映射的实体。 GetDbSet将该实体作为参数来利用类型推断,该类型推断使您可以访问泛型类型参数T

2

我的头顶部:

创建界面ISomething,使您的MyClassViewX实现该接口。

public interface ISomething 
{ 
    int Id{get;} 
} 

public class MyClassView1 : ISomething 
{ 
    //... 
} 

适应下面的类型的应用程序,其中YourEntityContainerTypedb变量和Collection<T>类型的方法是db属性的基本类型:db.MyClasse2sdb.MyClasse1s,...

public ActionResult_Create<T>(T content, Func<YourEntityContainerType, Collection<T>> collectionSelector, YourEntityContainerType db) where T:ISomething, new 
{ 
    if(ModelState.IsValid) 
    { 
     T Home = new T(); 
     Mapper.Map(content, Home); 
     if(content.Id <= 0) 
      collectionSelector(db).Add(Home); 
     else 
      db.Entry(Home).State = EntityState.Modified; 
     db.SaveChanges(); 
     return Content("Ok"); 
    } 
    return PartialView(content); 
} 
+0

嗨 谢谢你回答 但我得到错误在行:db.Entry(Home).State = EntityState.Modified; 也无法调用此方法购买Func(我不明白) 并且在你的功能内容是T类型,而家庭类型是t但是类型内容和变量首页音乐不同 – khoshghadam

+0

@ user1928692,你可以发布错误吗?此外,你可以请更明确(也许尝试重新格式化您的评论)? – RePierre