0

I have very method is equal same

I want have one method do jobs three or more method under

but I try use generic method or reflection but cannot write good method or class my method:

First method

    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);
    }

second method

    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);
    }

third method

    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);
    }

How can merge this class to one class?

thanks for answers

2 Answers 2

2

Of the top of my head:

Create an interface ISomething and make your MyClassViewX implement that interface.

public interface ISomething
{
    int Id{get;}
}

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

Adapt the method below to the types of your application, where YourEntityContainerType is the type of db variable and Collection<T> is the base type of properties of db: db.MyClasse1s, db.MyClasse2s...

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

hi thanks answer but I get Error in line : db.Entry(Home).State = EntityState.Modified; also cannot call this method buy Func (I don't understand) and in your function content is type T and Home type is t but type content and variable Home muste be diferent
@user1928692, can you post the error? Also, can you please be more explicit (perhaps try reformatting your comment)?
0

There is a method for this: AddOrUpdate<T>. But the problem is that you must find a way to supply the generic parameter T. Suppose you could change your view models MyClassViewX such that they map themselves to the entity they represent by a method GetEntity(). Then this is what you could do:

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>();
}

And the method GetEntity():

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

This reduces the repetitive code to a number of GetEntity methods exactly where they belong: in the classes that know about the entity they map from and to. The GetDbSet takes the entity as parameter to exploit type inference which gets you to the generic type parameter T.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.